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.h>
33 #include <babeltrace2/graph/graph.h>
34 #include "common/macros.h"
35 #include "compat/compiler.h"
36 #include <babeltrace2/types.h>
37 #include <babeltrace2/value.h>
38 #include "lib/value.h"
42 #include "component.h"
43 #include "component-class.h"
44 #include "component-source.h"
45 #include "component-filter.h"
46 #include "component-sink.h"
47 #include "connection.h"
49 #include "message/iterator.h"
51 #include "lib/func-status.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 const struct bt_error
*saved_error
;
106 saved_error
= bt_current_thread_take_error();
108 BT_LIB_LOGI("Calling user's component finalization method: "
109 "%![comp-]+c", comp
);
111 BT_ASSERT_POST_NO_ERROR();
114 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
120 void destroy_component(struct bt_object
*obj
)
122 struct bt_component
*component
= NULL
;
130 * The component's reference count is 0 if we're here. Increment
131 * it to avoid a double-destroy (possibly infinitely recursive).
132 * This could happen for example if the component's finalization
133 * function does bt_object_get_ref() (or anything that causes
134 * bt_object_get_ref() to be called) on itself (ref. count goes
135 * from 0 to 1), and then bt_object_put_ref(): the reference
136 * count would go from 1 to 0 again and this function would be
140 component
= container_of(obj
, struct bt_component
, base
);
141 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
142 component
, bt_component_borrow_graph(component
));
144 /* Call destroy listeners in reverse registration order */
145 BT_LOGD_STR("Calling destroy listeners.");
147 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
148 struct bt_component_destroy_listener
*listener
=
149 &g_array_index(component
->destroy_listeners
,
150 struct bt_component_destroy_listener
, i
);
152 listener
->func(component
, listener
->data
);
156 * User data is destroyed first, followed by the concrete
157 * component instance. Do not finalize if the component's user
158 * initialization method failed in the first place.
160 if (component
->initialized
) {
161 finalize_component(component
);
164 if (component
->destroy
) {
165 BT_LOGD_STR("Destroying type-specific data.");
166 component
->destroy(component
);
169 if (component
->input_ports
) {
170 BT_LOGD_STR("Destroying input ports.");
171 g_ptr_array_free(component
->input_ports
, TRUE
);
172 component
->input_ports
= NULL
;
175 if (component
->output_ports
) {
176 BT_LOGD_STR("Destroying output ports.");
177 g_ptr_array_free(component
->output_ports
, TRUE
);
178 component
->output_ports
= NULL
;
181 if (component
->destroy_listeners
) {
182 g_array_free(component
->destroy_listeners
, TRUE
);
183 component
->destroy_listeners
= NULL
;
186 if (component
->name
) {
187 g_string_free(component
->name
, TRUE
);
188 component
->name
= NULL
;
191 BT_LOGD_STR("Putting component class.");
192 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
196 enum bt_component_class_type
bt_component_get_class_type(
197 const struct bt_component
*component
)
199 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
200 return component
->class->type
;
204 enum bt_self_component_add_port_status
add_port(
205 struct bt_component
*component
, GPtrArray
*ports
,
206 enum bt_port_type port_type
, const char *name
, void *user_data
,
207 struct bt_port
**port
)
209 struct bt_port
*new_port
= NULL
;
210 struct bt_graph
*graph
= NULL
;
211 enum bt_self_component_add_port_status status
;
213 BT_ASSERT_PRE_NON_NULL(component
, "Component");
214 BT_ASSERT_PRE_NON_NULL(name
, "Name");
215 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
216 graph
= bt_component_borrow_graph(component
);
218 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
219 "Component's graph is already configured: "
220 "%![comp-]+c, %![graph-]+g", component
, graph
);
222 // TODO: Validate that the name is not already used.
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
);
277 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
279 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
280 return (uint64_t) comp
->input_ports
->len
;
284 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
286 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
287 return (uint64_t) comp
->output_ports
->len
;
291 int bt_component_create(struct bt_component_class
*component_class
,
292 const char *name
, bt_logging_level log_level
,
293 struct bt_component
**user_component
)
296 struct bt_component
*component
= NULL
;
297 enum bt_component_class_type type
;
299 BT_ASSERT(user_component
);
300 BT_ASSERT(component_class
);
302 type
= bt_component_class_get_type(component_class
);
303 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
304 "comp-name=\"%s\", log-level=%s", component_class
, name
,
305 bt_common_logging_level_string(log_level
));
306 component
= component_create_funcs
[type
](component_class
);
308 BT_LIB_LOGE_APPEND_CAUSE(
309 "Cannot create specific component object.");
314 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
315 component
->class = component_class
;
316 bt_object_get_ref_no_null_check(component
->class);
317 component
->destroy
= component_destroy_funcs
[type
];
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
);
359 const char *bt_component_get_name(const struct bt_component
*component
)
361 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
362 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_NON_NULL(component
, "Component");
369 return component
->class;
372 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
374 struct bt_component
*component
= (void *) self_comp
;
376 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
377 return component
->user_data
;
380 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
383 struct bt_component
*component
= (void *) self_comp
;
385 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
386 component
->user_data
= data
;
387 BT_LIB_LOGD("Set component's user data: %!+c", component
);
391 void bt_component_set_graph(struct bt_component
*component
,
392 struct bt_graph
*graph
)
394 bt_object_set_parent(&component
->base
,
395 graph
? &graph
->base
: NULL
);
399 struct bt_port
*borrow_port_by_name(GPtrArray
*ports
,
403 struct bt_port
*ret_port
= NULL
;
407 for (i
= 0; i
< ports
->len
; i
++) {
408 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
410 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
)
424 return (void *) borrow_port_by_name(comp
->input_ports
, name
);
428 struct bt_port_output
*bt_component_borrow_output_port_by_name(
429 struct bt_component
*comp
, const char *name
)
431 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
433 borrow_port_by_name(comp
->output_ports
, name
);
437 struct bt_port
*borrow_port_by_index(GPtrArray
*ports
, uint64_t index
)
439 BT_ASSERT(index
< ports
->len
);
440 return g_ptr_array_index(ports
, index
);
444 struct bt_port_input
*bt_component_borrow_input_port_by_index(
445 struct bt_component
*comp
, uint64_t index
)
447 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
448 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, comp
->input_ports
->len
);
450 borrow_port_by_index(comp
->input_ports
, index
);
454 struct bt_port_output
*bt_component_borrow_output_port_by_index(
455 struct bt_component
*comp
, uint64_t index
)
457 BT_ASSERT_PRE_DEV_NON_NULL(comp
, "Component");
458 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, comp
->output_ports
->len
);
460 borrow_port_by_index(comp
->output_ports
, index
);
464 enum bt_self_component_add_port_status
bt_component_add_input_port(
465 struct bt_component
*component
, const char *name
,
466 void *user_data
, struct bt_port
**port
)
468 /* add_port() logs details */
469 return add_port(component
, component
->input_ports
,
470 BT_PORT_TYPE_INPUT
, name
, user_data
, port
);
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
)
478 /* add_port() logs details */
479 return add_port(component
, component
->output_ports
,
480 BT_PORT_TYPE_OUTPUT
, name
, user_data
, port
);
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
;
497 BT_ASSERT(self_port
);
498 BT_ASSERT(other_port
);
500 switch (comp
->class->type
) {
501 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
503 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
505 switch (self_port
->type
) {
506 case BT_PORT_TYPE_OUTPUT
:
507 method
= (method_t
) src_cc
->methods
.output_port_connected
;
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
;
523 case BT_PORT_TYPE_OUTPUT
:
524 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
532 case BT_COMPONENT_CLASS_TYPE_SINK
:
534 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
536 switch (self_port
->type
) {
537 case BT_PORT_TYPE_INPUT
:
538 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
551 BT_LIB_LOGD("Calling user's \"port connected\" method: "
552 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
553 comp
, self_port
, other_port
);
554 status
= (int) method(comp
, self_port
, (void *) other_port
);
555 BT_LOGD("User method returned: status=%s",
556 bt_common_func_status_string(status
));
557 BT_ASSERT_POST(status
== BT_FUNC_STATUS_OK
||
558 status
== BT_FUNC_STATUS_ERROR
||
559 status
== BT_FUNC_STATUS_MEMORY_ERROR
,
560 "Unexpected returned component status: status=%s",
561 bt_common_func_status_string(status
));
562 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status
);
569 void bt_component_add_destroy_listener(struct bt_component
*component
,
570 bt_component_destroy_listener_func func
, void *data
)
572 struct bt_component_destroy_listener listener
;
574 BT_ASSERT(component
);
576 listener
.func
= func
;
577 listener
.data
= data
;
578 g_array_append_val(component
->destroy_listeners
, listener
);
579 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
580 "func-addr=%p, data-addr=%p",
581 component
, func
, data
);
585 void bt_component_remove_destroy_listener(struct bt_component
*component
,
586 bt_component_destroy_listener_func func
, void *data
)
590 BT_ASSERT(component
);
593 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
594 struct bt_component_destroy_listener
*listener
=
595 &g_array_index(component
->destroy_listeners
,
596 struct bt_component_destroy_listener
, i
);
598 if (listener
->func
== func
&& listener
->data
== data
) {
599 g_array_remove_index(component
->destroy_listeners
, i
);
601 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
602 "func-addr=%p, data-addr=%p",
603 component
, func
, data
);
608 bt_logging_level
bt_component_get_logging_level(
609 const struct bt_component
*component
)
611 BT_ASSERT_PRE_DEV_NON_NULL(component
, "Component");
612 return component
->log_level
;
615 uint64_t bt_self_component_get_graph_mip_version(
616 bt_self_component
*self_component
)
618 struct bt_component
*comp
= (void *) self_component
;
620 BT_ASSERT_PRE_NON_NULL(self_component
, "Component");
621 return bt_component_borrow_graph(comp
)->mip_version
;
624 void bt_component_get_ref(const struct bt_component
*component
)
626 bt_object_get_ref(component
);
629 void bt_component_put_ref(const struct bt_component
*component
)
631 bt_object_put_ref(component
);