2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/GRAPH"
25 #include "lib/logging.h"
27 #include "common/assert.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include <babeltrace2/graph/graph.h>
31 #include <babeltrace2/graph/component.h>
32 #include <babeltrace2/graph/port.h>
33 #include "lib/graph/message/message.h"
34 #include "compat/compiler.h"
35 #include "common/common.h"
36 #include <babeltrace2/types.h>
37 #include <babeltrace2/value.h>
38 #include "lib/value.h"
43 #include "component-class-sink-simple.h"
44 #include "component.h"
45 #include "component-sink.h"
46 #include "connection.h"
48 #include "interrupter.h"
49 #include "message/event.h"
50 #include "message/packet.h"
52 typedef enum bt_graph_listener_func_status
53 (*port_added_func_t
)(const void *, const void *, void *);
55 typedef enum bt_component_class_initialize_method_status
56 (*comp_init_method_t
)(const void *, void *, const void *, void *);
58 struct bt_graph_listener_port_added
{
59 port_added_func_t func
;
63 #define INIT_LISTENERS_ARRAY(_type, _listeners) \
65 _listeners = g_array_new(FALSE, TRUE, sizeof(_type)); \
66 if (!(_listeners)) { \
67 BT_LIB_LOGE_APPEND_CAUSE( \
68 "Failed to allocate one GArray."); \
73 void destroy_graph(struct bt_object
*obj
)
75 struct bt_graph
*graph
= container_of(obj
, struct bt_graph
, base
);
78 * The graph's reference count is 0 if we're here. Increment
79 * it to avoid a double-destroy (possibly infinitely recursive)
82 * 1. We put and destroy a connection.
83 * 2. This connection's destructor finalizes its active message
85 * 3. A message iterator's finalization function gets a new
86 * reference on its component (reference count goes from 0 to
88 * 4. Since this component's reference count goes to 1, it takes
89 * a reference on its parent (this graph). This graph's
90 * reference count goes from 0 to 1.
91 * 5. The message iterator's finalization function puts its
92 * component reference (reference count goes from 1 to 0).
93 * 6. Since this component's reference count goes from 1 to 0,
94 * it puts its parent (this graph). This graph's reference
95 * count goes from 1 to 0.
96 * 7. Since this graph's reference count goes from 1 to 0, its
97 * destructor is called (this function).
99 * With the incrementation below, the graph's reference count at
100 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
101 * ensures that this function is not called two times.
103 BT_LIB_LOGI("Destroying graph: %!+g", graph
);
105 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_DESTROYING
;
107 if (graph
->messages
) {
108 g_ptr_array_free(graph
->messages
, TRUE
);
109 graph
->messages
= NULL
;
112 if (graph
->connections
) {
113 BT_LOGD_STR("Destroying connections.");
114 g_ptr_array_free(graph
->connections
, TRUE
);
115 graph
->connections
= NULL
;
118 if (graph
->components
) {
119 BT_LOGD_STR("Destroying components.");
120 g_ptr_array_free(graph
->components
, TRUE
);
121 graph
->components
= NULL
;
124 if (graph
->interrupters
) {
125 BT_LOGD_STR("Putting interrupters.");
126 g_ptr_array_free(graph
->interrupters
, TRUE
);
127 graph
->interrupters
= NULL
;
130 BT_OBJECT_PUT_REF_AND_RESET(graph
->default_interrupter
);
132 if (graph
->sinks_to_consume
) {
133 g_queue_free(graph
->sinks_to_consume
);
134 graph
->sinks_to_consume
= NULL
;
137 if (graph
->listeners
.source_output_port_added
) {
138 g_array_free(graph
->listeners
.source_output_port_added
, TRUE
);
139 graph
->listeners
.source_output_port_added
= NULL
;
142 if (graph
->listeners
.filter_output_port_added
) {
143 g_array_free(graph
->listeners
.filter_output_port_added
, TRUE
);
144 graph
->listeners
.filter_output_port_added
= NULL
;
147 if (graph
->listeners
.filter_input_port_added
) {
148 g_array_free(graph
->listeners
.filter_input_port_added
, TRUE
);
149 graph
->listeners
.filter_input_port_added
= NULL
;
152 if (graph
->listeners
.sink_input_port_added
) {
153 g_array_free(graph
->listeners
.sink_input_port_added
, TRUE
);
154 graph
->listeners
.sink_input_port_added
= NULL
;
157 bt_object_pool_finalize(&graph
->event_msg_pool
);
158 bt_object_pool_finalize(&graph
->packet_begin_msg_pool
);
159 bt_object_pool_finalize(&graph
->packet_end_msg_pool
);
164 void destroy_message_event(struct bt_message
*msg
,
165 struct bt_graph
*graph
)
167 bt_message_event_destroy(msg
);
171 void destroy_message_packet_begin(struct bt_message
*msg
,
172 struct bt_graph
*graph
)
174 bt_message_packet_destroy(msg
);
178 void destroy_message_packet_end(struct bt_message
*msg
,
179 struct bt_graph
*graph
)
181 bt_message_packet_destroy(msg
);
185 void notify_message_graph_is_destroyed(struct bt_message
*msg
)
187 bt_message_unlink_graph(msg
);
190 struct bt_graph
*bt_graph_create(uint64_t mip_version
)
192 struct bt_graph
*graph
;
195 BT_ASSERT_PRE_NO_ERROR();
196 BT_ASSERT_PRE(mip_version
<= bt_get_maximal_mip_version(),
197 "Unknown MIP version: mip-version=%" PRIu64
", "
198 "max-mip-version=%" PRIu64
,
199 mip_version
, bt_get_maximal_mip_version());
200 BT_LOGI_STR("Creating graph object.");
201 graph
= g_new0(struct bt_graph
, 1);
203 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one graph.");
207 bt_object_init_shared(&graph
->base
, destroy_graph
);
208 graph
->mip_version
= mip_version
;
209 graph
->connections
= g_ptr_array_new_with_free_func(
210 (GDestroyNotify
) bt_object_try_spec_release
);
211 if (!graph
->connections
) {
212 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
215 graph
->components
= g_ptr_array_new_with_free_func(
216 (GDestroyNotify
) bt_object_try_spec_release
);
217 if (!graph
->components
) {
218 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
221 graph
->sinks_to_consume
= g_queue_new();
222 if (!graph
->sinks_to_consume
) {
223 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GQueue.");
227 bt_graph_set_can_consume(graph
, true);
228 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
229 graph
->listeners
.source_output_port_added
);
231 if (!graph
->listeners
.source_output_port_added
) {
235 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
236 graph
->listeners
.filter_output_port_added
);
238 if (!graph
->listeners
.filter_output_port_added
) {
242 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
243 graph
->listeners
.filter_input_port_added
);
245 if (!graph
->listeners
.filter_input_port_added
) {
249 INIT_LISTENERS_ARRAY(struct bt_graph_listener_port_added
,
250 graph
->listeners
.sink_input_port_added
);
252 if (!graph
->listeners
.sink_input_port_added
) {
256 graph
->interrupters
= g_ptr_array_new_with_free_func(
257 (GDestroyNotify
) bt_object_put_ref_no_null_check
);
258 if (!graph
->interrupters
) {
259 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
263 graph
->default_interrupter
= bt_interrupter_create();
264 if (!graph
->default_interrupter
) {
265 BT_LIB_LOGE_APPEND_CAUSE(
266 "Failed to create one interrupter object.");
270 bt_graph_add_interrupter(graph
, graph
->default_interrupter
);
271 ret
= bt_object_pool_initialize(&graph
->event_msg_pool
,
272 (bt_object_pool_new_object_func
) bt_message_event_new
,
273 (bt_object_pool_destroy_object_func
) destroy_message_event
,
276 BT_LIB_LOGE_APPEND_CAUSE(
277 "Failed to initialize event message pool: ret=%d",
282 ret
= bt_object_pool_initialize(&graph
->packet_begin_msg_pool
,
283 (bt_object_pool_new_object_func
) bt_message_packet_beginning_new
,
284 (bt_object_pool_destroy_object_func
) destroy_message_packet_begin
,
287 BT_LIB_LOGE_APPEND_CAUSE(
288 "Failed to initialize packet beginning message pool: ret=%d",
293 ret
= bt_object_pool_initialize(&graph
->packet_end_msg_pool
,
294 (bt_object_pool_new_object_func
) bt_message_packet_end_new
,
295 (bt_object_pool_destroy_object_func
) destroy_message_packet_end
,
298 BT_LIB_LOGE_APPEND_CAUSE(
299 "Failed to initialize packet end message pool: ret=%d",
304 graph
->messages
= g_ptr_array_new_with_free_func(
305 (GDestroyNotify
) notify_message_graph_is_destroyed
);
306 BT_LIB_LOGI("Created graph object: %!+g", graph
);
309 return (void *) graph
;
312 BT_OBJECT_PUT_REF_AND_RESET(graph
);
316 enum bt_graph_connect_ports_status
bt_graph_connect_ports(
317 struct bt_graph
*graph
,
318 const struct bt_port_output
*upstream_port_out
,
319 const struct bt_port_input
*downstream_port_in
,
320 const struct bt_connection
**user_connection
)
322 enum bt_graph_connect_ports_status status
= BT_FUNC_STATUS_OK
;
323 struct bt_connection
*connection
= NULL
;
324 struct bt_port
*upstream_port
= (void *) upstream_port_out
;
325 struct bt_port
*downstream_port
= (void *) downstream_port_in
;
326 struct bt_component
*upstream_component
= NULL
;
327 struct bt_component
*downstream_component
= NULL
;
328 enum bt_component_class_port_connected_method_status port_connected_status
;
329 bool init_can_consume
;
331 BT_ASSERT_PRE_NO_ERROR();
332 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
333 BT_ASSERT_PRE_NON_NULL(upstream_port
, "Upstream port");
334 BT_ASSERT_PRE_NON_NULL(downstream_port
, "Downstream port port");
336 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
337 "Graph is not in the \"configuring\" state: %!+g", graph
);
338 BT_ASSERT_PRE(!bt_port_is_connected(upstream_port
),
339 "Upstream port is already connected: %!+p", upstream_port
);
340 BT_ASSERT_PRE(!bt_port_is_connected(downstream_port
),
341 "Downstream port is already connected: %!+p", downstream_port
);
342 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) upstream_port
),
343 "Upstream port does not belong to a component: %!+p",
345 BT_ASSERT_PRE(bt_port_borrow_component_inline((void *) downstream_port
),
346 "Downstream port does not belong to a component: %!+p",
348 init_can_consume
= graph
->can_consume
;
349 BT_LIB_LOGI("Connecting component ports within graph: "
350 "%![graph-]+g, %![up-port-]+p, %![down-port-]+p",
351 graph
, upstream_port
, downstream_port
);
352 bt_graph_set_can_consume(graph
, false);
353 upstream_component
= bt_port_borrow_component_inline(
354 (void *) upstream_port
);
355 downstream_component
= bt_port_borrow_component_inline(
356 (void *) downstream_port
);
358 BT_LOGD_STR("Creating connection.");
359 connection
= bt_connection_create(graph
, (void *) upstream_port
,
360 (void *) downstream_port
);
362 BT_LIB_LOGE_APPEND_CAUSE("Cannot create connection object.");
363 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
367 BT_LIB_LOGD("Connection object created: %!+x", connection
);
370 * Ownership of upstream_component/downstream_component and of
371 * the connection object is transferred to the graph.
373 g_ptr_array_add(graph
->connections
, connection
);
376 * Notify both components that their port is connected.
378 BT_LIB_LOGD("Notifying upstream component that its port is connected: "
379 "%![comp-]+c, %![port-]+p", upstream_component
, upstream_port
);
380 port_connected_status
= bt_component_port_connected(upstream_component
,
381 (void *) upstream_port
, (void *) downstream_port
);
382 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
383 if (port_connected_status
< 0) {
384 BT_LIB_LOGW_APPEND_CAUSE(
385 "Upstream component's \"port connected\" method failed: "
386 "status=%s, %![graph-]+g, %![up-comp-]+c, "
387 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
388 bt_common_func_status_string(
389 port_connected_status
),
390 graph
, upstream_component
, downstream_component
,
391 upstream_port
, downstream_port
);
394 bt_connection_end(connection
, true);
395 status
= (int) port_connected_status
;
399 connection
->notified_upstream_port_connected
= true;
400 BT_LIB_LOGD("Notifying downstream component that its port is connected: "
401 "%![comp-]+c, %![port-]+p", downstream_component
,
403 port_connected_status
= bt_component_port_connected(downstream_component
,
404 (void *) downstream_port
, (void *) upstream_port
);
405 if (port_connected_status
!= BT_FUNC_STATUS_OK
) {
406 if (port_connected_status
< 0) {
407 BT_LIB_LOGW_APPEND_CAUSE(
408 "Downstream component's \"port connected\" method failed: "
409 "status=%s, %![graph-]+g, %![up-comp-]+c, "
410 "%![down-comp-]+c, %![up-port-]+p, %![down-port-]+p",
411 bt_common_func_status_string(
412 port_connected_status
),
413 graph
, upstream_component
, downstream_component
,
414 upstream_port
, downstream_port
);
417 bt_connection_end(connection
, true);
418 status
= (int) port_connected_status
;
422 connection
->notified_downstream_port_connected
= true;
424 BT_LIB_LOGI("Connected component ports within graph: "
425 "%![graph-]+g, %![up-comp-]+c, %![down-comp-]+c, "
426 "%![up-port-]+p, %![down-port-]+p",
427 graph
, upstream_component
, downstream_component
,
428 upstream_port
, downstream_port
);
430 if (user_connection
) {
431 /* Move reference to user */
432 *user_connection
= connection
;
436 if (status
!= BT_FUNC_STATUS_OK
) {
437 bt_graph_make_faulty(graph
);
440 bt_object_put_ref(connection
);
441 (void) init_can_consume
;
442 bt_graph_set_can_consume(graph
, init_can_consume
);
447 int consume_graph_sink(struct bt_component_sink
*comp
)
449 enum bt_component_class_sink_consume_method_status consume_status
;
450 struct bt_component_class_sink
*sink_class
= NULL
;
453 sink_class
= (void *) comp
->parent
.class;
454 BT_ASSERT_DBG(sink_class
->methods
.consume
);
455 BT_LIB_LOGD("Calling user's consume method: %!+c", comp
);
456 consume_status
= sink_class
->methods
.consume((void *) comp
);
457 BT_LOGD("User method returned: status=%s",
458 bt_common_func_status_string(consume_status
));
459 BT_ASSERT_POST_DEV(consume_status
== BT_FUNC_STATUS_OK
||
460 consume_status
== BT_FUNC_STATUS_END
||
461 consume_status
== BT_FUNC_STATUS_AGAIN
||
462 consume_status
== BT_FUNC_STATUS_ERROR
||
463 consume_status
== BT_FUNC_STATUS_MEMORY_ERROR
,
464 "Invalid component status returned by consuming method: "
465 "status=%s", bt_common_func_status_string(consume_status
));
466 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(consume_status
);
467 if (consume_status
) {
468 if (consume_status
< 0) {
469 BT_LIB_LOGW_APPEND_CAUSE(
470 "Component's \"consume\" method failed: "
471 "status=%s, %![comp-]+c",
472 bt_common_func_status_string(consume_status
),
479 BT_LIB_LOGD("Consumed from sink: %![comp-]+c, status=%s",
480 comp
, bt_common_func_status_string(consume_status
));
483 return consume_status
;
487 * `node` is removed from the queue of sinks to consume when passed to
488 * this function. This function adds it back to the queue if there's
489 * still something to consume afterwards.
492 int consume_sink_node(struct bt_graph
*graph
, GList
*node
)
495 struct bt_component_sink
*sink
;
498 status
= consume_graph_sink(sink
);
499 if (G_UNLIKELY(status
!= BT_FUNC_STATUS_END
)) {
500 g_queue_push_tail_link(graph
->sinks_to_consume
, node
);
504 /* End reached, the node is not added back to the queue and free'd. */
505 g_queue_delete_link(graph
->sinks_to_consume
, node
);
507 /* Don't forward an END status if there are sinks left to consume. */
508 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
509 status
= BT_FUNC_STATUS_OK
;
514 BT_LIB_LOGD("Consumed sink node: %![comp-]+c, status=%s",
515 sink
, bt_common_func_status_string(status
));
520 int bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
521 struct bt_component_sink
*sink
)
527 BT_LIB_LOGD("Making specific sink consume: %![comp-]+c", sink
);
528 BT_ASSERT_DBG(bt_component_borrow_graph((void *) sink
) == graph
);
530 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
531 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
532 status
= BT_FUNC_STATUS_END
;
536 index
= g_queue_index(graph
->sinks_to_consume
, sink
);
538 BT_LIB_LOGD("Sink component is not marked as consumable: "
539 "component sink is ended: %![comp-]+c", sink
);
540 status
= BT_FUNC_STATUS_END
;
544 sink_node
= g_queue_pop_nth_link(graph
->sinks_to_consume
, index
);
545 BT_ASSERT_DBG(sink_node
);
546 status
= consume_sink_node(graph
, sink_node
);
553 int consume_no_check(struct bt_graph
*graph
)
555 int status
= BT_FUNC_STATUS_OK
;
556 struct bt_component
*sink
;
559 BT_ASSERT_PRE_DEV(graph
->has_sink
,
560 "Graph has no sink component: %!+g", graph
);
561 BT_LIB_LOGD("Making next sink component consume: %![graph-]+g", graph
);
563 if (G_UNLIKELY(g_queue_is_empty(graph
->sinks_to_consume
))) {
564 BT_LOGD_STR("Graph's sink queue is empty: end of graph.");
565 status
= BT_FUNC_STATUS_END
;
569 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
570 sink
= current_node
->data
;
571 BT_LIB_LOGD("Chose next sink to consume: %!+c", sink
);
572 status
= consume_sink_node(graph
, current_node
);
578 enum bt_graph_run_once_status
bt_graph_run_once(struct bt_graph
*graph
)
580 enum bt_graph_run_once_status status
;
582 BT_ASSERT_PRE_NO_ERROR();
583 BT_ASSERT_PRE_DEV_NON_NULL(graph
, "Graph");
584 BT_ASSERT_PRE_DEV(graph
->can_consume
,
585 "Cannot consume graph in its current state: %!+g", graph
);
586 BT_ASSERT_PRE_DEV(graph
->config_state
!=
587 BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
588 "Graph is in a faulty state: %!+g", graph
);
589 bt_graph_set_can_consume(graph
, false);
590 status
= bt_graph_configure(graph
);
591 if (G_UNLIKELY(status
)) {
592 /* bt_graph_configure() logs errors */
596 status
= consume_no_check(graph
);
597 bt_graph_set_can_consume(graph
, true);
603 enum bt_graph_run_status
bt_graph_run(struct bt_graph
*graph
)
605 enum bt_graph_run_status status
;
607 BT_ASSERT_PRE_NO_ERROR();
608 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
609 BT_ASSERT_PRE(graph
->can_consume
,
610 "Cannot consume graph in its current state: %!+g", graph
);
611 BT_ASSERT_PRE(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
,
612 "Graph is in a faulty state: %!+g", graph
);
613 bt_graph_set_can_consume(graph
, false);
614 status
= bt_graph_configure(graph
);
615 if (G_UNLIKELY(status
)) {
616 /* bt_graph_configure() logs errors */
620 BT_LIB_LOGI("Running graph: %!+g", graph
);
624 * Check if the graph is interrupted at each iteration.
625 * If the graph was interrupted by another thread or by
626 * a signal handler, this is NOT a warning nor an error;
627 * it was intentional: log with an INFO level only.
629 if (G_UNLIKELY(bt_graph_is_interrupted(graph
))) {
630 BT_LIB_LOGI("Stopping the graph: "
631 "graph was interrupted: %!+g", graph
);
632 status
= BT_FUNC_STATUS_AGAIN
;
636 status
= consume_no_check(graph
);
637 if (G_UNLIKELY(status
== BT_FUNC_STATUS_AGAIN
)) {
639 * If AGAIN is received and there are multiple
640 * sinks, go ahead and consume from the next
643 * However, in the case where a single sink is
644 * left, the caller can decide to busy-wait and
645 * call bt_graph_run() continuously
646 * until the source is ready or it can decide to
647 * sleep for an arbitrary amount of time.
649 if (graph
->sinks_to_consume
->length
> 1) {
650 status
= BT_FUNC_STATUS_OK
;
653 } while (status
== BT_FUNC_STATUS_OK
);
655 if (status
== BT_FUNC_STATUS_END
) {
657 * The last call to consume_no_check() returned
658 * `BT_FUNC_STATUS_END`, but bt_graph_run() has no
659 * `BT_GRAPH_RUN_STATUS_END` status: replace with
660 * `BT_GRAPH_RUN_STATUS_OK` (success: graph ran
663 status
= BT_FUNC_STATUS_OK
;
667 BT_LIB_LOGI("Graph ran: %![graph-]+g, status=%s", graph
,
668 bt_common_func_status_string(status
));
669 bt_graph_set_can_consume(graph
, true);
673 enum bt_graph_add_listener_status
674 bt_graph_add_source_component_output_port_added_listener(
675 struct bt_graph
*graph
,
676 bt_graph_source_component_output_port_added_listener_func func
,
677 void *data
, bt_listener_id
*out_listener_id
)
679 struct bt_graph_listener_port_added listener
= {
680 .func
= (port_added_func_t
) func
,
683 bt_listener_id listener_id
;
685 BT_ASSERT_PRE_NO_ERROR();
686 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
687 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
688 g_array_append_val(graph
->listeners
.source_output_port_added
, listener
);
689 listener_id
= graph
->listeners
.source_output_port_added
->len
- 1;
690 BT_LIB_LOGD("Added \"source component output port added\" listener to graph: "
691 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
695 *out_listener_id
= listener_id
;
698 return BT_FUNC_STATUS_OK
;
701 enum bt_graph_add_listener_status
702 bt_graph_add_filter_component_output_port_added_listener(
703 struct bt_graph
*graph
,
704 bt_graph_filter_component_output_port_added_listener_func func
,
705 void *data
, bt_listener_id
*out_listener_id
)
707 struct bt_graph_listener_port_added listener
= {
708 .func
= (port_added_func_t
) func
,
711 bt_listener_id listener_id
;
713 BT_ASSERT_PRE_NO_ERROR();
714 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
715 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
716 g_array_append_val(graph
->listeners
.filter_output_port_added
, listener
);
717 listener_id
= graph
->listeners
.filter_output_port_added
->len
- 1;
718 BT_LIB_LOGD("Added \"filter component output port added\" listener to graph: "
719 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
723 *out_listener_id
= listener_id
;
726 return BT_FUNC_STATUS_OK
;
729 enum bt_graph_add_listener_status
730 bt_graph_add_filter_component_input_port_added_listener(
731 struct bt_graph
*graph
,
732 bt_graph_filter_component_input_port_added_listener_func func
,
733 void *data
, bt_listener_id
*out_listener_id
)
735 struct bt_graph_listener_port_added listener
= {
736 .func
= (port_added_func_t
) func
,
739 bt_listener_id listener_id
;
741 BT_ASSERT_PRE_NO_ERROR();
742 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
743 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
744 g_array_append_val(graph
->listeners
.filter_input_port_added
, listener
);
745 listener_id
= graph
->listeners
.filter_input_port_added
->len
- 1;
746 BT_LIB_LOGD("Added \"filter component input port added\" listener to graph: "
747 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
751 *out_listener_id
= listener_id
;
754 return BT_FUNC_STATUS_OK
;
757 enum bt_graph_add_listener_status
758 bt_graph_add_sink_component_input_port_added_listener(
759 struct bt_graph
*graph
,
760 bt_graph_sink_component_input_port_added_listener_func func
,
761 void *data
, bt_listener_id
*out_listener_id
)
763 struct bt_graph_listener_port_added listener
= {
764 .func
= (port_added_func_t
) func
,
767 bt_listener_id listener_id
;
769 BT_ASSERT_PRE_NO_ERROR();
770 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
771 BT_ASSERT_PRE_NON_NULL(func
, "Listener");
772 g_array_append_val(graph
->listeners
.sink_input_port_added
, listener
);
773 listener_id
= graph
->listeners
.sink_input_port_added
->len
- 1;
774 BT_LIB_LOGD("Added \"sink component input port added\" listener to graph: "
775 "%![graph-]+g, listener-addr=%p, id=%d", graph
, listener
,
779 *out_listener_id
= listener_id
;
782 return BT_FUNC_STATUS_OK
;
786 enum bt_graph_listener_func_status
bt_graph_notify_port_added(
787 struct bt_graph
*graph
, struct bt_port
*port
)
791 struct bt_component
*comp
;
792 enum bt_graph_listener_func_status status
= BT_FUNC_STATUS_OK
;
796 BT_LIB_LOGD("Notifying graph listeners that a port was added: "
797 "%![graph-]+g, %![port-]+p", graph
, port
);
798 comp
= bt_port_borrow_component_inline(port
);
801 switch (comp
->class->type
) {
802 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
804 switch (port
->type
) {
805 case BT_PORT_TYPE_OUTPUT
:
806 listeners
= graph
->listeners
.source_output_port_added
;
814 case BT_COMPONENT_CLASS_TYPE_FILTER
:
816 switch (port
->type
) {
817 case BT_PORT_TYPE_INPUT
:
818 listeners
= graph
->listeners
.filter_input_port_added
;
820 case BT_PORT_TYPE_OUTPUT
:
821 listeners
= graph
->listeners
.filter_output_port_added
;
829 case BT_COMPONENT_CLASS_TYPE_SINK
:
831 switch (port
->type
) {
832 case BT_PORT_TYPE_INPUT
:
833 listeners
= graph
->listeners
.sink_input_port_added
;
845 for (i
= 0; i
< listeners
->len
; i
++) {
846 struct bt_graph_listener_port_added
*listener
=
847 &g_array_index(listeners
,
848 struct bt_graph_listener_port_added
, i
);
851 BT_ASSERT(listener
->func
);
852 status
= listener
->func(comp
, port
, listener
->data
);
853 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
854 if (status
!= BT_FUNC_STATUS_OK
) {
864 void bt_graph_remove_connection(struct bt_graph
*graph
,
865 struct bt_connection
*connection
)
868 BT_ASSERT(connection
);
869 BT_LIB_LOGD("Removing graph's connection: %![graph-]+g, %![conn-]+x",
871 g_ptr_array_remove(graph
->connections
, connection
);
875 bool component_name_exists(struct bt_graph
*graph
, const char *name
)
880 for (i
= 0; i
< graph
->components
->len
; i
++) {
881 struct bt_component
*other_comp
= graph
->components
->pdata
[i
];
883 if (strcmp(name
, bt_component_get_name(other_comp
)) == 0) {
884 BT_ASSERT_PRE_MSG("Another component with the same name already exists in the graph: "
885 "%![other-comp-]+c, name=\"%s\"",
897 int add_component_with_init_method_data(
898 struct bt_graph
*graph
,
899 struct bt_component_class
*comp_cls
,
900 comp_init_method_t init_method
,
901 const char *name
, const struct bt_value
*params
,
902 void *init_method_data
, bt_logging_level log_level
,
903 const struct bt_component
**user_component
)
905 int status
= BT_FUNC_STATUS_OK
;
906 enum bt_component_class_initialize_method_status init_status
;
907 struct bt_component
*component
= NULL
;
909 bool init_can_consume
;
910 struct bt_value
*new_params
= NULL
;
913 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
914 BT_ASSERT_PRE_NON_NULL(name
, "Name");
916 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
917 "Graph is not in the \"configuring\" state: %!+g", graph
);
918 BT_ASSERT_PRE(!component_name_exists(graph
, name
),
919 "Duplicate component name: %!+g, name=\"%s\"", graph
, name
);
920 BT_ASSERT_PRE(!params
|| bt_value_is_map(params
),
921 "Parameter value is not a map value: %!+v", params
);
922 init_can_consume
= graph
->can_consume
;
923 bt_graph_set_can_consume(graph
, false);
924 BT_LIB_LOGI("Adding component to graph: "
925 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
926 "%![params-]+v, init-method-data-addr=%p",
927 graph
, comp_cls
, name
,
928 bt_common_logging_level_string(log_level
), params
,
932 new_params
= bt_value_map_create();
934 BT_LIB_LOGE_APPEND_CAUSE(
935 "Cannot create empty map value object.");
936 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
943 ret
= bt_component_create(comp_cls
, name
, log_level
, &component
);
945 BT_LIB_LOGE_APPEND_CAUSE(
946 "Cannot create empty component object: ret=%d",
948 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
953 * The user's initialization method needs to see that this
954 * component is part of the graph. If the user method fails, we
955 * immediately remove the component from the graph's components.
957 g_ptr_array_add(graph
->components
, component
);
958 bt_component_set_graph(component
, graph
);
959 bt_value_freeze(params
);
963 * There is no use for config objects right now, so just pass
966 BT_LOGD_STR("Calling user's initialization method.");
967 init_status
= init_method(component
, NULL
, params
, init_method_data
);
968 BT_LOGD("User method returned: status=%s",
969 bt_common_func_status_string(init_status
));
970 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(init_status
);
971 if (init_status
!= BT_FUNC_STATUS_OK
) {
972 if (init_status
< 0) {
973 BT_LIB_LOGW_APPEND_CAUSE(
974 "Component initialization method failed: "
975 "status=%s, %![comp-]+c",
976 bt_common_func_status_string(init_status
),
980 status
= init_status
;
981 bt_component_set_graph(component
, NULL
);
982 g_ptr_array_remove_fast(graph
->components
, component
);
988 * Mark the component as initialized so that its finalization
989 * method is called when it is destroyed.
991 component
->initialized
= true;
994 * If it's a sink component, it needs to be part of the graph's
995 * sink queue to be consumed by bt_graph_run() or
996 * bt_graph_run_once().
998 if (bt_component_is_sink(component
)) {
999 graph
->has_sink
= true;
1000 g_queue_push_tail(graph
->sinks_to_consume
, component
);
1004 * Freeze the component class now that it's instantiated at
1007 BT_LOGD_STR("Freezing component class.");
1008 bt_component_class_freeze(comp_cls
);
1009 BT_LIB_LOGI("Added component to graph: "
1010 "%![graph-]+g, %![cc-]+C, name=\"%s\", log-level=%s, "
1011 "%![params-]+v, init-method-data-addr=%p, %![comp-]+c",
1012 graph
, comp_cls
, name
,
1013 bt_common_logging_level_string(log_level
), params
,
1014 init_method_data
, component
);
1016 if (user_component
) {
1017 /* Move reference to user */
1018 *user_component
= component
;
1022 if (status
!= BT_FUNC_STATUS_OK
) {
1023 bt_graph_make_faulty(graph
);
1026 bt_object_put_ref(component
);
1027 bt_object_put_ref(new_params
);
1028 (void) init_can_consume
;
1029 bt_graph_set_can_consume(graph
, init_can_consume
);
1033 enum bt_graph_add_component_status
1034 bt_graph_add_source_component_with_initialize_method_data(
1035 struct bt_graph
*graph
,
1036 const struct bt_component_class_source
*comp_cls
,
1037 const char *name
, const struct bt_value
*params
,
1038 void *init_method_data
, bt_logging_level log_level
,
1039 const struct bt_component_source
**component
)
1041 BT_ASSERT_PRE_NO_ERROR();
1042 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1043 return add_component_with_init_method_data(graph
,
1044 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1045 name
, params
, init_method_data
, log_level
, (void *) component
);
1048 enum bt_graph_add_component_status
bt_graph_add_source_component(
1049 struct bt_graph
*graph
,
1050 const struct bt_component_class_source
*comp_cls
,
1051 const char *name
, const struct bt_value
*params
,
1052 enum bt_logging_level log_level
,
1053 const struct bt_component_source
**component
)
1055 BT_ASSERT_PRE_NO_ERROR();
1056 return bt_graph_add_source_component_with_initialize_method_data(
1057 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1060 enum bt_graph_add_component_status
1061 bt_graph_add_filter_component_with_initialize_method_data(
1062 struct bt_graph
*graph
,
1063 const struct bt_component_class_filter
*comp_cls
,
1064 const char *name
, const struct bt_value
*params
,
1065 void *init_method_data
, enum bt_logging_level log_level
,
1066 const struct bt_component_filter
**component
)
1068 BT_ASSERT_PRE_NO_ERROR();
1069 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1070 return add_component_with_init_method_data(graph
,
1071 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1072 name
, params
, init_method_data
, log_level
, (void *) component
);
1075 enum bt_graph_add_component_status
bt_graph_add_filter_component(
1076 struct bt_graph
*graph
,
1077 const struct bt_component_class_filter
*comp_cls
,
1078 const char *name
, const struct bt_value
*params
,
1079 enum bt_logging_level log_level
,
1080 const struct bt_component_filter
**component
)
1082 BT_ASSERT_PRE_NO_ERROR();
1083 return bt_graph_add_filter_component_with_initialize_method_data(
1084 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1087 enum bt_graph_add_component_status
1088 bt_graph_add_sink_component_with_initialize_method_data(
1089 struct bt_graph
*graph
,
1090 const struct bt_component_class_sink
*comp_cls
,
1091 const char *name
, const struct bt_value
*params
,
1092 void *init_method_data
, enum bt_logging_level log_level
,
1093 const struct bt_component_sink
**component
)
1095 BT_ASSERT_PRE_NO_ERROR();
1096 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
1097 return add_component_with_init_method_data(graph
,
1098 (void *) comp_cls
, (comp_init_method_t
) comp_cls
->methods
.init
,
1099 name
, params
, init_method_data
, log_level
, (void *) component
);
1102 enum bt_graph_add_component_status
bt_graph_add_sink_component(
1103 struct bt_graph
*graph
,
1104 const struct bt_component_class_sink
*comp_cls
,
1105 const char *name
, const struct bt_value
*params
,
1106 enum bt_logging_level log_level
,
1107 const struct bt_component_sink
**component
)
1109 BT_ASSERT_PRE_NO_ERROR();
1110 return bt_graph_add_sink_component_with_initialize_method_data(
1111 graph
, comp_cls
, name
, params
, NULL
, log_level
, component
);
1114 enum bt_graph_add_component_status
1115 bt_graph_add_simple_sink_component(struct bt_graph
*graph
, const char *name
,
1116 bt_graph_simple_sink_component_initialize_func init_func
,
1117 bt_graph_simple_sink_component_consume_func consume_func
,
1118 bt_graph_simple_sink_component_finalize_func finalize_func
,
1119 void *user_data
, const bt_component_sink
**component
)
1121 enum bt_graph_add_component_status status
;
1122 struct bt_component_class_sink
*comp_cls
;
1123 struct simple_sink_init_method_data init_method_data
= {
1124 .init_func
= init_func
,
1125 .consume_func
= consume_func
,
1126 .finalize_func
= finalize_func
,
1127 .user_data
= user_data
,
1130 BT_ASSERT_PRE_NO_ERROR();
1133 * Other preconditions are checked by
1134 * bt_graph_add_sink_component_with_init_method_data().
1136 BT_ASSERT_PRE_NON_NULL(consume_func
, "Consume function");
1138 comp_cls
= bt_component_class_sink_simple_borrow();
1140 BT_LIB_LOGE_APPEND_CAUSE(
1141 "Cannot borrow simple sink component class.");
1142 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1146 status
= bt_graph_add_sink_component_with_initialize_method_data(graph
,
1147 comp_cls
, name
, NULL
, &init_method_data
,
1148 BT_LOGGING_LEVEL_NONE
, component
);
1155 void bt_graph_add_message(struct bt_graph
*graph
,
1156 struct bt_message
*msg
)
1162 * It's okay not to take a reference because, when a
1163 * message's reference count drops to 0, either:
1165 * * It is recycled back to one of this graph's pool.
1166 * * It is destroyed because it doesn't have any link to any
1167 * graph, which means the original graph is already destroyed.
1169 g_ptr_array_add(graph
->messages
, msg
);
1173 bool bt_graph_is_interrupted(const struct bt_graph
*graph
)
1175 BT_ASSERT_DBG(graph
);
1176 return bt_interrupter_array_any_is_set(graph
->interrupters
);
1179 enum bt_graph_add_interrupter_status
bt_graph_add_interrupter(
1180 struct bt_graph
*graph
, const struct bt_interrupter
*intr
)
1182 BT_ASSERT_PRE_NO_ERROR();
1183 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1184 BT_ASSERT_PRE_NON_NULL(intr
, "Interrupter");
1185 g_ptr_array_add(graph
->interrupters
, (void *) intr
);
1186 bt_object_get_ref_no_null_check(intr
);
1187 BT_LIB_LOGD("Added interrupter to graph: %![graph-]+g, %![intr-]+z",
1189 return BT_FUNC_STATUS_OK
;
1192 struct bt_interrupter
*bt_graph_borrow_default_interrupter(bt_graph
*graph
)
1194 BT_ASSERT_PRE_NON_NULL(graph
, "Graph");
1195 return graph
->default_interrupter
;
1198 void bt_graph_get_ref(const struct bt_graph
*graph
)
1200 bt_object_get_ref(graph
);
1203 void bt_graph_put_ref(const struct bt_graph
*graph
)
1205 bt_object_put_ref(graph
);