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
[])(void) = {
38 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
39 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
40 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
44 void finalize_component(struct bt_component
*comp
)
46 typedef void (*method_t
)(void *);
47 const char *method_name
;
49 method_t method
= NULL
;
53 switch (comp
->class->type
) {
54 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
56 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
58 method
= (method_t
) src_cc
->methods
.finalize
;
59 method_name
= "bt_component_class_source_finalize_method";
62 case BT_COMPONENT_CLASS_TYPE_FILTER
:
64 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
66 method
= (method_t
) flt_cc
->methods
.finalize
;
67 method_name
= "bt_component_class_filter_finalize_method";
70 case BT_COMPONENT_CLASS_TYPE_SINK
:
72 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
74 method
= (method_t
) sink_cc
->methods
.finalize
;
75 method_name
= "bt_component_class_sink_finalize_method";
83 const struct bt_error
*saved_error
;
85 saved_error
= bt_current_thread_take_error();
87 BT_LIB_LOGI("Calling user's component finalization method: "
90 BT_ASSERT_POST_NO_ERROR(method_name
);
93 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
99 void destroy_component(struct bt_object
*obj
)
101 struct bt_component
*component
= NULL
;
109 * The component's reference count is 0 if we're here. Increment
110 * it to avoid a double-destroy (possibly infinitely recursive).
111 * This could happen for example if the component's finalization
112 * function does bt_object_get_ref() (or anything that causes
113 * bt_object_get_ref() to be called) on itself (ref. count goes
114 * from 0 to 1), and then bt_object_put_ref(): the reference
115 * count would go from 1 to 0 again and this function would be
119 component
= container_of(obj
, struct bt_component
, base
);
120 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
121 component
, bt_component_borrow_graph(component
));
123 /* Call destroy listeners in reverse registration order */
124 BT_LOGD_STR("Calling destroy listeners.");
126 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
127 struct bt_component_destroy_listener
*listener
=
128 &bt_g_array_index(component
->destroy_listeners
,
129 struct bt_component_destroy_listener
, i
);
131 listener
->func(component
, listener
->data
);
135 * User data is destroyed first, followed by the concrete
136 * component instance. Do not finalize if the component's user
137 * initialization method failed in the first place.
139 if (component
->initialized
) {
140 finalize_component(component
);
143 if (component
->input_ports
) {
144 BT_LOGD_STR("Destroying input ports.");
145 g_ptr_array_free(component
->input_ports
, TRUE
);
146 component
->input_ports
= NULL
;
149 if (component
->output_ports
) {
150 BT_LOGD_STR("Destroying output ports.");
151 g_ptr_array_free(component
->output_ports
, TRUE
);
152 component
->output_ports
= NULL
;
155 if (component
->destroy_listeners
) {
156 g_array_free(component
->destroy_listeners
, TRUE
);
157 component
->destroy_listeners
= NULL
;
160 if (component
->name
) {
161 g_string_free(component
->name
, TRUE
);
162 component
->name
= NULL
;
165 BT_LOGD_STR("Putting component class.");
166 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
171 enum bt_component_class_type
bt_component_get_class_type(
172 const struct bt_component
*component
)
174 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
175 return component
->class->type
;
179 bool port_name_is_unique(GPtrArray
*ports
, const char *name
)
184 for (i
= 0; i
< ports
->len
; i
++) {
185 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
187 if (strcmp(port
->name
->str
, name
) == 0) {
200 enum bt_self_component_add_port_status
add_port(
201 struct bt_component
*component
, GPtrArray
*ports
,
202 enum bt_port_type port_type
, const char *name
, void *user_data
,
203 struct bt_port
**port
, const char *api_func
, bool input
)
205 struct bt_port
*new_port
= NULL
;
206 struct bt_graph
*graph
= NULL
;
207 enum bt_self_component_add_port_status status
;
209 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
210 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func
, component
);
211 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
212 BT_ASSERT_PRE_FROM_FUNC(api_func
,
213 input
? "input" : "output" "-port-name-is-unique",
214 port_name_is_unique(component
->output_ports
, name
),
215 input
? "Input" : "Output"
216 " port name is not unique: name=\"%s\", %![comp-]c",
218 BT_ASSERT_PRE_FROM_FUNC(api_func
, "name-is-not-empty",
219 strlen(name
) > 0, "Name is empty");
220 graph
= bt_component_borrow_graph(component
);
221 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-not-configured",
222 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
223 "Component's graph is already configured: "
224 "%![comp-]+c, %![graph-]+g", component
, graph
);
226 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
227 "port-type=%s, port-name=\"%s\"", component
,
228 bt_port_type_string(port_type
), name
);
230 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
232 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
233 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
238 * No name clash, add the port.
239 * The component is now the port's parent; it should _not_
240 * hold a reference to the port since the port's lifetime
241 * is now protected by the component's own lifetime.
243 g_ptr_array_add(ports
, new_port
);
246 * Notify the graph's creator that a new port was added.
248 graph
= bt_component_borrow_graph(component
);
250 enum bt_graph_listener_func_status listener_status
;
252 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
253 if (listener_status
!= BT_FUNC_STATUS_OK
) {
254 bt_graph_make_faulty(graph
);
255 status
= (int) listener_status
;
260 BT_LIB_LOGI("Created and added port to component: "
261 "%![comp-]+c, %![port-]+p", component
, new_port
);
264 status
= BT_FUNC_STATUS_OK
;
269 * We need to release the reference that we would otherwise have
270 * returned to the caller.
272 BT_PORT_PUT_REF_AND_RESET(new_port
);
278 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
,
279 const char *api_func
)
281 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
282 return (uint64_t) comp
->input_ports
->len
;
285 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
,
286 const char *api_func
)
288 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
289 return (uint64_t) comp
->output_ports
->len
;
292 int bt_component_create(struct bt_component_class
*component_class
,
293 const char *name
, bt_logging_level log_level
,
294 struct bt_component
**user_component
)
297 struct bt_component
*component
= NULL
;
298 enum bt_component_class_type type
;
300 BT_ASSERT(user_component
);
301 BT_ASSERT(component_class
);
303 type
= bt_component_class_get_type(component_class
);
304 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
305 "comp-name=\"%s\", log-level=%s", component_class
, name
,
306 bt_common_logging_level_string(log_level
));
307 component
= component_create_funcs
[type
]();
309 BT_LIB_LOGE_APPEND_CAUSE(
310 "Cannot create specific component object.");
315 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
316 component
->class = component_class
;
317 bt_object_get_ref_no_null_check(component
->class);
318 component
->name
= g_string_new(name
);
319 if (!component
->name
) {
320 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
325 component
->log_level
= log_level
;
326 component
->input_ports
= g_ptr_array_new_with_free_func(
327 (GDestroyNotify
) bt_object_try_spec_release
);
328 if (!component
->input_ports
) {
329 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
334 component
->output_ports
= g_ptr_array_new_with_free_func(
335 (GDestroyNotify
) bt_object_try_spec_release
);
336 if (!component
->output_ports
) {
337 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
342 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
343 sizeof(struct bt_component_destroy_listener
));
344 if (!component
->destroy_listeners
) {
345 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
350 BT_LIB_LOGI("Created empty component from component class: "
351 "%![cc-]+C, %![comp-]+c", component_class
, component
);
352 BT_OBJECT_MOVE_REF(*user_component
, component
);
355 bt_object_put_ref(component
);
360 const char *bt_component_get_name(const struct bt_component
*component
)
362 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
363 return component
->name
->str
;
367 const struct bt_component_class
*bt_component_borrow_class_const(
368 const struct bt_component
*component
)
370 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
371 return component
->class;
375 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
377 struct bt_component
*component
= (void *) self_comp
;
379 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
380 return component
->user_data
;
384 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
387 struct bt_component
*component
= (void *) self_comp
;
389 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
390 component
->user_data
= data
;
391 BT_LIB_LOGD("Set component's user data: %!+c", component
);
394 void bt_component_set_graph(struct bt_component
*component
,
395 struct bt_graph
*graph
)
397 bt_object_set_parent(&component
->base
,
398 graph
? &graph
->base
: NULL
);
402 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
403 const char *name
, const char *api_func
)
406 struct bt_port
*ret_port
= NULL
;
408 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
410 for (i
= 0; i
< ports
->len
; i
++) {
411 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
413 if (strcmp(name
, port
->name
->str
) == 0) {
422 struct bt_port_input
*bt_component_borrow_input_port_by_name(
423 struct bt_component
*comp
, const char *name
,
424 const char *api_func
)
426 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
427 return (void *) borrow_port_by_name(comp
->input_ports
, name
, api_func
);
430 struct bt_port_output
*bt_component_borrow_output_port_by_name(
431 struct bt_component
*comp
, const char *name
,
432 const char *api_func
)
434 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
436 borrow_port_by_name(comp
->output_ports
, name
, api_func
);
440 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
,
441 const char *api_func
)
443 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
, ports
->len
);
444 return g_ptr_array_index(ports
, index
);
447 struct bt_port_input
*bt_component_borrow_input_port_by_index(
448 struct bt_component
*comp
, uint64_t index
,
449 const char *api_func
)
451 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
453 borrow_port_by_index(comp
->input_ports
, index
, api_func
);
456 struct bt_port_output
*bt_component_borrow_output_port_by_index(
457 struct bt_component
*comp
, uint64_t index
,
458 const char *api_func
)
460 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
462 borrow_port_by_index(comp
->output_ports
, index
, api_func
);
465 enum bt_self_component_add_port_status
bt_component_add_input_port(
466 struct bt_component
*component
, const char *name
,
467 void *user_data
, struct bt_port
**port
, const char *api_func
)
469 /* add_port() logs details and checks preconditions */
470 return add_port(component
, component
->input_ports
,
471 BT_PORT_TYPE_INPUT
, name
, user_data
, port
, api_func
, true);
474 enum bt_self_component_add_port_status
bt_component_add_output_port(
475 struct bt_component
*component
, const char *name
,
476 void *user_data
, struct bt_port
**port
,
477 const char *api_func
)
479 /* add_port() logs details and checks preconditions */
480 return add_port(component
, component
->output_ports
,
481 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
, api_func
, false);
484 enum bt_component_class_port_connected_method_status
485 bt_component_port_connected(
486 struct bt_component
*comp
, struct bt_port
*self_port
,
487 struct bt_port
*other_port
)
489 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
490 void *, void *, const void *);
492 enum bt_component_class_port_connected_method_status status
=
494 method_t method
= NULL
;
495 const char *method_name
= NULL
;
498 BT_ASSERT(self_port
);
499 BT_ASSERT(other_port
);
501 switch (comp
->class->type
) {
502 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
504 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
506 switch (self_port
->type
) {
507 case BT_PORT_TYPE_OUTPUT
:
508 method
= (method_t
) src_cc
->methods
.output_port_connected
;
509 method_name
= "bt_component_class_source_output_port_connected_method";
517 case BT_COMPONENT_CLASS_TYPE_FILTER
:
519 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
521 switch (self_port
->type
) {
522 case BT_PORT_TYPE_INPUT
:
523 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
524 method_name
= "bt_component_class_filter_input_port_connected_method";
526 case BT_PORT_TYPE_OUTPUT
:
527 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
528 method_name
= "bt_component_class_filter_output_port_connected_method";
536 case BT_COMPONENT_CLASS_TYPE_SINK
:
538 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
540 switch (self_port
->type
) {
541 case BT_PORT_TYPE_INPUT
:
542 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
543 method_name
= "bt_component_class_sink_input_port_connected_method";
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(method_name
, "valid-status",
563 status
== BT_FUNC_STATUS_OK
||
564 status
== BT_FUNC_STATUS_ERROR
||
565 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
566 "Unexpected returned component status: status=%s",
567 bt_common_func_status_string(status
));
568 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name
, 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
);
589 void bt_component_remove_destroy_listener(struct bt_component
*component
,
590 bt_component_destroy_listener_func func
, void *data
)
594 BT_ASSERT(component
);
597 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
598 struct bt_component_destroy_listener
*listener
=
599 &bt_g_array_index(component
->destroy_listeners
,
600 struct bt_component_destroy_listener
, i
);
602 if (listener
->func
== func
&& listener
->data
== data
) {
603 g_array_remove_index(component
->destroy_listeners
, i
);
605 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
606 "func-addr=%p, data-addr=%p",
607 component
, func
, data
);
613 bt_logging_level
bt_component_get_logging_level(
614 const struct bt_component
*component
)
616 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
617 return component
->log_level
;
621 uint64_t bt_self_component_get_graph_mip_version(
622 bt_self_component
*self_component
)
624 struct bt_component
*comp
= (void *) self_component
;
626 BT_ASSERT_PRE_COMP_NON_NULL(self_component
);
627 return bt_component_borrow_graph(comp
)->mip_version
;
631 void bt_component_get_ref(const struct bt_component
*component
)
633 bt_object_get_ref(component
);
637 void bt_component_put_ref(const struct bt_component
*component
)
639 bt_object_put_ref(component
);