4 * Babeltrace Plugin Component
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "COMP"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/component.h>
34 #include <babeltrace/graph/component-internal.h>
35 #include <babeltrace/graph/component-class-internal.h>
36 #include <babeltrace/graph/component-source-internal.h>
37 #include <babeltrace/graph/component-filter-internal.h>
38 #include <babeltrace/graph/component-sink-internal.h>
39 #include <babeltrace/graph/private-connection.h>
40 #include <babeltrace/graph/connection-internal.h>
41 #include <babeltrace/graph/graph-internal.h>
42 #include <babeltrace/graph/notification-iterator-internal.h>
43 #include <babeltrace/graph/private-notification-iterator.h>
44 #include <babeltrace/babeltrace-internal.h>
45 #include <babeltrace/compiler-internal.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/types.h>
48 #include <babeltrace/values.h>
49 #include <babeltrace/values-internal.h>
54 struct bt_component
* (* const component_create_funcs
[])(
55 struct bt_component_class
*, struct bt_value
*) = {
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 bt_component_destroy(struct bt_object
*obj
)
71 struct bt_component
*component
= NULL
;
72 struct bt_component_class
*component_class
= NULL
;
80 * The component's reference count is 0 if we're here. Increment
81 * it to avoid a double-destroy (possibly infinitely recursive).
82 * This could happen for example if the component's finalization
83 * function does bt_get() (or anything that causes bt_get() to
84 * be called) on itself (ref. count goes from 0 to 1), and then
85 * bt_put(): the reference count would go from 1 to 0 again and
86 * this function would be called again.
88 obj
->ref_count
.count
++;
89 component
= container_of(obj
, struct bt_component
, base
);
90 BT_LOGD("Destroying component: addr=%p, name=\"%s\", graph-addr=%p",
91 component
, bt_component_get_name(component
),
94 /* Call destroy listeners in reverse registration order */
95 BT_LOGD_STR("Calling destroy listeners.");
97 for (i
= component
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
98 struct bt_component_destroy_listener
*listener
=
99 &g_array_index(component
->destroy_listeners
,
100 struct bt_component_destroy_listener
, i
);
102 listener
->func(component
, listener
->data
);
105 component_class
= component
->class;
108 * User data is destroyed first, followed by the concrete component
111 if (component
->class->methods
.finalize
) {
112 BT_LOGD_STR("Calling user's finalization method.");
113 component
->class->methods
.finalize(
114 bt_private_component_from_component(component
));
117 if (component
->destroy
) {
118 BT_LOGD_STR("Destroying type-specific data.");
119 component
->destroy(component
);
122 if (component
->input_ports
) {
123 BT_LOGD_STR("Destroying input ports.");
124 g_ptr_array_free(component
->input_ports
, TRUE
);
127 if (component
->output_ports
) {
128 BT_LOGD_STR("Destroying output ports.");
129 g_ptr_array_free(component
->output_ports
, TRUE
);
132 if (component
->destroy_listeners
) {
133 g_array_free(component
->destroy_listeners
, TRUE
);
136 if (component
->name
) {
137 g_string_free(component
->name
, TRUE
);
140 BT_LOGD("Putting component class.");
141 bt_put(component_class
);
145 struct bt_component
*bt_component_from_private_component(
146 struct bt_private_component
*private_component
)
148 return bt_get(bt_component_from_private(private_component
));
151 enum bt_component_class_type
bt_component_get_class_type(
152 struct bt_component
*component
)
154 return component
? component
->class->type
: BT_COMPONENT_CLASS_TYPE_UNKNOWN
;
158 struct bt_port
*bt_component_add_port(
159 struct bt_component
*component
, GPtrArray
*ports
,
160 enum bt_port_type port_type
, const char *name
, void *user_data
)
163 struct bt_port
*new_port
= NULL
;
164 struct bt_graph
*graph
= NULL
;
167 BT_LOGW_STR("Invalid parameter: name is NULL.");
171 if (strlen(name
) == 0) {
172 BT_LOGW_STR("Invalid parameter: name is an empty string.");
176 BT_LOGD("Adding port to component: comp-addr=%p, comp-name=\"%s\", "
177 "port-type=%s, port-name=\"%s\"", component
,
178 bt_component_get_name(component
),
179 bt_port_type_string(port_type
), name
);
181 /* Look for a port having the same name. */
182 for (i
= 0; i
< ports
->len
; i
++) {
183 const char *port_name
;
184 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
186 port_name
= bt_port_get_name(port
);
189 if (!strcmp(name
, port_name
)) {
190 /* Port name clash, abort. */
191 BT_LOGW("Invalid parameter: another port with the same name already exists in the component: "
192 "other-port-addr=%p", port
);
197 new_port
= bt_port_create(component
, port_type
, name
, user_data
);
199 BT_LOGE("Cannot create port object.");
204 * No name clash, add the port.
205 * The component is now the port's parent; it should _not_
206 * hold a reference to the port since the port's lifetime
207 * is now protected by the component's own lifetime.
209 g_ptr_array_add(ports
, new_port
);
212 * Notify the graph's creator that a new port was added.
214 graph
= bt_component_get_graph(component
);
216 bt_graph_notify_port_added(graph
, new_port
);
220 BT_LOGD("Created and added port to component: comp-addr=%p, comp-name=\"%s\", "
221 "port-type=%s, port-name=\"%s\", port-addr=%p", component
,
222 bt_component_get_name(component
),
223 bt_port_type_string(port_type
), name
, new_port
);
230 int64_t bt_component_get_input_port_count(struct bt_component
*comp
)
233 return (int64_t) comp
->input_ports
->len
;
237 int64_t bt_component_get_output_port_count(struct bt_component
*comp
)
240 return (int64_t) comp
->output_ports
->len
;
243 struct bt_component
*bt_component_create_with_init_method_data(
244 struct bt_component_class
*component_class
, const char *name
,
245 struct bt_value
*params
, void *init_method_data
)
248 struct bt_component
*component
= NULL
;
249 enum bt_component_class_type type
;
253 if (!component_class
) {
254 BT_LOGW_STR("Invalid parameter: component class is NULL.");
258 type
= bt_component_class_get_type(component_class
);
259 if (type
<= BT_COMPONENT_CLASS_TYPE_UNKNOWN
||
260 type
> BT_COMPONENT_CLASS_TYPE_FILTER
) {
261 BT_LOGW("Invalid parameter: unknown component class type: "
262 "type=%s", bt_component_class_type_string(type
));
266 BT_LOGD("Creating component from component class: "
267 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", "
268 "params-addr=%p, init-method-data-addr=%p",
269 component_class
, bt_component_class_type_string(type
),
270 name
, params
, init_method_data
);
273 * Parameters must be a map value, but we create a convenient
274 * empty one if it's NULL.
277 if (!bt_value_is_map(params
)) {
278 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
280 bt_value_type_string(bt_value_get_type(params
)));
284 params
= bt_value_map_create();
286 BT_LOGE_STR("Cannot create map value object.");
291 component
= component_create_funcs
[type
](component_class
, params
);
293 BT_LOGE_STR("Cannot create specific component object.");
297 bt_object_init(component
, bt_component_destroy
);
298 component
->class = bt_get(component_class
);
299 component
->destroy
= component_destroy_funcs
[type
];
300 component
->name
= g_string_new(name
);
301 if (!component
->name
) {
302 BT_LOGE_STR("Failed to allocate one GString.");
307 component
->input_ports
= g_ptr_array_new_with_free_func(
309 if (!component
->input_ports
) {
310 BT_LOGE_STR("Failed to allocate one GPtrArray.");
315 component
->output_ports
= g_ptr_array_new_with_free_func(
317 if (!component
->output_ports
) {
318 BT_LOGE_STR("Failed to allocate one GPtrArray.");
323 component
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
324 sizeof(struct bt_component_destroy_listener
));
325 if (!component
->destroy_listeners
) {
326 BT_LOGE_STR("Failed to allocate one GArray.");
331 if (component_class
->methods
.init
) {
332 BT_LOGD_STR("Calling user's initialization method.");
333 ret
= component_class
->methods
.init(
334 bt_private_component_from_component(component
), params
,
336 BT_LOGD("User method returned: status=%s",
337 bt_component_status_string(ret
));
338 if (ret
!= BT_COMPONENT_STATUS_OK
) {
339 BT_LOGW_STR("Initialization method failed.");
345 BT_LOGD_STR("Freezing component class.");
346 bt_component_class_freeze(component
->class);
347 BT_LOGD("Created component from component class: "
348 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", "
349 "params-addr=%p, init-method-data-addr=%p, comp-addr=%p",
350 component_class
, bt_component_class_type_string(type
),
351 name
, params
, init_method_data
, component
);
358 struct bt_component
*bt_component_create(
359 struct bt_component_class
*component_class
, const char *name
,
360 struct bt_value
*params
)
362 /* bt_component_create_with_init_method_data() logs details */
363 return bt_component_create_with_init_method_data(component_class
, name
,
367 const char *bt_component_get_name(struct bt_component
*component
)
369 const char *ret
= NULL
;
372 BT_LOGW_STR("Invalid parameter: component is NULL.");
376 ret
= component
->name
->len
== 0 ? NULL
: component
->name
->str
;
382 struct bt_component_class
*bt_component_get_class(
383 struct bt_component
*component
)
385 return component
? bt_get(component
->class) : NULL
;
388 void *bt_private_component_get_user_data(
389 struct bt_private_component
*private_component
)
391 struct bt_component
*component
=
392 bt_component_from_private(private_component
);
394 return component
? component
->user_data
: NULL
;
397 enum bt_component_status
bt_private_component_set_user_data(
398 struct bt_private_component
*private_component
,
401 struct bt_component
*component
=
402 bt_component_from_private(private_component
);
403 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
406 BT_LOGW_STR("Invalid parameter: component is NULL.");
407 ret
= BT_COMPONENT_STATUS_INVALID
;
411 component
->user_data
= data
;
412 BT_LOGV("Set component's user data: "
413 "comp-addr=%p, comp-name=\"%s\", user-data-addr=%p",
414 component
, bt_component_get_name(component
), data
);
421 void bt_component_set_graph(struct bt_component
*component
,
422 struct bt_graph
*graph
)
424 struct bt_object
*parent
= bt_object_get_parent(&component
->base
);
426 assert(!parent
|| parent
== &graph
->base
);
428 bt_object_set_parent(component
, &graph
->base
);
433 struct bt_graph
*bt_component_get_graph(
434 struct bt_component
*component
)
436 return (struct bt_graph
*) bt_object_get_parent(&component
->base
);
440 struct bt_port
*bt_component_get_port_by_name(GPtrArray
*ports
,
444 struct bt_port
*ret_port
= NULL
;
448 for (i
= 0; i
< ports
->len
; i
++) {
449 struct bt_port
*port
= g_ptr_array_index(ports
, i
);
450 const char *port_name
= bt_port_get_name(port
);
456 if (!strcmp(name
, port_name
)) {
457 ret_port
= bt_get(port
);
466 struct bt_port
*bt_component_get_input_port_by_name(struct bt_component
*comp
,
471 return bt_component_get_port_by_name(comp
->input_ports
, name
);
475 struct bt_port
*bt_component_get_output_port_by_name(struct bt_component
*comp
,
480 return bt_component_get_port_by_name(comp
->output_ports
, name
);
484 struct bt_port
*bt_component_get_port_by_index(GPtrArray
*ports
, uint64_t index
)
486 struct bt_port
*port
= NULL
;
488 if (index
>= ports
->len
) {
489 BT_LOGW("Invalid parameter: index is out of bounds: "
490 "index=%" PRIu64
", count=%u",
495 port
= bt_get(g_ptr_array_index(ports
, index
));
501 struct bt_port
*bt_component_get_input_port_by_index(struct bt_component
*comp
,
506 return bt_component_get_port_by_index(comp
->input_ports
, index
);
510 struct bt_port
*bt_component_get_output_port_by_index(struct bt_component
*comp
,
515 return bt_component_get_port_by_index(comp
->output_ports
, index
);
519 struct bt_port
*bt_component_add_input_port(
520 struct bt_component
*component
, const char *name
,
523 /* bt_component_add_port() logs details */
524 return bt_component_add_port(component
, component
->input_ports
,
525 BT_PORT_TYPE_INPUT
, name
, user_data
);
529 struct bt_port
*bt_component_add_output_port(
530 struct bt_component
*component
, const char *name
,
533 /* bt_component_add_port() logs details */
534 return bt_component_add_port(component
, component
->output_ports
,
535 BT_PORT_TYPE_OUTPUT
, name
, user_data
);
539 void bt_component_remove_port_by_index(struct bt_component
*component
,
540 GPtrArray
*ports
, size_t index
)
542 struct bt_port
*port
;
543 struct bt_graph
*graph
;
546 assert(index
< ports
->len
);
547 port
= g_ptr_array_index(ports
, index
);
549 BT_LOGD("Removing port from component: "
550 "comp-addr=%p, comp-name=\"%s\", "
551 "port-addr=%p, port-name=\"%s\"",
552 component
, bt_component_get_name(component
),
553 port
, bt_port_get_name(port
));
555 /* Disconnect both ports of this port's connection, if any */
556 if (port
->connection
) {
557 bt_connection_disconnect_ports(port
->connection
);
560 /* Remove from parent's array of ports (weak refs) */
561 g_ptr_array_remove_index(ports
, index
);
563 /* Detach port from its component parent */
564 BT_PUT(port
->base
.parent
);
567 * Notify the graph's creator that a port is removed.
569 graph
= bt_component_get_graph(component
);
571 bt_graph_notify_port_removed(graph
, component
, port
);
575 BT_LOGD("Removed port from component: "
576 "comp-addr=%p, comp-name=\"%s\", "
577 "port-addr=%p, port-name=\"%s\"",
578 component
, bt_component_get_name(component
),
579 port
, bt_port_get_name(port
));
583 enum bt_component_status
bt_component_remove_port(
584 struct bt_component
*component
, struct bt_port
*port
)
587 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
588 GPtrArray
*ports
= NULL
;
591 BT_LOGW_STR("Invalid parameter: component is NULL.");
592 status
= BT_COMPONENT_STATUS_INVALID
;
597 BT_LOGW_STR("Invalid parameter: port is NULL.");
598 status
= BT_COMPONENT_STATUS_INVALID
;
602 if (bt_port_get_type(port
) == BT_PORT_TYPE_INPUT
) {
603 ports
= component
->input_ports
;
604 } else if (bt_port_get_type(port
) == BT_PORT_TYPE_OUTPUT
) {
605 ports
= component
->output_ports
;
610 for (i
= 0; i
< ports
->len
; i
++) {
611 struct bt_port
*cur_port
= g_ptr_array_index(ports
, i
);
613 if (cur_port
== port
) {
614 bt_component_remove_port_by_index(component
,
620 status
= BT_COMPONENT_STATUS_NOT_FOUND
;
621 BT_LOGW("Port to remove from component was not found: "
622 "comp-addr=%p, comp-name=\"%s\", "
623 "port-addr=%p, port-name=\"%s\"",
624 component
, bt_component_get_name(component
),
625 port
, bt_port_get_name(port
));
632 enum bt_component_status
bt_component_accept_port_connection(
633 struct bt_component
*comp
, struct bt_port
*self_port
,
634 struct bt_port
*other_port
)
636 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
642 if (comp
->class->methods
.accept_port_connection
) {
643 BT_LOGD("Calling user's \"accept port connection\" method: "
644 "comp-addr=%p, comp-name=\"%s\", "
645 "self-port-addr=%p, self-port-name=\"%s\", "
646 "other-port-addr=%p, other-port-name=\"%s\"",
647 comp
, bt_component_get_name(comp
),
648 self_port
, bt_port_get_name(self_port
),
649 other_port
, bt_port_get_name(other_port
));
650 status
= comp
->class->methods
.accept_port_connection(
651 bt_private_component_from_component(comp
),
652 bt_private_port_from_port(self_port
),
654 BT_LOGD("User method returned: status=%s",
655 bt_component_status_string(status
));
662 void bt_component_port_connected(struct bt_component
*comp
,
663 struct bt_port
*self_port
, struct bt_port
*other_port
)
669 if (comp
->class->methods
.port_connected
) {
670 BT_LOGD("Calling user's \"port connected\" method: "
671 "comp-addr=%p, comp-name=\"%s\", "
672 "self-port-addr=%p, self-port-name=\"%s\", "
673 "other-port-addr=%p, other-port-name=\"%s\"",
674 comp
, bt_component_get_name(comp
),
675 self_port
, bt_port_get_name(self_port
),
676 other_port
, bt_port_get_name(other_port
));
677 comp
->class->methods
.port_connected(
678 bt_private_component_from_component(comp
),
679 bt_private_port_from_port(self_port
), other_port
);
684 void bt_component_port_disconnected(struct bt_component
*comp
,
685 struct bt_port
*port
)
690 if (comp
->class->methods
.port_disconnected
) {
691 BT_LOGD("Calling user's \"port disconnected\" method: "
692 "comp-addr=%p, comp-name=\"%s\", "
693 "port-addr=%p, port-name=\"%s\"",
694 comp
, bt_component_get_name(comp
),
695 port
, bt_port_get_name(port
));
696 comp
->class->methods
.port_disconnected(
697 bt_private_component_from_component(comp
),
698 bt_private_port_from_port(port
));
703 void bt_component_add_destroy_listener(struct bt_component
*component
,
704 bt_component_destroy_listener_func func
, void *data
)
706 struct bt_component_destroy_listener listener
;
710 listener
.func
= func
;
711 listener
.data
= data
;
712 g_array_append_val(component
->destroy_listeners
, listener
);
713 BT_LOGV("Added destroy listener: "
714 "comp-addr=%p, comp-name=\"%s\", "
715 "func-addr=%p, data-addr=%p",
716 component
, bt_component_get_name(component
),
721 void bt_component_remove_destroy_listener(struct bt_component
*component
,
722 bt_component_destroy_listener_func func
, void *data
)
729 for (i
= 0; i
< component
->destroy_listeners
->len
; i
++) {
730 struct bt_component_destroy_listener
*listener
=
731 &g_array_index(component
->destroy_listeners
,
732 struct bt_component_destroy_listener
, i
);
734 if (listener
->func
== func
&& listener
->data
== data
) {
735 g_array_remove_index(component
->destroy_listeners
, i
);
737 BT_LOGV("Removed destroy listener: "
738 "comp-addr=%p, comp-name=\"%s\", "
739 "func-addr=%p, data-addr=%p",
740 component
, bt_component_get_name(component
),