Document libbabeltrace2's C API
[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.h>
33 #include <babeltrace2/graph/graph.h>
34 #include "common/macros.h"
35 #include "compat/compiler.h"
36 #include <babeltrace2/types.h>
37 #include <babeltrace2/value.h>
38 #include "lib/value.h"
39 #include <stdint.h>
40 #include <inttypes.h>
41
42 #include "component.h"
43 #include "component-class.h"
44 #include "component-source.h"
45 #include "component-filter.h"
46 #include "component-sink.h"
47 #include "connection.h"
48 #include "graph.h"
49 #include "message/iterator.h"
50 #include "port.h"
51 #include "lib/func-status.h"
52
53 static
54 struct bt_component * (* const component_create_funcs[])(
55 const struct bt_component_class *) = {
56 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_create,
57 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_create,
58 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_create,
59 };
60
61 static
62 void (*component_destroy_funcs[])(struct bt_component *) = {
63 [BT_COMPONENT_CLASS_TYPE_SOURCE] = bt_component_source_destroy,
64 [BT_COMPONENT_CLASS_TYPE_SINK] = bt_component_sink_destroy,
65 [BT_COMPONENT_CLASS_TYPE_FILTER] = bt_component_filter_destroy,
66 };
67
68 static
69 void finalize_component(struct bt_component *comp)
70 {
71 typedef void (*method_t)(void *);
72
73 method_t method = NULL;
74
75 BT_ASSERT(comp);
76
77 switch (comp->class->type) {
78 case BT_COMPONENT_CLASS_TYPE_SOURCE:
79 {
80 struct bt_component_class_source *src_cc = (void *) comp->class;
81
82 method = (method_t) src_cc->methods.finalize;
83 break;
84 }
85 case BT_COMPONENT_CLASS_TYPE_FILTER:
86 {
87 struct bt_component_class_filter *flt_cc = (void *) comp->class;
88
89 method = (method_t) flt_cc->methods.finalize;
90 break;
91 }
92 case BT_COMPONENT_CLASS_TYPE_SINK:
93 {
94 struct bt_component_class_sink *sink_cc = (void *) comp->class;
95
96 method = (method_t) sink_cc->methods.finalize;
97 break;
98 }
99 default:
100 bt_common_abort();
101 }
102
103 if (method) {
104 const struct bt_error *saved_error;
105
106 saved_error = bt_current_thread_take_error();
107
108 BT_LIB_LOGI("Calling user's component finalization method: "
109 "%![comp-]+c", comp);
110 method(comp);
111 BT_ASSERT_POST_NO_ERROR();
112
113 if (saved_error) {
114 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
115 }
116 }
117 }
118
119 static
120 void destroy_component(struct bt_object *obj)
121 {
122 struct bt_component *component = NULL;
123 int i;
124
125 if (!obj) {
126 return;
127 }
128
129 /*
130 * The component's reference count is 0 if we're here. Increment
131 * it to avoid a double-destroy (possibly infinitely recursive).
132 * This could happen for example if the component's finalization
133 * function does bt_object_get_ref() (or anything that causes
134 * bt_object_get_ref() to be called) on itself (ref. count goes
135 * from 0 to 1), and then bt_object_put_ref(): the reference
136 * count would go from 1 to 0 again and this function would be
137 * called again.
138 */
139 obj->ref_count++;
140 component = container_of(obj, struct bt_component, base);
141 BT_LIB_LOGI("Destroying component: %![comp-]+c, %![graph-]+g",
142 component, bt_component_borrow_graph(component));
143
144 /* Call destroy listeners in reverse registration order */
145 BT_LOGD_STR("Calling destroy listeners.");
146
147 for (i = component->destroy_listeners->len - 1; i >= 0; i--) {
148 struct bt_component_destroy_listener *listener =
149 &g_array_index(component->destroy_listeners,
150 struct bt_component_destroy_listener, i);
151
152 listener->func(component, listener->data);
153 }
154
155 /*
156 * User data is destroyed first, followed by the concrete
157 * component instance. Do not finalize if the component's user
158 * initialization method failed in the first place.
159 */
160 if (component->initialized) {
161 finalize_component(component);
162 }
163
164 if (component->destroy) {
165 BT_LOGD_STR("Destroying type-specific data.");
166 component->destroy(component);
167 }
168
169 if (component->input_ports) {
170 BT_LOGD_STR("Destroying input ports.");
171 g_ptr_array_free(component->input_ports, TRUE);
172 component->input_ports = NULL;
173 }
174
175 if (component->output_ports) {
176 BT_LOGD_STR("Destroying output ports.");
177 g_ptr_array_free(component->output_ports, TRUE);
178 component->output_ports = NULL;
179 }
180
181 if (component->destroy_listeners) {
182 g_array_free(component->destroy_listeners, TRUE);
183 component->destroy_listeners = NULL;
184 }
185
186 if (component->name) {
187 g_string_free(component->name, TRUE);
188 component->name = NULL;
189 }
190
191 BT_LOGD_STR("Putting component class.");
192 BT_OBJECT_PUT_REF_AND_RESET(component->class);
193 g_free(component);
194 }
195
196 enum bt_component_class_type bt_component_get_class_type(
197 const struct bt_component *component)
198 {
199 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
200 return component->class->type;
201 }
202
203 static
204 enum bt_self_component_add_port_status add_port(
205 struct bt_component *component, GPtrArray *ports,
206 enum bt_port_type port_type, const char *name, void *user_data,
207 struct bt_port **port)
208 {
209 struct bt_port *new_port = NULL;
210 struct bt_graph *graph = NULL;
211 enum bt_self_component_add_port_status status;
212
213 BT_ASSERT_PRE_NON_NULL(component, "Component");
214 BT_ASSERT_PRE_NON_NULL(name, "Name");
215 BT_ASSERT_PRE(strlen(name) > 0, "Name is empty");
216 graph = bt_component_borrow_graph(component);
217 BT_ASSERT_PRE(
218 graph->config_state == BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
219 "Component's graph is already configured: "
220 "%![comp-]+c, %![graph-]+g", component, graph);
221
222 // TODO: Validate that the name is not already used.
223
224 BT_LIB_LOGI("Adding port to component: %![comp-]+c, "
225 "port-type=%s, port-name=\"%s\"", component,
226 bt_port_type_string(port_type), name);
227
228 new_port = bt_port_create(component, port_type, name, user_data);
229 if (!new_port) {
230 BT_LIB_LOGE_APPEND_CAUSE("Cannot create port object.");
231 status = BT_FUNC_STATUS_MEMORY_ERROR;
232 goto error;
233 }
234
235 /*
236 * No name clash, add the port.
237 * The component is now the port's parent; it should _not_
238 * hold a reference to the port since the port's lifetime
239 * is now protected by the component's own lifetime.
240 */
241 g_ptr_array_add(ports, new_port);
242
243 /*
244 * Notify the graph's creator that a new port was added.
245 */
246 graph = bt_component_borrow_graph(component);
247 if (graph) {
248 enum bt_graph_listener_func_status listener_status;
249
250 listener_status = bt_graph_notify_port_added(graph, new_port);
251 if (listener_status != BT_FUNC_STATUS_OK) {
252 bt_graph_make_faulty(graph);
253 status = (int) listener_status;
254 goto error;
255 }
256 }
257
258 BT_LIB_LOGI("Created and added port to component: "
259 "%![comp-]+c, %![port-]+p", component, new_port);
260
261 *port = new_port;
262 status = BT_FUNC_STATUS_OK;
263
264 goto end;
265 error:
266 /*
267 * We need to release the reference that we would otherwise have
268 * returned to the caller.
269 */
270 BT_PORT_PUT_REF_AND_RESET(new_port);
271
272 end:
273 return status;
274 }
275
276 BT_HIDDEN
277 uint64_t bt_component_get_input_port_count(const struct bt_component *comp)
278 {
279 BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component");
280 return (uint64_t) comp->input_ports->len;
281 }
282
283 BT_HIDDEN
284 uint64_t bt_component_get_output_port_count(const struct bt_component *comp)
285 {
286 BT_ASSERT_PRE_DEV_NON_NULL(comp, "Component");
287 return (uint64_t) comp->output_ports->len;
288 }
289
290 BT_HIDDEN
291 int bt_component_create(struct bt_component_class *component_class,
292 const char *name, bt_logging_level log_level,
293 struct bt_component **user_component)
294 {
295 int ret = 0;
296 struct bt_component *component = NULL;
297 enum bt_component_class_type type;
298
299 BT_ASSERT(user_component);
300 BT_ASSERT(component_class);
301 BT_ASSERT(name);
302 type = bt_component_class_get_type(component_class);
303 BT_LIB_LOGI("Creating empty component from component class: %![cc-]+C, "
304 "comp-name=\"%s\", log-level=%s", component_class, name,
305 bt_common_logging_level_string(log_level));
306 component = component_create_funcs[type](component_class);
307 if (!component) {
308 BT_LIB_LOGE_APPEND_CAUSE(
309 "Cannot create specific component object.");
310 ret = -1;
311 goto end;
312 }
313
314 bt_object_init_shared_with_parent(&component->base, destroy_component);
315 component->class = component_class;
316 bt_object_get_ref_no_null_check(component->class);
317 component->destroy = component_destroy_funcs[type];
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 const char *bt_component_get_name(const struct bt_component *component)
360 {
361 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
362 return component->name->str;
363 }
364
365 const struct bt_component_class *bt_component_borrow_class_const(
366 const struct bt_component *component)
367 {
368 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
369 return component->class;
370 }
371
372 void *bt_self_component_get_data(const struct bt_self_component *self_comp)
373 {
374 struct bt_component *component = (void *) self_comp;
375
376 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
377 return component->user_data;
378 }
379
380 void bt_self_component_set_data(struct bt_self_component *self_comp,
381 void *data)
382 {
383 struct bt_component *component = (void *) self_comp;
384
385 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
386 component->user_data = data;
387 BT_LIB_LOGD("Set component's user data: %!+c", component);
388 }
389
390 BT_HIDDEN
391 void bt_component_set_graph(struct bt_component *component,
392 struct bt_graph *graph)
393 {
394 bt_object_set_parent(&component->base,
395 graph ? &graph->base : NULL);
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) == 0) {
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_DEV_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_DEV_NON_NULL(comp, "Component");
448 BT_ASSERT_PRE_DEV_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_DEV_NON_NULL(comp, "Component");
458 BT_ASSERT_PRE_DEV_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_add_port_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_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 {
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_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
496 BT_ASSERT(comp);
497 BT_ASSERT(self_port);
498 BT_ASSERT(other_port);
499
500 switch (comp->class->type) {
501 case BT_COMPONENT_CLASS_TYPE_SOURCE:
502 {
503 struct bt_component_class_source *src_cc = (void *) comp->class;
504
505 switch (self_port->type) {
506 case BT_PORT_TYPE_OUTPUT:
507 method = (method_t) src_cc->methods.output_port_connected;
508 break;
509 default:
510 bt_common_abort();
511 }
512
513 break;
514 }
515 case BT_COMPONENT_CLASS_TYPE_FILTER:
516 {
517 struct bt_component_class_filter *flt_cc = (void *) comp->class;
518
519 switch (self_port->type) {
520 case BT_PORT_TYPE_INPUT:
521 method = (method_t) flt_cc->methods.input_port_connected;
522 break;
523 case BT_PORT_TYPE_OUTPUT:
524 method = (method_t) flt_cc->methods.output_port_connected;
525 break;
526 default:
527 bt_common_abort();
528 }
529
530 break;
531 }
532 case BT_COMPONENT_CLASS_TYPE_SINK:
533 {
534 struct bt_component_class_sink *sink_cc = (void *) comp->class;
535
536 switch (self_port->type) {
537 case BT_PORT_TYPE_INPUT:
538 method = (method_t) sink_cc->methods.input_port_connected;
539 break;
540 default:
541 bt_common_abort();
542 }
543
544 break;
545 }
546 default:
547 bt_common_abort();
548 }
549
550 if (method) {
551 BT_LIB_LOGD("Calling user's \"port connected\" method: "
552 "%![comp-]+c, %![self-port-]+p, %![other-port-]+p",
553 comp, self_port, other_port);
554 status = (int) method(comp, self_port, (void *) other_port);
555 BT_LOGD("User method returned: status=%s",
556 bt_common_func_status_string(status));
557 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
558 status == BT_FUNC_STATUS_ERROR ||
559 status == BT_FUNC_STATUS_MEMORY_ERROR,
560 "Unexpected returned component status: status=%s",
561 bt_common_func_status_string(status));
562 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
563 }
564
565 return status;
566 }
567
568 BT_HIDDEN
569 void bt_component_add_destroy_listener(struct bt_component *component,
570 bt_component_destroy_listener_func func, void *data)
571 {
572 struct bt_component_destroy_listener listener;
573
574 BT_ASSERT(component);
575 BT_ASSERT(func);
576 listener.func = func;
577 listener.data = data;
578 g_array_append_val(component->destroy_listeners, listener);
579 BT_LIB_LOGD("Added destroy listener: %![comp-]+c, "
580 "func-addr=%p, data-addr=%p",
581 component, func, data);
582 }
583
584 BT_HIDDEN
585 void bt_component_remove_destroy_listener(struct bt_component *component,
586 bt_component_destroy_listener_func func, void *data)
587 {
588 uint64_t i;
589
590 BT_ASSERT(component);
591 BT_ASSERT(func);
592
593 for (i = 0; i < component->destroy_listeners->len; i++) {
594 struct bt_component_destroy_listener *listener =
595 &g_array_index(component->destroy_listeners,
596 struct bt_component_destroy_listener, i);
597
598 if (listener->func == func && listener->data == data) {
599 g_array_remove_index(component->destroy_listeners, i);
600 i--;
601 BT_LIB_LOGD("Removed destroy listener: %![comp-]+c, "
602 "func-addr=%p, data-addr=%p",
603 component, func, data);
604 }
605 }
606 }
607
608 bt_logging_level bt_component_get_logging_level(
609 const struct bt_component *component)
610 {
611 BT_ASSERT_PRE_DEV_NON_NULL(component, "Component");
612 return component->log_level;
613 }
614
615 uint64_t bt_self_component_get_graph_mip_version(
616 bt_self_component *self_component)
617 {
618 struct bt_component *comp = (void *) self_component;
619
620 BT_ASSERT_PRE_NON_NULL(self_component, "Component");
621 return bt_component_borrow_graph(comp)->mip_version;
622 }
623
624 void bt_component_get_ref(const struct bt_component *component)
625 {
626 bt_object_get_ref(component);
627 }
628
629 void bt_component_put_ref(const struct bt_component *component)
630 {
631 bt_object_put_ref(component);
632 }
This page took 0.044161 seconds and 4 git commands to generate.