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