Add bt_graph_add_component(), make bt_component_create() internal
[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 *) = {
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
109 * component instance. Do not finalize if the component's user
110 * initialization method failed in the first place.
111 */
112 if (component->initialized && component->class->methods.finalize) {
113 BT_LOGD_STR("Calling user's finalization method.");
114 component->class->methods.finalize(
115 bt_private_component_from_component(component));
116 }
117
118 if (component->destroy) {
119 BT_LOGD_STR("Destroying type-specific data.");
120 component->destroy(component);
121 }
122
123 if (component->input_ports) {
124 BT_LOGD_STR("Destroying input ports.");
125 g_ptr_array_free(component->input_ports, TRUE);
126 }
127
128 if (component->output_ports) {
129 BT_LOGD_STR("Destroying output ports.");
130 g_ptr_array_free(component->output_ports, TRUE);
131 }
132
133 if (component->destroy_listeners) {
134 g_array_free(component->destroy_listeners, TRUE);
135 }
136
137 if (component->name) {
138 g_string_free(component->name, TRUE);
139 }
140
141 BT_LOGD("Putting component class.");
142 bt_put(component_class);
143 g_free(component);
144 }
145
146 struct bt_component *bt_component_from_private_component(
147 struct bt_private_component *private_component)
148 {
149 return bt_get(bt_component_from_private(private_component));
150 }
151
152 enum bt_component_class_type bt_component_get_class_type(
153 struct bt_component *component)
154 {
155 return component ? component->class->type : BT_COMPONENT_CLASS_TYPE_UNKNOWN;
156 }
157
158 static
159 struct bt_port *bt_component_add_port(
160 struct bt_component *component, GPtrArray *ports,
161 enum bt_port_type port_type, const char *name, void *user_data)
162 {
163 size_t i;
164 struct bt_port *new_port = NULL;
165 struct bt_graph *graph = NULL;
166
167 if (!name) {
168 BT_LOGW_STR("Invalid parameter: name is NULL.");
169 goto end;
170 }
171
172 if (strlen(name) == 0) {
173 BT_LOGW_STR("Invalid parameter: name is an empty string.");
174 goto end;
175 }
176
177 BT_LOGD("Adding port to component: comp-addr=%p, comp-name=\"%s\", "
178 "port-type=%s, port-name=\"%s\"", component,
179 bt_component_get_name(component),
180 bt_port_type_string(port_type), name);
181
182 /* Look for a port having the same name. */
183 for (i = 0; i < ports->len; i++) {
184 const char *port_name;
185 struct bt_port *port = g_ptr_array_index(ports, i);
186
187 port_name = bt_port_get_name(port);
188 assert(port_name);
189
190 if (!strcmp(name, port_name)) {
191 /* Port name clash, abort. */
192 BT_LOGW("Invalid parameter: another port with the same name already exists in the component: "
193 "other-port-addr=%p", port);
194 goto end;
195 }
196 }
197
198 new_port = bt_port_create(component, port_type, name, user_data);
199 if (!new_port) {
200 BT_LOGE("Cannot create port object.");
201 goto end;
202 }
203
204 /*
205 * No name clash, add the port.
206 * The component is now the port's parent; it should _not_
207 * hold a reference to the port since the port's lifetime
208 * is now protected by the component's own lifetime.
209 */
210 g_ptr_array_add(ports, new_port);
211
212 /*
213 * Notify the graph's creator that a new port was added.
214 */
215 graph = bt_component_get_graph(component);
216 if (graph) {
217 bt_graph_notify_port_added(graph, new_port);
218 BT_PUT(graph);
219 }
220
221 BT_LOGD("Created and added port to component: comp-addr=%p, comp-name=\"%s\", "
222 "port-type=%s, port-name=\"%s\", port-addr=%p", component,
223 bt_component_get_name(component),
224 bt_port_type_string(port_type), name, new_port);
225
226 end:
227 return new_port;
228 }
229
230 BT_HIDDEN
231 int64_t bt_component_get_input_port_count(struct bt_component *comp)
232 {
233 assert(comp);
234 return (int64_t) comp->input_ports->len;
235 }
236
237 BT_HIDDEN
238 int64_t bt_component_get_output_port_count(struct bt_component *comp)
239 {
240 assert(comp);
241 return (int64_t) comp->output_ports->len;
242 }
243
244 enum bt_component_status bt_component_create(
245 struct bt_component_class *component_class,
246 const char *name, struct bt_component **user_component)
247 {
248 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
249 struct bt_component *component = NULL;
250 enum bt_component_class_type type;
251
252 assert(user_component);
253 assert(component_class);
254 assert(name);
255
256 type = bt_component_class_get_type(component_class);
257 BT_LOGD("Creating empty component from component class: "
258 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\"",
259 component_class, bt_component_class_type_string(type), name);
260 component = component_create_funcs[type](component_class);
261 if (!component) {
262 BT_LOGE_STR("Cannot create specific component object.");
263 status = BT_COMPONENT_STATUS_NOMEM;
264 goto end;
265 }
266
267 bt_object_init(component, bt_component_destroy);
268 component->class = bt_get(component_class);
269 component->destroy = component_destroy_funcs[type];
270 component->name = g_string_new(name);
271 if (!component->name) {
272 BT_LOGE_STR("Failed to allocate one GString.");
273 status = BT_COMPONENT_STATUS_NOMEM;
274 goto end;
275 }
276
277 component->input_ports = g_ptr_array_new_with_free_func(
278 bt_object_release);
279 if (!component->input_ports) {
280 BT_LOGE_STR("Failed to allocate one GPtrArray.");
281 status = BT_COMPONENT_STATUS_NOMEM;
282 goto end;
283 }
284
285 component->output_ports = g_ptr_array_new_with_free_func(
286 bt_object_release);
287 if (!component->output_ports) {
288 BT_LOGE_STR("Failed to allocate one GPtrArray.");
289 status = BT_COMPONENT_STATUS_NOMEM;
290 goto end;
291 }
292
293 component->destroy_listeners = g_array_new(FALSE, TRUE,
294 sizeof(struct bt_component_destroy_listener));
295 if (!component->destroy_listeners) {
296 BT_LOGE_STR("Failed to allocate one GArray.");
297 status = BT_COMPONENT_STATUS_NOMEM;
298 goto end;
299 }
300
301 BT_LOGD("Created empty component from component class: "
302 "comp-cls-addr=%p, comp-cls-type=%s, name=\"%s\", comp-addr=%p",
303 component_class, bt_component_class_type_string(type), name,
304 component);
305 BT_MOVE(*user_component, component);
306
307 end:
308 bt_put(component);
309 return status;
310 }
311
312 const char *bt_component_get_name(struct bt_component *component)
313 {
314 const char *ret = NULL;
315
316 if (!component) {
317 BT_LOGW_STR("Invalid parameter: component is NULL.");
318 goto end;
319 }
320
321 ret = component->name->len == 0 ? NULL : component->name->str;
322
323 end:
324 return ret;
325 }
326
327 struct bt_component_class *bt_component_get_class(
328 struct bt_component *component)
329 {
330 return component ? bt_get(component->class) : NULL;
331 }
332
333 void *bt_private_component_get_user_data(
334 struct bt_private_component *private_component)
335 {
336 struct bt_component *component =
337 bt_component_from_private(private_component);
338
339 return component ? component->user_data : NULL;
340 }
341
342 enum bt_component_status bt_private_component_set_user_data(
343 struct bt_private_component *private_component,
344 void *data)
345 {
346 struct bt_component *component =
347 bt_component_from_private(private_component);
348 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
349
350 if (!component) {
351 BT_LOGW_STR("Invalid parameter: component is NULL.");
352 ret = BT_COMPONENT_STATUS_INVALID;
353 goto end;
354 }
355
356 component->user_data = data;
357 BT_LOGV("Set component's user data: "
358 "comp-addr=%p, comp-name=\"%s\", user-data-addr=%p",
359 component, bt_component_get_name(component), data);
360
361 end:
362 return ret;
363 }
364
365 BT_HIDDEN
366 void bt_component_set_graph(struct bt_component *component,
367 struct bt_graph *graph)
368 {
369 bt_object_set_parent(component, graph ? &graph->base : NULL);
370 }
371
372 struct bt_graph *bt_component_get_graph(
373 struct bt_component *component)
374 {
375 return (struct bt_graph *) bt_object_get_parent(&component->base);
376 }
377
378 static
379 struct bt_port *bt_component_get_port_by_name(GPtrArray *ports,
380 const char *name)
381 {
382 size_t i;
383 struct bt_port *ret_port = NULL;
384
385 assert(name);
386
387 for (i = 0; i < ports->len; i++) {
388 struct bt_port *port = g_ptr_array_index(ports, i);
389 const char *port_name = bt_port_get_name(port);
390
391 if (!port_name) {
392 continue;
393 }
394
395 if (!strcmp(name, port_name)) {
396 ret_port = bt_get(port);
397 break;
398 }
399 }
400
401 return ret_port;
402 }
403
404 BT_HIDDEN
405 struct bt_port *bt_component_get_input_port_by_name(struct bt_component *comp,
406 const char *name)
407 {
408 assert(comp);
409
410 return bt_component_get_port_by_name(comp->input_ports, name);
411 }
412
413 BT_HIDDEN
414 struct bt_port *bt_component_get_output_port_by_name(struct bt_component *comp,
415 const char *name)
416 {
417 assert(comp);
418
419 return bt_component_get_port_by_name(comp->output_ports, name);
420 }
421
422 static
423 struct bt_port *bt_component_get_port_by_index(GPtrArray *ports, uint64_t index)
424 {
425 struct bt_port *port = NULL;
426
427 if (index >= ports->len) {
428 BT_LOGW("Invalid parameter: index is out of bounds: "
429 "index=%" PRIu64 ", count=%u",
430 index, ports->len);
431 goto end;
432 }
433
434 port = bt_get(g_ptr_array_index(ports, index));
435 end:
436 return port;
437 }
438
439 BT_HIDDEN
440 struct bt_port *bt_component_get_input_port_by_index(struct bt_component *comp,
441 uint64_t index)
442 {
443 assert(comp);
444
445 return bt_component_get_port_by_index(comp->input_ports, index);
446 }
447
448 BT_HIDDEN
449 struct bt_port *bt_component_get_output_port_by_index(struct bt_component *comp,
450 uint64_t index)
451 {
452 assert(comp);
453
454 return bt_component_get_port_by_index(comp->output_ports, index);
455 }
456
457 BT_HIDDEN
458 struct bt_port *bt_component_add_input_port(
459 struct bt_component *component, const char *name,
460 void *user_data)
461 {
462 /* bt_component_add_port() logs details */
463 return bt_component_add_port(component, component->input_ports,
464 BT_PORT_TYPE_INPUT, name, user_data);
465 }
466
467 BT_HIDDEN
468 struct bt_port *bt_component_add_output_port(
469 struct bt_component *component, const char *name,
470 void *user_data)
471 {
472 /* bt_component_add_port() logs details */
473 return bt_component_add_port(component, component->output_ports,
474 BT_PORT_TYPE_OUTPUT, name, user_data);
475 }
476
477 static
478 void bt_component_remove_port_by_index(struct bt_component *component,
479 GPtrArray *ports, size_t index)
480 {
481 struct bt_port *port;
482 struct bt_graph *graph;
483
484 assert(ports);
485 assert(index < ports->len);
486 port = g_ptr_array_index(ports, index);
487
488 BT_LOGD("Removing port from component: "
489 "comp-addr=%p, comp-name=\"%s\", "
490 "port-addr=%p, port-name=\"%s\"",
491 component, bt_component_get_name(component),
492 port, bt_port_get_name(port));
493
494 /* Disconnect both ports of this port's connection, if any */
495 if (port->connection) {
496 bt_connection_end(port->connection, true);
497 }
498
499 /* Remove from parent's array of ports (weak refs) */
500 g_ptr_array_remove_index(ports, index);
501
502 /* Detach port from its component parent */
503 BT_PUT(port->base.parent);
504
505 /*
506 * Notify the graph's creator that a port is removed.
507 */
508 graph = bt_component_get_graph(component);
509 if (graph) {
510 bt_graph_notify_port_removed(graph, component, port);
511 BT_PUT(graph);
512 }
513
514 BT_LOGD("Removed port from component: "
515 "comp-addr=%p, comp-name=\"%s\", "
516 "port-addr=%p, port-name=\"%s\"",
517 component, bt_component_get_name(component),
518 port, bt_port_get_name(port));
519 }
520
521 BT_HIDDEN
522 enum bt_component_status bt_component_remove_port(
523 struct bt_component *component, struct bt_port *port)
524 {
525 size_t i;
526 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
527 GPtrArray *ports = NULL;
528
529 if (!component) {
530 BT_LOGW_STR("Invalid parameter: component is NULL.");
531 status = BT_COMPONENT_STATUS_INVALID;
532 goto end;
533 }
534
535 if (!port) {
536 BT_LOGW_STR("Invalid parameter: port is NULL.");
537 status = BT_COMPONENT_STATUS_INVALID;
538 goto end;
539 }
540
541 if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
542 ports = component->input_ports;
543 } else if (bt_port_get_type(port) == BT_PORT_TYPE_OUTPUT) {
544 ports = component->output_ports;
545 }
546
547 assert(ports);
548
549 for (i = 0; i < ports->len; i++) {
550 struct bt_port *cur_port = g_ptr_array_index(ports, i);
551
552 if (cur_port == port) {
553 bt_component_remove_port_by_index(component,
554 ports, i);
555 goto end;
556 }
557 }
558
559 status = BT_COMPONENT_STATUS_NOT_FOUND;
560 BT_LOGW("Port to remove from component was not found: "
561 "comp-addr=%p, comp-name=\"%s\", "
562 "port-addr=%p, port-name=\"%s\"",
563 component, bt_component_get_name(component),
564 port, bt_port_get_name(port));
565
566 end:
567 return status;
568 }
569
570 BT_HIDDEN
571 enum bt_component_status bt_component_accept_port_connection(
572 struct bt_component *comp, struct bt_port *self_port,
573 struct bt_port *other_port)
574 {
575 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
576
577 assert(comp);
578 assert(self_port);
579 assert(other_port);
580
581 if (comp->class->methods.accept_port_connection) {
582 BT_LOGD("Calling user's \"accept port connection\" method: "
583 "comp-addr=%p, comp-name=\"%s\", "
584 "self-port-addr=%p, self-port-name=\"%s\", "
585 "other-port-addr=%p, other-port-name=\"%s\"",
586 comp, bt_component_get_name(comp),
587 self_port, bt_port_get_name(self_port),
588 other_port, bt_port_get_name(other_port));
589 status = comp->class->methods.accept_port_connection(
590 bt_private_component_from_component(comp),
591 bt_private_port_from_port(self_port),
592 other_port);
593 BT_LOGD("User method returned: status=%s",
594 bt_component_status_string(status));
595 }
596
597 return status;
598 }
599
600 BT_HIDDEN
601 void bt_component_port_connected(struct bt_component *comp,
602 struct bt_port *self_port, struct bt_port *other_port)
603 {
604 assert(comp);
605 assert(self_port);
606 assert(other_port);
607
608 if (comp->class->methods.port_connected) {
609 BT_LOGD("Calling user's \"port connected\" method: "
610 "comp-addr=%p, comp-name=\"%s\", "
611 "self-port-addr=%p, self-port-name=\"%s\", "
612 "other-port-addr=%p, other-port-name=\"%s\"",
613 comp, bt_component_get_name(comp),
614 self_port, bt_port_get_name(self_port),
615 other_port, bt_port_get_name(other_port));
616 comp->class->methods.port_connected(
617 bt_private_component_from_component(comp),
618 bt_private_port_from_port(self_port), other_port);
619 }
620 }
621
622 BT_HIDDEN
623 void bt_component_port_disconnected(struct bt_component *comp,
624 struct bt_port *port)
625 {
626 assert(comp);
627 assert(port);
628
629 if (comp->class->methods.port_disconnected) {
630 BT_LOGD("Calling user's \"port disconnected\" method: "
631 "comp-addr=%p, comp-name=\"%s\", "
632 "port-addr=%p, port-name=\"%s\"",
633 comp, bt_component_get_name(comp),
634 port, bt_port_get_name(port));
635 comp->class->methods.port_disconnected(
636 bt_private_component_from_component(comp),
637 bt_private_port_from_port(port));
638 }
639 }
640
641 BT_HIDDEN
642 void bt_component_add_destroy_listener(struct bt_component *component,
643 bt_component_destroy_listener_func func, void *data)
644 {
645 struct bt_component_destroy_listener listener;
646
647 assert(component);
648 assert(func);
649 listener.func = func;
650 listener.data = data;
651 g_array_append_val(component->destroy_listeners, listener);
652 BT_LOGV("Added destroy listener: "
653 "comp-addr=%p, comp-name=\"%s\", "
654 "func-addr=%p, data-addr=%p",
655 component, bt_component_get_name(component),
656 func, data);
657 }
658
659 BT_HIDDEN
660 void bt_component_remove_destroy_listener(struct bt_component *component,
661 bt_component_destroy_listener_func func, void *data)
662 {
663 size_t i;
664
665 assert(component);
666 assert(func);
667
668 for (i = 0; i < component->destroy_listeners->len; i++) {
669 struct bt_component_destroy_listener *listener =
670 &g_array_index(component->destroy_listeners,
671 struct bt_component_destroy_listener, i);
672
673 if (listener->func == func && listener->data == data) {
674 g_array_remove_index(component->destroy_listeners, i);
675 i--;
676 BT_LOGV("Removed destroy listener: "
677 "comp-addr=%p, comp-name=\"%s\", "
678 "func-addr=%p, data-addr=%p",
679 component, bt_component_get_name(component),
680 func, data);
681 }
682 }
683 }
This page took 0.044151 seconds and 4 git commands to generate.