2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/GRAPH"
9 #include "lib/logging.h"
11 #include "common/assert.h"
12 #include "lib/assert-pre.h"
13 #include "lib/assert-post.h"
14 #include <babeltrace2/graph/graph.h>
15 #include <babeltrace2/graph/component.h>
16 #include <babeltrace2/graph/port.h>
17 #include "lib/graph/message/message.h"
18 #include "compat/compiler.h"
19 #include "common/common.h"
20 #include <babeltrace2/types.h>
21 #include <babeltrace2/value.h>
22 #include "lib/value.h"
27 #include "component-class-sink-simple.h"
28 #include "component.h"
29 #include "component-sink.h"
30 #include "connection.h"
32 #include "interrupter.h"
33 #include "message/event.h"
34 #include "message/packet.h"
36 typedef enum bt_graph_listener_func_status
37 (*port_added_func_t
)(const void *, const void *, void *);
39 typedef enum bt_component_class_initialize_method_status
40 (*comp_init_method_t
)(const void *, void *, const void *, void *);
42 struct bt_graph_listener_port_added
{
43 port_added_func_t func
;
47 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
49 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
50 if (!(_listeners)) { \
51 BT_LIB_LOGE_APPEND_CAUSE( \
52 "Failed to allocate one GArray."); \
57 void destroy_graph(struct bt_object
*obj
)
59 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
62 * The graph's reference count is 0 if we're here. Increment
63 * it to avoid a double-destroy (possibly infinitely recursive)
66 * 1. We put and destroy a connection.
67 * 2. This connection's destructor finalizes its active message
69 * 3. A message iterator's finalization function gets a new
70 * reference on its component (reference count goes from 0 to
72 * 4. Since this component's reference count goes to 1, it takes
73 * a reference on its parent (this graph). This graph's
74 * reference count goes from 0 to 1.
75 * 5. The message iterator's finalization function puts its
76 * component reference (reference count goes from 1 to 0).
77 * 6. Since this component's reference count goes from 1 to 0,
78 * it puts its parent (this graph). This graph's reference
79 * count goes from 1 to 0.
80 * 7. Since this graph's reference count goes from 1 to 0, its
81 * destructor is called (this function).
83 * With the incrementation below, the graph's reference count at
84 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
85 * ensures that this function is not called two times.
87 BT_LIB_LOGI("Destroying graph: %!+g", graph
);
89 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_DESTROYING
;
91 if (graph
->messages
) {
92 g_ptr_array_free(graph
->messages
, TRUE
);
93 graph
->messages
= NULL
;
96 if (graph
->connections
) {
97 BT_LOGD_STR("Destroying connections.");
98 g_ptr_array_free(graph
->connections
, TRUE
);
99 graph
->connections
= NULL
;
102 if (graph
->components
) {
103 BT_LOGD_STR("Destroying components.");
104 g_ptr_array_free(graph
->components
, TRUE
);
105 graph
->components
= NULL
;
108 if (graph
->interrupters
) {
109 BT_LOGD_STR("Putting interrupters.");
110 g_ptr_array_free(graph
->interrupters
, TRUE
);
111 graph
->interrupters
= NULL
;
114 BT_OBJECT_PUT_REF_AND_RESET(graph
->default_interrupter
);
116 if (graph
->sinks_to_consume
) {
117 g_queue_free(graph
->sinks_to_consume
);
118 graph
->sinks_to_consume
= NULL
;
121 if (graph
->listeners
.source_output_port_added
) {
122 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
123 graph
->listeners
.source_output_port_added
= NULL
;
126 if (graph
->listeners
.filter_output_port_added
) {
127 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
128 graph
->listeners
.filter_output_port_added
= NULL
;
131 if (graph
->listeners
.filter_input_port_added
) {
132 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
133 graph
->listeners
.filter_input_port_added
= NULL
;
136 if (graph
->listeners
.sink_input_port_added
) {
137 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
138 graph
->listeners
.sink_input_port_added
= NULL
;
141 bt_object_pool_finalize(&graph
->event_msg_pool
);
142 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
143 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
148 void destroy_message_event(struct bt_message
*msg
,
149 struct bt_graph
*graph
)
151 bt_message_event_destroy(msg
);
155 void destroy_message_packet_begin(struct bt_message
*msg
,
156 struct bt_graph
*graph
)
158 bt_message_packet_destroy(msg
);
162 void destroy_message_packet_end(struct bt_message
*msg
,
163 struct bt_graph
*graph
)
165 bt_message_packet_destroy(msg
);
169 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
171 bt_message_unlink_graph(msg
);
174 struct bt_graph
*bt_graph_create(uint64_t mip_version
)
176 struct bt_graph
*graph
;
179 BT_ASSERT_PRE_NO_ERROR();
180 BT_ASSERT_PRE(mip_version
<= bt_get_maximal_mip_version(),
181 "Unknown MIP version: mip-version=%" PRIu64
", "
182 "max-mip-version=%" PRIu64
,
183 mip_version
, bt_get_maximal_mip_version());
184 BT_LOGI_STR("Creating graph object.");
185 graph
= g_new0(struct bt_graph
, 1);
187 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
191 bt_object_init_shared(&graph
->base
, destroy_graph
);
192 graph
->mip_version
= mip_version
;
193 graph
->connections
= g_ptr_array_new_with_free_func(
194 (GDestroyNotify
) bt_object_try_spec_release
);
195 if (!graph
->connections
) {
196 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
199 graph
->components
= g_ptr_array_new_with_free_func(
200 (GDestroyNotify
) bt_object_try_spec_release
);
201 if (!graph
->components
) {
202 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
205 graph
->sinks_to_consume
= g_queue_new();
206 if (!graph
->sinks_to_consume
) {
207 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
211 bt_graph_set_can_consume(graph
, true);
212 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
213 graph
->listeners
.source_output_port_added
);
215 if (!graph
->listeners
.source_output_port_added
) {
219 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
220 graph
->listeners
.filter_output_port_added
);
222 if (!graph
->listeners
.filter_output_port_added
) {
226 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
227 graph
->listeners
.filter_input_port_added
);
229 if (!graph
->listeners
.filter_input_port_added
) {
233 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
234 graph
->listeners
.sink_input_port_added
);
236 if (!graph
->listeners
.sink_input_port_added
) {
240 graph
->interrupters
= g_ptr_array_new_with_free_func(
241 (GDestroyNotify
) bt_object_put_ref_no_null_check
);
242 if (!graph
->interrupters
) {
243 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
247 graph
->default_interrupter
= bt_interrupter_create();
248 if (!graph
->default_interrupter
) {
249 BT_LIB_LOGE_APPEND_CAUSE(
250 "Failed to create one interrupter object.");
254 bt_graph_add_interrupter(graph
, graph
->default_interrupter
);
255 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
256 (bt_object_pool_new_object_func
) bt_message_event_new
,
257 (bt_object_pool_destroy_object_func
) destroy_message_event
,
260 BT_LIB_LOGE_APPEND_CAUSE(
261 "Failed to initialize event message pool: ret=%d",
266 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
267 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
268 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
271 BT_LIB_LOGE_APPEND_CAUSE(
272 "Failed to initialize packet beginning message pool: ret=%d",
277 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
278 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
279 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
282 BT_LIB_LOGE_APPEND_CAUSE(
283 "Failed to initialize packet end message pool: ret=%d",
288 graph
->messages
= g_ptr_array_new_with_free_func(
289 (GDestroyNotify
) notify_message_graph_is_destroyed
);
290 BT_LIB_LOGI("Created graph object: %!+g", graph
);
293 return (void *) graph
;
296 BT_OBJECT_PUT_REF_AND_RESET(graph
);
300 enum bt_graph_connect_ports_status
bt_graph_connect_ports(
301 struct bt_graph
*graph
,
302 const struct bt_port_output
*upstream_port_out
,
303 const struct bt_port_input
*downstream_port_in
,
304 const struct bt_connection
**user_connection
)
306 enum bt_graph_connect_ports_status status
= BT_FUNC_STATUS_OK
;
307 struct bt_connection
*connection
= NULL
;
308 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
309 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
310 struct bt_component
*upstream_component
= NULL
;
311 struct bt_component
*downstream_component
= NULL
;
312 enum bt_component_class_port_connected_method_status port_connected_status
;
313 bool init_can_consume
;
315 BT_ASSERT_PRE_NO_ERROR();
316 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
317 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
318 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
320 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
321 "Graph is not in the \"configuring\" state: %!+g", graph
);
322 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
323 "Upstream port is already connected: %!+p", upstream_port
);
324 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
325 "Downstream port is already connected: %!+p", downstream_port
);
326 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
327 "Upstream port does not belong to a component: %!+p",
329 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
330 "Downstream port does not belong to a component: %!+p",
332 init_can_consume
= graph
->can_consume
;
333 BT_LIB_LOGI("Connecting component ports within graph: "
334 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
335 graph
, upstream_port
, downstream_port
);
336 bt_graph_set_can_consume(graph
, false);
337 upstream_component
= bt_port_borrow_component_inline(
338 (void *) upstream_port
);
339 downstream_component
= bt_port_borrow_component_inline(
340 (void *) downstream_port
);
342 BT_LOGD_STR("Creating connection.");
343 connection
= bt_connection_create(graph
, (void *) upstream_port
,
344 (void *) downstream_port
);
346 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
347 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
351 BT_LIB_LOGD("Connection object created: %!+x", connection
);
354 * Ownership of upstream_component/downstream_component and of
355 * the connection object is transferred to the graph.
357 g_ptr_array_add(graph
->connections
, connection
);
360 * Notify both components that their port is connected.
362 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
363 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
364 port_connected_status
= bt_component_port_connected(upstream_component
,
365 (void *) upstream_port
, (void *) downstream_port
);
366 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
367 if (port_connected_status
< 0) {
368 BT_LIB_LOGW_APPEND_CAUSE(
369 "Upstream component's \"port connected\" method failed: "
370 "status=%s, %![graph-]+g, %![up-comp-]+c, "
371 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
372 bt_common_func_status_string(
373 port_connected_status
),
374 graph
, upstream_component
, downstream_component
,
375 upstream_port
, downstream_port
);
378 bt_connection_end(connection
, true);
379 status
= (int) port_connected_status
;
383 connection
->notified_upstream_port_connected
= true;
384 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
385 "%![comp-]+c, %![port-]+p", downstream_component
,
387 port_connected_status
= bt_component_port_connected(downstream_component
,
388 (void *) downstream_port
, (void *) upstream_port
);
389 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
390 if (port_connected_status
< 0) {
391 BT_LIB_LOGW_APPEND_CAUSE(
392 "Downstream component's \"port connected\" method failed: "
393 "status=%s, %![graph-]+g, %![up-comp-]+c, "
394 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
395 bt_common_func_status_string(
396 port_connected_status
),
397 graph
, upstream_component
, downstream_component
,
398 upstream_port
, downstream_port
);
401 bt_connection_end(connection
, true);
402 status
= (int) port_connected_status
;
406 connection
->notified_downstream_port_connected
= true;
408 BT_LIB_LOGI("Connected component ports within graph: "
409 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
410 "%![up-port-]+p, %![down-port-]+p",
411 graph
, upstream_component
, downstream_component
,
412 upstream_port
, downstream_port
);
414 if (user_connection
) {
415 /* Move reference to user */
416 *user_connection
= connection
;
420 if (status
!= BT_FUNC_STATUS_OK
) {
421 bt_graph_make_faulty(graph
);
424 bt_object_put_ref(connection
);
425 (void) init_can_consume
;
426 bt_graph_set_can_consume(graph
, init_can_consume
);
431 int consume_graph_sink(struct bt_component_sink
*comp
)
433 enum bt_component_class_sink_consume_method_status consume_status
;
434 struct bt_component_class_sink
*sink_class
= NULL
;
437 sink_class
= (void *) comp
->parent
.class;
438 BT_ASSERT_DBG(sink_class
->methods
.consume
);
439 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
440 consume_status
= sink_class
->methods
.consume((void *) comp
);
441 BT_LOGD("User method returned: status=%s",
442 bt_common_func_status_string(consume_status
));
443 BT_ASSERT_POST_DEV(consume_status
== BT_FUNC_STATUS_OK
||
444 consume_status
== BT_FUNC_STATUS_END
||
445 consume_status
== BT_FUNC_STATUS_AGAIN
||
446 consume_status
== BT_FUNC_STATUS_ERROR
||
447 consume_status
== BT_FUNC_STATUS_MEMORY_ERROR
,
448 "Invalid component status returned by consuming method: "
449 "status=%s", bt_common_func_status_string(consume_status
));
450 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(consume_status
);
451 if (consume_status
) {
452 if (consume_status
< 0) {
453 BT_LIB_LOGW_APPEND_CAUSE(
454 "Component's \"consume\" method failed: "
455 "status=%s, %![comp-]+c",
456 bt_common_func_status_string(consume_status
),
463 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
464 comp
, bt_common_func_status_string(consume_status
));
467 return consume_status
;
471 * `node` is removed from the queue of sinks to consume when passed to
472 * this function. This function adds it back to the queue if there's
473 * still something to consume afterwards.
476 int consume_sink_node(struct bt_graph
*graph
, GList
*node
)
479 struct bt_component_sink
*sink
;
482 status
= consume_graph_sink(sink
);
483 if (G_UNLIKELY(status
!= BT_FUNC_STATUS_END
)) {
484 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
488 /* End reached, the node is not added back to the queue and free'd. */
489 g_queue_delete_link(graph
->sinks_to_consume
, node
);
491 /* Don't forward an END status if there are sinks left to consume. */
492 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
493 status
= BT_FUNC_STATUS_OK
;
498 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
499 sink
, bt_common_func_status_string(status
));
504 int bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
505 struct bt_component_sink
*sink
)
511 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink
);
512 BT_ASSERT_DBG(bt_component_borrow_graph((void *) sink
) == graph
);
514 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
515 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
516 status
= BT_FUNC_STATUS_END
;
520 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
522 BT_LIB_LOGD("Sink component is not marked as consumable: "
523 "component sink is ended: %![comp-]+c", sink
);
524 status
= BT_FUNC_STATUS_END
;
528 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
529 BT_ASSERT_DBG(sink_node
);
530 status
= consume_sink_node(graph
, sink_node
);
537 int consume_no_check(struct bt_graph
*graph
)
539 int status
= BT_FUNC_STATUS_OK
;
540 struct bt_component
*sink
;
543 BT_ASSERT_PRE_DEV(graph
->has_sink
,
544 "Graph has no sink component: %!+g", graph
);
545 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph
);
547 if (G_UNLIKELY(g_queue_is_empty(graph
->sinks_to_consume
))) {
548 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
549 status
= BT_FUNC_STATUS_END
;
553 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
554 sink
= current_node
->data
;
555 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink
);
556 status
= consume_sink_node(graph
, current_node
);
562 enum bt_graph_run_once_status
bt_graph_run_once(struct bt_graph
*graph
)
564 enum bt_graph_run_once_status status
;
566 BT_ASSERT_PRE_NO_ERROR();
567 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
568 BT_ASSERT_PRE_DEV(graph
->can_consume
,
569 "Cannot consume graph in its current state: %!+g", graph
);
570 BT_ASSERT_PRE_DEV(graph
->config_state
!=
571 BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
572 "Graph is in a faulty state: %!+g", graph
);
573 bt_graph_set_can_consume(graph
, false);
574 status
= bt_graph_configure(graph
);
575 if (G_UNLIKELY(status
)) {
576 /* bt_graph_configure() logs errors */
580 status
= consume_no_check(graph
);
581 bt_graph_set_can_consume(graph
, true);
587 enum bt_graph_run_status
bt_graph_run(struct bt_graph
*graph
)
589 enum bt_graph_run_status status
;
591 BT_ASSERT_PRE_NO_ERROR();
592 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
593 BT_ASSERT_PRE(graph
->can_consume
,
594 "Cannot consume graph in its current state: %!+g", graph
);
595 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
596 "Graph is in a faulty state: %!+g", graph
);
597 bt_graph_set_can_consume(graph
, false);
598 status
= bt_graph_configure(graph
);
599 if (G_UNLIKELY(status
)) {
600 /* bt_graph_configure() logs errors */
604 BT_LIB_LOGI("Running graph: %!+g", graph
);
608 * Check if the graph is interrupted at each iteration.
609 * If the graph was interrupted by another thread or by
610 * a signal handler, this is NOT a warning nor an error;
611 * it was intentional: log with an INFO level only.
613 if (G_UNLIKELY(bt_graph_is_interrupted(graph
))) {
614 BT_LIB_LOGI("Stopping the graph: "
615 "graph was interrupted: %!+g", graph
);
616 status
= BT_FUNC_STATUS_AGAIN
;
620 status
= consume_no_check(graph
);
621 if (G_UNLIKELY(status
== BT_FUNC_STATUS_AGAIN
)) {
623 * If AGAIN is received and there are multiple
624 * sinks, go ahead and consume from the next
627 * However, in the case where a single sink is
628 * left, the caller can decide to busy-wait and
629 * call bt_graph_run() continuously
630 * until the source is ready or it can decide to
631 * sleep for an arbitrary amount of time.
633 if (graph
->sinks_to_consume
->length
> 1) {
634 status
= BT_FUNC_STATUS_OK
;
637 } while (status
== BT_FUNC_STATUS_OK
);
639 if (status
== BT_FUNC_STATUS_END
) {
641 * The last call to consume_no_check() returned
642 * `BT_FUNC_STATUS_END`, but bt_graph_run() has no
643 * `BT_GRAPH_RUN_STATUS_END` status: replace with
644 * `BT_GRAPH_RUN_STATUS_OK` (success: graph ran
647 status
= BT_FUNC_STATUS_OK
;
651 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph
,
652 bt_common_func_status_string(status
));
653 bt_graph_set_can_consume(graph
, true);
657 enum bt_graph_add_listener_status
658 bt_graph_add_source_component_output_port_added_listener(
659 struct bt_graph
*graph
,
660 bt_graph_source_component_output_port_added_listener_func func
,
661 void *data
, bt_listener_id
*out_listener_id
)
663 struct bt_graph_listener_port_added listener
= {
664 .func
= (port_added_func_t
) func
,
667 bt_listener_id listener_id
;
669 BT_ASSERT_PRE_NO_ERROR();
670 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
671 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
672 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
673 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
674 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
675 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
679 *out_listener_id
= listener_id
;
682 return BT_FUNC_STATUS_OK
;
685 enum bt_graph_add_listener_status
686 bt_graph_add_filter_component_output_port_added_listener(
687 struct bt_graph
*graph
,
688 bt_graph_filter_component_output_port_added_listener_func func
,
689 void *data
, bt_listener_id
*out_listener_id
)
691 struct bt_graph_listener_port_added listener
= {
692 .func
= (port_added_func_t
) func
,
695 bt_listener_id listener_id
;
697 BT_ASSERT_PRE_NO_ERROR();
698 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
699 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
700 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
701 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
702 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
703 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
707 *out_listener_id
= listener_id
;
710 return BT_FUNC_STATUS_OK
;
713 enum bt_graph_add_listener_status
714 bt_graph_add_filter_component_input_port_added_listener(
715 struct bt_graph
*graph
,
716 bt_graph_filter_component_input_port_added_listener_func func
,
717 void *data
, bt_listener_id
*out_listener_id
)
719 struct bt_graph_listener_port_added listener
= {
720 .func
= (port_added_func_t
) func
,
723 bt_listener_id listener_id
;
725 BT_ASSERT_PRE_NO_ERROR();
726 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
727 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
728 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
729 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
730 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
731 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
735 *out_listener_id
= listener_id
;
738 return BT_FUNC_STATUS_OK
;
741 enum bt_graph_add_listener_status
742 bt_graph_add_sink_component_input_port_added_listener(
743 struct bt_graph
*graph
,
744 bt_graph_sink_component_input_port_added_listener_func func
,
745 void *data
, bt_listener_id
*out_listener_id
)
747 struct bt_graph_listener_port_added listener
= {
748 .func
= (port_added_func_t
) func
,
751 bt_listener_id listener_id
;
753 BT_ASSERT_PRE_NO_ERROR();
754 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
755 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
756 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
757 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
758 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
759 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
763 *out_listener_id
= listener_id
;
766 return BT_FUNC_STATUS_OK
;
770 enum bt_graph_listener_func_status
bt_graph_notify_port_added(
771 struct bt_graph
*graph
, struct bt_port
*port
)
775 struct bt_component
*comp
;
776 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
780 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
781 "%![graph-]+g, %![port-]+p", graph
, port
);
782 comp
= bt_port_borrow_component_inline(port
);
785 switch (comp
->class->type
) {
786 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
788 switch (port
->type
) {
789 case BT_PORT_TYPE_OUTPUT
:
790 listeners
= graph
->listeners
.source_output_port_added
;
798 case BT_COMPONENT_CLASS_TYPE_FILTER
:
800 switch (port
->type
) {
801 case BT_PORT_TYPE_INPUT
:
802 listeners
= graph
->listeners
.filter_input_port_added
;
804 case BT_PORT_TYPE_OUTPUT
:
805 listeners
= graph
->listeners
.filter_output_port_added
;
813 case BT_COMPONENT_CLASS_TYPE_SINK
:
815 switch (port
->type
) {
816 case BT_PORT_TYPE_INPUT
:
817 listeners
= graph
->listeners
.sink_input_port_added
;
829 for (i
= 0; i
< listeners
->len
; i
++) {
830 struct bt_graph_listener_port_added
*listener
=
831 &g_array_index(listeners
,
832 struct bt_graph_listener_port_added
, i
);
835 BT_ASSERT(listener
->func
);
836 status
= listener
->func(comp
, port
, listener
->data
);
837 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
838 if (status
!= BT_FUNC_STATUS_OK
) {
848 void bt_graph_remove_connection(struct bt_graph
*graph
,
849 struct bt_connection
*connection
)
852 BT_ASSERT(connection
);
853 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
855 g_ptr_array_remove(graph
->connections
, connection
);
859 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
864 for (i
= 0; i
< graph
->components
->len
; i
++) {
865 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
867 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
868 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
869 "%![other-comp-]+c, name=\"%s\"",
881 int add_component_with_init_method_data(
882 struct bt_graph
*graph
,
883 struct bt_component_class
*comp_cls
,
884 comp_init_method_t init_method
,
885 const char *name
, const struct bt_value
*params
,
886 void *init_method_data
, bt_logging_level log_level
,
887 const struct bt_component
**user_component
)
889 int status
= BT_FUNC_STATUS_OK
;
890 enum bt_component_class_initialize_method_status init_status
;
891 struct bt_component
*component
= NULL
;
893 bool init_can_consume
;
894 struct bt_value
*new_params
= NULL
;
897 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
898 BT_ASSERT_PRE_NON_NULL(name
, "Name");
900 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
901 "Graph is not in the \"configuring\" state: %!+g", graph
);
902 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
903 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
904 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
905 "Parameter value is not a map value: %!+v", params
);
906 init_can_consume
= graph
->can_consume
;
907 bt_graph_set_can_consume(graph
, false);
908 BT_LIB_LOGI("Adding component to graph: "
909 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
910 "%![params-]+v, init-method-data-addr=%p",
911 graph
, comp_cls
, name
,
912 bt_common_logging_level_string(log_level
), params
,
916 new_params
= bt_value_map_create();
918 BT_LIB_LOGE_APPEND_CAUSE(
919 "Cannot create empty map value object.");
920 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
927 ret
= bt_component_create(comp_cls
, name
, log_level
, &component
);
929 BT_LIB_LOGE_APPEND_CAUSE(
930 "Cannot create empty component object: ret=%d",
932 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
937 * The user's initialization method needs to see that this
938 * component is part of the graph. If the user method fails, we
939 * immediately remove the component from the graph's components.
941 g_ptr_array_add(graph
->components
, component
);
942 bt_component_set_graph(component
, graph
);
943 bt_value_freeze(params
);
947 * There is no use for config objects right now, so just pass
950 BT_LOGD_STR("Calling user's initialization method.");
951 init_status
= init_method(component
, NULL
, params
, init_method_data
);
952 BT_LOGD("User method returned: status=%s",
953 bt_common_func_status_string(init_status
));
954 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(init_status
);
955 if (init_status
!= BT_FUNC_STATUS_OK
) {
956 if (init_status
< 0) {
957 BT_LIB_LOGW_APPEND_CAUSE(
958 "Component initialization method failed: "
959 "status=%s, %![comp-]+c",
960 bt_common_func_status_string(init_status
),
964 status
= init_status
;
965 bt_component_set_graph(component
, NULL
);
966 g_ptr_array_remove_fast(graph
->components
, component
);
972 * Mark the component as initialized so that its finalization
973 * method is called when it is destroyed.
975 component
->initialized
= true;
978 * If it's a sink component, it needs to be part of the graph's
979 * sink queue to be consumed by bt_graph_run() or
980 * bt_graph_run_once().
982 if (bt_component_is_sink(component
)) {
983 graph
->has_sink
= true;
984 g_queue_push_tail(graph
->sinks_to_consume
, component
);
988 * Freeze the component class now that it's instantiated at
991 BT_LOGD_STR("Freezing component class.");
992 bt_component_class_freeze(comp_cls
);
993 BT_LIB_LOGI("Added component to graph: "
994 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
995 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
996 graph
, comp_cls
, name
,
997 bt_common_logging_level_string(log_level
), params
,
998 init_method_data
, component
);
1000 if (user_component
) {
1001 /* Move reference to user */
1002 *user_component
= component
;
1006 if (status
!= BT_FUNC_STATUS_OK
) {
1007 bt_graph_make_faulty(graph
);
1010 bt_object_put_ref(component
);
1011 bt_object_put_ref(new_params
);
1012 (void) init_can_consume
;
1013 bt_graph_set_can_consume(graph
, init_can_consume
);
1017 enum bt_graph_add_component_status
1018 bt_graph_add_source_component_with_initialize_method_data(
1019 struct bt_graph
*graph
,
1020 const struct bt_component_class_source
*comp_cls
,
1021 const char *name
, const struct bt_value
*params
,
1022 void *init_method_data
, bt_logging_level log_level
,
1023 const struct bt_component_source
**component
)
1025 BT_ASSERT_PRE_NO_ERROR();
1026 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1027 return add_component_with_init_method_data(graph
,
1028 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1029 name
, params
, init_method_data
, log_level
, (void *) component
);
1032 enum bt_graph_add_component_status
bt_graph_add_source_component(
1033 struct bt_graph
*graph
,
1034 const struct bt_component_class_source
*comp_cls
,
1035 const char *name
, const struct bt_value
*params
,
1036 enum bt_logging_level log_level
,
1037 const struct bt_component_source
**component
)
1039 BT_ASSERT_PRE_NO_ERROR();
1040 return bt_graph_add_source_component_with_initialize_method_data(
1041 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1044 enum bt_graph_add_component_status
1045 bt_graph_add_filter_component_with_initialize_method_data(
1046 struct bt_graph
*graph
,
1047 const struct bt_component_class_filter
*comp_cls
,
1048 const char *name
, const struct bt_value
*params
,
1049 void *init_method_data
, enum bt_logging_level log_level
,
1050 const struct bt_component_filter
**component
)
1052 BT_ASSERT_PRE_NO_ERROR();
1053 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1054 return add_component_with_init_method_data(graph
,
1055 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1056 name
, params
, init_method_data
, log_level
, (void *) component
);
1059 enum bt_graph_add_component_status
bt_graph_add_filter_component(
1060 struct bt_graph
*graph
,
1061 const struct bt_component_class_filter
*comp_cls
,
1062 const char *name
, const struct bt_value
*params
,
1063 enum bt_logging_level log_level
,
1064 const struct bt_component_filter
**component
)
1066 BT_ASSERT_PRE_NO_ERROR();
1067 return bt_graph_add_filter_component_with_initialize_method_data(
1068 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1071 enum bt_graph_add_component_status
1072 bt_graph_add_sink_component_with_initialize_method_data(
1073 struct bt_graph
*graph
,
1074 const struct bt_component_class_sink
*comp_cls
,
1075 const char *name
, const struct bt_value
*params
,
1076 void *init_method_data
, enum bt_logging_level log_level
,
1077 const struct bt_component_sink
**component
)
1079 BT_ASSERT_PRE_NO_ERROR();
1080 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1081 return add_component_with_init_method_data(graph
,
1082 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1083 name
, params
, init_method_data
, log_level
, (void *) component
);
1086 enum bt_graph_add_component_status
bt_graph_add_sink_component(
1087 struct bt_graph
*graph
,
1088 const struct bt_component_class_sink
*comp_cls
,
1089 const char *name
, const struct bt_value
*params
,
1090 enum bt_logging_level log_level
,
1091 const struct bt_component_sink
**component
)
1093 BT_ASSERT_PRE_NO_ERROR();
1094 return bt_graph_add_sink_component_with_initialize_method_data(
1095 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1098 enum bt_graph_add_component_status
1099 bt_graph_add_simple_sink_component(struct bt_graph
*graph
, const char *name
,
1100 bt_graph_simple_sink_component_initialize_func init_func
,
1101 bt_graph_simple_sink_component_consume_func consume_func
,
1102 bt_graph_simple_sink_component_finalize_func finalize_func
,
1103 void *user_data
, const bt_component_sink
**component
)
1105 enum bt_graph_add_component_status status
;
1106 struct bt_component_class_sink
*comp_cls
;
1107 struct simple_sink_init_method_data init_method_data
= {
1108 .init_func
= init_func
,
1109 .consume_func
= consume_func
,
1110 .finalize_func
= finalize_func
,
1111 .user_data
= user_data
,
1114 BT_ASSERT_PRE_NO_ERROR();
1117 * Other preconditions are checked by
1118 * bt_graph_add_sink_component_with_init_method_data().
1120 BT_ASSERT_PRE_NON_NULL(consume_func
, "Consume function");
1122 comp_cls
= bt_component_class_sink_simple_borrow();
1124 BT_LIB_LOGE_APPEND_CAUSE(
1125 "Cannot borrow simple sink component class.");
1126 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1130 status
= bt_graph_add_sink_component_with_initialize_method_data(graph
,
1131 comp_cls
, name
, NULL
, &init_method_data
,
1132 BT_LOGGING_LEVEL_NONE
, component
);
1139 void bt_graph_add_message(struct bt_graph
*graph
,
1140 struct bt_message
*msg
)
1146 * It's okay not to take a reference because, when a
1147 * message's reference count drops to 0, either:
1149 * * It is recycled back to one of this graph's pool.
1150 * * It is destroyed because it doesn't have any link to any
1151 * graph, which means the original graph is already destroyed.
1153 g_ptr_array_add(graph
->messages
, msg
);
1157 bool bt_graph_is_interrupted(const struct bt_graph
*graph
)
1159 BT_ASSERT_DBG(graph
);
1160 return bt_interrupter_array_any_is_set(graph
->interrupters
);
1163 enum bt_graph_add_interrupter_status
bt_graph_add_interrupter(
1164 struct bt_graph
*graph
, const struct bt_interrupter
*intr
)
1166 BT_ASSERT_PRE_NO_ERROR();
1167 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1168 BT_ASSERT_PRE_NON_NULL(intr
, "Interrupter");
1169 g_ptr_array_add(graph
->interrupters
, (void *) intr
);
1170 bt_object_get_ref_no_null_check(intr
);
1171 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1173 return BT_FUNC_STATUS_OK
;
1176 struct bt_interrupter
*bt_graph_borrow_default_interrupter(bt_graph
*graph
)
1178 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1179 return graph
->default_interrupter
;
1182 void bt_graph_get_ref(const struct bt_graph
*graph
)
1184 bt_object_get_ref(graph
);
1187 void bt_graph_put_ref(const struct bt_graph
*graph
)
1189 bt_object_put_ref(graph
);