End connection on destruction
[babeltrace.git] / lib / graph / component.c
1 /*
2 * component.c
3 *
4 * Babeltrace Plugin Component
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "COMP"
30 #include <babeltrace/lib-logging-internal.h>
31
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>
50 #include <stdint.h>
51 #include <inttypes.h>
52
53 static
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,
59 };
60
61 static
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,
66 };
67
68 static
69 void bt_component_destroy(struct bt_object *obj)
70 {
71 struct bt_component *component = NULL;
72 struct bt_component_class *component_class = NULL;
73 int i;
74
75 if (!obj) {
76 return;
77 }
78
79 /*
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.
87 */
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),
92 obj->parent);
93
94 /* Call destroy listeners in reverse registration order */
95 BT_LOGD_STR("Calling destroy listeners.");
96
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);
101
102 listener->func(component, listener->data);
103 }
104
105 component_class = component->class;
106
107 /*
108 * User data is destroyed first, followed by the concrete component
109 * instance.
110 */
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));
115 }
116
117 if (component->destroy) {
118 BT_LOGD_STR("Destroying type-specific data.");
119 component->destroy(component);
120 }
121
122 if (component->input_ports) {
123 BT_LOGD_STR("Destroying input ports.");
124 g_ptr_array_free(component->input_ports, TRUE);
125 }
126
127 if (component->output_ports) {
128 BT_LOGD_STR("Destroying output ports.");
129 g_ptr_array_free(component->output_ports, TRUE);
130 }
131
132 if (component->destroy_listeners) {
133 g_array_free(component->destroy_listeners, TRUE);
134 }
135
136 if (component->name) {
137 g_string_free(component->name, TRUE);
138 }
139
140 BT_LOGD("Putting component class.");
141 bt_put(component_class);
142 g_free(component);
143 }
144
145 struct bt_component *bt_component_from_private_component(
146 struct bt_private_component *private_component)
147 {
148 return bt_get(bt_component_from_private(private_component));
149 }
150
151 enum bt_component_class_type bt_component_get_class_type(
152 struct bt_component *component)
153 {
154 return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN;
155 }
156
157 static
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)
161 {
162 size_t i;
163 struct bt_port *new_port = NULL;
164 struct bt_graph *graph = NULL;
165
166 if (!name) {
167 BT_LOGW_STR("Invalid parameter: name is NULL.");
168 goto end;
169 }
170
171 if (strlen(name) == 0) {
172 BT_LOGW_STR("Invalid parameter: name is an empty string.");
173 goto end;
174 }
175
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);
180
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);
185
186 port_name = bt_port_get_name(port);
187 assert(port_name);
188
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);
193 goto end;
194 }
195 }
196
197 new_port = bt_port_create(component, port_type, name, user_data);
198 if (!new_port) {
199 BT_LOGE("Cannot create port object.");
200 goto end;
201 }
202
203 /*
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.
208 */
209 g_ptr_array_add(ports, new_port);
210
211 /*
212 * Notify the graph's creator that a new port was added.
213 */
214 graph = bt_component_get_graph(component);
215 if (graph) {
216 bt_graph_notify_port_added(graph, new_port);
217 BT_PUT(graph);
218 }
219
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);
224
225 end:
226 return new_port;
227 }
228
229 BT_HIDDEN
230 int64_t bt_component_get_input_port_count(struct bt_component *comp)
231 {
232 assert(comp);
233 return (int64_t) comp->input_ports->len;
234 }
235
236 BT_HIDDEN
237 int64_t bt_component_get_output_port_count(struct bt_component *comp)
238 {
239 assert(comp);
240 return (int64_t) comp->output_ports->len;
241 }
242
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)
246 {
247 int ret;
248 struct bt_component *component = NULL;
249 enum bt_component_class_type type;
250
251 bt_get(params);
252
253 if (!component_class) {
254 BT_LOGW_STR("Invalid parameter: component class is NULL.");
255 goto end;
256 }
257
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));
263 goto end;
264 }
265
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);
271
272 /*
273 * Parameters must be a map value, but we create a convenient
274 * empty one if it's NULL.
275 */
276 if (params) {
277 if (!bt_value_is_map(params)) {
278 BT_LOGW("Invalid parameter: initialization parameters must be a map value: "
279 "type=%s",
280 bt_value_type_string(bt_value_get_type(params)));
281 goto end;
282 }
283 } else {
284 params = bt_value_map_create();
285 if (!params) {
286 BT_LOGE_STR("Cannot create map value object.");
287 goto end;
288 }
289 }
290
291 component = component_create_funcs[type](component_class, params);
292 if (!component) {
293 BT_LOGE_STR("Cannot create specific component object.");
294 goto end;
295 }
296
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.");
303 BT_PUT(component);
304 goto end;
305 }
306
307 component->input_ports = g_ptr_array_new_with_free_func(
308 bt_object_release);
309 if (!component->input_ports) {
310 BT_LOGE_STR("Failed to allocate one GPtrArray.");
311 BT_PUT(component);
312 goto end;
313 }
314
315 component->output_ports = g_ptr_array_new_with_free_func(
316 bt_object_release);
317 if (!component->output_ports) {
318 BT_LOGE_STR("Failed to allocate one GPtrArray.");
319 BT_PUT(component);
320 goto end;
321 }
322
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.");
327 BT_PUT(component);
328 goto end;
329 }
330
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,
335 init_method_data);
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.");
340 BT_PUT(component);
341 goto end;
342 }
343 }
344
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);
352
353 end:
354 bt_put(params);
355 return component;
356 }
357
358 struct bt_component *bt_component_create(
359 struct bt_component_class *component_class, const char *name,
360 struct bt_value *params)
361 {
362 /* bt_component_create_with_init_method_data() logs details */
363 return bt_component_create_with_init_method_data(component_class, name,
364 params, NULL);
365 }
366
367 const char *bt_component_get_name(struct bt_component *component)
368 {
369 const char *ret = NULL;
370
371 if (!component) {
372 BT_LOGW_STR("Invalid parameter: component is NULL.");
373 goto end;
374 }
375
376 ret = component->name->len == 0 ? NULL : component->name->str;
377
378 end:
379 return ret;
380 }
381
382 struct bt_component_class *bt_component_get_class(
383 struct bt_component *component)
384 {
385 return component ? bt_get(component->class) : NULL;
386 }
387
388 void *bt_private_component_get_user_data(
389 struct bt_private_component *private_component)
390 {
391 struct bt_component *component =
392 bt_component_from_private(private_component);
393
394 return component ? component->user_data : NULL;
395 }
396
397 enum bt_component_status bt_private_component_set_user_data(
398 struct bt_private_component *private_component,
399 void *data)
400 {
401 struct bt_component *component =
402 bt_component_from_private(private_component);
403 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
404
405 if (!component) {
406 BT_LOGW_STR("Invalid parameter: component is NULL.");
407 ret = BT_COMPONENT_STATUS_INVALID;
408 goto end;
409 }
410
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);
415
416 end:
417 return ret;
418 }
419
420 BT_HIDDEN
421 void bt_component_set_graph(struct bt_component *component,
422 struct bt_graph *graph)
423 {
424 struct bt_object *parent = bt_object_get_parent(&component->base);
425
426 assert(!parent || parent == &graph->base);
427 if (!parent) {
428 bt_object_set_parent(component, &graph->base);
429 }
430 bt_put(parent);
431 }
432
433 struct bt_graph *bt_component_get_graph(
434 struct bt_component *component)
435 {
436 return (struct bt_graph *) bt_object_get_parent(&component->base);
437 }
438
439 static
440 struct bt_port *bt_component_get_port_by_name(GPtrArray *ports,
441 const char *name)
442 {
443 size_t i;
444 struct bt_port *ret_port = NULL;
445
446 assert(name);
447
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);
451
452 if (!port_name) {
453 continue;
454 }
455
456 if (!strcmp(name, port_name)) {
457 ret_port = bt_get(port);
458 break;
459 }
460 }
461
462 return ret_port;
463 }
464
465 BT_HIDDEN
466 struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp,
467 const char *name)
468 {
469 assert(comp);
470
471 return bt_component_get_port_by_name(comp->input_ports, name);
472 }
473
474 BT_HIDDEN
475 struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp,
476 const char *name)
477 {
478 assert(comp);
479
480 return bt_component_get_port_by_name(comp->output_ports, name);
481 }
482
483 static
484 struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index)
485 {
486 struct bt_port *port = NULL;
487
488 if (index >= ports->len) {
489 BT_LOGW("Invalid parameter: index is out of bounds: "
490 "index=%" PRIu64 ", count=%u",
491 index, ports->len);
492 goto end;
493 }
494
495 port = bt_get(g_ptr_array_index(ports, index));
496 end:
497 return port;
498 }
499
500 BT_HIDDEN
501 struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp,
502 uint64_t index)
503 {
504 assert(comp);
505
506 return bt_component_get_port_by_index(comp->input_ports, index);
507 }
508
509 BT_HIDDEN
510 struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp,
511 uint64_t index)
512 {
513 assert(comp);
514
515 return bt_component_get_port_by_index(comp->output_ports, index);
516 }
517
518 BT_HIDDEN
519 struct bt_port *bt_component_add_input_port(
520 struct bt_component *component, const char *name,
521 void *user_data)
522 {
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);
526 }
527
528 BT_HIDDEN
529 struct bt_port *bt_component_add_output_port(
530 struct bt_component *component, const char *name,
531 void *user_data)
532 {
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);
536 }
537
538 static
539 void bt_component_remove_port_by_index(struct bt_component *component,
540 GPtrArray *ports, size_t index)
541 {
542 struct bt_port *port;
543 struct bt_graph *graph;
544
545 assert(ports);
546 assert(index < ports->len);
547 port = g_ptr_array_index(ports, index);
548
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));
554
555 /* Disconnect both ports of this port's connection, if any */
556 if (port->connection) {
557 bt_connection_end(port->connection, true);
558 }
559
560 /* Remove from parent's array of ports (weak refs) */
561 g_ptr_array_remove_index(ports, index);
562
563 /* Detach port from its component parent */
564 BT_PUT(port->base.parent);
565
566 /*
567 * Notify the graph's creator that a port is removed.
568 */
569 graph = bt_component_get_graph(component);
570 if (graph) {
571 bt_graph_notify_port_removed(graph, component, port);
572 BT_PUT(graph);
573 }
574
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));
580 }
581
582 BT_HIDDEN
583 enum bt_component_status bt_component_remove_port(
584 struct bt_component *component, struct bt_port *port)
585 {
586 size_t i;
587 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
588 GPtrArray *ports = NULL;
589
590 if (!component) {
591 BT_LOGW_STR("Invalid parameter: component is NULL.");
592 status = BT_COMPONENT_STATUS_INVALID;
593 goto end;
594 }
595
596 if (!port) {
597 BT_LOGW_STR("Invalid parameter: port is NULL.");
598 status = BT_COMPONENT_STATUS_INVALID;
599 goto end;
600 }
601
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;
606 }
607
608 assert(ports);
609
610 for (i = 0; i < ports->len; i++) {
611 struct bt_port *cur_port = g_ptr_array_index(ports, i);
612
613 if (cur_port == port) {
614 bt_component_remove_port_by_index(component,
615 ports, i);
616 goto end;
617 }
618 }
619
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));
626
627 end:
628 return status;
629 }
630
631 BT_HIDDEN
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)
635 {
636 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
637
638 assert(comp);
639 assert(self_port);
640 assert(other_port);
641
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),
653 other_port);
654 BT_LOGD("User method returned: status=%s",
655 bt_component_status_string(status));
656 }
657
658 return status;
659 }
660
661 BT_HIDDEN
662 void bt_component_port_connected(struct bt_component *comp,
663 struct bt_port *self_port, struct bt_port *other_port)
664 {
665 assert(comp);
666 assert(self_port);
667 assert(other_port);
668
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);
680 }
681 }
682
683 BT_HIDDEN
684 void bt_component_port_disconnected(struct bt_component *comp,
685 struct bt_port *port)
686 {
687 assert(comp);
688 assert(port);
689
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));
699 }
700 }
701
702 BT_HIDDEN
703 void bt_component_add_destroy_listener(struct bt_component *component,
704 bt_component_destroy_listener_func func, void *data)
705 {
706 struct bt_component_destroy_listener listener;
707
708 assert(component);
709 assert(func);
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),
717 func, data);
718 }
719
720 BT_HIDDEN
721 void bt_component_remove_destroy_listener(struct bt_component *component,
722 bt_component_destroy_listener_func func, void *data)
723 {
724 size_t i;
725
726 assert(component);
727 assert(func);
728
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);
733
734 if (listener->func == func && listener->data == data) {
735 g_array_remove_index(component->destroy_listeners, i);
736 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),
741 func, data);
742 }
743 }
744 }
This page took 0.04331 seconds and 4 git commands to generate.