2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/COMPONENT"
25 #include "lib/logging.h"
27 #include "common/common.h"
28 #include "common/assert.h"
29 #include "lib/assert-pre.h"
30 #include "lib/assert-post.h"
31 #include <babeltrace2/graph/self-component.h>
32 #include <babeltrace2/graph/component-const.h>
33 #include <babeltrace2/graph/component-source-const.h>
34 #include <babeltrace2/graph/component-filter-const.h>
35 #include <babeltrace2/graph/component-sink-const.h>
36 #include <babeltrace2/graph/graph-const.h>
37 #include "common/macros.h"
38 #include "compat/compiler.h"
39 #include <babeltrace2/types.h>
40 #include <babeltrace2/value.h>
41 #include "lib/value.h"
45 #include "component.h"
46 #include "component-class.h"
47 #include "component-source.h"
48 #include "component-filter.h"
49 #include "component-sink.h"
50 #include "connection.h"
52 #include "message/iterator.h"
54 #include "lib/func-status.h"
57 struct bt_component
* (* const component_create_funcs
[])(
58 const struct bt_component_class
*) = {
59 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
60 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
61 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
65 void (*component_destroy_funcs
[])(struct bt_component
*) = {
66 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
67 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
68 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
72 void finalize_component(struct bt_component
*comp
)
74 typedef void (*method_t
)(void *);
76 method_t method
= NULL
;
80 switch (comp
->class->type
) {
81 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
83 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
85 method
= (method_t
) src_cc
->methods
.finalize
;
88 case BT_COMPONENT_CLASS_TYPE_FILTER
:
90 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
92 method
= (method_t
) flt_cc
->methods
.finalize
;
95 case BT_COMPONENT_CLASS_TYPE_SINK
:
97 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
99 method
= (method_t
) sink_cc
->methods
.finalize
;
107 BT_LIB_LOGI("Calling user's component finalization method: "
108 "%![comp-]+c", comp
);
114 void destroy_component(struct bt_object
*obj
)
116 struct bt_component
*component
= NULL
;
124 * The component's reference count is 0 if we're here. Increment
125 * it to avoid a double-destroy (possibly infinitely recursive).
126 * This could happen for example if the component's finalization
127 * function does bt_object_get_ref() (or anything that causes
128 * bt_object_get_ref() to be called) on itself (ref. count goes
129 * from 0 to 1), and then bt_object_put_ref(): the reference
130 * count would go from 1 to 0 again and this function would be
134 component
= container_of(obj
, struct bt_component
, base
);
135 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
136 component
, bt_component_borrow_graph(component
));
138 /* Call destroy listeners in reverse registration order */
139 BT_LOGD_STR("Calling destroy listeners.");
141 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
142 struct bt_component_destroy_listener
*listener
=
143 &g_array_index(component
->destroy_listeners
,
144 struct bt_component_destroy_listener
, i
);
146 listener
->func(component
, listener
->data
);
150 * User data is destroyed first, followed by the concrete
151 * component instance. Do not finalize if the component's user
152 * initialization method failed in the first place.
154 if (component
->initialized
) {
155 finalize_component(component
);
158 if (component
->destroy
) {
159 BT_LOGD_STR("Destroying type-specific data.");
160 component
->destroy(component
);
163 if (component
->input_ports
) {
164 BT_LOGD_STR("Destroying input ports.");
165 g_ptr_array_free(component
->input_ports
, TRUE
);
166 component
->input_ports
= NULL
;
169 if (component
->output_ports
) {
170 BT_LOGD_STR("Destroying output ports.");
171 g_ptr_array_free(component
->output_ports
, TRUE
);
172 component
->output_ports
= NULL
;
175 if (component
->destroy_listeners
) {
176 g_array_free(component
->destroy_listeners
, TRUE
);
177 component
->destroy_listeners
= NULL
;
180 if (component
->name
) {
181 g_string_free(component
->name
, TRUE
);
182 component
->name
= NULL
;
185 BT_LOGD_STR("Putting component class.");
186 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
190 enum bt_component_class_type
bt_component_get_class_type(
191 const struct bt_component
*component
)
193 BT_ASSERT_PRE_NON_NULL(component
, "Component");
194 return component
->class->type
;
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
)
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_NON_NULL(component
, "Component");
208 BT_ASSERT_PRE_NON_NULL(name
, "Name");
209 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
210 graph
= bt_component_borrow_graph(component
);
211 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
212 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
215 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
216 "Component's graph is already configured: "
217 "%![comp-]+c, %![graph-]+g", component
, graph
);
219 // TODO: Validate that the name is not already used.
221 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
222 "port-type=%s, port-name=\"%s\"", component
,
223 bt_port_type_string(port_type
), name
);
225 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
227 BT_LOGE_STR("Cannot create port object.");
228 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
233 * No name clash, add the port.
234 * The component is now the port's parent; it should _not_
235 * hold a reference to the port since the port's lifetime
236 * is now protected by the component's own lifetime.
238 g_ptr_array_add(ports
, new_port
);
241 * Notify the graph's creator that a new port was added.
243 graph
= bt_component_borrow_graph(component
);
245 enum bt_graph_listener_func_status listener_status
;
247 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
248 if (listener_status
!= BT_FUNC_STATUS_OK
) {
249 bt_graph_make_faulty(graph
);
250 status
= (int) listener_status
;
255 BT_LIB_LOGI("Created and added port to component: "
256 "%![comp-]+c, %![port-]+p", component
, new_port
);
259 status
= BT_FUNC_STATUS_OK
;
264 * We need to release the reference that we would otherwise have
265 * returned to the caller.
267 BT_PORT_PUT_REF_AND_RESET(new_port
);
274 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
276 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
277 return (uint64_t) comp
->input_ports
->len
;
281 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
283 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
284 return (uint64_t) comp
->output_ports
->len
;
288 int bt_component_create(struct bt_component_class
*component_class
,
289 const char *name
, bt_logging_level log_level
,
290 struct bt_component
**user_component
)
293 struct bt_component
*component
= NULL
;
294 enum bt_component_class_type type
;
296 BT_ASSERT(user_component
);
297 BT_ASSERT(component_class
);
299 type
= bt_component_class_get_type(component_class
);
300 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
301 "comp-name=\"%s\", log-level=%s", component_class
, name
,
302 bt_common_logging_level_string(log_level
));
303 component
= component_create_funcs
[type
](component_class
);
305 BT_LOGE_STR("Cannot create specific component object.");
310 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
311 component
->class = component_class
;
312 bt_object_get_no_null_check(component
->class);
313 component
->destroy
= component_destroy_funcs
[type
];
314 component
->name
= g_string_new(name
);
315 if (!component
->name
) {
316 BT_LOGE_STR("Failed to allocate one GString.");
321 component
->log_level
= log_level
;
322 component
->input_ports
= g_ptr_array_new_with_free_func(
323 (GDestroyNotify
) bt_object_try_spec_release
);
324 if (!component
->input_ports
) {
325 BT_LOGE_STR("Failed to allocate one GPtrArray.");
330 component
->output_ports
= g_ptr_array_new_with_free_func(
331 (GDestroyNotify
) bt_object_try_spec_release
);
332 if (!component
->output_ports
) {
333 BT_LOGE_STR("Failed to allocate one GPtrArray.");
338 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
339 sizeof(struct bt_component_destroy_listener
));
340 if (!component
->destroy_listeners
) {
341 BT_LOGE_STR("Failed to allocate one GArray.");
346 BT_LIB_LOGI("Created empty component from component class: "
347 "%![cc-]+C, %![comp-]+c", component_class
, component
);
348 BT_OBJECT_MOVE_REF(*user_component
, component
);
351 bt_object_put_ref(component
);
355 const char *bt_component_get_name(const struct bt_component
*component
)
357 BT_ASSERT_PRE_NON_NULL(component
, "Component");
358 return component
->name
->str
;
361 const struct bt_component_class
*bt_component_borrow_class_const(
362 const struct bt_component
*component
)
364 BT_ASSERT_PRE_NON_NULL(component
, "Component");
365 return component
->class;
368 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
370 struct bt_component
*component
= (void *) self_comp
;
372 BT_ASSERT_PRE_NON_NULL(component
, "Component");
373 return component
->user_data
;
376 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
379 struct bt_component
*component
= (void *) self_comp
;
381 BT_ASSERT_PRE_NON_NULL(component
, "Component");
382 component
->user_data
= data
;
383 BT_LIB_LOGD("Set component's user data: %!+c", component
);
387 void bt_component_set_graph(struct bt_component
*component
,
388 struct bt_graph
*graph
)
390 bt_object_set_parent(&component
->base
,
391 graph
? &graph
->base
: NULL
);
394 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
396 return bt_graph_is_canceled(
397 (void *) bt_object_borrow_parent(&component
->base
));
401 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
405 struct bt_port
*ret_port
= NULL
;
409 for (i
= 0; i
< ports
->len
; i
++) {
410 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
412 if (!strcmp(name
, port
->name
->str
)) {
422 struct bt_port_input
*bt_component_borrow_input_port_by_name(
423 struct bt_component
*comp
, const char *name
)
426 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
430 struct bt_port_output
*bt_component_borrow_output_port_by_name(
431 struct bt_component
*comp
, const char *name
)
433 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
435 borrow_port_by_name(comp
->output_ports
, name
);
439 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
441 BT_ASSERT(index
< ports
->len
);
442 return g_ptr_array_index(ports
, index
);
446 struct bt_port_input
*bt_component_borrow_input_port_by_index(
447 struct bt_component
*comp
, uint64_t index
)
449 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
450 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->input_ports
->len
);
452 borrow_port_by_index(comp
->input_ports
, index
);
456 struct bt_port_output
*bt_component_borrow_output_port_by_index(
457 struct bt_component
*comp
, uint64_t index
)
459 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
460 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
462 borrow_port_by_index(comp
->output_ports
, index
);
466 enum bt_self_component_add_port_status
bt_component_add_input_port(
467 struct bt_component
*component
, const char *name
,
468 void *user_data
, struct bt_port
**port
)
470 /* add_port() logs details */
471 return add_port(component
, component
->input_ports
,
472 BT_PORT_TYPE_INPUT
, name
, user_data
, port
);
476 enum bt_self_component_add_port_status
bt_component_add_output_port(
477 struct bt_component
*component
, const char *name
,
478 void *user_data
, struct bt_port
**port
)
480 /* add_port() logs details */
481 return add_port(component
, component
->output_ports
,
482 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
);
486 enum bt_component_class_port_connected_method_status
487 bt_component_port_connected(
488 struct bt_component
*comp
, struct bt_port
*self_port
,
489 struct bt_port
*other_port
)
491 typedef enum bt_component_class_port_connected_method_status (*method_t
)(
492 void *, void *, const void *);
494 enum bt_component_class_port_connected_method_status status
=
496 method_t method
= NULL
;
499 BT_ASSERT(self_port
);
500 BT_ASSERT(other_port
);
502 switch (comp
->class->type
) {
503 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
505 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
507 switch (self_port
->type
) {
508 case BT_PORT_TYPE_OUTPUT
:
509 method
= (method_t
) src_cc
->methods
.output_port_connected
;
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
;
525 case BT_PORT_TYPE_OUTPUT
:
526 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
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
;
553 BT_LIB_LOGD("Calling user's \"port connected\" method: "
554 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
555 comp
, self_port
, other_port
);
556 status
= (int) method(comp
, self_port
, (void *) other_port
);
557 BT_LOGD("User method returned: status=%s",
558 bt_common_func_status_string(status
));
559 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
560 status
== BT_FUNC_STATUS_ERROR
||
561 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
562 "Unexpected returned component status: status=%s",
563 bt_common_func_status_string(status
));
570 void bt_component_add_destroy_listener(struct bt_component
*component
,
571 bt_component_destroy_listener_func func
, void *data
)
573 struct bt_component_destroy_listener listener
;
575 BT_ASSERT(component
);
577 listener
.func
= func
;
578 listener
.data
= data
;
579 g_array_append_val(component
->destroy_listeners
, listener
);
580 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
581 "func-addr=%p, data-addr=%p",
582 component
, func
, data
);
586 void bt_component_remove_destroy_listener(struct bt_component
*component
,
587 bt_component_destroy_listener_func func
, void *data
)
591 BT_ASSERT(component
);
594 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
595 struct bt_component_destroy_listener
*listener
=
596 &g_array_index(component
->destroy_listeners
,
597 struct bt_component_destroy_listener
, i
);
599 if (listener
->func
== func
&& listener
->data
== data
) {
600 g_array_remove_index(component
->destroy_listeners
, i
);
602 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
603 "func-addr=%p, data-addr=%p",
604 component
, func
, data
);
609 bt_logging_level
bt_component_get_logging_level(
610 const struct bt_component
*component
)
612 BT_ASSERT_PRE_NON_NULL(component
, "Component");
613 return component
->log_level
;
616 void bt_component_get_ref(const struct bt_component
*component
)
618 bt_object_get_ref(component
);
621 void bt_component_put_ref(const struct bt_component
*component
)
623 bt_object_put_ref(component
);