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