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/lib-logging.h"
27 #include "common/common.h"
28 #include "common/assert.h"
29 #include "lib/assert-pre.h"
30 #include <babeltrace2/graph/self-component.h>
31 #include <babeltrace2/graph/component-const.h>
32 #include <babeltrace2/graph/component-source-const.h>
33 #include <babeltrace2/graph/component-filter-const.h>
34 #include <babeltrace2/graph/component-sink-const.h>
35 #include "common/macros.h"
36 #include "compat/compiler.h"
37 #include <babeltrace2/types.h>
38 #include <babeltrace2/value.h>
39 #include "lib/value.h"
43 #include "component.h"
44 #include "component-class.h"
45 #include "component-source.h"
46 #include "component-filter.h"
47 #include "component-sink.h"
48 #include "connection.h"
50 #include "message/iterator.h"
54 struct bt_component
* (* const component_create_funcs
[])(
55 const struct bt_component_class
*) = {
56 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
57 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
58 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
62 void (*component_destroy_funcs
[])(struct bt_component
*) = {
63 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
64 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
65 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
69 void finalize_component(struct bt_component
*comp
)
71 typedef void (*method_t
)(void *);
73 method_t method
= NULL
;
77 switch (comp
->class->type
) {
78 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
80 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
82 method
= (method_t
) src_cc
->methods
.finalize
;
85 case BT_COMPONENT_CLASS_TYPE_FILTER
:
87 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
89 method
= (method_t
) flt_cc
->methods
.finalize
;
92 case BT_COMPONENT_CLASS_TYPE_SINK
:
94 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
96 method
= (method_t
) sink_cc
->methods
.finalize
;
104 BT_LIB_LOGI("Calling user's component finalization method: "
105 "%![comp-]+c", comp
);
111 void destroy_component(struct bt_object
*obj
)
113 struct bt_component
*component
= NULL
;
121 * The component's reference count is 0 if we're here. Increment
122 * it to avoid a double-destroy (possibly infinitely recursive).
123 * This could happen for example if the component's finalization
124 * function does bt_object_get_ref() (or anything that causes
125 * bt_object_get_ref() to be called) on itself (ref. count goes
126 * from 0 to 1), and then bt_object_put_ref(): the reference
127 * count would go from 1 to 0 again and this function would be
131 component
= container_of(obj
, struct bt_component
, base
);
132 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
133 component
, bt_component_borrow_graph(component
));
135 /* Call destroy listeners in reverse registration order */
136 BT_LOGD_STR("Calling destroy listeners.");
138 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
139 struct bt_component_destroy_listener
*listener
=
140 &g_array_index(component
->destroy_listeners
,
141 struct bt_component_destroy_listener
, i
);
143 listener
->func(component
, listener
->data
);
147 * User data is destroyed first, followed by the concrete
148 * component instance. Do not finalize if the component's user
149 * initialization method failed in the first place.
151 if (component
->initialized
) {
152 finalize_component(component
);
155 if (component
->destroy
) {
156 BT_LOGD_STR("Destroying type-specific data.");
157 component
->destroy(component
);
160 if (component
->input_ports
) {
161 BT_LOGD_STR("Destroying input ports.");
162 g_ptr_array_free(component
->input_ports
, TRUE
);
163 component
->input_ports
= NULL
;
166 if (component
->output_ports
) {
167 BT_LOGD_STR("Destroying output ports.");
168 g_ptr_array_free(component
->output_ports
, TRUE
);
169 component
->output_ports
= NULL
;
172 if (component
->destroy_listeners
) {
173 g_array_free(component
->destroy_listeners
, TRUE
);
174 component
->destroy_listeners
= NULL
;
177 if (component
->name
) {
178 g_string_free(component
->name
, TRUE
);
179 component
->name
= NULL
;
182 BT_LOGD_STR("Putting component class.");
183 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
187 enum bt_component_class_type
bt_component_get_class_type(
188 const struct bt_component
*component
)
190 BT_ASSERT_PRE_NON_NULL(component
, "Component");
191 return component
->class->type
;
195 enum bt_self_component_status
add_port(
196 struct bt_component
*component
, GPtrArray
*ports
,
197 enum bt_port_type port_type
, const char *name
, void *user_data
,
198 struct bt_port
**port
)
200 struct bt_port
*new_port
= NULL
;
201 struct bt_graph
*graph
= NULL
;
202 enum bt_self_component_status status
;
204 BT_ASSERT_PRE_NON_NULL(component
, "Component");
205 BT_ASSERT_PRE_NON_NULL(name
, "Name");
206 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
207 graph
= bt_component_borrow_graph(component
);
208 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
209 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
212 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
213 "Component's graph is already configured: "
214 "%![comp-]+c, %![graph-]+g", component
, graph
);
216 // TODO: Validate that the name is not already used.
218 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
219 "port-type=%s, port-name=\"%s\"", component
,
220 bt_port_type_string(port_type
), name
);
222 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
224 BT_LOGE_STR("Cannot create port object.");
225 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
230 * No name clash, add the port.
231 * The component is now the port's parent; it should _not_
232 * hold a reference to the port since the port's lifetime
233 * is now protected by the component's own lifetime.
235 g_ptr_array_add(ports
, new_port
);
238 * Notify the graph's creator that a new port was added.
240 graph
= bt_component_borrow_graph(component
);
242 enum bt_graph_listener_status listener_status
;
244 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
245 if (listener_status
!= BT_GRAPH_LISTENER_STATUS_OK
) {
246 bt_graph_make_faulty(graph
);
247 status
= listener_status
;
252 BT_LIB_LOGI("Created and added port to component: "
253 "%![comp-]+c, %![port-]+p", component
, new_port
);
256 status
= BT_SELF_COMPONENT_STATUS_OK
;
261 * We need to release the reference that we would otherwise have
262 * returned to the caller.
264 BT_PORT_PUT_REF_AND_RESET(new_port
);
271 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
273 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
274 return (uint64_t) comp
->input_ports
->len
;
278 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
280 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
281 return (uint64_t) comp
->output_ports
->len
;
285 int bt_component_create(struct bt_component_class
*component_class
,
286 const char *name
, bt_logging_level log_level
,
287 struct bt_component
**user_component
)
290 struct bt_component
*component
= NULL
;
291 enum bt_component_class_type type
;
293 BT_ASSERT(user_component
);
294 BT_ASSERT(component_class
);
296 type
= bt_component_class_get_type(component_class
);
297 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
298 "comp-name=\"%s\", log-level=%s", component_class
, name
,
299 bt_common_logging_level_string(log_level
));
300 component
= component_create_funcs
[type
](component_class
);
302 BT_LOGE_STR("Cannot create specific component object.");
307 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
308 component
->class = component_class
;
309 bt_object_get_no_null_check(component
->class);
310 component
->destroy
= component_destroy_funcs
[type
];
311 component
->name
= g_string_new(name
);
312 if (!component
->name
) {
313 BT_LOGE_STR("Failed to allocate one GString.");
318 component
->log_level
= log_level
;
319 component
->input_ports
= g_ptr_array_new_with_free_func(
320 (GDestroyNotify
) bt_object_try_spec_release
);
321 if (!component
->input_ports
) {
322 BT_LOGE_STR("Failed to allocate one GPtrArray.");
327 component
->output_ports
= g_ptr_array_new_with_free_func(
328 (GDestroyNotify
) bt_object_try_spec_release
);
329 if (!component
->output_ports
) {
330 BT_LOGE_STR("Failed to allocate one GPtrArray.");
335 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
336 sizeof(struct bt_component_destroy_listener
));
337 if (!component
->destroy_listeners
) {
338 BT_LOGE_STR("Failed to allocate one GArray.");
343 BT_LIB_LOGI("Created empty component from component class: "
344 "%![cc-]+C, %![comp-]+c", component_class
, component
);
345 BT_OBJECT_MOVE_REF(*user_component
, component
);
348 bt_object_put_ref(component
);
352 const char *bt_component_get_name(const struct bt_component
*component
)
354 BT_ASSERT_PRE_NON_NULL(component
, "Component");
355 return component
->name
->str
;
358 const struct bt_component_class
*bt_component_borrow_class_const(
359 const struct bt_component
*component
)
361 BT_ASSERT_PRE_NON_NULL(component
, "Component");
362 return component
->class;
365 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
367 struct bt_component
*component
= (void *) self_comp
;
369 BT_ASSERT_PRE_NON_NULL(component
, "Component");
370 return component
->user_data
;
373 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
376 struct bt_component
*component
= (void *) self_comp
;
378 BT_ASSERT_PRE_NON_NULL(component
, "Component");
379 component
->user_data
= data
;
380 BT_LIB_LOGD("Set component's user data: %!+c", component
);
384 void bt_component_set_graph(struct bt_component
*component
,
385 struct bt_graph
*graph
)
387 bt_object_set_parent(&component
->base
,
388 graph
? &graph
->base
: NULL
);
391 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
393 return bt_graph_is_canceled(
394 (void *) bt_object_borrow_parent(&component
->base
));
398 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
402 struct bt_port
*ret_port
= NULL
;
406 for (i
= 0; i
< ports
->len
; i
++) {
407 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
409 if (!strcmp(name
, port
->name
->str
)) {
419 struct bt_port_input
*bt_component_borrow_input_port_by_name(
420 struct bt_component
*comp
, const char *name
)
423 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
427 struct bt_port_output
*bt_component_borrow_output_port_by_name(
428 struct bt_component
*comp
, const char *name
)
430 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
432 borrow_port_by_name(comp
->output_ports
, name
);
436 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
438 BT_ASSERT(index
< ports
->len
);
439 return g_ptr_array_index(ports
, index
);
443 struct bt_port_input
*bt_component_borrow_input_port_by_index(
444 struct bt_component
*comp
, uint64_t index
)
446 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
447 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->input_ports
->len
);
449 borrow_port_by_index(comp
->input_ports
, index
);
453 struct bt_port_output
*bt_component_borrow_output_port_by_index(
454 struct bt_component
*comp
, uint64_t index
)
456 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
457 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
459 borrow_port_by_index(comp
->output_ports
, index
);
463 enum bt_self_component_status
bt_component_add_input_port(
464 struct bt_component
*component
, const char *name
,
465 void *user_data
, struct bt_port
**port
)
467 /* add_port() logs details */
468 return add_port(component
, component
->input_ports
,
469 BT_PORT_TYPE_INPUT
, name
, user_data
, port
);
473 enum bt_self_component_status
bt_component_add_output_port(
474 struct bt_component
*component
, const char *name
,
475 void *user_data
, struct bt_port
**port
)
477 /* add_port() logs details */
478 return add_port(component
, component
->output_ports
,
479 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
);
483 enum bt_self_component_status
bt_component_accept_port_connection(
484 struct bt_component
*comp
, struct bt_port
*self_port
,
485 struct bt_port
*other_port
)
487 typedef enum bt_self_component_status (*method_t
)(
488 void *, void *, const void *);
490 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
491 method_t method
= NULL
;
494 BT_ASSERT(self_port
);
495 BT_ASSERT(other_port
);
497 switch (comp
->class->type
) {
498 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
500 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
502 switch (self_port
->type
) {
503 case BT_PORT_TYPE_OUTPUT
:
504 method
= (method_t
) src_cc
->methods
.accept_output_port_connection
;
512 case BT_COMPONENT_CLASS_TYPE_FILTER
:
514 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
516 switch (self_port
->type
) {
517 case BT_PORT_TYPE_INPUT
:
518 method
= (method_t
) flt_cc
->methods
.accept_input_port_connection
;
520 case BT_PORT_TYPE_OUTPUT
:
521 method
= (method_t
) flt_cc
->methods
.accept_output_port_connection
;
529 case BT_COMPONENT_CLASS_TYPE_SINK
:
531 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
533 switch (self_port
->type
) {
534 case BT_PORT_TYPE_INPUT
:
535 method
= (method_t
) sink_cc
->methods
.accept_input_port_connection
;
548 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
549 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
550 comp
, self_port
, other_port
);
551 status
= method(comp
, self_port
, (void *) other_port
);
552 BT_LOGD("User method returned: status=%s",
553 bt_self_component_status_string(status
));
560 enum bt_self_component_status
bt_component_port_connected(
561 struct bt_component
*comp
, struct bt_port
*self_port
,
562 struct bt_port
*other_port
)
564 typedef enum bt_self_component_status (*method_t
)(
565 void *, void *, const void *);
567 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
568 method_t method
= NULL
;
571 BT_ASSERT(self_port
);
572 BT_ASSERT(other_port
);
574 switch (comp
->class->type
) {
575 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
577 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
579 switch (self_port
->type
) {
580 case BT_PORT_TYPE_OUTPUT
:
581 method
= (method_t
) src_cc
->methods
.output_port_connected
;
589 case BT_COMPONENT_CLASS_TYPE_FILTER
:
591 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
593 switch (self_port
->type
) {
594 case BT_PORT_TYPE_INPUT
:
595 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
597 case BT_PORT_TYPE_OUTPUT
:
598 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
606 case BT_COMPONENT_CLASS_TYPE_SINK
:
608 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
610 switch (self_port
->type
) {
611 case BT_PORT_TYPE_INPUT
:
612 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
625 BT_LIB_LOGD("Calling user's \"port connected\" method: "
626 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
627 comp
, self_port
, other_port
);
628 status
= method(comp
, self_port
, (void *) other_port
);
629 BT_LOGD("User method returned: status=%s",
630 bt_self_component_status_string(status
));
631 BT_ASSERT_PRE(status
== BT_SELF_COMPONENT_STATUS_OK
||
632 status
== BT_SELF_COMPONENT_STATUS_ERROR
||
633 status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
634 "Unexpected returned component status: status=%s",
635 bt_self_component_status_string(status
));
642 void bt_component_add_destroy_listener(struct bt_component
*component
,
643 bt_component_destroy_listener_func func
, void *data
)
645 struct bt_component_destroy_listener listener
;
647 BT_ASSERT(component
);
649 listener
.func
= func
;
650 listener
.data
= data
;
651 g_array_append_val(component
->destroy_listeners
, listener
);
652 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
653 "func-addr=%p, data-addr=%p",
654 component
, func
, data
);
658 void bt_component_remove_destroy_listener(struct bt_component
*component
,
659 bt_component_destroy_listener_func func
, void *data
)
663 BT_ASSERT(component
);
666 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
667 struct bt_component_destroy_listener
*listener
=
668 &g_array_index(component
->destroy_listeners
,
669 struct bt_component_destroy_listener
, i
);
671 if (listener
->func
== func
&& listener
->data
== data
) {
672 g_array_remove_index(component
->destroy_listeners
, i
);
674 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
675 "func-addr=%p, data-addr=%p",
676 component
, func
, data
);
681 bt_logging_level
bt_component_get_logging_level(
682 const struct bt_component
*component
)
684 BT_ASSERT_PRE_NON_NULL(component
, "Component");
685 return component
->log_level
;
688 void bt_component_get_ref(const struct bt_component
*component
)
690 bt_object_get_ref(component
);
693 void bt_component_put_ref(const struct bt_component
*component
)
695 bt_object_put_ref(component
);