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