Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / graph / component.c
... / ...
CommitLineData
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
37static
38struct 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
45static
46void (*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
52static
53void 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
103static
104void 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
180enum 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
187static
188enum 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;
249error:
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
256end:
257 return status;
258}
259
260BT_HIDDEN
261uint64_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
267BT_HIDDEN
268uint64_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
274BT_HIDDEN
275int 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
338end:
339 bt_object_put_ref(component);
340 return ret;
341}
342
343const 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
349const 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
356void *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
364void 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
374BT_HIDDEN
375void 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
382static
383struct 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
403BT_HIDDEN
404struct 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
411BT_HIDDEN
412struct 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
420static
421struct 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
427BT_HIDDEN
428struct 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
437BT_HIDDEN
438struct 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
447BT_HIDDEN
448enum 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
457BT_HIDDEN
458enum 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
467BT_HIDDEN
468enum bt_component_class_port_connected_method_status
469bt_component_port_connected(
470 struct bt_component *comp, struct bt_port *self_port,
471 struct bt_port *other_port)
472{
473 typedef enum bt_component_class_port_connected_method_status (*method_t)(
474 void *, void *, const void *);
475
476 enum bt_component_class_port_connected_method_status status =
477 BT_FUNC_STATUS_OK;
478 method_t method = NULL;
479
480 BT_ASSERT(comp);
481 BT_ASSERT(self_port);
482 BT_ASSERT(other_port);
483
484 switch (comp->class->type) {
485 case BT_COMPONENT_CLASS_TYPE_SOURCE:
486 {
487 struct bt_component_class_source *src_cc = (void *) comp->class;
488
489 switch (self_port->type) {
490 case BT_PORT_TYPE_OUTPUT:
491 method = (method_t) src_cc->methods.output_port_connected;
492 break;
493 default:
494 bt_common_abort();
495 }
496
497 break;
498 }
499 case BT_COMPONENT_CLASS_TYPE_FILTER:
500 {
501 struct bt_component_class_filter *flt_cc = (void *) comp->class;
502
503 switch (self_port->type) {
504 case BT_PORT_TYPE_INPUT:
505 method = (method_t) flt_cc->methods.input_port_connected;
506 break;
507 case BT_PORT_TYPE_OUTPUT:
508 method = (method_t) flt_cc->methods.output_port_connected;
509 break;
510 default:
511 bt_common_abort();
512 }
513
514 break;
515 }
516 case BT_COMPONENT_CLASS_TYPE_SINK:
517 {
518 struct bt_component_class_sink *sink_cc = (void *) comp->class;
519
520 switch (self_port->type) {
521 case BT_PORT_TYPE_INPUT:
522 method = (method_t) sink_cc->methods.input_port_connected;
523 break;
524 default:
525 bt_common_abort();
526 }
527
528 break;
529 }
530 default:
531 bt_common_abort();
532 }
533
534 if (method) {
535 BT_LIB_LOGD("Calling user's \"port connected\" method: "
536 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
537 comp, self_port, other_port);
538 status = (int) method(comp, self_port, (void *) other_port);
539 BT_LOGD("User method returned: status=%s",
540 bt_common_func_status_string(status));
541 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
542 status == BT_FUNC_STATUS_ERROR ||
543 status == BT_FUNC_STATUS_MEMORY_ERROR,
544 "Unexpected returned component status: status=%s",
545 bt_common_func_status_string(status));
546 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
547 }
548
549 return status;
550}
551
552BT_HIDDEN
553void bt_component_add_destroy_listener(struct bt_component *component,
554 bt_component_destroy_listener_func func, void *data)
555{
556 struct bt_component_destroy_listener listener;
557
558 BT_ASSERT(component);
559 BT_ASSERT(func);
560 listener.func = func;
561 listener.data = data;
562 g_array_append_val(component->destroy_listeners, listener);
563 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
564 "func-addr=%p, data-addr=%p",
565 component, func, data);
566}
567
568BT_HIDDEN
569void bt_component_remove_destroy_listener(struct bt_component *component,
570 bt_component_destroy_listener_func func, void *data)
571{
572 uint64_t i;
573
574 BT_ASSERT(component);
575 BT_ASSERT(func);
576
577 for (i = 0; i < component->destroy_listeners->len; i++) {
578 struct bt_component_destroy_listener *listener =
579 &g_array_index(component->destroy_listeners,
580 struct bt_component_destroy_listener, i);
581
582 if (listener->func == func && listener->data == data) {
583 g_array_remove_index(component->destroy_listeners, i);
584 i--;
585 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
586 "func-addr=%p, data-addr=%p",
587 component, func, data);
588 }
589 }
590}
591
592bt_logging_level bt_component_get_logging_level(
593 const struct bt_component *component)
594{
595 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
596 return component->log_level;
597}
598
599uint64_t bt_self_component_get_graph_mip_version(
600 bt_self_component *self_component)
601{
602 struct bt_component *comp = (void *) self_component;
603
604 BT_ASSERT_PRE_NON_NULL(self_component, "Component");
605 return bt_component_borrow_graph(comp)->mip_version;
606}
607
608void bt_component_get_ref(const struct bt_component *component)
609{
610 bt_object_get_ref(component);
611}
612
613void bt_component_put_ref(const struct bt_component *component)
614{
615 bt_object_put_ref(component);
616}
This page took 0.024779 seconds and 4 git commands to generate.