2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/COMPONENT"
9 #include "lib/logging.h"
11 #include "common/common.h"
12 #include "common/assert.h"
13 #include "lib/assert-cond.h"
14 #include <babeltrace2/graph/self-component.h>
15 #include <babeltrace2/graph/component.h>
16 #include <babeltrace2/graph/graph.h>
17 #include "common/macros.h"
18 #include "compat/compiler.h"
19 #include <babeltrace2/types.h>
20 #include <babeltrace2/value.h>
21 #include "lib/value.h"
25 #include "component.h"
26 #include "component-class.h"
27 #include "component-source.h"
28 #include "component-filter.h"
29 #include "component-sink.h"
30 #include "connection.h"
32 #include "message/iterator.h"
34 #include "lib/func-status.h"
37 struct bt_component
* (* const component_create_funcs
[])(
38 const struct bt_component_class
*) = {
39 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
40 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
41 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
45 void (*component_destroy_funcs
[])(struct bt_component
*) = {
46 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
47 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
48 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
52 void finalize_component(struct bt_component
*comp
)
54 typedef void (*method_t
)(void *);
55 const char *method_name
;
57 method_t method
= NULL
;
61 switch (comp
->class->type
) {
62 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
64 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
66 method
= (method_t
) src_cc
->methods
.finalize
;
67 method_name
= "bt_component_class_source_finalize_method";
70 case BT_COMPONENT_CLASS_TYPE_FILTER
:
72 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
74 method
= (method_t
) flt_cc
->methods
.finalize
;
75 method_name
= "bt_component_class_filter_finalize_method";
78 case BT_COMPONENT_CLASS_TYPE_SINK
:
80 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
82 method
= (method_t
) sink_cc
->methods
.finalize
;
83 method_name
= "bt_component_class_sink_finalize_method";
91 const struct bt_error
*saved_error
;
93 saved_error
= bt_current_thread_take_error();
95 BT_LIB_LOGI("Calling user's component finalization method: "
98 BT_ASSERT_POST_NO_ERROR(method_name
);
101 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
107 void destroy_component(struct bt_object
*obj
)
109 struct bt_component
*component
= NULL
;
117 * The component's reference count is 0 if we're here. Increment
118 * it to avoid a double-destroy (possibly infinitely recursive).
119 * This could happen for example if the component's finalization
120 * function does bt_object_get_ref() (or anything that causes
121 * bt_object_get_ref() to be called) on itself (ref. count goes
122 * from 0 to 1), and then bt_object_put_ref(): the reference
123 * count would go from 1 to 0 again and this function would be
127 component
= container_of(obj
, struct bt_component
, base
);
128 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
129 component
, bt_component_borrow_graph(component
));
131 /* Call destroy listeners in reverse registration order */
132 BT_LOGD_STR("Calling destroy listeners.");
134 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
135 struct bt_component_destroy_listener
*listener
=
136 &g_array_index(component
->destroy_listeners
,
137 struct bt_component_destroy_listener
, i
);
139 listener
->func(component
, listener
->data
);
143 * User data is destroyed first, followed by the concrete
144 * component instance. Do not finalize if the component's user
145 * initialization method failed in the first place.
147 if (component
->initialized
) {
148 finalize_component(component
);
151 if (component
->destroy
) {
152 BT_LOGD_STR("Destroying type-specific data.");
153 component
->destroy(component
);
156 if (component
->input_ports
) {
157 BT_LOGD_STR("Destroying input ports.");
158 g_ptr_array_free(component
->input_ports
, TRUE
);
159 component
->input_ports
= NULL
;
162 if (component
->output_ports
) {
163 BT_LOGD_STR("Destroying output ports.");
164 g_ptr_array_free(component
->output_ports
, TRUE
);
165 component
->output_ports
= NULL
;
168 if (component
->destroy_listeners
) {
169 g_array_free(component
->destroy_listeners
, TRUE
);
170 component
->destroy_listeners
= NULL
;
173 if (component
->name
) {
174 g_string_free(component
->name
, TRUE
);
175 component
->name
= NULL
;
178 BT_LOGD_STR("Putting component class.");
179 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
184 enum bt_component_class_type
bt_component_get_class_type(
185 const struct bt_component
*component
)
187 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
188 return component
->class->type
;
192 enum bt_self_component_add_port_status
add_port(
193 struct bt_component
*component
, GPtrArray
*ports
,
194 enum bt_port_type port_type
, const char *name
, void *user_data
,
195 struct bt_port
**port
, const char *api_func
)
197 struct bt_port
*new_port
= NULL
;
198 struct bt_graph
*graph
= NULL
;
199 enum bt_self_component_add_port_status status
;
201 BT_ASSERT(component
);
203 BT_ASSERT_PRE_FROM_FUNC(api_func
, "name-is-not-empty",
204 strlen(name
) > 0, "Name is empty");
205 graph
= bt_component_borrow_graph(component
);
206 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-not-configured",
207 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
208 "Component's graph is already configured: "
209 "%![comp-]+c, %![graph-]+g", component
, graph
);
211 // TODO: Validate that the name is not already used.
213 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
214 "port-type=%s, port-name=\"%s\"", component
,
215 bt_port_type_string(port_type
), name
);
217 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
219 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
220 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
225 * No name clash, add the port.
226 * The component is now the port's parent; it should _not_
227 * hold a reference to the port since the port's lifetime
228 * is now protected by the component's own lifetime.
230 g_ptr_array_add(ports
, new_port
);
233 * Notify the graph's creator that a new port was added.
235 graph
= bt_component_borrow_graph(component
);
237 enum bt_graph_listener_func_status listener_status
;
239 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
240 if (listener_status
!= BT_FUNC_STATUS_OK
) {
241 bt_graph_make_faulty(graph
);
242 status
= (int) listener_status
;
247 BT_LIB_LOGI("Created and added port to component: "
248 "%![comp-]+c, %![port-]+p", component
, new_port
);
251 status
= BT_FUNC_STATUS_OK
;
256 * We need to release the reference that we would otherwise have
257 * returned to the caller.
259 BT_PORT_PUT_REF_AND_RESET(new_port
);
265 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
,
266 const char *api_func
)
268 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
269 return (uint64_t) comp
->input_ports
->len
;
272 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
,
273 const char *api_func
)
275 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
276 return (uint64_t) comp
->output_ports
->len
;
279 int bt_component_create(struct bt_component_class
*component_class
,
280 const char *name
, bt_logging_level log_level
,
281 struct bt_component
**user_component
)
284 struct bt_component
*component
= NULL
;
285 enum bt_component_class_type type
;
287 BT_ASSERT(user_component
);
288 BT_ASSERT(component_class
);
290 type
= bt_component_class_get_type(component_class
);
291 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
292 "comp-name=\"%s\", log-level=%s", component_class
, name
,
293 bt_common_logging_level_string(log_level
));
294 component
= component_create_funcs
[type
](component_class
);
296 BT_LIB_LOGE_APPEND_CAUSE(
297 "Cannot create specific component object.");
302 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
303 component
->class = component_class
;
304 bt_object_get_ref_no_null_check(component
->class);
305 component
->destroy
= component_destroy_funcs
[type
];
306 component
->name
= g_string_new(name
);
307 if (!component
->name
) {
308 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
313 component
->log_level
= log_level
;
314 component
->input_ports
= g_ptr_array_new_with_free_func(
315 (GDestroyNotify
) bt_object_try_spec_release
);
316 if (!component
->input_ports
) {
317 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
322 component
->output_ports
= g_ptr_array_new_with_free_func(
323 (GDestroyNotify
) bt_object_try_spec_release
);
324 if (!component
->output_ports
) {
325 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
330 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
331 sizeof(struct bt_component_destroy_listener
));
332 if (!component
->destroy_listeners
) {
333 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
338 BT_LIB_LOGI("Created empty component from component class: "
339 "%![cc-]+C, %![comp-]+c", component_class
, component
);
340 BT_OBJECT_MOVE_REF(*user_component
, component
);
343 bt_object_put_ref(component
);
348 const char *bt_component_get_name(const struct bt_component
*component
)
350 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
351 return component
->name
->str
;
355 const struct bt_component_class
*bt_component_borrow_class_const(
356 const struct bt_component
*component
)
358 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
359 return component
->class;
363 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
365 struct bt_component
*component
= (void *) self_comp
;
367 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
368 return component
->user_data
;
372 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
375 struct bt_component
*component
= (void *) self_comp
;
377 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
378 component
->user_data
= data
;
379 BT_LIB_LOGD("Set component's user data: %!+c", component
);
382 void bt_component_set_graph(struct bt_component
*component
,
383 struct bt_graph
*graph
)
385 bt_object_set_parent(&component
->base
,
386 graph
? &graph
->base
: NULL
);
390 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
391 const char *name
, const char *api_func
)
394 struct bt_port
*ret_port
= NULL
;
396 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
398 for (i
= 0; i
< ports
->len
; i
++) {
399 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
401 if (strcmp(name
, port
->name
->str
) == 0) {
410 struct bt_port_input
*bt_component_borrow_input_port_by_name(
411 struct bt_component
*comp
, const char *name
,
412 const char *api_func
)
414 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
415 return (void *) borrow_port_by_name(comp
->input_ports
, name
, api_func
);
418 struct bt_port_output
*bt_component_borrow_output_port_by_name(
419 struct bt_component
*comp
, const char *name
,
420 const char *api_func
)
422 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
424 borrow_port_by_name(comp
->output_ports
, name
, api_func
);
428 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
,
429 const char *api_func
)
431 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
, ports
->len
);
432 return g_ptr_array_index(ports
, index
);
435 struct bt_port_input
*bt_component_borrow_input_port_by_index(
436 struct bt_component
*comp
, uint64_t index
,
437 const char *api_func
)
439 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
441 borrow_port_by_index(comp
->input_ports
, index
, api_func
);
444 struct bt_port_output
*bt_component_borrow_output_port_by_index(
445 struct bt_component
*comp
, uint64_t index
,
446 const char *api_func
)
448 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
450 borrow_port_by_index(comp
->output_ports
, index
, api_func
);
454 bool port_name_is_unique(GPtrArray
*ports
, const char *name
)
459 for (i
= 0; i
< ports
->len
; i
++) {
460 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
462 if (strcmp(port
->name
->str
, name
) == 0) {
474 enum bt_self_component_add_port_status
bt_component_add_input_port(
475 struct bt_component
*component
, const char *name
,
476 void *user_data
, struct bt_port
**port
, const char *api_func
)
478 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
479 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func
, component
);
480 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
481 BT_ASSERT_PRE_FROM_FUNC(api_func
, "input-port-name-is-unique",
482 port_name_is_unique(component
->input_ports
, name
),
483 "Input port name is not unique: name=\"%s\", %![comp-]c",
486 /* add_port() logs details and checks preconditions */
487 return add_port(component
, component
->input_ports
,
488 BT_PORT_TYPE_INPUT
, name
, user_data
, port
, api_func
);
491 enum bt_self_component_add_port_status
bt_component_add_output_port(
492 struct bt_component
*component
, const char *name
,
493 void *user_data
, struct bt_port
**port
,
494 const char *api_func
)
496 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
497 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func
, component
);
498 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
499 BT_ASSERT_PRE_FROM_FUNC(api_func
, "output-port-name-is-unique",
500 port_name_is_unique(component
->output_ports
, name
),
501 "Output port name is not unique: name=\"%s\", %![comp-]c",
504 /* add_port() logs details and checks preconditions */
505 return add_port(component
, component
->output_ports
,
506 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
, api_func
);
509 enum bt_component_class_port_connected_method_status
510 bt_component_port_connected(
511 struct bt_component
*comp
, struct bt_port
*self_port
,
512 struct bt_port
*other_port
)
514 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
515 void *, void *, const void *);
517 enum bt_component_class_port_connected_method_status status
=
519 method_t method
= NULL
;
520 const char *method_name
= NULL
;
523 BT_ASSERT(self_port
);
524 BT_ASSERT(other_port
);
526 switch (comp
->class->type
) {
527 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
529 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
531 switch (self_port
->type
) {
532 case BT_PORT_TYPE_OUTPUT
:
533 method
= (method_t
) src_cc
->methods
.output_port_connected
;
534 method_name
= "bt_component_class_source_output_port_connected_method";
542 case BT_COMPONENT_CLASS_TYPE_FILTER
:
544 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
546 switch (self_port
->type
) {
547 case BT_PORT_TYPE_INPUT
:
548 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
549 method_name
= "bt_component_class_filter_input_port_connected_method";
551 case BT_PORT_TYPE_OUTPUT
:
552 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
553 method_name
= "bt_component_class_filter_output_port_connected_method";
561 case BT_COMPONENT_CLASS_TYPE_SINK
:
563 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
565 switch (self_port
->type
) {
566 case BT_PORT_TYPE_INPUT
:
567 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
568 method_name
= "bt_component_class_sink_input_port_connected_method";
581 BT_LIB_LOGD("Calling user's \"port connected\" method: "
582 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
583 comp
, self_port
, other_port
);
584 status
= (int) method(comp
, self_port
, (void *) other_port
);
585 BT_LOGD("User method returned: status=%s",
586 bt_common_func_status_string(status
));
587 BT_ASSERT_POST(method_name
, "valid-status",
588 status
== BT_FUNC_STATUS_OK
||
589 status
== BT_FUNC_STATUS_ERROR
||
590 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
591 "Unexpected returned component status: status=%s",
592 bt_common_func_status_string(status
));
593 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name
, status
);
599 void bt_component_add_destroy_listener(struct bt_component
*component
,
600 bt_component_destroy_listener_func func
, void *data
)
602 struct bt_component_destroy_listener listener
;
604 BT_ASSERT(component
);
606 listener
.func
= func
;
607 listener
.data
= data
;
608 g_array_append_val(component
->destroy_listeners
, listener
);
609 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
610 "func-addr=%p, data-addr=%p",
611 component
, func
, data
);
614 void bt_component_remove_destroy_listener(struct bt_component
*component
,
615 bt_component_destroy_listener_func func
, void *data
)
619 BT_ASSERT(component
);
622 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
623 struct bt_component_destroy_listener
*listener
=
624 &g_array_index(component
->destroy_listeners
,
625 struct bt_component_destroy_listener
, i
);
627 if (listener
->func
== func
&& listener
->data
== data
) {
628 g_array_remove_index(component
->destroy_listeners
, i
);
630 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
631 "func-addr=%p, data-addr=%p",
632 component
, func
, data
);
638 bt_logging_level
bt_component_get_logging_level(
639 const struct bt_component
*component
)
641 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
642 return component
->log_level
;
646 uint64_t bt_self_component_get_graph_mip_version(
647 bt_self_component
*self_component
)
649 struct bt_component
*comp
= (void *) self_component
;
651 BT_ASSERT_PRE_COMP_NON_NULL(self_component
);
652 return bt_component_borrow_graph(comp
)->mip_version
;
656 void bt_component_get_ref(const struct bt_component
*component
)
658 bt_object_get_ref(component
);
662 void bt_component_put_ref(const struct bt_component
*component
)
664 bt_object_put_ref(component
);