2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 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 "COMP"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/graph/self-component.h>
28 #include <babeltrace/graph/component-const.h>
29 #include <babeltrace/graph/component-source-const.h>
30 #include <babeltrace/graph/component-filter-const.h>
31 #include <babeltrace/graph/component-sink-const.h>
32 #include <babeltrace/graph/component-internal.h>
33 #include <babeltrace/graph/component-class-internal.h>
34 #include <babeltrace/graph/component-source-internal.h>
35 #include <babeltrace/graph/component-filter-internal.h>
36 #include <babeltrace/graph/component-sink-internal.h>
37 #include <babeltrace/graph/connection-internal.h>
38 #include <babeltrace/graph/graph-internal.h>
39 #include <babeltrace/graph/notification-iterator-internal.h>
40 #include <babeltrace/graph/port-internal.h>
41 #include <babeltrace/babeltrace-internal.h>
42 #include <babeltrace/compiler-internal.h>
43 #include <babeltrace/object.h>
44 #include <babeltrace/types.h>
45 #include <babeltrace/value.h>
46 #include <babeltrace/value-internal.h>
47 #include <babeltrace/assert-internal.h>
48 #include <babeltrace/assert-pre-internal.h>
53 struct bt_component
* (* const component_create_funcs
[])(
54 const struct bt_component_class
*) = {
55 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
56 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
57 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
61 void (*component_destroy_funcs
[])(struct bt_component
*) = {
62 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
63 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
64 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
68 void finalize_component(struct bt_component
*comp
)
70 typedef void (*method_t
)(void *);
72 method_t method
= NULL
;
76 switch (comp
->class->type
) {
77 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
79 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
81 method
= (method_t
) src_cc
->methods
.finalize
;
84 case BT_COMPONENT_CLASS_TYPE_FILTER
:
86 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
88 method
= (method_t
) flt_cc
->methods
.finalize
;
91 case BT_COMPONENT_CLASS_TYPE_SINK
:
93 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
95 method
= (method_t
) sink_cc
->methods
.finalize
;
103 BT_LIB_LOGD("Calling user's finalization method: "
104 "%![comp-]+c", comp
);
110 void destroy_component(struct bt_object
*obj
)
112 struct bt_component
*component
= NULL
;
120 * The component's reference count is 0 if we're here. Increment
121 * it to avoid a double-destroy (possibly infinitely recursive).
122 * This could happen for example if the component's finalization
123 * function does bt_object_get_ref() (or anything that causes
124 * bt_object_get_ref() to be called) on itself (ref. count goes
125 * from 0 to 1), and then bt_object_put_ref(): the reference
126 * count would go from 1 to 0 again and this function would be
130 component
= container_of(obj
, struct bt_component
, base
);
131 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
132 component
, bt_component_borrow_graph(component
));
134 /* Call destroy listeners in reverse registration order */
135 BT_LOGD_STR("Calling destroy listeners.");
137 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
138 struct bt_component_destroy_listener
*listener
=
139 &g_array_index(component
->destroy_listeners
,
140 struct bt_component_destroy_listener
, i
);
142 listener
->func(component
, listener
->data
);
146 * User data is destroyed first, followed by the concrete
147 * component instance. Do not finalize if the component's user
148 * initialization method failed in the first place.
150 if (component
->initialized
) {
151 finalize_component(component
);
154 if (component
->destroy
) {
155 BT_LOGD_STR("Destroying type-specific data.");
156 component
->destroy(component
);
159 if (component
->input_ports
) {
160 BT_LOGD_STR("Destroying input ports.");
161 g_ptr_array_free(component
->input_ports
, TRUE
);
162 component
->input_ports
= NULL
;
165 if (component
->output_ports
) {
166 BT_LOGD_STR("Destroying output ports.");
167 g_ptr_array_free(component
->output_ports
, TRUE
);
168 component
->output_ports
= NULL
;
171 if (component
->destroy_listeners
) {
172 g_array_free(component
->destroy_listeners
, TRUE
);
173 component
->destroy_listeners
= NULL
;
176 if (component
->name
) {
177 g_string_free(component
->name
, TRUE
);
178 component
->name
= NULL
;
181 BT_LOGD_STR("Putting component class.");
182 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
186 enum bt_component_class_type
bt_component_get_class_type(
187 const struct bt_component
*component
)
189 BT_ASSERT_PRE_NON_NULL(component
, "Component");
190 return component
->class->type
;
194 struct bt_port
*add_port(
195 struct bt_component
*component
, GPtrArray
*ports
,
196 enum bt_port_type port_type
, const char *name
, void *user_data
)
198 struct bt_port
*new_port
= NULL
;
199 struct bt_graph
*graph
= NULL
;
201 BT_ASSERT_PRE_NON_NULL(component
, "Component");
202 BT_ASSERT_PRE_NON_NULL(name
, "Name");
203 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
204 graph
= bt_component_borrow_graph(component
);
205 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
206 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
209 // TODO: Validate that the name is not already used.
211 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
212 "port-type=%s, port-name=\"%s\"", component
,
213 bt_port_type_string(port_type
), name
);
215 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
217 BT_LOGE_STR("Cannot create port object.");
222 * No name clash, add the port.
223 * The component is now the port's parent; it should _not_
224 * hold a reference to the port since the port's lifetime
225 * is now protected by the component's own lifetime.
227 g_ptr_array_add(ports
, new_port
);
230 * Notify the graph's creator that a new port was added.
232 bt_object_get_ref(bt_component_borrow_graph(component
));
233 graph
= bt_component_borrow_graph(component
);
235 bt_graph_notify_port_added(graph
, new_port
);
236 BT_OBJECT_PUT_REF_AND_RESET(graph
);
239 BT_LIB_LOGD("Created and added port to component: "
240 "%![comp-]+c, %![port-]+p", component
, new_port
);
247 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
249 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
250 return (uint64_t) comp
->input_ports
->len
;
254 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
256 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
257 return (uint64_t) comp
->output_ports
->len
;
261 int bt_component_create(struct bt_component_class
*component_class
,
262 const char *name
, struct bt_component
**user_component
)
265 struct bt_component
*component
= NULL
;
266 enum bt_component_class_type type
;
268 BT_ASSERT(user_component
);
269 BT_ASSERT(component_class
);
272 type
= bt_component_class_get_type(component_class
);
273 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
274 "comp-name=\"%s\"", component_class
, name
);
275 component
= component_create_funcs
[type
](component_class
);
277 BT_LOGE_STR("Cannot create specific component object.");
282 bt_object_init_shared_with_parent(&component
->base
,
284 component
->class = component_class
;
285 bt_object_get_no_null_check(component
->class);
286 component
->destroy
= component_destroy_funcs
[type
];
287 component
->name
= g_string_new(name
);
288 if (!component
->name
) {
289 BT_LOGE_STR("Failed to allocate one GString.");
294 component
->input_ports
= g_ptr_array_new_with_free_func(
295 (GDestroyNotify
) bt_object_try_spec_release
);
296 if (!component
->input_ports
) {
297 BT_LOGE_STR("Failed to allocate one GPtrArray.");
302 component
->output_ports
= g_ptr_array_new_with_free_func(
303 (GDestroyNotify
) bt_object_try_spec_release
);
304 if (!component
->output_ports
) {
305 BT_LOGE_STR("Failed to allocate one GPtrArray.");
310 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
311 sizeof(struct bt_component_destroy_listener
));
312 if (!component
->destroy_listeners
) {
313 BT_LOGE_STR("Failed to allocate one GArray.");
318 BT_LIB_LOGD("Created empty component from component class: "
319 "%![cc-]+C, %![comp-]+c", component_class
, component
);
320 BT_OBJECT_MOVE_REF(*user_component
, component
);
323 bt_object_put_ref(component
);
327 const char *bt_component_get_name(const struct bt_component
*component
)
329 BT_ASSERT_PRE_NON_NULL(component
, "Component");
330 return component
->name
->str
;
333 struct bt_component_class
*bt_component_borrow_class(
334 const struct bt_component
*component
)
336 BT_ASSERT_PRE_NON_NULL(component
, "Component");
337 return component
->class;
340 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
342 struct bt_component
*component
= (void *) self_comp
;
344 BT_ASSERT_PRE_NON_NULL(component
, "Component");
345 return component
->user_data
;
348 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
351 struct bt_component
*component
= (void *) self_comp
;
353 BT_ASSERT_PRE_NON_NULL(component
, "Component");
354 component
->user_data
= data
;
355 BT_LIB_LOGV("Set component's user data: %!+c", component
);
359 void bt_component_set_graph(struct bt_component
*component
,
360 struct bt_graph
*graph
)
362 bt_object_set_parent(&component
->base
,
363 graph
? &graph
->base
: NULL
);
366 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
368 return bt_graph_is_canceled(
369 (void *) bt_object_borrow_parent(&component
->base
));
373 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
377 struct bt_port
*ret_port
= NULL
;
381 for (i
= 0; i
< ports
->len
; i
++) {
382 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
384 if (!strcmp(name
, port
->name
->str
)) {
394 struct bt_port_input
*bt_component_borrow_input_port_by_name(
395 struct bt_component
*comp
, const char *name
)
398 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
402 struct bt_port_output
*bt_component_borrow_output_port_by_name(
403 struct bt_component
*comp
, const char *name
)
405 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
407 borrow_port_by_name(comp
->output_ports
, name
);
411 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
413 BT_ASSERT(index
< ports
->len
);
414 return g_ptr_array_index(ports
, index
);
418 struct bt_port_input
*bt_component_borrow_input_port_by_index(
419 struct bt_component
*comp
, uint64_t index
)
421 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
422 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->input_ports
->len
);
424 borrow_port_by_index(comp
->input_ports
, index
);
428 struct bt_port_output
*bt_component_borrow_output_port_by_index(
429 struct bt_component
*comp
, uint64_t index
)
431 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
432 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
434 borrow_port_by_index(comp
->output_ports
, index
);
438 struct bt_port_input
*bt_component_add_input_port(
439 struct bt_component
*component
, const char *name
,
442 /* add_port() logs details */
444 add_port(component
, component
->input_ports
,
445 BT_PORT_TYPE_INPUT
, name
, user_data
);
449 struct bt_port_output
*bt_component_add_output_port(
450 struct bt_component
*component
, const char *name
,
453 /* add_port() logs details */
455 add_port(component
, component
->output_ports
,
456 BT_PORT_TYPE_OUTPUT
, name
, user_data
);
460 void remove_port_by_index(struct bt_component
*component
,
461 GPtrArray
*ports
, uint64_t index
)
463 struct bt_port
*port
;
464 struct bt_graph
*graph
;
467 BT_ASSERT(index
< ports
->len
);
468 port
= g_ptr_array_index(ports
, index
);
469 BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p",
472 /* Disconnect both ports of this port's connection, if any */
473 if (port
->connection
) {
474 bt_connection_end(port
->connection
, true);
478 * The port's current reference count can be 0 at this point,
479 * which means its parent (component) keeps it alive. We are
480 * about to remove the port from its parent's container (with
481 * the g_ptr_array_remove_index() call below), which in this
482 * case would destroy it. This is not good because we still
483 * need the port for the bt_graph_notify_port_removed() call
484 * below (in which its component is `NULL` as expected because
485 * of the bt_object_set_parent() call below).
487 * To avoid a destroyed port during the notification callback,
488 * get a reference now, and put it (destroying the port if its
489 * reference count is 0 at this point) after notifying the
492 bt_object_get_no_null_check(&port
->base
);
495 * Remove from parent's array of ports (weak refs). This never
496 * destroys the port object because its reference count is at
497 * least 1 thanks to the bt_object_get_no_null_check() call
500 g_ptr_array_remove_index(ports
, index
);
502 /* Detach port from its component parent */
503 bt_object_set_parent(&port
->base
, NULL
);
506 * Notify the graph's creator that a port is removed.
508 graph
= bt_component_borrow_graph(component
);
510 bt_graph_notify_port_removed(graph
, component
, port
);
513 BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p",
517 * Put the local reference. If this port's reference count was 0
518 * when entering this function, it is 1 now, so it is destroyed
521 bt_object_put_no_null_check(&port
->base
);
525 void bt_component_remove_port(struct bt_component
*component
,
526 struct bt_port
*port
)
529 GPtrArray
*ports
= NULL
;
531 BT_ASSERT(component
);
534 switch (port
->type
) {
535 case BT_PORT_TYPE_INPUT
:
536 ports
= component
->input_ports
;
538 case BT_PORT_TYPE_OUTPUT
:
539 ports
= component
->output_ports
;
547 for (i
= 0; i
< ports
->len
; i
++) {
548 struct bt_port
*cur_port
= g_ptr_array_index(ports
, i
);
550 if (cur_port
== port
) {
551 remove_port_by_index(component
,
557 BT_LIB_LOGW("Port to remove from component was not found: "
558 "%![comp-]+c, %![port-]+p", component
, port
);
565 enum bt_self_component_status
bt_component_accept_port_connection(
566 struct bt_component
*comp
, struct bt_port
*self_port
,
567 struct bt_port
*other_port
)
569 typedef enum bt_self_component_status (*method_t
)(
570 void *, void *, const void *);
572 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
573 method_t method
= NULL
;
576 BT_ASSERT(self_port
);
577 BT_ASSERT(other_port
);
579 switch (comp
->class->type
) {
580 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
582 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
584 switch (self_port
->type
) {
585 case BT_PORT_TYPE_OUTPUT
:
586 method
= (method_t
) src_cc
->methods
.accept_output_port_connection
;
594 case BT_COMPONENT_CLASS_TYPE_FILTER
:
596 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
598 switch (self_port
->type
) {
599 case BT_PORT_TYPE_INPUT
:
600 method
= (method_t
) flt_cc
->methods
.accept_input_port_connection
;
602 case BT_PORT_TYPE_OUTPUT
:
603 method
= (method_t
) flt_cc
->methods
.accept_output_port_connection
;
611 case BT_COMPONENT_CLASS_TYPE_SINK
:
613 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
615 switch (self_port
->type
) {
616 case BT_PORT_TYPE_INPUT
:
617 method
= (method_t
) sink_cc
->methods
.accept_input_port_connection
;
630 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
631 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
632 comp
, self_port
, other_port
);
633 status
= method(comp
, self_port
, (void *) other_port
);
634 BT_LOGD("User method returned: status=%s",
635 bt_self_component_status_string(status
));
642 enum bt_self_component_status
bt_component_port_connected(
643 struct bt_component
*comp
, struct bt_port
*self_port
,
644 struct bt_port
*other_port
)
646 typedef enum bt_self_component_status (*method_t
)(
647 void *, void *, const void *);
649 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
650 method_t method
= NULL
;
653 BT_ASSERT(self_port
);
654 BT_ASSERT(other_port
);
656 switch (comp
->class->type
) {
657 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
659 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
661 switch (self_port
->type
) {
662 case BT_PORT_TYPE_OUTPUT
:
663 method
= (method_t
) src_cc
->methods
.output_port_connected
;
671 case BT_COMPONENT_CLASS_TYPE_FILTER
:
673 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
675 switch (self_port
->type
) {
676 case BT_PORT_TYPE_INPUT
:
677 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
679 case BT_PORT_TYPE_OUTPUT
:
680 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
688 case BT_COMPONENT_CLASS_TYPE_SINK
:
690 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
692 switch (self_port
->type
) {
693 case BT_PORT_TYPE_INPUT
:
694 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
707 BT_LIB_LOGD("Calling user's \"port connected\" method: "
708 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
709 comp
, self_port
, other_port
);
710 status
= method(comp
, self_port
, (void *) other_port
);
711 BT_LOGD("User method returned: status=%s",
712 bt_self_component_status_string(status
));
719 void bt_component_port_disconnected(struct bt_component
*comp
,
720 struct bt_port
*port
)
722 typedef void (*method_t
)(void *, void *);
724 method_t method
= NULL
;
729 switch (comp
->class->type
) {
730 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
732 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
734 switch (port
->type
) {
735 case BT_PORT_TYPE_OUTPUT
:
736 method
= (method_t
) src_cc
->methods
.output_port_disconnected
;
744 case BT_COMPONENT_CLASS_TYPE_FILTER
:
746 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
748 switch (port
->type
) {
749 case BT_PORT_TYPE_INPUT
:
750 method
= (method_t
) flt_cc
->methods
.input_port_disconnected
;
752 case BT_PORT_TYPE_OUTPUT
:
753 method
= (method_t
) flt_cc
->methods
.output_port_disconnected
;
761 case BT_COMPONENT_CLASS_TYPE_SINK
:
763 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
765 switch (port
->type
) {
766 case BT_PORT_TYPE_INPUT
:
767 method
= (method_t
) sink_cc
->methods
.input_port_disconnected
;
780 BT_LIB_LOGD("Calling user's \"port disconnected\" method: "
781 "%![comp-]+c, %![port-]+p", comp
, port
);
787 void bt_component_add_destroy_listener(struct bt_component
*component
,
788 bt_component_destroy_listener_func func
, void *data
)
790 struct bt_component_destroy_listener listener
;
792 BT_ASSERT(component
);
794 listener
.func
= func
;
795 listener
.data
= data
;
796 g_array_append_val(component
->destroy_listeners
, listener
);
797 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
798 "func-addr=%p, data-addr=%p",
799 component
, func
, data
);
803 void bt_component_remove_destroy_listener(struct bt_component
*component
,
804 bt_component_destroy_listener_func func
, void *data
)
808 BT_ASSERT(component
);
811 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
812 struct bt_component_destroy_listener
*listener
=
813 &g_array_index(component
->destroy_listeners
,
814 struct bt_component_destroy_listener
, i
);
816 if (listener
->func
== func
&& listener
->data
== data
) {
817 g_array_remove_index(component
->destroy_listeners
, i
);
819 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
820 "func-addr=%p, data-addr=%p",
821 component
, func
, data
);