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>
23 #include "component.h"
24 #include "component-class.h"
25 #include "component-source.h"
26 #include "component-filter.h"
27 #include "component-sink.h"
28 #include "connection.h"
30 #include "message/iterator.h"
32 #include "lib/func-status.h"
35 struct bt_component
* (* const component_create_funcs
[])(void) = {
36 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
37 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
38 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
42 void finalize_component(struct bt_component
*comp
)
44 typedef void (*method_t
)(void *);
45 const char *method_name
;
47 method_t method
= NULL
;
51 switch (comp
->class->type
) {
52 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
54 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
56 method
= (method_t
) src_cc
->methods
.finalize
;
57 method_name
= "bt_component_class_source_finalize_method";
60 case BT_COMPONENT_CLASS_TYPE_FILTER
:
62 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
64 method
= (method_t
) flt_cc
->methods
.finalize
;
65 method_name
= "bt_component_class_filter_finalize_method";
68 case BT_COMPONENT_CLASS_TYPE_SINK
:
70 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
72 method
= (method_t
) sink_cc
->methods
.finalize
;
73 method_name
= "bt_component_class_sink_finalize_method";
81 const struct bt_error
*saved_error
;
83 saved_error
= bt_current_thread_take_error();
85 BT_LIB_LOGI("Calling user's component finalization method: "
88 BT_ASSERT_POST_NO_ERROR(method_name
);
91 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
97 void destroy_component(struct bt_object
*obj
)
99 struct bt_component
*component
= NULL
;
107 * The component's reference count is 0 if we're here. Increment
108 * it to avoid a double-destroy (possibly infinitely recursive).
109 * This could happen for example if the component's finalization
110 * function does bt_object_get_ref() (or anything that causes
111 * bt_object_get_ref() to be called) on itself (ref. count goes
112 * from 0 to 1), and then bt_object_put_ref(): the reference
113 * count would go from 1 to 0 again and this function would be
117 component
= container_of(obj
, struct bt_component
, base
);
118 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
119 component
, bt_component_borrow_graph(component
));
121 /* Call destroy listeners in reverse registration order */
122 BT_LOGD_STR("Calling destroy listeners.");
124 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
125 struct bt_component_destroy_listener
*listener
=
126 &bt_g_array_index(component
->destroy_listeners
,
127 struct bt_component_destroy_listener
, i
);
129 listener
->func(component
, listener
->data
);
133 * User data is destroyed first, followed by the concrete
134 * component instance. Do not finalize if the component's user
135 * initialization method failed in the first place.
137 if (component
->initialized
) {
138 finalize_component(component
);
141 if (component
->input_ports
) {
142 BT_LOGD_STR("Destroying input ports.");
143 g_ptr_array_free(component
->input_ports
, TRUE
);
144 component
->input_ports
= NULL
;
147 if (component
->output_ports
) {
148 BT_LOGD_STR("Destroying output ports.");
149 g_ptr_array_free(component
->output_ports
, TRUE
);
150 component
->output_ports
= NULL
;
153 if (component
->destroy_listeners
) {
154 g_array_free(component
->destroy_listeners
, TRUE
);
155 component
->destroy_listeners
= NULL
;
158 if (component
->name
) {
159 g_string_free(component
->name
, TRUE
);
160 component
->name
= NULL
;
163 BT_LOGD_STR("Putting component class.");
164 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
169 enum bt_component_class_type
bt_component_get_class_type(
170 const struct bt_component
*component
)
172 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
173 return component
->class->type
;
177 bool port_name_is_unique(GPtrArray
*ports
, const char *name
)
182 for (i
= 0; i
< ports
->len
; i
++) {
183 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
185 if (strcmp(port
->name
->str
, name
) == 0) {
198 enum bt_self_component_add_port_status
add_port(
199 struct bt_component
*component
, GPtrArray
*ports
,
200 enum bt_port_type port_type
, const char *name
, void *user_data
,
201 struct bt_port
**port
, const char *api_func
, bool input
)
203 struct bt_port
*new_port
= NULL
;
204 struct bt_graph
*graph
= NULL
;
205 enum bt_self_component_add_port_status status
;
207 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
208 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func
, component
);
209 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
210 BT_ASSERT_PRE_FROM_FUNC(api_func
,
211 input
? "input" : "output" "-port-name-is-unique",
212 port_name_is_unique(component
->output_ports
, name
),
213 input
? "Input" : "Output"
214 " port name is not unique: name=\"%s\", %![comp-]c",
216 BT_ASSERT_PRE_FROM_FUNC(api_func
, "name-is-not-empty",
217 strlen(name
) > 0, "Name is empty");
218 graph
= bt_component_borrow_graph(component
);
219 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-not-configured",
220 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
221 "Component's graph is already configured: "
222 "%![comp-]+c, %![graph-]+g", component
, graph
);
224 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
225 "port-type=%s, port-name=\"%s\"", component
,
226 bt_port_type_string(port_type
), name
);
228 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
230 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
231 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
236 * No name clash, add the port.
237 * The component is now the port's parent; it should _not_
238 * hold a reference to the port since the port's lifetime
239 * is now protected by the component's own lifetime.
241 g_ptr_array_add(ports
, new_port
);
244 * Notify the graph's creator that a new port was added.
246 graph
= bt_component_borrow_graph(component
);
248 enum bt_graph_listener_func_status listener_status
;
250 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
251 if (listener_status
!= BT_FUNC_STATUS_OK
) {
252 bt_graph_make_faulty(graph
);
253 status
= (int) listener_status
;
258 BT_LIB_LOGI("Created and added port to component: "
259 "%![comp-]+c, %![port-]+p", component
, new_port
);
262 status
= BT_FUNC_STATUS_OK
;
267 * We need to release the reference that we would otherwise have
268 * returned to the caller.
270 BT_PORT_PUT_REF_AND_RESET(new_port
);
276 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
,
277 const char *api_func
)
279 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
280 return (uint64_t) comp
->input_ports
->len
;
283 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
,
284 const char *api_func
)
286 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
287 return (uint64_t) comp
->output_ports
->len
;
290 int bt_component_create(struct bt_component_class
*component_class
,
291 const char *name
, bt_logging_level log_level
,
292 struct bt_component
**user_component
)
295 struct bt_component
*component
= NULL
;
296 enum bt_component_class_type type
;
298 BT_ASSERT(user_component
);
299 BT_ASSERT(component_class
);
301 type
= bt_component_class_get_type(component_class
);
302 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
303 "comp-name=\"%s\", log-level=%s", component_class
, name
,
304 bt_common_logging_level_string(log_level
));
305 component
= component_create_funcs
[type
]();
307 BT_LIB_LOGE_APPEND_CAUSE(
308 "Cannot create specific component object.");
313 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
314 component
->class = component_class
;
315 bt_object_get_ref_no_null_check(component
->class);
316 component
->name
= g_string_new(name
);
317 if (!component
->name
) {
318 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
323 component
->log_level
= log_level
;
324 component
->input_ports
= g_ptr_array_new_with_free_func(
325 (GDestroyNotify
) bt_object_try_spec_release
);
326 if (!component
->input_ports
) {
327 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
332 component
->output_ports
= g_ptr_array_new_with_free_func(
333 (GDestroyNotify
) bt_object_try_spec_release
);
334 if (!component
->output_ports
) {
335 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
340 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
341 sizeof(struct bt_component_destroy_listener
));
342 if (!component
->destroy_listeners
) {
343 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
348 BT_LIB_LOGI("Created empty component from component class: "
349 "%![cc-]+C, %![comp-]+c", component_class
, component
);
350 BT_OBJECT_MOVE_REF(*user_component
, component
);
353 bt_object_put_ref(component
);
358 const char *bt_component_get_name(const struct bt_component
*component
)
360 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
361 return component
->name
->str
;
365 const struct bt_component_class
*bt_component_borrow_class_const(
366 const struct bt_component
*component
)
368 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
369 return component
->class;
373 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
375 struct bt_component
*component
= (void *) self_comp
;
377 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
378 return component
->user_data
;
382 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
385 struct bt_component
*component
= (void *) self_comp
;
387 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
388 component
->user_data
= data
;
389 BT_LIB_LOGD("Set component's user data: %!+c", component
);
392 void bt_component_set_graph(struct bt_component
*component
,
393 struct bt_graph
*graph
)
395 bt_object_set_parent(&component
->base
,
396 graph
? &graph
->base
: NULL
);
400 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
401 const char *name
, const char *api_func
)
404 struct bt_port
*ret_port
= NULL
;
406 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
408 for (i
= 0; i
< ports
->len
; i
++) {
409 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
411 if (strcmp(name
, port
->name
->str
) == 0) {
420 struct bt_port_input
*bt_component_borrow_input_port_by_name(
421 struct bt_component
*comp
, const char *name
,
422 const char *api_func
)
424 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
425 return (void *) borrow_port_by_name(comp
->input_ports
, name
, api_func
);
428 struct bt_port_output
*bt_component_borrow_output_port_by_name(
429 struct bt_component
*comp
, const char *name
,
430 const char *api_func
)
432 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
434 borrow_port_by_name(comp
->output_ports
, name
, api_func
);
438 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
,
439 const char *api_func
)
441 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
, ports
->len
);
442 return g_ptr_array_index(ports
, index
);
445 struct bt_port_input
*bt_component_borrow_input_port_by_index(
446 struct bt_component
*comp
, uint64_t index
,
447 const char *api_func
)
449 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
451 borrow_port_by_index(comp
->input_ports
, index
, api_func
);
454 struct bt_port_output
*bt_component_borrow_output_port_by_index(
455 struct bt_component
*comp
, uint64_t index
,
456 const char *api_func
)
458 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
460 borrow_port_by_index(comp
->output_ports
, index
, api_func
);
463 enum bt_self_component_add_port_status
bt_component_add_input_port(
464 struct bt_component
*component
, const char *name
,
465 void *user_data
, struct bt_port
**port
, const char *api_func
)
467 /* add_port() logs details and checks preconditions */
468 return add_port(component
, component
->input_ports
,
469 BT_PORT_TYPE_INPUT
, name
, user_data
, port
, api_func
, true);
472 enum bt_self_component_add_port_status
bt_component_add_output_port(
473 struct bt_component
*component
, const char *name
,
474 void *user_data
, struct bt_port
**port
,
475 const char *api_func
)
477 /* add_port() logs details and checks preconditions */
478 return add_port(component
, component
->output_ports
,
479 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
, api_func
, false);
482 enum bt_component_class_port_connected_method_status
483 bt_component_port_connected(
484 struct bt_component
*comp
, struct bt_port
*self_port
,
485 struct bt_port
*other_port
)
487 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
488 void *, void *, const void *);
490 enum bt_component_class_port_connected_method_status status
=
492 method_t method
= NULL
;
493 const char *method_name
= NULL
;
496 BT_ASSERT(self_port
);
497 BT_ASSERT(other_port
);
499 switch (comp
->class->type
) {
500 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
502 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
504 switch (self_port
->type
) {
505 case BT_PORT_TYPE_OUTPUT
:
506 method
= (method_t
) src_cc
->methods
.output_port_connected
;
507 method_name
= "bt_component_class_source_output_port_connected_method";
515 case BT_COMPONENT_CLASS_TYPE_FILTER
:
517 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
519 switch (self_port
->type
) {
520 case BT_PORT_TYPE_INPUT
:
521 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
522 method_name
= "bt_component_class_filter_input_port_connected_method";
524 case BT_PORT_TYPE_OUTPUT
:
525 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
526 method_name
= "bt_component_class_filter_output_port_connected_method";
534 case BT_COMPONENT_CLASS_TYPE_SINK
:
536 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
538 switch (self_port
->type
) {
539 case BT_PORT_TYPE_INPUT
:
540 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
541 method_name
= "bt_component_class_sink_input_port_connected_method";
554 BT_LIB_LOGD("Calling user's \"port connected\" method: "
555 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
556 comp
, self_port
, other_port
);
557 status
= (int) method(comp
, self_port
, (void *) other_port
);
558 BT_LOGD("User method returned: status=%s",
559 bt_common_func_status_string(status
));
560 BT_ASSERT_POST(method_name
, "valid-status",
561 status
== BT_FUNC_STATUS_OK
||
562 status
== BT_FUNC_STATUS_ERROR
||
563 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
564 "Unexpected returned component status: status=%s",
565 bt_common_func_status_string(status
));
566 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name
, status
);
572 void bt_component_add_destroy_listener(struct bt_component
*component
,
573 bt_component_destroy_listener_func func
, void *data
)
575 struct bt_component_destroy_listener listener
;
577 BT_ASSERT(component
);
579 listener
.func
= func
;
580 listener
.data
= data
;
581 g_array_append_val(component
->destroy_listeners
, listener
);
582 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
583 "func-addr=%p, data-addr=%p",
584 component
, func
, data
);
587 void bt_component_remove_destroy_listener(struct bt_component
*component
,
588 bt_component_destroy_listener_func func
, void *data
)
592 BT_ASSERT(component
);
595 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
596 struct bt_component_destroy_listener
*listener
=
597 &bt_g_array_index(component
->destroy_listeners
,
598 struct bt_component_destroy_listener
, i
);
600 if (listener
->func
== func
&& listener
->data
== data
) {
601 g_array_remove_index(component
->destroy_listeners
, i
);
603 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
604 "func-addr=%p, data-addr=%p",
605 component
, func
, data
);
611 bt_logging_level
bt_component_get_logging_level(
612 const struct bt_component
*component
)
614 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
615 return component
->log_level
;
619 uint64_t bt_self_component_get_graph_mip_version(
620 bt_self_component
*self_component
)
622 struct bt_component
*comp
= (void *) self_component
;
624 BT_ASSERT_PRE_COMP_NON_NULL(self_component
);
625 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
);
635 void bt_component_put_ref(const struct bt_component
*component
)
637 bt_object_put_ref(component
);