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/assert-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/graph/self-component.h>
30 #include <babeltrace/graph/component-const.h>
31 #include <babeltrace/graph/component-source-const.h>
32 #include <babeltrace/graph/component-filter-const.h>
33 #include <babeltrace/graph/component-sink-const.h>
34 #include <babeltrace/graph/component-internal.h>
35 #include <babeltrace/graph/component-class-internal.h>
36 #include <babeltrace/graph/component-source-internal.h>
37 #include <babeltrace/graph/component-filter-internal.h>
38 #include <babeltrace/graph/component-sink-internal.h>
39 #include <babeltrace/graph/connection-internal.h>
40 #include <babeltrace/graph/graph-internal.h>
41 #include <babeltrace/graph/message-iterator-internal.h>
42 #include <babeltrace/graph/port-internal.h>
43 #include <babeltrace/babeltrace-internal.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/types.h>
46 #include <babeltrace/value.h>
47 #include <babeltrace/value-internal.h>
52 struct bt_component
* (* const component_create_funcs
[])(
53 const struct bt_component_class
*) = {
54 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
55 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
56 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
60 void (*component_destroy_funcs
[])(struct bt_component
*) = {
61 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
62 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
63 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
67 void finalize_component(struct bt_component
*comp
)
69 typedef void (*method_t
)(void *);
71 method_t method
= NULL
;
75 switch (comp
->class->type
) {
76 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
78 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
80 method
= (method_t
) src_cc
->methods
.finalize
;
83 case BT_COMPONENT_CLASS_TYPE_FILTER
:
85 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
87 method
= (method_t
) flt_cc
->methods
.finalize
;
90 case BT_COMPONENT_CLASS_TYPE_SINK
:
92 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
94 method
= (method_t
) sink_cc
->methods
.finalize
;
102 BT_LIB_LOGD("Calling user's finalization method: "
103 "%![comp-]+c", comp
);
109 void destroy_component(struct bt_object
*obj
)
111 struct bt_component
*component
= NULL
;
119 * The component's reference count is 0 if we're here. Increment
120 * it to avoid a double-destroy (possibly infinitely recursive).
121 * This could happen for example if the component's finalization
122 * function does bt_object_get_ref() (or anything that causes
123 * bt_object_get_ref() to be called) on itself (ref. count goes
124 * from 0 to 1), and then bt_object_put_ref(): the reference
125 * count would go from 1 to 0 again and this function would be
129 component
= container_of(obj
, struct bt_component
, base
);
130 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
131 component
, bt_component_borrow_graph(component
));
133 /* Call destroy listeners in reverse registration order */
134 BT_LOGD_STR("Calling destroy listeners.");
136 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
137 struct bt_component_destroy_listener
*listener
=
138 &g_array_index(component
->destroy_listeners
,
139 struct bt_component_destroy_listener
, i
);
141 listener
->func(component
, listener
->data
);
145 * User data is destroyed first, followed by the concrete
146 * component instance. Do not finalize if the component's user
147 * initialization method failed in the first place.
149 if (component
->initialized
) {
150 finalize_component(component
);
153 if (component
->destroy
) {
154 BT_LOGD_STR("Destroying type-specific data.");
155 component
->destroy(component
);
158 if (component
->input_ports
) {
159 BT_LOGD_STR("Destroying input ports.");
160 g_ptr_array_free(component
->input_ports
, TRUE
);
161 component
->input_ports
= NULL
;
164 if (component
->output_ports
) {
165 BT_LOGD_STR("Destroying output ports.");
166 g_ptr_array_free(component
->output_ports
, TRUE
);
167 component
->output_ports
= NULL
;
170 if (component
->destroy_listeners
) {
171 g_array_free(component
->destroy_listeners
, TRUE
);
172 component
->destroy_listeners
= NULL
;
175 if (component
->name
) {
176 g_string_free(component
->name
, TRUE
);
177 component
->name
= NULL
;
180 BT_LOGD_STR("Putting component class.");
181 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
185 enum bt_component_class_type
bt_component_get_class_type(
186 const struct bt_component
*component
)
188 BT_ASSERT_PRE_NON_NULL(component
, "Component");
189 return component
->class->type
;
193 struct bt_port
*add_port(
194 struct bt_component
*component
, GPtrArray
*ports
,
195 enum bt_port_type port_type
, const char *name
, void *user_data
)
197 struct bt_port
*new_port
= NULL
;
198 struct bt_graph
*graph
= NULL
;
200 BT_ASSERT_PRE_NON_NULL(component
, "Component");
201 BT_ASSERT_PRE_NON_NULL(name
, "Name");
202 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
203 graph
= bt_component_borrow_graph(component
);
204 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
205 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
208 // TODO: Validate that the name is not already used.
210 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
211 "port-type=%s, port-name=\"%s\"", component
,
212 bt_port_type_string(port_type
), name
);
214 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
216 BT_LOGE_STR("Cannot create port object.");
221 * No name clash, add the port.
222 * The component is now the port's parent; it should _not_
223 * hold a reference to the port since the port's lifetime
224 * is now protected by the component's own lifetime.
226 g_ptr_array_add(ports
, new_port
);
229 * Notify the graph's creator that a new port was added.
231 bt_object_get_ref(bt_component_borrow_graph(component
));
232 graph
= bt_component_borrow_graph(component
);
234 bt_graph_notify_port_added(graph
, new_port
);
235 BT_OBJECT_PUT_REF_AND_RESET(graph
);
238 BT_LIB_LOGD("Created and added port to component: "
239 "%![comp-]+c, %![port-]+p", component
, new_port
);
246 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
248 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
249 return (uint64_t) comp
->input_ports
->len
;
253 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
255 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
256 return (uint64_t) comp
->output_ports
->len
;
260 int bt_component_create(struct bt_component_class
*component_class
,
261 const char *name
, struct bt_component
**user_component
)
264 struct bt_component
*component
= NULL
;
265 enum bt_component_class_type type
;
267 BT_ASSERT(user_component
);
268 BT_ASSERT(component_class
);
270 type
= bt_component_class_get_type(component_class
);
271 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
272 "comp-name=\"%s\"", component_class
, name
);
273 component
= component_create_funcs
[type
](component_class
);
275 BT_LOGE_STR("Cannot create specific component object.");
280 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
281 component
->class = component_class
;
282 bt_object_get_no_null_check(component
->class);
283 component
->destroy
= component_destroy_funcs
[type
];
284 component
->name
= g_string_new(name
);
285 if (!component
->name
) {
286 BT_LOGE_STR("Failed to allocate one GString.");
291 component
->input_ports
= g_ptr_array_new_with_free_func(
292 (GDestroyNotify
) bt_object_try_spec_release
);
293 if (!component
->input_ports
) {
294 BT_LOGE_STR("Failed to allocate one GPtrArray.");
299 component
->output_ports
= g_ptr_array_new_with_free_func(
300 (GDestroyNotify
) bt_object_try_spec_release
);
301 if (!component
->output_ports
) {
302 BT_LOGE_STR("Failed to allocate one GPtrArray.");
307 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
308 sizeof(struct bt_component_destroy_listener
));
309 if (!component
->destroy_listeners
) {
310 BT_LOGE_STR("Failed to allocate one GArray.");
315 BT_LIB_LOGD("Created empty component from component class: "
316 "%![cc-]+C, %![comp-]+c", component_class
, component
);
317 BT_OBJECT_MOVE_REF(*user_component
, component
);
320 bt_object_put_ref(component
);
324 const char *bt_component_get_name(const struct bt_component
*component
)
326 BT_ASSERT_PRE_NON_NULL(component
, "Component");
327 return component
->name
->str
;
330 struct bt_component_class
*bt_component_borrow_class(
331 const struct bt_component
*component
)
333 BT_ASSERT_PRE_NON_NULL(component
, "Component");
334 return component
->class;
337 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
339 struct bt_component
*component
= (void *) self_comp
;
341 BT_ASSERT_PRE_NON_NULL(component
, "Component");
342 return component
->user_data
;
345 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
348 struct bt_component
*component
= (void *) self_comp
;
350 BT_ASSERT_PRE_NON_NULL(component
, "Component");
351 component
->user_data
= data
;
352 BT_LIB_LOGV("Set component's user data: %!+c", component
);
356 void bt_component_set_graph(struct bt_component
*component
,
357 struct bt_graph
*graph
)
359 bt_object_set_parent(&component
->base
,
360 graph
? &graph
->base
: NULL
);
363 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
365 return bt_graph_is_canceled(
366 (void *) bt_object_borrow_parent(&component
->base
));
370 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
374 struct bt_port
*ret_port
= NULL
;
378 for (i
= 0; i
< ports
->len
; i
++) {
379 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
381 if (!strcmp(name
, port
->name
->str
)) {
391 struct bt_port_input
*bt_component_borrow_input_port_by_name(
392 struct bt_component
*comp
, const char *name
)
395 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
399 struct bt_port_output
*bt_component_borrow_output_port_by_name(
400 struct bt_component
*comp
, const char *name
)
402 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
404 borrow_port_by_name(comp
->output_ports
, name
);
408 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
410 BT_ASSERT(index
< ports
->len
);
411 return g_ptr_array_index(ports
, index
);
415 struct bt_port_input
*bt_component_borrow_input_port_by_index(
416 struct bt_component
*comp
, uint64_t index
)
418 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
419 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->input_ports
->len
);
421 borrow_port_by_index(comp
->input_ports
, index
);
425 struct bt_port_output
*bt_component_borrow_output_port_by_index(
426 struct bt_component
*comp
, uint64_t index
)
428 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
429 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
431 borrow_port_by_index(comp
->output_ports
, index
);
435 struct bt_port_input
*bt_component_add_input_port(
436 struct bt_component
*component
, const char *name
,
439 /* add_port() logs details */
441 add_port(component
, component
->input_ports
,
442 BT_PORT_TYPE_INPUT
, name
, user_data
);
446 struct bt_port_output
*bt_component_add_output_port(
447 struct bt_component
*component
, const char *name
,
450 /* add_port() logs details */
452 add_port(component
, component
->output_ports
,
453 BT_PORT_TYPE_OUTPUT
, name
, user_data
);
457 void remove_port_by_index(struct bt_component
*component
,
458 GPtrArray
*ports
, uint64_t index
)
460 struct bt_port
*port
;
461 struct bt_graph
*graph
;
464 BT_ASSERT(index
< ports
->len
);
465 port
= g_ptr_array_index(ports
, index
);
466 BT_LIB_LOGD("Removing port from component: %![comp-]+c, %![port-]+p",
469 /* Disconnect both ports of this port's connection, if any */
470 if (port
->connection
) {
471 bt_connection_end(port
->connection
, true);
475 * The port's current reference count can be 0 at this point,
476 * which means its parent (component) keeps it alive. We are
477 * about to remove the port from its parent's container (with
478 * the g_ptr_array_remove_index() call below), which in this
479 * case would destroy it. This is not good because we still
480 * need the port for the bt_graph_notify_port_removed() call
481 * below (in which its component is `NULL` as expected because
482 * of the bt_object_set_parent() call below).
484 * To avoid a destroyed port during the message callback,
485 * get a reference now, and put it (destroying the port if its
486 * reference count is 0 at this point) after notifying the
489 bt_object_get_no_null_check(&port
->base
);
492 * Remove from parent's array of ports (weak refs). This never
493 * destroys the port object because its reference count is at
494 * least 1 thanks to the bt_object_get_no_null_check() call
497 g_ptr_array_remove_index(ports
, index
);
499 /* Detach port from its component parent */
500 bt_object_set_parent(&port
->base
, NULL
);
503 * Notify the graph's creator that a port is removed.
505 graph
= bt_component_borrow_graph(component
);
507 bt_graph_notify_port_removed(graph
, component
, port
);
510 BT_LIB_LOGD("Removed port from component: %![comp-]+c, %![port-]+p",
514 * Put the local reference. If this port's reference count was 0
515 * when entering this function, it is 1 now, so it is destroyed
518 bt_object_put_no_null_check(&port
->base
);
522 void bt_component_remove_port(struct bt_component
*component
,
523 struct bt_port
*port
)
526 GPtrArray
*ports
= NULL
;
528 BT_ASSERT(component
);
531 switch (port
->type
) {
532 case BT_PORT_TYPE_INPUT
:
533 ports
= component
->input_ports
;
535 case BT_PORT_TYPE_OUTPUT
:
536 ports
= component
->output_ports
;
544 for (i
= 0; i
< ports
->len
; i
++) {
545 struct bt_port
*cur_port
= g_ptr_array_index(ports
, i
);
547 if (cur_port
== port
) {
548 remove_port_by_index(component
, ports
, i
);
553 BT_LIB_LOGW("Port to remove from component was not found: "
554 "%![comp-]+c, %![port-]+p", component
, port
);
561 enum bt_self_component_status
bt_component_accept_port_connection(
562 struct bt_component
*comp
, struct bt_port
*self_port
,
563 struct bt_port
*other_port
)
565 typedef enum bt_self_component_status (*method_t
)(
566 void *, void *, const void *);
568 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
569 method_t method
= NULL
;
572 BT_ASSERT(self_port
);
573 BT_ASSERT(other_port
);
575 switch (comp
->class->type
) {
576 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
578 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
580 switch (self_port
->type
) {
581 case BT_PORT_TYPE_OUTPUT
:
582 method
= (method_t
) src_cc
->methods
.accept_output_port_connection
;
590 case BT_COMPONENT_CLASS_TYPE_FILTER
:
592 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
594 switch (self_port
->type
) {
595 case BT_PORT_TYPE_INPUT
:
596 method
= (method_t
) flt_cc
->methods
.accept_input_port_connection
;
598 case BT_PORT_TYPE_OUTPUT
:
599 method
= (method_t
) flt_cc
->methods
.accept_output_port_connection
;
607 case BT_COMPONENT_CLASS_TYPE_SINK
:
609 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
611 switch (self_port
->type
) {
612 case BT_PORT_TYPE_INPUT
:
613 method
= (method_t
) sink_cc
->methods
.accept_input_port_connection
;
626 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
627 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
628 comp
, self_port
, other_port
);
629 status
= method(comp
, self_port
, (void *) other_port
);
630 BT_LOGD("User method returned: status=%s",
631 bt_self_component_status_string(status
));
638 enum bt_self_component_status
bt_component_port_connected(
639 struct bt_component
*comp
, struct bt_port
*self_port
,
640 struct bt_port
*other_port
)
642 typedef enum bt_self_component_status (*method_t
)(
643 void *, void *, const void *);
645 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
646 method_t method
= NULL
;
649 BT_ASSERT(self_port
);
650 BT_ASSERT(other_port
);
652 switch (comp
->class->type
) {
653 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
655 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
657 switch (self_port
->type
) {
658 case BT_PORT_TYPE_OUTPUT
:
659 method
= (method_t
) src_cc
->methods
.output_port_connected
;
667 case BT_COMPONENT_CLASS_TYPE_FILTER
:
669 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
671 switch (self_port
->type
) {
672 case BT_PORT_TYPE_INPUT
:
673 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
675 case BT_PORT_TYPE_OUTPUT
:
676 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
684 case BT_COMPONENT_CLASS_TYPE_SINK
:
686 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
688 switch (self_port
->type
) {
689 case BT_PORT_TYPE_INPUT
:
690 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
703 BT_LIB_LOGD("Calling user's \"port connected\" method: "
704 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
705 comp
, self_port
, other_port
);
706 status
= method(comp
, self_port
, (void *) other_port
);
707 BT_LOGD("User method returned: status=%s",
708 bt_self_component_status_string(status
));
715 void bt_component_port_disconnected(struct bt_component
*comp
,
716 struct bt_port
*port
)
718 typedef void (*method_t
)(void *, void *);
720 method_t method
= NULL
;
725 switch (comp
->class->type
) {
726 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
728 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
730 switch (port
->type
) {
731 case BT_PORT_TYPE_OUTPUT
:
732 method
= (method_t
) src_cc
->methods
.output_port_disconnected
;
740 case BT_COMPONENT_CLASS_TYPE_FILTER
:
742 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
744 switch (port
->type
) {
745 case BT_PORT_TYPE_INPUT
:
746 method
= (method_t
) flt_cc
->methods
.input_port_disconnected
;
748 case BT_PORT_TYPE_OUTPUT
:
749 method
= (method_t
) flt_cc
->methods
.output_port_disconnected
;
757 case BT_COMPONENT_CLASS_TYPE_SINK
:
759 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
761 switch (port
->type
) {
762 case BT_PORT_TYPE_INPUT
:
763 method
= (method_t
) sink_cc
->methods
.input_port_disconnected
;
776 BT_LIB_LOGD("Calling user's \"port disconnected\" method: "
777 "%![comp-]+c, %![port-]+p", comp
, port
);
783 void bt_component_add_destroy_listener(struct bt_component
*component
,
784 bt_component_destroy_listener_func func
, void *data
)
786 struct bt_component_destroy_listener listener
;
788 BT_ASSERT(component
);
790 listener
.func
= func
;
791 listener
.data
= data
;
792 g_array_append_val(component
->destroy_listeners
, listener
);
793 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
794 "func-addr=%p, data-addr=%p",
795 component
, func
, data
);
799 void bt_component_remove_destroy_listener(struct bt_component
*component
,
800 bt_component_destroy_listener_func func
, void *data
)
804 BT_ASSERT(component
);
807 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
808 struct bt_component_destroy_listener
*listener
=
809 &g_array_index(component
->destroy_listeners
,
810 struct bt_component_destroy_listener
, i
);
812 if (listener
->func
== func
&& listener
->data
== data
) {
813 g_array_remove_index(component
->destroy_listeners
, i
);
815 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
816 "func-addr=%p, data-addr=%p",
817 component
, func
, data
);
822 void bt_component_get_ref(const struct bt_component
*component
)
824 bt_object_get_ref(component
);
827 void bt_component_put_ref(const struct bt_component
*component
)
829 bt_object_put_ref(component
);