lib: fully configure graph (add components, connect ports), then run
[babeltrace.git] / lib / graph / component.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "COMP"
25 #include <babeltrace/lib-logging-internal.h>
26
27 #include <babeltrace/assert-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/graph/self-component.h>
30 #include <babeltrace/graph/component-const.h>
31 #include <babeltrace/graph/component-source-const.h>
32 #include <babeltrace/graph/component-filter-const.h>
33 #include <babeltrace/graph/component-sink-const.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/connection-internal.h>
40 #include <babeltrace/graph/graph-internal.h>
41 #include <babeltrace/graph/message-iterator-internal.h>
42 #include <babeltrace/graph/port-internal.h>
43 #include <babeltrace/babeltrace-internal.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/types.h>
46 #include <babeltrace/value.h>
47 #include <babeltrace/value-internal.h>
48 #include <stdint.h>
49 #include <inttypes.h>
50
51 static
52 struct bt_component * (* const component_create_funcs[])(
53 const struct bt_component_class *) = {
54 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
55 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
56 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
57 };
58
59 static
60 void (*component_destroy_funcs[])(struct bt_component *) = {
61 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
62 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
63 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
64 };
65
66 static
67 void finalize_component(struct bt_component *comp)
68 {
69 typedef void (*method_t)(void *);
70
71 method_t method = NULL;
72
73 BT_ASSERT(comp);
74
75 switch (comp->class->type) {
76 case BT_COMPONENT_CLASS_TYPE_SOURCE:
77 {
78 struct bt_component_class_source *src_cc = (void *) comp->class;
79
80 method = (method_t) src_cc->methods.finalize;
81 break;
82 }
83 case BT_COMPONENT_CLASS_TYPE_FILTER:
84 {
85 struct bt_component_class_filter *flt_cc = (void *) comp->class;
86
87 method = (method_t) flt_cc->methods.finalize;
88 break;
89 }
90 case BT_COMPONENT_CLASS_TYPE_SINK:
91 {
92 struct bt_component_class_sink *sink_cc = (void *) comp->class;
93
94 method = (method_t) sink_cc->methods.finalize;
95 break;
96 }
97 default:
98 abort();
99 }
100
101 if (method) {
102 BT_LIB_LOGD("Calling user's finalization method: "
103 "%![comp-]+c", comp);
104 method(comp);
105 }
106 }
107
108 static
109 void destroy_component(struct bt_object *obj)
110 {
111 struct bt_component *component = NULL;
112 int i;
113
114 if (!obj) {
115 return;
116 }
117
118 /*
119 * The component's reference count is 0 if we're here. Increment
120 * it to avoid a double-destroy (possibly infinitely recursive).
121 * This could happen for example if the component's finalization
122 * function does bt_object_get_ref() (or anything that causes
123 * bt_object_get_ref() to be called) on itself (ref. count goes
124 * from 0 to 1), and then bt_object_put_ref(): the reference
125 * count would go from 1 to 0 again and this function would be
126 * called again.
127 */
128 obj->ref_count++;
129 component = container_of(obj, struct bt_component, base);
130 BT_LIB_LOGD("Destroying component: %![comp-]+c, %![graph-]+g",
131 component, bt_component_borrow_graph(component));
132
133 /* Call destroy listeners in reverse registration order */
134 BT_LOGD_STR("Calling destroy listeners.");
135
136 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
137 struct bt_component_destroy_listener *listener =
138 &g_array_index(component->destroy_listeners,
139 struct bt_component_destroy_listener, i);
140
141 listener->func(component, listener->data);
142 }
143
144 /*
145 * User data is destroyed first, followed by the concrete
146 * component instance. Do not finalize if the component's user
147 * initialization method failed in the first place.
148 */
149 if (component->initialized) {
150 finalize_component(component);
151 }
152
153 if (component->destroy) {
154 BT_LOGD_STR("Destroying type-specific data.");
155 component->destroy(component);
156 }
157
158 if (component->input_ports) {
159 BT_LOGD_STR("Destroying input ports.");
160 g_ptr_array_free(component->input_ports, TRUE);
161 component->input_ports = NULL;
162 }
163
164 if (component->output_ports) {
165 BT_LOGD_STR("Destroying output ports.");
166 g_ptr_array_free(component->output_ports, TRUE);
167 component->output_ports = NULL;
168 }
169
170 if (component->destroy_listeners) {
171 g_array_free(component->destroy_listeners, TRUE);
172 component->destroy_listeners = NULL;
173 }
174
175 if (component->name) {
176 g_string_free(component->name, TRUE);
177 component->name = NULL;
178 }
179
180 BT_LOGD_STR("Putting component class.");
181 BT_OBJECT_PUT_REF_AND_RESET(component->class);
182 g_free(component);
183 }
184
185 enum bt_component_class_type bt_component_get_class_type(
186 const struct bt_component *component)
187 {
188 BT_ASSERT_PRE_NON_NULL(component, "Component");
189 return component->class->type;
190 }
191
192 static
193 struct bt_port *add_port(
194 struct bt_component *component, GPtrArray *ports,
195 enum bt_port_type port_type, const char *name, void *user_data)
196 {
197 struct bt_port *new_port = NULL;
198 struct bt_graph *graph = NULL;
199
200 BT_ASSERT_PRE_NON_NULL(component, "Component");
201 BT_ASSERT_PRE_NON_NULL(name, "Name");
202 BT_ASSERT_PRE(strlen(name) > 0, "Name is empty");
203 graph = bt_component_borrow_graph(component);
204 BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph),
205 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
206 component, graph);
207 BT_ASSERT_PRE(!graph->is_configured,
208 "Component's graph is already configured: "
209 "%![comp-]+c, %![graph-]+g", component, graph);
210
211 // TODO: Validate that the name is not already used.
212
213 BT_LIB_LOGD("Adding port to component: %![comp-]+c, "
214 "port-type=%s, port-name=\"%s\"", component,
215 bt_port_type_string(port_type), name);
216
217 new_port = bt_port_create(component, port_type, name, user_data);
218 if (!new_port) {
219 BT_LOGE_STR("Cannot create port object.");
220 goto end;
221 }
222
223 /*
224 * No name clash, add the port.
225 * The component is now the port's parent; it should _not_
226 * hold a reference to the port since the port's lifetime
227 * is now protected by the component's own lifetime.
228 */
229 g_ptr_array_add(ports, new_port);
230
231 /*
232 * Notify the graph's creator that a new port was added.
233 */
234 bt_object_get_ref(bt_component_borrow_graph(component));
235 graph = bt_component_borrow_graph(component);
236 if (graph) {
237 bt_graph_notify_port_added(graph, new_port);
238 BT_OBJECT_PUT_REF_AND_RESET(graph);
239 }
240
241 BT_LIB_LOGD("Created and added port to component: "
242 "%![comp-]+c, %![port-]+p", component, new_port);
243
244 end:
245 return new_port;
246 }
247
248 BT_HIDDEN
249 uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
250 {
251 BT_ASSERT_PRE_NON_NULL(comp, "Component");
252 return (uint64_t) comp->input_ports->len;
253 }
254
255 BT_HIDDEN
256 uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
257 {
258 BT_ASSERT_PRE_NON_NULL(comp, "Component");
259 return (uint64_t) comp->output_ports->len;
260 }
261
262 BT_HIDDEN
263 int bt_component_create(struct bt_component_class *component_class,
264 const char *name, struct bt_component **user_component)
265 {
266 int ret = 0;
267 struct bt_component *component = NULL;
268 enum bt_component_class_type type;
269
270 BT_ASSERT(user_component);
271 BT_ASSERT(component_class);
272 BT_ASSERT(name);
273 type = bt_component_class_get_type(component_class);
274 BT_LIB_LOGD("Creating empty component from component class: %![cc-]+C, "
275 "comp-name=\"%s\"", component_class, name);
276 component = component_create_funcs[type](component_class);
277 if (!component) {
278 BT_LOGE_STR("Cannot create specific component object.");
279 ret = -1;
280 goto end;
281 }
282
283 bt_object_init_shared_with_parent(&component->base, destroy_component);
284 component->class = component_class;
285 bt_object_get_no_null_check(component->class);
286 component->destroy = component_destroy_funcs[type];
287 component->name = g_string_new(name);
288 if (!component->name) {
289 BT_LOGE_STR("Failed to allocate one GString.");
290 ret = -1;
291 goto end;
292 }
293
294 component->input_ports = g_ptr_array_new_with_free_func(
295 (GDestroyNotify) bt_object_try_spec_release);
296 if (!component->input_ports) {
297 BT_LOGE_STR("Failed to allocate one GPtrArray.");
298 ret = -1;
299 goto end;
300 }
301
302 component->output_ports = g_ptr_array_new_with_free_func(
303 (GDestroyNotify) bt_object_try_spec_release);
304 if (!component->output_ports) {
305 BT_LOGE_STR("Failed to allocate one GPtrArray.");
306 ret = -1;
307 goto end;
308 }
309
310 component->destroy_listeners = g_array_new(FALSE, TRUE,
311 sizeof(struct bt_component_destroy_listener));
312 if (!component->destroy_listeners) {
313 BT_LOGE_STR("Failed to allocate one GArray.");
314 ret = -1;
315 goto end;
316 }
317
318 BT_LIB_LOGD("Created empty component from component class: "
319 "%![cc-]+C, %![comp-]+c", component_class, component);
320 BT_OBJECT_MOVE_REF(*user_component, component);
321
322 end:
323 bt_object_put_ref(component);
324 return ret;
325 }
326
327 const char *bt_component_get_name(const struct bt_component *component)
328 {
329 BT_ASSERT_PRE_NON_NULL(component, "Component");
330 return component->name->str;
331 }
332
333 struct bt_component_class *bt_component_borrow_class(
334 const struct bt_component *component)
335 {
336 BT_ASSERT_PRE_NON_NULL(component, "Component");
337 return component->class;
338 }
339
340 void *bt_self_component_get_data(const struct bt_self_component *self_comp)
341 {
342 struct bt_component *component = (void *) self_comp;
343
344 BT_ASSERT_PRE_NON_NULL(component, "Component");
345 return component->user_data;
346 }
347
348 void bt_self_component_set_data(struct bt_self_component *self_comp,
349 void *data)
350 {
351 struct bt_component *component = (void *) self_comp;
352
353 BT_ASSERT_PRE_NON_NULL(component, "Component");
354 component->user_data = data;
355 BT_LIB_LOGV("Set component's user data: %!+c", component);
356 }
357
358 BT_HIDDEN
359 void bt_component_set_graph(struct bt_component *component,
360 struct bt_graph *graph)
361 {
362 bt_object_set_parent(&component->base,
363 graph ? &graph->base : NULL);
364 }
365
366 bt_bool bt_component_graph_is_canceled(const struct bt_component *component)
367 {
368 return bt_graph_is_canceled(
369 (void *) bt_object_borrow_parent(&component->base));
370 }
371
372 static
373 struct bt_port *borrow_port_by_name(GPtrArray *ports,
374 const char *name)
375 {
376 uint64_t i;
377 struct bt_port *ret_port = NULL;
378
379 BT_ASSERT(name);
380
381 for (i = 0; i < ports->len; i++) {
382 struct bt_port *port = g_ptr_array_index(ports, i);
383
384 if (!strcmp(name, port->name->str)) {
385 ret_port = port;
386 break;
387 }
388 }
389
390 return ret_port;
391 }
392
393 BT_HIDDEN
394 struct bt_port_input *bt_component_borrow_input_port_by_name(
395 struct bt_component *comp, const char *name)
396 {
397 BT_ASSERT(comp);
398 return (void *) borrow_port_by_name(comp->input_ports, name);
399 }
400
401 BT_HIDDEN
402 struct bt_port_output *bt_component_borrow_output_port_by_name(
403 struct bt_component *comp, const char *name)
404 {
405 BT_ASSERT_PRE_NON_NULL(comp, "Component");
406 return (void *)
407 borrow_port_by_name(comp->output_ports, name);
408 }
409
410 static
411 struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index)
412 {
413 BT_ASSERT(index < ports->len);
414 return g_ptr_array_index(ports, index);
415 }
416
417 BT_HIDDEN
418 struct bt_port_input *bt_component_borrow_input_port_by_index(
419 struct bt_component *comp, uint64_t index)
420 {
421 BT_ASSERT_PRE_NON_NULL(comp, "Component");
422 BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len);
423 return (void *)
424 borrow_port_by_index(comp->input_ports, index);
425 }
426
427 BT_HIDDEN
428 struct bt_port_output *bt_component_borrow_output_port_by_index(
429 struct bt_component *comp, uint64_t index)
430 {
431 BT_ASSERT_PRE_NON_NULL(comp, "Component");
432 BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len);
433 return (void *)
434 borrow_port_by_index(comp->output_ports, index);
435 }
436
437 BT_HIDDEN
438 struct bt_port_input *bt_component_add_input_port(
439 struct bt_component *component, const char *name,
440 void *user_data)
441 {
442 /* add_port() logs details */
443 return (void *)
444 add_port(component, component->input_ports,
445 BT_PORT_TYPE_INPUT, name, user_data);
446 }
447
448 BT_HIDDEN
449 struct bt_port_output *bt_component_add_output_port(
450 struct bt_component *component, const char *name,
451 void *user_data)
452 {
453 /* add_port() logs details */
454 return (void *)
455 add_port(component, component->output_ports,
456 BT_PORT_TYPE_OUTPUT, name, user_data);
457 }
458
459 BT_HIDDEN
460 enum bt_self_component_status bt_component_accept_port_connection(
461 struct bt_component *comp, struct bt_port *self_port,
462 struct bt_port *other_port)
463 {
464 typedef enum bt_self_component_status (*method_t)(
465 void *, void *, const void *);
466
467 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
468 method_t method = NULL;
469
470 BT_ASSERT(comp);
471 BT_ASSERT(self_port);
472 BT_ASSERT(other_port);
473
474 switch (comp->class->type) {
475 case BT_COMPONENT_CLASS_TYPE_SOURCE:
476 {
477 struct bt_component_class_source *src_cc = (void *) comp->class;
478
479 switch (self_port->type) {
480 case BT_PORT_TYPE_OUTPUT:
481 method = (method_t) src_cc->methods.accept_output_port_connection;
482 break;
483 default:
484 abort();
485 }
486
487 break;
488 }
489 case BT_COMPONENT_CLASS_TYPE_FILTER:
490 {
491 struct bt_component_class_filter *flt_cc = (void *) comp->class;
492
493 switch (self_port->type) {
494 case BT_PORT_TYPE_INPUT:
495 method = (method_t) flt_cc->methods.accept_input_port_connection;
496 break;
497 case BT_PORT_TYPE_OUTPUT:
498 method = (method_t) flt_cc->methods.accept_output_port_connection;
499 break;
500 default:
501 abort();
502 }
503
504 break;
505 }
506 case BT_COMPONENT_CLASS_TYPE_SINK:
507 {
508 struct bt_component_class_sink *sink_cc = (void *) comp->class;
509
510 switch (self_port->type) {
511 case BT_PORT_TYPE_INPUT:
512 method = (method_t) sink_cc->methods.accept_input_port_connection;
513 break;
514 default:
515 abort();
516 }
517
518 break;
519 }
520 default:
521 abort();
522 }
523
524 if (method) {
525 BT_LIB_LOGD("Calling user's \"accept port connection\" method: "
526 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
527 comp, self_port, other_port);
528 status = method(comp, self_port, (void *) other_port);
529 BT_LOGD("User method returned: status=%s",
530 bt_self_component_status_string(status));
531 }
532
533 return status;
534 }
535
536 BT_HIDDEN
537 enum bt_self_component_status bt_component_port_connected(
538 struct bt_component *comp, struct bt_port *self_port,
539 struct bt_port *other_port)
540 {
541 typedef enum bt_self_component_status (*method_t)(
542 void *, void *, const void *);
543
544 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
545 method_t method = NULL;
546
547 BT_ASSERT(comp);
548 BT_ASSERT(self_port);
549 BT_ASSERT(other_port);
550
551 switch (comp->class->type) {
552 case BT_COMPONENT_CLASS_TYPE_SOURCE:
553 {
554 struct bt_component_class_source *src_cc = (void *) comp->class;
555
556 switch (self_port->type) {
557 case BT_PORT_TYPE_OUTPUT:
558 method = (method_t) src_cc->methods.output_port_connected;
559 break;
560 default:
561 abort();
562 }
563
564 break;
565 }
566 case BT_COMPONENT_CLASS_TYPE_FILTER:
567 {
568 struct bt_component_class_filter *flt_cc = (void *) comp->class;
569
570 switch (self_port->type) {
571 case BT_PORT_TYPE_INPUT:
572 method = (method_t) flt_cc->methods.input_port_connected;
573 break;
574 case BT_PORT_TYPE_OUTPUT:
575 method = (method_t) flt_cc->methods.output_port_connected;
576 break;
577 default:
578 abort();
579 }
580
581 break;
582 }
583 case BT_COMPONENT_CLASS_TYPE_SINK:
584 {
585 struct bt_component_class_sink *sink_cc = (void *) comp->class;
586
587 switch (self_port->type) {
588 case BT_PORT_TYPE_INPUT:
589 method = (method_t) sink_cc->methods.input_port_connected;
590 break;
591 default:
592 abort();
593 }
594
595 break;
596 }
597 default:
598 abort();
599 }
600
601 if (method) {
602 BT_LIB_LOGD("Calling user's \"port connected\" method: "
603 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
604 comp, self_port, other_port);
605 status = method(comp, self_port, (void *) other_port);
606 BT_LOGD("User method returned: status=%s",
607 bt_self_component_status_string(status));
608 BT_ASSERT_PRE(status == BT_SELF_COMPONENT_STATUS_OK ||
609 status == BT_SELF_COMPONENT_STATUS_ERROR ||
610 status == BT_SELF_COMPONENT_STATUS_NOMEM,
611 "Unexpected returned component status: status=%s",
612 bt_self_component_status_string(status));
613 }
614
615 return status;
616 }
617
618 BT_HIDDEN
619 void bt_component_add_destroy_listener(struct bt_component *component,
620 bt_component_destroy_listener_func func, void *data)
621 {
622 struct bt_component_destroy_listener listener;
623
624 BT_ASSERT(component);
625 BT_ASSERT(func);
626 listener.func = func;
627 listener.data = data;
628 g_array_append_val(component->destroy_listeners, listener);
629 BT_LIB_LOGV("Added destroy listener: %![comp-]+c, "
630 "func-addr=%p, data-addr=%p",
631 component, func, data);
632 }
633
634 BT_HIDDEN
635 void bt_component_remove_destroy_listener(struct bt_component *component,
636 bt_component_destroy_listener_func func, void *data)
637 {
638 uint64_t i;
639
640 BT_ASSERT(component);
641 BT_ASSERT(func);
642
643 for (i = 0; i < component->destroy_listeners->len; i++) {
644 struct bt_component_destroy_listener *listener =
645 &g_array_index(component->destroy_listeners,
646 struct bt_component_destroy_listener, i);
647
648 if (listener->func == func && listener->data == data) {
649 g_array_remove_index(component->destroy_listeners, i);
650 i--;
651 BT_LIB_LOGV("Removed destroy listener: %![comp-]+c, "
652 "func-addr=%p, data-addr=%p",
653 component, func, data);
654 }
655 }
656 }
657
658 void bt_component_get_ref(const struct bt_component *component)
659 {
660 bt_object_get_ref(component);
661 }
662
663 void bt_component_put_ref(const struct bt_component *component)
664 {
665 bt_object_put_ref(component);
666 }
This page took 0.045793 seconds and 4 git commands to generate.