90129b073d46429f8f3a2f6d337f10b61a686fe5
[babeltrace.git] / src / lib / graph / component.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/COMPONENT"
25 #include "lib/logging.h"
26
27 #include "common/common.h"
28 #include "common/assert.h"
29 #include "lib/assert-pre.h"
30 #include "lib/assert-post.h"
31 #include <babeltrace2/graph/self-component.h>
32 #include <babeltrace2/graph/component-const.h>
33 #include <babeltrace2/graph/component-source-const.h>
34 #include <babeltrace2/graph/component-filter-const.h>
35 #include <babeltrace2/graph/component-sink-const.h>
36 #include "common/macros.h"
37 #include "compat/compiler.h"
38 #include <babeltrace2/types.h>
39 #include <babeltrace2/value.h>
40 #include "lib/value.h"
41 #include <stdint.h>
42 #include <inttypes.h>
43
44 #include "component.h"
45 #include "component-class.h"
46 #include "component-source.h"
47 #include "component-filter.h"
48 #include "component-sink.h"
49 #include "connection.h"
50 #include "graph.h"
51 #include "message/iterator.h"
52 #include "port.h"
53
54 static
55 struct bt_component * (* const component_create_funcs[])(
56 const struct bt_component_class *) = {
57 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
58 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
59 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
60 };
61
62 static
63 void (*component_destroy_funcs[])(struct bt_component *) = {
64 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
65 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
66 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
67 };
68
69 static
70 void finalize_component(struct bt_component *comp)
71 {
72 typedef void (*method_t)(void *);
73
74 method_t method = NULL;
75
76 BT_ASSERT(comp);
77
78 switch (comp->class->type) {
79 case BT_COMPONENT_CLASS_TYPE_SOURCE:
80 {
81 struct bt_component_class_source *src_cc = (void *) comp->class;
82
83 method = (method_t) src_cc->methods.finalize;
84 break;
85 }
86 case BT_COMPONENT_CLASS_TYPE_FILTER:
87 {
88 struct bt_component_class_filter *flt_cc = (void *) comp->class;
89
90 method = (method_t) flt_cc->methods.finalize;
91 break;
92 }
93 case BT_COMPONENT_CLASS_TYPE_SINK:
94 {
95 struct bt_component_class_sink *sink_cc = (void *) comp->class;
96
97 method = (method_t) sink_cc->methods.finalize;
98 break;
99 }
100 default:
101 abort();
102 }
103
104 if (method) {
105 BT_LIB_LOGI("Calling user's component finalization method: "
106 "%![comp-]+c", comp);
107 method(comp);
108 }
109 }
110
111 static
112 void destroy_component(struct bt_object *obj)
113 {
114 struct bt_component *component = NULL;
115 int i;
116
117 if (!obj) {
118 return;
119 }
120
121 /*
122 * The component's reference count is 0 if we're here. Increment
123 * it to avoid a double-destroy (possibly infinitely recursive).
124 * This could happen for example if the component's finalization
125 * function does bt_object_get_ref() (or anything that causes
126 * bt_object_get_ref() to be called) on itself (ref. count goes
127 * from 0 to 1), and then bt_object_put_ref(): the reference
128 * count would go from 1 to 0 again and this function would be
129 * called again.
130 */
131 obj->ref_count++;
132 component = container_of(obj, struct bt_component, base);
133 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
134 component, bt_component_borrow_graph(component));
135
136 /* Call destroy listeners in reverse registration order */
137 BT_LOGD_STR("Calling destroy listeners.");
138
139 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
140 struct bt_component_destroy_listener *listener =
141 &g_array_index(component->destroy_listeners,
142 struct bt_component_destroy_listener, i);
143
144 listener->func(component, listener->data);
145 }
146
147 /*
148 * User data is destroyed first, followed by the concrete
149 * component instance. Do not finalize if the component's user
150 * initialization method failed in the first place.
151 */
152 if (component->initialized) {
153 finalize_component(component);
154 }
155
156 if (component->destroy) {
157 BT_LOGD_STR("Destroying type-specific data.");
158 component->destroy(component);
159 }
160
161 if (component->input_ports) {
162 BT_LOGD_STR("Destroying input ports.");
163 g_ptr_array_free(component->input_ports, TRUE);
164 component->input_ports = NULL;
165 }
166
167 if (component->output_ports) {
168 BT_LOGD_STR("Destroying output ports.");
169 g_ptr_array_free(component->output_ports, TRUE);
170 component->output_ports = NULL;
171 }
172
173 if (component->destroy_listeners) {
174 g_array_free(component->destroy_listeners, TRUE);
175 component->destroy_listeners = NULL;
176 }
177
178 if (component->name) {
179 g_string_free(component->name, TRUE);
180 component->name = NULL;
181 }
182
183 BT_LOGD_STR("Putting component class.");
184 BT_OBJECT_PUT_REF_AND_RESET(component->class);
185 g_free(component);
186 }
187
188 enum bt_component_class_type bt_component_get_class_type(
189 const struct bt_component *component)
190 {
191 BT_ASSERT_PRE_NON_NULL(component, "Component");
192 return component->class->type;
193 }
194
195 static
196 enum bt_self_component_status add_port(
197 struct bt_component *component, GPtrArray *ports,
198 enum bt_port_type port_type, const char *name, void *user_data,
199 struct bt_port **port)
200 {
201 struct bt_port *new_port = NULL;
202 struct bt_graph *graph = NULL;
203 enum bt_self_component_status status;
204
205 BT_ASSERT_PRE_NON_NULL(component, "Component");
206 BT_ASSERT_PRE_NON_NULL(name, "Name");
207 BT_ASSERT_PRE(strlen(name) > 0, "Name is empty");
208 graph = bt_component_borrow_graph(component);
209 BT_ASSERT_PRE(graph && !bt_graph_is_canceled(graph),
210 "Component's graph is canceled: %![comp-]+c, %![graph-]+g",
211 component, graph);
212 BT_ASSERT_PRE(
213 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
214 "Component's graph is already configured: "
215 "%![comp-]+c, %![graph-]+g", component, graph);
216
217 // TODO: Validate that the name is not already used.
218
219 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
220 "port-type=%s, port-name=\"%s\"", component,
221 bt_port_type_string(port_type), name);
222
223 new_port = bt_port_create(component, port_type, name, user_data);
224 if (!new_port) {
225 BT_LOGE_STR("Cannot create port object.");
226 status = BT_SELF_COMPONENT_STATUS_NOMEM;
227 goto error;
228 }
229
230 /*
231 * No name clash, add the port.
232 * The component is now the port's parent; it should _not_
233 * hold a reference to the port since the port's lifetime
234 * is now protected by the component's own lifetime.
235 */
236 g_ptr_array_add(ports, new_port);
237
238 /*
239 * Notify the graph's creator that a new port was added.
240 */
241 graph = bt_component_borrow_graph(component);
242 if (graph) {
243 enum bt_graph_listener_status listener_status;
244
245 listener_status = bt_graph_notify_port_added(graph, new_port);
246 if (listener_status != BT_GRAPH_LISTENER_STATUS_OK) {
247 bt_graph_make_faulty(graph);
248 status = listener_status;
249 goto error;
250 }
251 }
252
253 BT_LIB_LOGI("Created and added port to component: "
254 "%![comp-]+c, %![port-]+p", component, new_port);
255
256 *port = new_port;
257 status = BT_SELF_COMPONENT_STATUS_OK;
258
259 goto end;
260 error:
261 /*
262 * We need to release the reference that we would otherwise have
263 * returned to the caller.
264 */
265 BT_PORT_PUT_REF_AND_RESET(new_port);
266
267 end:
268 return status;
269 }
270
271 BT_HIDDEN
272 uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
273 {
274 BT_ASSERT_PRE_NON_NULL(comp, "Component");
275 return (uint64_t) comp->input_ports->len;
276 }
277
278 BT_HIDDEN
279 uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
280 {
281 BT_ASSERT_PRE_NON_NULL(comp, "Component");
282 return (uint64_t) comp->output_ports->len;
283 }
284
285 BT_HIDDEN
286 int bt_component_create(struct bt_component_class *component_class,
287 const char *name, bt_logging_level log_level,
288 struct bt_component **user_component)
289 {
290 int ret = 0;
291 struct bt_component *component = NULL;
292 enum bt_component_class_type type;
293
294 BT_ASSERT(user_component);
295 BT_ASSERT(component_class);
296 BT_ASSERT(name);
297 type = bt_component_class_get_type(component_class);
298 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
299 "comp-name=\"%s\", log-level=%s", component_class, name,
300 bt_common_logging_level_string(log_level));
301 component = component_create_funcs[type](component_class);
302 if (!component) {
303 BT_LOGE_STR("Cannot create specific component object.");
304 ret = -1;
305 goto end;
306 }
307
308 bt_object_init_shared_with_parent(&component->base, destroy_component);
309 component->class = component_class;
310 bt_object_get_no_null_check(component->class);
311 component->destroy = component_destroy_funcs[type];
312 component->name = g_string_new(name);
313 if (!component->name) {
314 BT_LOGE_STR("Failed to allocate one GString.");
315 ret = -1;
316 goto end;
317 }
318
319 component->log_level = log_level;
320 component->input_ports = g_ptr_array_new_with_free_func(
321 (GDestroyNotify) bt_object_try_spec_release);
322 if (!component->input_ports) {
323 BT_LOGE_STR("Failed to allocate one GPtrArray.");
324 ret = -1;
325 goto end;
326 }
327
328 component->output_ports = g_ptr_array_new_with_free_func(
329 (GDestroyNotify) bt_object_try_spec_release);
330 if (!component->output_ports) {
331 BT_LOGE_STR("Failed to allocate one GPtrArray.");
332 ret = -1;
333 goto end;
334 }
335
336 component->destroy_listeners = g_array_new(FALSE, TRUE,
337 sizeof(struct bt_component_destroy_listener));
338 if (!component->destroy_listeners) {
339 BT_LOGE_STR("Failed to allocate one GArray.");
340 ret = -1;
341 goto end;
342 }
343
344 BT_LIB_LOGI("Created empty component from component class: "
345 "%![cc-]+C, %![comp-]+c", component_class, component);
346 BT_OBJECT_MOVE_REF(*user_component, component);
347
348 end:
349 bt_object_put_ref(component);
350 return ret;
351 }
352
353 const char *bt_component_get_name(const struct bt_component *component)
354 {
355 BT_ASSERT_PRE_NON_NULL(component, "Component");
356 return component->name->str;
357 }
358
359 const struct bt_component_class *bt_component_borrow_class_const(
360 const struct bt_component *component)
361 {
362 BT_ASSERT_PRE_NON_NULL(component, "Component");
363 return component->class;
364 }
365
366 void *bt_self_component_get_data(const struct bt_self_component *self_comp)
367 {
368 struct bt_component *component = (void *) self_comp;
369
370 BT_ASSERT_PRE_NON_NULL(component, "Component");
371 return component->user_data;
372 }
373
374 void bt_self_component_set_data(struct bt_self_component *self_comp,
375 void *data)
376 {
377 struct bt_component *component = (void *) self_comp;
378
379 BT_ASSERT_PRE_NON_NULL(component, "Component");
380 component->user_data = data;
381 BT_LIB_LOGD("Set component's user data: %!+c", component);
382 }
383
384 BT_HIDDEN
385 void bt_component_set_graph(struct bt_component *component,
386 struct bt_graph *graph)
387 {
388 bt_object_set_parent(&component->base,
389 graph ? &graph->base : NULL);
390 }
391
392 bt_bool bt_component_graph_is_canceled(const struct bt_component *component)
393 {
394 return bt_graph_is_canceled(
395 (void *) bt_object_borrow_parent(&component->base));
396 }
397
398 static
399 struct bt_port *borrow_port_by_name(GPtrArray *ports,
400 const char *name)
401 {
402 uint64_t i;
403 struct bt_port *ret_port = NULL;
404
405 BT_ASSERT(name);
406
407 for (i = 0; i < ports->len; i++) {
408 struct bt_port *port = g_ptr_array_index(ports, i);
409
410 if (!strcmp(name, port->name->str)) {
411 ret_port = port;
412 break;
413 }
414 }
415
416 return ret_port;
417 }
418
419 BT_HIDDEN
420 struct bt_port_input *bt_component_borrow_input_port_by_name(
421 struct bt_component *comp, const char *name)
422 {
423 BT_ASSERT(comp);
424 return (void *) borrow_port_by_name(comp->input_ports, name);
425 }
426
427 BT_HIDDEN
428 struct bt_port_output *bt_component_borrow_output_port_by_name(
429 struct bt_component *comp, const char *name)
430 {
431 BT_ASSERT_PRE_NON_NULL(comp, "Component");
432 return (void *)
433 borrow_port_by_name(comp->output_ports, name);
434 }
435
436 static
437 struct bt_port *borrow_port_by_index(GPtrArray *ports, uint64_t index)
438 {
439 BT_ASSERT(index < ports->len);
440 return g_ptr_array_index(ports, index);
441 }
442
443 BT_HIDDEN
444 struct bt_port_input *bt_component_borrow_input_port_by_index(
445 struct bt_component *comp, uint64_t index)
446 {
447 BT_ASSERT_PRE_NON_NULL(comp, "Component");
448 BT_ASSERT_PRE_VALID_INDEX(index, comp->input_ports->len);
449 return (void *)
450 borrow_port_by_index(comp->input_ports, index);
451 }
452
453 BT_HIDDEN
454 struct bt_port_output *bt_component_borrow_output_port_by_index(
455 struct bt_component *comp, uint64_t index)
456 {
457 BT_ASSERT_PRE_NON_NULL(comp, "Component");
458 BT_ASSERT_PRE_VALID_INDEX(index, comp->output_ports->len);
459 return (void *)
460 borrow_port_by_index(comp->output_ports, index);
461 }
462
463 BT_HIDDEN
464 enum bt_self_component_status bt_component_add_input_port(
465 struct bt_component *component, const char *name,
466 void *user_data, struct bt_port **port)
467 {
468 /* add_port() logs details */
469 return add_port(component, component->input_ports,
470 BT_PORT_TYPE_INPUT, name, user_data, port);
471 }
472
473 BT_HIDDEN
474 enum bt_self_component_status bt_component_add_output_port(
475 struct bt_component *component, const char *name,
476 void *user_data, struct bt_port **port)
477 {
478 /* add_port() logs details */
479 return add_port(component, component->output_ports,
480 BT_PORT_TYPE_OUTPUT, name, user_data, port);
481 }
482
483 BT_HIDDEN
484 enum bt_self_component_status bt_component_port_connected(
485 struct bt_component *comp, struct bt_port *self_port,
486 struct bt_port *other_port)
487 {
488 typedef enum bt_self_component_status (*method_t)(
489 void *, void *, const void *);
490
491 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
492 method_t method = NULL;
493
494 BT_ASSERT(comp);
495 BT_ASSERT(self_port);
496 BT_ASSERT(other_port);
497
498 switch (comp->class->type) {
499 case BT_COMPONENT_CLASS_TYPE_SOURCE:
500 {
501 struct bt_component_class_source *src_cc = (void *) comp->class;
502
503 switch (self_port->type) {
504 case BT_PORT_TYPE_OUTPUT:
505 method = (method_t) src_cc->methods.output_port_connected;
506 break;
507 default:
508 abort();
509 }
510
511 break;
512 }
513 case BT_COMPONENT_CLASS_TYPE_FILTER:
514 {
515 struct bt_component_class_filter *flt_cc = (void *) comp->class;
516
517 switch (self_port->type) {
518 case BT_PORT_TYPE_INPUT:
519 method = (method_t) flt_cc->methods.input_port_connected;
520 break;
521 case BT_PORT_TYPE_OUTPUT:
522 method = (method_t) flt_cc->methods.output_port_connected;
523 break;
524 default:
525 abort();
526 }
527
528 break;
529 }
530 case BT_COMPONENT_CLASS_TYPE_SINK:
531 {
532 struct bt_component_class_sink *sink_cc = (void *) comp->class;
533
534 switch (self_port->type) {
535 case BT_PORT_TYPE_INPUT:
536 method = (method_t) sink_cc->methods.input_port_connected;
537 break;
538 default:
539 abort();
540 }
541
542 break;
543 }
544 default:
545 abort();
546 }
547
548 if (method) {
549 BT_LIB_LOGD("Calling user's \"port connected\" method: "
550 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
551 comp, self_port, other_port);
552 status = method(comp, self_port, (void *) other_port);
553 BT_LOGD("User method returned: status=%s",
554 bt_self_component_status_string(status));
555 BT_ASSERT_POST(status == BT_SELF_COMPONENT_STATUS_OK ||
556 status == BT_SELF_COMPONENT_STATUS_ERROR ||
557 status == BT_SELF_COMPONENT_STATUS_NOMEM,
558 "Unexpected returned component status: status=%s",
559 bt_self_component_status_string(status));
560 }
561
562 return status;
563 }
564
565 BT_HIDDEN
566 void bt_component_add_destroy_listener(struct bt_component *component,
567 bt_component_destroy_listener_func func, void *data)
568 {
569 struct bt_component_destroy_listener listener;
570
571 BT_ASSERT(component);
572 BT_ASSERT(func);
573 listener.func = func;
574 listener.data = data;
575 g_array_append_val(component->destroy_listeners, listener);
576 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
577 "func-addr=%p, data-addr=%p",
578 component, func, data);
579 }
580
581 BT_HIDDEN
582 void bt_component_remove_destroy_listener(struct bt_component *component,
583 bt_component_destroy_listener_func func, void *data)
584 {
585 uint64_t i;
586
587 BT_ASSERT(component);
588 BT_ASSERT(func);
589
590 for (i = 0; i < component->destroy_listeners->len; i++) {
591 struct bt_component_destroy_listener *listener =
592 &g_array_index(component->destroy_listeners,
593 struct bt_component_destroy_listener, i);
594
595 if (listener->func == func && listener->data == data) {
596 g_array_remove_index(component->destroy_listeners, i);
597 i--;
598 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
599 "func-addr=%p, data-addr=%p",
600 component, func, data);
601 }
602 }
603 }
604
605 bt_logging_level bt_component_get_logging_level(
606 const struct bt_component *component)
607 {
608 BT_ASSERT_PRE_NON_NULL(component, "Component");
609 return component->log_level;
610 }
611
612 void bt_component_get_ref(const struct bt_component *component)
613 {
614 bt_object_get_ref(component);
615 }
616
617 void bt_component_put_ref(const struct bt_component *component)
618 {
619 bt_object_put_ref(component);
620 }
This page took 0.040972 seconds and 3 git commands to generate.