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 "common/macros.h"
37 #include "compat/compiler.h"
38 #include <babeltrace2/types.h>
39 #include <babeltrace2/value.h>
40 #include "lib/value.h"
44 #include "component.h"
45 #include "component-class.h"
46 #include "component-source.h"
47 #include "component-filter.h"
48 #include "component-sink.h"
49 #include "connection.h"
51 #include "message/iterator.h"
55 struct bt_component
* (* const component_create_funcs
[])(
56 const struct bt_component_class
*) = {
57 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_create
,
58 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_create
,
59 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_create
,
63 void (*component_destroy_funcs
[])(struct bt_component
*) = {
64 [BT_COMPONENT_CLASS_TYPE_SOURCE
] = bt_component_source_destroy
,
65 [BT_COMPONENT_CLASS_TYPE_SINK
] = bt_component_sink_destroy
,
66 [BT_COMPONENT_CLASS_TYPE_FILTER
] = bt_component_filter_destroy
,
70 void finalize_component(struct bt_component
*comp
)
72 typedef void (*method_t
)(void *);
74 method_t method
= NULL
;
78 switch (comp
->class->type
) {
79 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
81 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
83 method
= (method_t
) src_cc
->methods
.finalize
;
86 case BT_COMPONENT_CLASS_TYPE_FILTER
:
88 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
90 method
= (method_t
) flt_cc
->methods
.finalize
;
93 case BT_COMPONENT_CLASS_TYPE_SINK
:
95 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
97 method
= (method_t
) sink_cc
->methods
.finalize
;
105 BT_LIB_LOGI("Calling user's component finalization method: "
106 "%![comp-]+c", comp
);
112 void destroy_component(struct bt_object
*obj
)
114 struct bt_component
*component
= NULL
;
122 * The component's reference count is 0 if we're here. Increment
123 * it to avoid a double-destroy (possibly infinitely recursive).
124 * This could happen for example if the component's finalization
125 * function does bt_object_get_ref() (or anything that causes
126 * bt_object_get_ref() to be called) on itself (ref. count goes
127 * from 0 to 1), and then bt_object_put_ref(): the reference
128 * count would go from 1 to 0 again and this function would be
132 component
= container_of(obj
, struct bt_component
, base
);
133 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
134 component
, bt_component_borrow_graph(component
));
136 /* Call destroy listeners in reverse registration order */
137 BT_LOGD_STR("Calling destroy listeners.");
139 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
140 struct bt_component_destroy_listener
*listener
=
141 &g_array_index(component
->destroy_listeners
,
142 struct bt_component_destroy_listener
, i
);
144 listener
->func(component
, listener
->data
);
148 * User data is destroyed first, followed by the concrete
149 * component instance. Do not finalize if the component's user
150 * initialization method failed in the first place.
152 if (component
->initialized
) {
153 finalize_component(component
);
156 if (component
->destroy
) {
157 BT_LOGD_STR("Destroying type-specific data.");
158 component
->destroy(component
);
161 if (component
->input_ports
) {
162 BT_LOGD_STR("Destroying input ports.");
163 g_ptr_array_free(component
->input_ports
, TRUE
);
164 component
->input_ports
= NULL
;
167 if (component
->output_ports
) {
168 BT_LOGD_STR("Destroying output ports.");
169 g_ptr_array_free(component
->output_ports
, TRUE
);
170 component
->output_ports
= NULL
;
173 if (component
->destroy_listeners
) {
174 g_array_free(component
->destroy_listeners
, TRUE
);
175 component
->destroy_listeners
= NULL
;
178 if (component
->name
) {
179 g_string_free(component
->name
, TRUE
);
180 component
->name
= NULL
;
183 BT_LOGD_STR("Putting component class.");
184 BT_OBJECT_PUT_REF_AND_RESET(component
->class);
188 enum bt_component_class_type
bt_component_get_class_type(
189 const struct bt_component
*component
)
191 BT_ASSERT_PRE_NON_NULL(component
, "Component");
192 return component
->class->type
;
196 enum bt_self_component_status
add_port(
197 struct bt_component
*component
, GPtrArray
*ports
,
198 enum bt_port_type port_type
, const char *name
, void *user_data
,
199 struct bt_port
**port
)
201 struct bt_port
*new_port
= NULL
;
202 struct bt_graph
*graph
= NULL
;
203 enum bt_self_component_status status
;
205 BT_ASSERT_PRE_NON_NULL(component
, "Component");
206 BT_ASSERT_PRE_NON_NULL(name
, "Name");
207 BT_ASSERT_PRE(strlen(name
) > 0, "Name is empty");
208 graph
= bt_component_borrow_graph(component
);
209 BT_ASSERT_PRE(graph
&& !bt_graph_is_canceled(graph
),
210 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
213 graph
->config_state
== BT_GRAPH_CONFIGURATION_STATE_CONFIGURING
,
214 "Component's graph is already configured: "
215 "%![comp-]+c, %![graph-]+g", component
, graph
);
217 // TODO: Validate that the name is not already used.
219 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
220 "port-type=%s, port-name=\"%s\"", component
,
221 bt_port_type_string(port_type
), name
);
223 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
225 BT_LOGE_STR("Cannot create port object.");
226 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
231 * No name clash, add the port.
232 * The component is now the port's parent; it should _not_
233 * hold a reference to the port since the port's lifetime
234 * is now protected by the component's own lifetime.
236 g_ptr_array_add(ports
, new_port
);
239 * Notify the graph's creator that a new port was added.
241 graph
= bt_component_borrow_graph(component
);
243 enum bt_graph_listener_status listener_status
;
245 listener_status
= bt_graph_notify_port_added(graph
, new_port
);
246 if (listener_status
!= BT_GRAPH_LISTENER_STATUS_OK
) {
247 bt_graph_make_faulty(graph
);
248 status
= listener_status
;
253 BT_LIB_LOGI("Created and added port to component: "
254 "%![comp-]+c, %![port-]+p", component
, new_port
);
257 status
= BT_SELF_COMPONENT_STATUS_OK
;
262 * We need to release the reference that we would otherwise have
263 * returned to the caller.
265 BT_PORT_PUT_REF_AND_RESET(new_port
);
272 uint64_t bt_component_get_input_port_count(const struct bt_component
*comp
)
274 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
275 return (uint64_t) comp
->input_ports
->len
;
279 uint64_t bt_component_get_output_port_count(const struct bt_component
*comp
)
281 BT_ASSERT_PRE_NON_NULL(comp
, "Component");
282 return (uint64_t) comp
->output_ports
->len
;
286 int bt_component_create(struct bt_component_class
*component_class
,
287 const char *name
, bt_logging_level log_level
,
288 struct bt_component
**user_component
)
291 struct bt_component
*component
= NULL
;
292 enum bt_component_class_type type
;
294 BT_ASSERT(user_component
);
295 BT_ASSERT(component_class
);
297 type
= bt_component_class_get_type(component_class
);
298 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
299 "comp-name=\"%s\", log-level=%s", component_class
, name
,
300 bt_common_logging_level_string(log_level
));
301 component
= component_create_funcs
[type
](component_class
);
303 BT_LOGE_STR("Cannot create specific component object.");
308 bt_object_init_shared_with_parent(&component
->base
, destroy_component
);
309 component
->class = component_class
;
310 bt_object_get_no_null_check(component
->class);
311 component
->destroy
= component_destroy_funcs
[type
];
312 component
->name
= g_string_new(name
);
313 if (!component
->name
) {
314 BT_LOGE_STR("Failed to allocate one GString.");
319 component
->log_level
= log_level
;
320 component
->input_ports
= g_ptr_array_new_with_free_func(
321 (GDestroyNotify
) bt_object_try_spec_release
);
322 if (!component
->input_ports
) {
323 BT_LOGE_STR("Failed to allocate one GPtrArray.");
328 component
->output_ports
= g_ptr_array_new_with_free_func(
329 (GDestroyNotify
) bt_object_try_spec_release
);
330 if (!component
->output_ports
) {
331 BT_LOGE_STR("Failed to allocate one GPtrArray.");
336 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
337 sizeof(struct bt_component_destroy_listener
));
338 if (!component
->destroy_listeners
) {
339 BT_LOGE_STR("Failed to allocate one GArray.");
344 BT_LIB_LOGI("Created empty component from component class: "
345 "%![cc-]+C, %![comp-]+c", component_class
, component
);
346 BT_OBJECT_MOVE_REF(*user_component
, component
);
349 bt_object_put_ref(component
);
353 const char *bt_component_get_name(const struct bt_component
*component
)
355 BT_ASSERT_PRE_NON_NULL(component
, "Component");
356 return component
->name
->str
;
359 const struct bt_component_class
*bt_component_borrow_class_const(
360 const struct bt_component
*component
)
362 BT_ASSERT_PRE_NON_NULL(component
, "Component");
363 return component
->class;
366 void *bt_self_component_get_data(const struct bt_self_component
*self_comp
)
368 struct bt_component
*component
= (void *) self_comp
;
370 BT_ASSERT_PRE_NON_NULL(component
, "Component");
371 return component
->user_data
;
374 void bt_self_component_set_data(struct bt_self_component
*self_comp
,
377 struct bt_component
*component
= (void *) self_comp
;
379 BT_ASSERT_PRE_NON_NULL(component
, "Component");
380 component
->user_data
= data
;
381 BT_LIB_LOGD("Set component's user data: %!+c", component
);
385 void bt_component_set_graph(struct bt_component
*component
,
386 struct bt_graph
*graph
)
388 bt_object_set_parent(&component
->base
,
389 graph
? &graph
->base
: NULL
);
392 bt_bool
bt_component_graph_is_canceled(const struct bt_component
*component
)
394 return bt_graph_is_canceled(
395 (void *) bt_object_borrow_parent(&component
->base
));
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
)) {
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_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_NON_NULL(comp
, "Component");
448 BT_ASSERT_PRE_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_NON_NULL(comp
, "Component");
458 BT_ASSERT_PRE_VALID_INDEX(index
, comp
->output_ports
->len
);
460 borrow_port_by_index(comp
->output_ports
, index
);
464 enum bt_self_component_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_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_self_component_status
bt_component_port_connected(
485 struct bt_component
*comp
, struct bt_port
*self_port
,
486 struct bt_port
*other_port
)
488 typedef enum bt_self_component_status (*method_t
)(
489 void *, void *, const void *);
491 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
492 method_t method
= NULL
;
495 BT_ASSERT(self_port
);
496 BT_ASSERT(other_port
);
498 switch (comp
->class->type
) {
499 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
501 struct bt_component_class_source
*src_cc
= (void *) comp
->class;
503 switch (self_port
->type
) {
504 case BT_PORT_TYPE_OUTPUT
:
505 method
= (method_t
) src_cc
->methods
.output_port_connected
;
513 case BT_COMPONENT_CLASS_TYPE_FILTER
:
515 struct bt_component_class_filter
*flt_cc
= (void *) comp
->class;
517 switch (self_port
->type
) {
518 case BT_PORT_TYPE_INPUT
:
519 method
= (method_t
) flt_cc
->methods
.input_port_connected
;
521 case BT_PORT_TYPE_OUTPUT
:
522 method
= (method_t
) flt_cc
->methods
.output_port_connected
;
530 case BT_COMPONENT_CLASS_TYPE_SINK
:
532 struct bt_component_class_sink
*sink_cc
= (void *) comp
->class;
534 switch (self_port
->type
) {
535 case BT_PORT_TYPE_INPUT
:
536 method
= (method_t
) sink_cc
->methods
.input_port_connected
;
549 BT_LIB_LOGD("Calling user's \"port connected\" method: "
550 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
551 comp
, self_port
, other_port
);
552 status
= method(comp
, self_port
, (void *) other_port
);
553 BT_LOGD("User method returned: status=%s",
554 bt_self_component_status_string(status
));
555 BT_ASSERT_POST(status
== BT_SELF_COMPONENT_STATUS_OK
||
556 status
== BT_SELF_COMPONENT_STATUS_ERROR
||
557 status
== BT_SELF_COMPONENT_STATUS_NOMEM
,
558 "Unexpected returned component status: status=%s",
559 bt_self_component_status_string(status
));
566 void bt_component_add_destroy_listener(struct bt_component
*component
,
567 bt_component_destroy_listener_func func
, void *data
)
569 struct bt_component_destroy_listener listener
;
571 BT_ASSERT(component
);
573 listener
.func
= func
;
574 listener
.data
= data
;
575 g_array_append_val(component
->destroy_listeners
, listener
);
576 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
577 "func-addr=%p, data-addr=%p",
578 component
, func
, data
);
582 void bt_component_remove_destroy_listener(struct bt_component
*component
,
583 bt_component_destroy_listener_func func
, void *data
)
587 BT_ASSERT(component
);
590 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
591 struct bt_component_destroy_listener
*listener
=
592 &g_array_index(component
->destroy_listeners
,
593 struct bt_component_destroy_listener
, i
);
595 if (listener
->func
== func
&& listener
->data
== data
) {
596 g_array_remove_index(component
->destroy_listeners
, i
);
598 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
599 "func-addr=%p, data-addr=%p",
600 component
, func
, data
);
605 bt_logging_level
bt_component_get_logging_level(
606 const struct bt_component
*component
)
608 BT_ASSERT_PRE_NON_NULL(component
, "Component");
609 return component
->log_level
;
612 void bt_component_get_ref(const struct bt_component
*component
)
614 bt_object_get_ref(component
);
617 void bt_component_put_ref(const struct bt_component
*component
)
619 bt_object_put_ref(component
);