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-pre.h"
14 #include "lib/assert-post.h"
15 #include <babeltrace2/graph/self-component.h>
16 #include <babeltrace2/graph/component.h>
17 #include <babeltrace2/graph/graph.h>
18 #include "common/macros.h"
19 #include "compat/compiler.h"
20 #include <babeltrace2/types.h>
21 #include <babeltrace2/value.h>
22 #include "lib/value.h"
26 #include "component.h"
27 #include "component-class.h"
28 #include "component-source.h"
29 #include "component-filter.h"
30 #include "component-sink.h"
31 #include "connection.h"
33 #include "message/iterator.h"
35 #include "lib/func-status.h"
38 struct bt_component
* (* const component_create_funcs
[])(
39 const struct bt_component_class
*) = {
40 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
41 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
42 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
46 void (*component_destroy_funcs
[])(struct bt_component
*) = {
47 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
48 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
49 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
53 void finalize_component(struct bt_component
*comp
)
55 typedef void (*method_t
)(void *);
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
;
69 case BT_COMPONENT_CLASS_TYPE_FILTER
:
71 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
73 method
= (method_t
) flt_cc
->methods
.finalize
;
76 case BT_COMPONENT_CLASS_TYPE_SINK
:
78 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
80 method
= (method_t
) sink_cc
->methods
.finalize
;
88 const struct bt_error
*saved_error
;
90 saved_error
= bt_current_thread_take_error();
92 BT_LIB_LOGI("Calling user's component finalization method: "
95 BT_ASSERT_POST_NO_ERROR();
98 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
104 void destroy_component(struct bt_object
*obj
)
106 struct bt_component
*component
= NULL
;
114 * The component's reference count is 0 if we're here. Increment
115 * it to avoid a double-destroy (possibly infinitely recursive).
116 * This could happen for example if the component's finalization
117 * function does bt_object_get_ref() (or anything that causes
118 * bt_object_get_ref() to be called) on itself (ref. count goes
119 * from 0 to 1), and then bt_object_put_ref(): the reference
120 * count would go from 1 to 0 again and this function would be
124 component
= container_of(obj
, struct bt_component
, base
);
125 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
126 component
, bt_component_borrow_graph(component
));
128 /* Call destroy listeners in reverse registration order */
129 BT_LOGD_STR("Calling destroy listeners.");
131 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
132 struct bt_component_destroy_listener
*listener
=
133 &g_array_index(component
->destroy_listeners
,
134 struct bt_component_destroy_listener
, i
);
136 listener
->func(component
, listener
->data
);
140 * User data is destroyed first, followed by the concrete
141 * component instance. Do not finalize if the component's user
142 * initialization method failed in the first place.
144 if (component
->initialized
) {
145 finalize_component(component
);
148 if (component
->destroy
) {
149 BT_LOGD_STR("Destroying type-specific data.");
150 component
->destroy(component
);
153 if (component
->input_ports
) {
154 BT_LOGD_STR("Destroying input ports.");
155 g_ptr_array_free(component
->input_ports
, TRUE
);
156 component
->input_ports
= NULL
;
159 if (component
->output_ports
) {
160 BT_LOGD_STR("Destroying output ports.");
161 g_ptr_array_free(component
->output_ports
, TRUE
);
162 component
->output_ports
= NULL
;
165 if (component
->destroy_listeners
) {
166 g_array_free(component
->destroy_listeners
, TRUE
);
167 component
->destroy_listeners
= NULL
;
170 if (component
->name
) {
171 g_string_free(component
->name
, TRUE
);
172 component
->name
= NULL
;
175 BT_LOGD_STR("Putting component class.");
176 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
180 enum bt_component_class_type
bt_component_get_class_type(
181 const struct bt_component
*component
)
183 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
184 return component
->class->type
;
188 enum bt_self_component_add_port_status
add_port(
189 struct bt_component
*component
, GPtrArray
*ports
,
190 enum bt_port_type port_type
, const char *name
, void *user_data
,
191 struct bt_port
**port
)
193 struct bt_port
*new_port
= NULL
;
194 struct bt_graph
*graph
= NULL
;
195 enum bt_self_component_add_port_status status
;
197 BT_ASSERT_PRE_NON_NULL(component
, "Component");
198 BT_ASSERT_PRE_NON_NULL(name
, "Name");
199 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
200 graph
= bt_component_borrow_graph(component
);
202 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
203 "Component's graph is already configured: "
204 "%![comp-]+c, %![graph-]+g", component
, graph
);
206 // TODO: Validate that the name is not already used.
208 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
209 "port-type=%s, port-name=\"%s\"", component
,
210 bt_port_type_string(port_type
), name
);
212 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
214 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
215 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
220 * No name clash, add the port.
221 * The component is now the port's parent; it should _not_
222 * hold a reference to the port since the port's lifetime
223 * is now protected by the component's own lifetime.
225 g_ptr_array_add(ports
, new_port
);
228 * Notify the graph's creator that a new port was added.
230 graph
= bt_component_borrow_graph(component
);
232 enum bt_graph_listener_func_status listener_status
;
234 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
235 if (listener_status
!= BT_FUNC_STATUS_OK
) {
236 bt_graph_make_faulty(graph
);
237 status
= (int) listener_status
;
242 BT_LIB_LOGI("Created and added port to component: "
243 "%![comp-]+c, %![port-]+p", component
, new_port
);
246 status
= BT_FUNC_STATUS_OK
;
251 * We need to release the reference that we would otherwise have
252 * returned to the caller.
254 BT_PORT_PUT_REF_AND_RESET(new_port
);
261 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
263 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
264 return (uint64_t) comp
->input_ports
->len
;
268 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
270 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
271 return (uint64_t) comp
->output_ports
->len
;
275 int bt_component_create(struct bt_component_class
*component_class
,
276 const char *name
, bt_logging_level log_level
,
277 struct bt_component
**user_component
)
280 struct bt_component
*component
= NULL
;
281 enum bt_component_class_type type
;
283 BT_ASSERT(user_component
);
284 BT_ASSERT(component_class
);
286 type
= bt_component_class_get_type(component_class
);
287 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
288 "comp-name=\"%s\", log-level=%s", component_class
, name
,
289 bt_common_logging_level_string(log_level
));
290 component
= component_create_funcs
[type
](component_class
);
292 BT_LIB_LOGE_APPEND_CAUSE(
293 "Cannot create specific component object.");
298 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
299 component
->class = component_class
;
300 bt_object_get_ref_no_null_check(component
->class);
301 component
->destroy
= component_destroy_funcs
[type
];
302 component
->name
= g_string_new(name
);
303 if (!component
->name
) {
304 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
309 component
->log_level
= log_level
;
310 component
->input_ports
= g_ptr_array_new_with_free_func(
311 (GDestroyNotify
) bt_object_try_spec_release
);
312 if (!component
->input_ports
) {
313 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
318 component
->output_ports
= g_ptr_array_new_with_free_func(
319 (GDestroyNotify
) bt_object_try_spec_release
);
320 if (!component
->output_ports
) {
321 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
326 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
327 sizeof(struct bt_component_destroy_listener
));
328 if (!component
->destroy_listeners
) {
329 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
334 BT_LIB_LOGI("Created empty component from component class: "
335 "%![cc-]+C, %![comp-]+c", component_class
, component
);
336 BT_OBJECT_MOVE_REF(*user_component
, component
);
339 bt_object_put_ref(component
);
343 const char *bt_component_get_name(const struct bt_component
*component
)
345 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
346 return component
->name
->str
;
349 const struct bt_component_class
*bt_component_borrow_class_const(
350 const struct bt_component
*component
)
352 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
353 return component
->class;
356 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
358 struct bt_component
*component
= (void *) self_comp
;
360 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
361 return component
->user_data
;
364 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
367 struct bt_component
*component
= (void *) self_comp
;
369 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
370 component
->user_data
= data
;
371 BT_LIB_LOGD("Set component's user data: %!+c", component
);
375 void bt_component_set_graph(struct bt_component
*component
,
376 struct bt_graph
*graph
)
378 bt_object_set_parent(&component
->base
,
379 graph
? &graph
->base
: NULL
);
383 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
387 struct bt_port
*ret_port
= NULL
;
391 for (i
= 0; i
< ports
->len
; i
++) {
392 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
394 if (strcmp(name
, port
->name
->str
) == 0) {
404 struct bt_port_input
*bt_component_borrow_input_port_by_name(
405 struct bt_component
*comp
, const char *name
)
408 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
412 struct bt_port_output
*bt_component_borrow_output_port_by_name(
413 struct bt_component
*comp
, const char *name
)
415 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
417 borrow_port_by_name(comp
->output_ports
, name
);
421 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
423 BT_ASSERT(index
< ports
->len
);
424 return g_ptr_array_index(ports
, index
);
428 struct bt_port_input
*bt_component_borrow_input_port_by_index(
429 struct bt_component
*comp
, uint64_t index
)
431 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
432 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, comp
->input_ports
->len
);
434 borrow_port_by_index(comp
->input_ports
, index
);
438 struct bt_port_output
*bt_component_borrow_output_port_by_index(
439 struct bt_component
*comp
, uint64_t index
)
441 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
442 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, comp
->output_ports
->len
);
444 borrow_port_by_index(comp
->output_ports
, index
);
448 enum bt_self_component_add_port_status
bt_component_add_input_port(
449 struct bt_component
*component
, const char *name
,
450 void *user_data
, struct bt_port
**port
)
452 /* add_port() logs details */
453 return add_port(component
, component
->input_ports
,
454 BT_PORT_TYPE_INPUT
, name
, user_data
, port
);
458 enum bt_self_component_add_port_status
bt_component_add_output_port(
459 struct bt_component
*component
, const char *name
,
460 void *user_data
, struct bt_port
**port
)
462 /* add_port() logs details */
463 return add_port(component
, component
->output_ports
,
464 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
);
468 bool bt_component_port_name_is_unique(GPtrArray
*ports
, const char *name
)
473 for (i
= 0; i
< ports
->len
; i
++) {
474 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
476 if (strcmp(port
->name
->str
, name
) == 0) {
489 enum bt_component_class_port_connected_method_status
490 bt_component_port_connected(
491 struct bt_component
*comp
, struct bt_port
*self_port
,
492 struct bt_port
*other_port
)
494 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
495 void *, void *, const void *);
497 enum bt_component_class_port_connected_method_status status
=
499 method_t method
= NULL
;
502 BT_ASSERT(self_port
);
503 BT_ASSERT(other_port
);
505 switch (comp
->class->type
) {
506 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
508 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
510 switch (self_port
->type
) {
511 case BT_PORT_TYPE_OUTPUT
:
512 method
= (method_t
) src_cc
->methods
.output_port_connected
;
520 case BT_COMPONENT_CLASS_TYPE_FILTER
:
522 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
524 switch (self_port
->type
) {
525 case BT_PORT_TYPE_INPUT
:
526 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
528 case BT_PORT_TYPE_OUTPUT
:
529 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
537 case BT_COMPONENT_CLASS_TYPE_SINK
:
539 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
541 switch (self_port
->type
) {
542 case BT_PORT_TYPE_INPUT
:
543 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
556 BT_LIB_LOGD("Calling user's \"port connected\" method: "
557 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
558 comp
, self_port
, other_port
);
559 status
= (int) method(comp
, self_port
, (void *) other_port
);
560 BT_LOGD("User method returned: status=%s",
561 bt_common_func_status_string(status
));
562 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
563 status
== BT_FUNC_STATUS_ERROR
||
564 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
565 "Unexpected returned component status: status=%s",
566 bt_common_func_status_string(status
));
567 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
574 void bt_component_add_destroy_listener(struct bt_component
*component
,
575 bt_component_destroy_listener_func func
, void *data
)
577 struct bt_component_destroy_listener listener
;
579 BT_ASSERT(component
);
581 listener
.func
= func
;
582 listener
.data
= data
;
583 g_array_append_val(component
->destroy_listeners
, listener
);
584 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
585 "func-addr=%p, data-addr=%p",
586 component
, func
, data
);
590 void bt_component_remove_destroy_listener(struct bt_component
*component
,
591 bt_component_destroy_listener_func func
, void *data
)
595 BT_ASSERT(component
);
598 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
599 struct bt_component_destroy_listener
*listener
=
600 &g_array_index(component
->destroy_listeners
,
601 struct bt_component_destroy_listener
, i
);
603 if (listener
->func
== func
&& listener
->data
== data
) {
604 g_array_remove_index(component
->destroy_listeners
, i
);
606 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
607 "func-addr=%p, data-addr=%p",
608 component
, func
, data
);
613 bt_logging_level
bt_component_get_logging_level(
614 const struct bt_component
*component
)
616 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
617 return component
->log_level
;
620 uint64_t bt_self_component_get_graph_mip_version(
621 bt_self_component
*self_component
)
623 struct bt_component
*comp
= (void *) self_component
;
625 BT_ASSERT_PRE_NON_NULL(self_component
, "Component");
626 return bt_component_borrow_graph(comp
)->mip_version
;
629 void bt_component_get_ref(const struct bt_component
*component
)
631 bt_object_get_ref(component
);
634 void bt_component_put_ref(const struct bt_component
*component
)
636 bt_object_put_ref(component
);