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