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