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 (*component_destroy_funcs
[])(struct bt_component
*) = {
45 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
46 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
47 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
51 void finalize_component(struct bt_component
*comp
)
53 typedef void (*method_t
)(void *);
54 const char *method_name
;
56 method_t method
= NULL
;
60 switch (comp
->class->type
) {
61 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
63 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
65 method
= (method_t
) src_cc
->methods
.finalize
;
66 method_name
= "bt_component_class_source_finalize_method";
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
;
74 method_name
= "bt_component_class_filter_finalize_method";
77 case BT_COMPONENT_CLASS_TYPE_SINK
:
79 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
81 method
= (method_t
) sink_cc
->methods
.finalize
;
82 method_name
= "bt_component_class_sink_finalize_method";
90 const struct bt_error
*saved_error
;
92 saved_error
= bt_current_thread_take_error();
94 BT_LIB_LOGI("Calling user's component finalization method: "
97 BT_ASSERT_POST_NO_ERROR(method_name
);
100 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
106 void destroy_component(struct bt_object
*obj
)
108 struct bt_component
*component
= NULL
;
116 * The component's reference count is 0 if we're here. Increment
117 * it to avoid a double-destroy (possibly infinitely recursive).
118 * This could happen for example if the component's finalization
119 * function does bt_object_get_ref() (or anything that causes
120 * bt_object_get_ref() to be called) on itself (ref. count goes
121 * from 0 to 1), and then bt_object_put_ref(): the reference
122 * count would go from 1 to 0 again and this function would be
126 component
= container_of(obj
, struct bt_component
, base
);
127 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
128 component
, bt_component_borrow_graph(component
));
130 /* Call destroy listeners in reverse registration order */
131 BT_LOGD_STR("Calling destroy listeners.");
133 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
134 struct bt_component_destroy_listener
*listener
=
135 &bt_g_array_index(component
->destroy_listeners
,
136 struct bt_component_destroy_listener
, i
);
138 listener
->func(component
, listener
->data
);
142 * User data is destroyed first, followed by the concrete
143 * component instance. Do not finalize if the component's user
144 * initialization method failed in the first place.
146 if (component
->initialized
) {
147 finalize_component(component
);
150 if (component
->destroy
) {
151 BT_LOGD_STR("Destroying type-specific data.");
152 component
->destroy(component
);
155 if (component
->input_ports
) {
156 BT_LOGD_STR("Destroying input ports.");
157 g_ptr_array_free(component
->input_ports
, TRUE
);
158 component
->input_ports
= NULL
;
161 if (component
->output_ports
) {
162 BT_LOGD_STR("Destroying output ports.");
163 g_ptr_array_free(component
->output_ports
, TRUE
);
164 component
->output_ports
= NULL
;
167 if (component
->destroy_listeners
) {
168 g_array_free(component
->destroy_listeners
, TRUE
);
169 component
->destroy_listeners
= NULL
;
172 if (component
->name
) {
173 g_string_free(component
->name
, TRUE
);
174 component
->name
= NULL
;
177 BT_LOGD_STR("Putting component class.");
178 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
183 enum bt_component_class_type
bt_component_get_class_type(
184 const struct bt_component
*component
)
186 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
187 return component
->class->type
;
191 bool port_name_is_unique(GPtrArray
*ports
, const char *name
)
196 for (i
= 0; i
< ports
->len
; i
++) {
197 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
199 if (strcmp(port
->name
->str
, name
) == 0) {
212 enum bt_self_component_add_port_status
add_port(
213 struct bt_component
*component
, GPtrArray
*ports
,
214 enum bt_port_type port_type
, const char *name
, void *user_data
,
215 struct bt_port
**port
, const char *api_func
, bool input
)
217 struct bt_port
*new_port
= NULL
;
218 struct bt_graph
*graph
= NULL
;
219 enum bt_self_component_add_port_status status
;
221 BT_ASSERT_PRE_NO_ERROR_FROM_FUNC(api_func
);
222 BT_ASSERT_PRE_COMP_NON_NULL_FROM_FUNC(api_func
, component
);
223 BT_ASSERT_PRE_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
224 BT_ASSERT_PRE_FROM_FUNC(api_func
,
225 input
? "input" : "output" "-port-name-is-unique",
226 port_name_is_unique(component
->output_ports
, name
),
227 input
? "Input" : "Output"
228 " port name is not unique: name=\"%s\", %![comp-]c",
230 BT_ASSERT_PRE_FROM_FUNC(api_func
, "name-is-not-empty",
231 strlen(name
) > 0, "Name is empty");
232 graph
= bt_component_borrow_graph(component
);
233 BT_ASSERT_PRE_FROM_FUNC(api_func
, "graph-is-not-configured",
234 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
235 "Component's graph is already configured: "
236 "%![comp-]+c, %![graph-]+g", component
, graph
);
238 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
239 "port-type=%s, port-name=\"%s\"", component
,
240 bt_port_type_string(port_type
), name
);
242 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
244 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
245 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
250 * No name clash, add the port.
251 * The component is now the port's parent; it should _not_
252 * hold a reference to the port since the port's lifetime
253 * is now protected by the component's own lifetime.
255 g_ptr_array_add(ports
, new_port
);
258 * Notify the graph's creator that a new port was added.
260 graph
= bt_component_borrow_graph(component
);
262 enum bt_graph_listener_func_status listener_status
;
264 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
265 if (listener_status
!= BT_FUNC_STATUS_OK
) {
266 bt_graph_make_faulty(graph
);
267 status
= (int) listener_status
;
272 BT_LIB_LOGI("Created and added port to component: "
273 "%![comp-]+c, %![port-]+p", component
, new_port
);
276 status
= BT_FUNC_STATUS_OK
;
281 * We need to release the reference that we would otherwise have
282 * returned to the caller.
284 BT_PORT_PUT_REF_AND_RESET(new_port
);
290 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
,
291 const char *api_func
)
293 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
294 return (uint64_t) comp
->input_ports
->len
;
297 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
,
298 const char *api_func
)
300 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
301 return (uint64_t) comp
->output_ports
->len
;
304 int bt_component_create(struct bt_component_class
*component_class
,
305 const char *name
, bt_logging_level log_level
,
306 struct bt_component
**user_component
)
309 struct bt_component
*component
= NULL
;
310 enum bt_component_class_type type
;
312 BT_ASSERT(user_component
);
313 BT_ASSERT(component_class
);
315 type
= bt_component_class_get_type(component_class
);
316 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
317 "comp-name=\"%s\", log-level=%s", component_class
, name
,
318 bt_common_logging_level_string(log_level
));
319 component
= component_create_funcs
[type
]();
321 BT_LIB_LOGE_APPEND_CAUSE(
322 "Cannot create specific component object.");
327 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
328 component
->class = component_class
;
329 bt_object_get_ref_no_null_check(component
->class);
330 component
->destroy
= component_destroy_funcs
[type
];
331 component
->name
= g_string_new(name
);
332 if (!component
->name
) {
333 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
338 component
->log_level
= log_level
;
339 component
->input_ports
= g_ptr_array_new_with_free_func(
340 (GDestroyNotify
) bt_object_try_spec_release
);
341 if (!component
->input_ports
) {
342 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
347 component
->output_ports
= g_ptr_array_new_with_free_func(
348 (GDestroyNotify
) bt_object_try_spec_release
);
349 if (!component
->output_ports
) {
350 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
355 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
356 sizeof(struct bt_component_destroy_listener
));
357 if (!component
->destroy_listeners
) {
358 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
363 BT_LIB_LOGI("Created empty component from component class: "
364 "%![cc-]+C, %![comp-]+c", component_class
, component
);
365 BT_OBJECT_MOVE_REF(*user_component
, component
);
368 bt_object_put_ref(component
);
373 const char *bt_component_get_name(const struct bt_component
*component
)
375 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
376 return component
->name
->str
;
380 const struct bt_component_class
*bt_component_borrow_class_const(
381 const struct bt_component
*component
)
383 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
384 return component
->class;
388 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
390 struct bt_component
*component
= (void *) self_comp
;
392 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
393 return component
->user_data
;
397 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
400 struct bt_component
*component
= (void *) self_comp
;
402 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
403 component
->user_data
= data
;
404 BT_LIB_LOGD("Set component's user data: %!+c", component
);
407 void bt_component_set_graph(struct bt_component
*component
,
408 struct bt_graph
*graph
)
410 bt_object_set_parent(&component
->base
,
411 graph
? &graph
->base
: NULL
);
415 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
416 const char *name
, const char *api_func
)
419 struct bt_port
*ret_port
= NULL
;
421 BT_ASSERT_PRE_DEV_NAME_NON_NULL_FROM_FUNC(api_func
, name
);
423 for (i
= 0; i
< ports
->len
; i
++) {
424 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
426 if (strcmp(name
, port
->name
->str
) == 0) {
435 struct bt_port_input
*bt_component_borrow_input_port_by_name(
436 struct bt_component
*comp
, const char *name
,
437 const char *api_func
)
439 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
440 return (void *) borrow_port_by_name(comp
->input_ports
, name
, api_func
);
443 struct bt_port_output
*bt_component_borrow_output_port_by_name(
444 struct bt_component
*comp
, const char *name
,
445 const char *api_func
)
447 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
449 borrow_port_by_name(comp
->output_ports
, name
, api_func
);
453 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
,
454 const char *api_func
)
456 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func
, index
, ports
->len
);
457 return g_ptr_array_index(ports
, index
);
460 struct bt_port_input
*bt_component_borrow_input_port_by_index(
461 struct bt_component
*comp
, uint64_t index
,
462 const char *api_func
)
464 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
466 borrow_port_by_index(comp
->input_ports
, index
, api_func
);
469 struct bt_port_output
*bt_component_borrow_output_port_by_index(
470 struct bt_component
*comp
, uint64_t index
,
471 const char *api_func
)
473 BT_ASSERT_PRE_DEV_COMP_NON_NULL_FROM_FUNC(api_func
, comp
);
475 borrow_port_by_index(comp
->output_ports
, index
, api_func
);
478 enum bt_self_component_add_port_status
bt_component_add_input_port(
479 struct bt_component
*component
, const char *name
,
480 void *user_data
, struct bt_port
**port
, const char *api_func
)
482 /* add_port() logs details and checks preconditions */
483 return add_port(component
, component
->input_ports
,
484 BT_PORT_TYPE_INPUT
, name
, user_data
, port
, api_func
, true);
487 enum bt_self_component_add_port_status
bt_component_add_output_port(
488 struct bt_component
*component
, const char *name
,
489 void *user_data
, struct bt_port
**port
,
490 const char *api_func
)
492 /* add_port() logs details and checks preconditions */
493 return add_port(component
, component
->output_ports
,
494 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
, api_func
, false);
497 enum bt_component_class_port_connected_method_status
498 bt_component_port_connected(
499 struct bt_component
*comp
, struct bt_port
*self_port
,
500 struct bt_port
*other_port
)
502 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
503 void *, void *, const void *);
505 enum bt_component_class_port_connected_method_status status
=
507 method_t method
= NULL
;
508 const char *method_name
= NULL
;
511 BT_ASSERT(self_port
);
512 BT_ASSERT(other_port
);
514 switch (comp
->class->type
) {
515 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
517 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
519 switch (self_port
->type
) {
520 case BT_PORT_TYPE_OUTPUT
:
521 method
= (method_t
) src_cc
->methods
.output_port_connected
;
522 method_name
= "bt_component_class_source_output_port_connected_method";
530 case BT_COMPONENT_CLASS_TYPE_FILTER
:
532 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
534 switch (self_port
->type
) {
535 case BT_PORT_TYPE_INPUT
:
536 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
537 method_name
= "bt_component_class_filter_input_port_connected_method";
539 case BT_PORT_TYPE_OUTPUT
:
540 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
541 method_name
= "bt_component_class_filter_output_port_connected_method";
549 case BT_COMPONENT_CLASS_TYPE_SINK
:
551 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
553 switch (self_port
->type
) {
554 case BT_PORT_TYPE_INPUT
:
555 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
556 method_name
= "bt_component_class_sink_input_port_connected_method";
569 BT_LIB_LOGD("Calling user's \"port connected\" method: "
570 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
571 comp
, self_port
, other_port
);
572 status
= (int) method(comp
, self_port
, (void *) other_port
);
573 BT_LOGD("User method returned: status=%s",
574 bt_common_func_status_string(status
));
575 BT_ASSERT_POST(method_name
, "valid-status",
576 status
== BT_FUNC_STATUS_OK
||
577 status
== BT_FUNC_STATUS_ERROR
||
578 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
579 "Unexpected returned component status: status=%s",
580 bt_common_func_status_string(status
));
581 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name
, status
);
587 void bt_component_add_destroy_listener(struct bt_component
*component
,
588 bt_component_destroy_listener_func func
, void *data
)
590 struct bt_component_destroy_listener listener
;
592 BT_ASSERT(component
);
594 listener
.func
= func
;
595 listener
.data
= data
;
596 g_array_append_val(component
->destroy_listeners
, listener
);
597 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
598 "func-addr=%p, data-addr=%p",
599 component
, func
, data
);
602 void bt_component_remove_destroy_listener(struct bt_component
*component
,
603 bt_component_destroy_listener_func func
, void *data
)
607 BT_ASSERT(component
);
610 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
611 struct bt_component_destroy_listener
*listener
=
612 &bt_g_array_index(component
->destroy_listeners
,
613 struct bt_component_destroy_listener
, i
);
615 if (listener
->func
== func
&& listener
->data
== data
) {
616 g_array_remove_index(component
->destroy_listeners
, i
);
618 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
619 "func-addr=%p, data-addr=%p",
620 component
, func
, data
);
626 bt_logging_level
bt_component_get_logging_level(
627 const struct bt_component
*component
)
629 BT_ASSERT_PRE_DEV_COMP_NON_NULL(component
);
630 return component
->log_level
;
634 uint64_t bt_self_component_get_graph_mip_version(
635 bt_self_component
*self_component
)
637 struct bt_component
*comp
= (void *) self_component
;
639 BT_ASSERT_PRE_COMP_NON_NULL(self_component
);
640 return bt_component_borrow_graph(comp
)->mip_version
;
644 void bt_component_get_ref(const struct bt_component
*component
)
646 bt_object_get_ref(component
);
650 void bt_component_put_ref(const struct bt_component
*component
)
652 bt_object_put_ref(component
);