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