Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / graph / graph.h
CommitLineData
c0418dd9 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
f60c8b34 5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
c0418dd9
JG
6 */
7
0235b0db
MJ
8#ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
9#define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
10
870631a2
PP
11/* Protection: this file uses BT_LIB_LOG*() macros directly */
12#ifndef BT_LIB_LOG_SUPPORTED
13# error Please include "lib/logging.h" before including this file.
14#endif
15
3fadfbc0 16#include <babeltrace2/graph/graph.h>
43c59509 17#include <babeltrace2/graph/message.h>
91d81473 18#include "common/macros.h"
578e048b
MJ
19#include "lib/object.h"
20#include "lib/object-pool.h"
21#include "common/assert.h"
de625d1c 22#include "common/common.h"
c4f23e30 23#include <stdbool.h>
a256a42d 24#include <stdlib.h>
c0418dd9
JG
25#include <glib.h>
26
578e048b
MJ
27#include "component.h"
28#include "component-sink.h"
29#include "connection.h"
d24d5663 30#include "lib/func-status.h"
578e048b 31
51375aa9
PP
32/* Protection: this file uses BT_LIB_LOG*() macros directly */
33#ifndef BT_LIB_LOG_SUPPORTED
34# error Please include "lib/logging.h" before including this file.
35#endif
36
37/* Protection: this file uses BT_ASSERT_PRE*() macros directly */
38#ifndef BT_ASSERT_PRE_SUPPORTED
39# error Please include "lib/assert-pre.h" before including this file.
40#endif
41
42/* Protection: this file uses BT_ASSERT_POST*() macros directly */
43#ifndef BT_ASSERT_POST_SUPPORTED
44# error Please include "lib/assert-post.h" before including this file.
45#endif
46
1bf957a0
PP
47struct bt_component;
48struct bt_port;
49
5badd463
PP
50enum bt_graph_configuration_state {
51 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
52 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED,
53 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
38cda5da 54 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
9b4f9b42 55 BT_GRAPH_CONFIGURATION_STATE_DESTROYING,
5badd463
PP
56};
57
f60c8b34
JG
58struct bt_graph {
59 /**
60 * A component graph contains components and point-to-point connection
61 * between these components.
c0418dd9 62 *
f60c8b34
JG
63 * In terms of ownership:
64 * 1) The graph is the components' parent,
65 * 2) The graph is the connnections' parent,
66 * 3) Components share the ownership of their connections,
67 * 4) A connection holds weak references to its two component endpoints.
c0418dd9 68 */
f60c8b34
JG
69 struct bt_object base;
70
71 /* Array of pointers to bt_connection. */
72 GPtrArray *connections;
73 /* Array of pointers to bt_component. */
74 GPtrArray *components;
75 /* Queue of pointers (weak references) to sink bt_components. */
76 GQueue *sinks_to_consume;
1bf957a0 77
056deb59
PP
78 uint64_t mip_version;
79
9b4f9b42
PP
80 /*
81 * Array of `struct bt_interrupter *`, each one owned by this.
82 * If any interrupter is set, then this graph is deemed
83 * interrupted.
84 */
85 GPtrArray *interrupters;
86
87 /*
c513d240 88 * Default interrupter, owned by this.
9b4f9b42
PP
89 */
90 struct bt_interrupter *default_interrupter;
91
d94d92ac 92 bool has_sink;
8ed535b5
PP
93
94 /*
d94d92ac 95 * If this is false, then the public API's consuming
8ed535b5 96 * functions (bt_graph_consume() and bt_graph_run()) return
d24d5663 97 * BT_FUNC_STATUS_CANNOT_CONSUME. The internal "no check"
8ed535b5
PP
98 * functions always work.
99 *
d6e69534 100 * In bt_port_output_message_iterator_create(), on success,
8ed535b5
PP
101 * this flag is cleared so that the iterator remains the only
102 * consumer for the graph's lifetime.
103 */
d94d92ac 104 bool can_consume;
202a3a13 105
5badd463 106 enum bt_graph_configuration_state config_state;
4725a201 107
1bf957a0 108 struct {
d94d92ac
PP
109 GArray *source_output_port_added;
110 GArray *filter_output_port_added;
111 GArray *filter_input_port_added;
112 GArray *sink_input_port_added;
1bf957a0 113 } listeners;
5c563278 114
d6e69534
PP
115 /* Pool of `struct bt_message_event *` */
116 struct bt_object_pool event_msg_pool;
5c563278 117
d6e69534
PP
118 /* Pool of `struct bt_message_packet_beginning *` */
119 struct bt_object_pool packet_begin_msg_pool;
5c563278 120
d6e69534
PP
121 /* Pool of `struct bt_message_packet_end *` */
122 struct bt_object_pool packet_end_msg_pool;
5c563278
PP
123
124 /*
d6e69534 125 * Array of `struct bt_message *` (weak).
5c563278 126 *
d6e69534 127 * This is an array of all the messages ever created from
5c563278 128 * this graph. Some of them can be in one of the pools above,
d6e69534 129 * some of them can be at large. Because each message has a
5c563278 130 * weak pointer to the graph containing its pool, we need to
d6e69534 131 * notify each message that the graph is gone on graph
5c563278
PP
132 * destruction.
133 *
134 * TODO: When we support a maximum size for object pools,
d6e69534 135 * add a way for a message to remove itself from this
5c563278
PP
136 * array (on destruction).
137 */
d6e69534 138 GPtrArray *messages;
c0418dd9
JG
139};
140
8ed535b5 141static inline
bdb288b3 142void bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
8ed535b5 143{
98b15851 144 BT_ASSERT_DBG(graph);
8ed535b5
PP
145 graph->can_consume = can_consume;
146}
147
8ed535b5 148BT_HIDDEN
d24d5663 149int bt_graph_consume_sink_no_check(struct bt_graph *graph,
d94d92ac 150 struct bt_component_sink *sink);
8ed535b5 151
1bf957a0 152BT_HIDDEN
d24d5663 153enum bt_graph_listener_func_status bt_graph_notify_port_added(struct bt_graph *graph,
8cc56726 154 struct bt_port *port);
1bf957a0 155
f167d3c0
PP
156BT_HIDDEN
157void bt_graph_remove_connection(struct bt_graph *graph,
158 struct bt_connection *connection);
159
5c563278 160BT_HIDDEN
d6e69534
PP
161void bt_graph_add_message(struct bt_graph *graph,
162 struct bt_message *msg);
5c563278 163
9b4f9b42
PP
164BT_HIDDEN
165bool bt_graph_is_interrupted(const struct bt_graph *graph);
166
5badd463
PP
167static inline
168const char *bt_graph_configuration_state_string(
169 enum bt_graph_configuration_state state)
170{
171 switch (state) {
172 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURING:
8a432889 173 return "CONFIGURING";
5badd463 174 case BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED:
8a432889 175 return "PARTIALLY_CONFIGURED";
5badd463 176 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURED:
8a432889
PP
177 return "CONFIGURED";
178 case BT_GRAPH_CONFIGURATION_STATE_FAULTY:
179 return "FAULTY";
180 case BT_GRAPH_CONFIGURATION_STATE_DESTROYING:
181 return "DESTROYING";
5badd463
PP
182 default:
183 return "(unknown)";
184 }
185}
186
187static inline
d24d5663 188int bt_graph_configure(struct bt_graph *graph)
5badd463 189{
d24d5663 190 int status = BT_FUNC_STATUS_OK;
5badd463
PP
191 uint64_t i;
192
98b15851
PP
193 BT_ASSERT_DBG(graph->config_state !=
194 BT_GRAPH_CONFIGURATION_STATE_FAULTY);
38cda5da 195
91d81473 196 if (G_LIKELY(graph->config_state ==
5badd463
PP
197 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED)) {
198 goto end;
199 }
200
cd6128ca 201 BT_ASSERT_PRE(graph->has_sink, "Graph has no sink component: %!+g", graph);
5badd463
PP
202 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED;
203
204 for (i = 0; i < graph->components->len; i++) {
205 struct bt_component *comp = graph->components->pdata[i];
206 struct bt_component_sink *comp_sink = (void *) comp;
207 struct bt_component_class_sink *comp_cls_sink =
208 (void *) comp->class;
209
210 if (comp->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
211 continue;
212 }
213
214 if (comp_sink->graph_is_configured_method_called) {
215 continue;
216 }
217
218 if (comp_cls_sink->methods.graph_is_configured) {
d24d5663 219 enum bt_component_class_sink_graph_is_configured_method_status comp_status;
5badd463 220
5badd463
PP
221 BT_LIB_LOGD("Calling user's \"graph is configured\" method: "
222 "%![graph-]+g, %![comp-]+c",
223 graph, comp);
5badd463
PP
224 comp_status = comp_cls_sink->methods.graph_is_configured(
225 (void *) comp_sink);
5badd463 226 BT_LIB_LOGD("User method returned: status=%s",
d24d5663
PP
227 bt_common_func_status_string(comp_status));
228 BT_ASSERT_POST(comp_status == BT_FUNC_STATUS_OK ||
229 comp_status == BT_FUNC_STATUS_ERROR ||
230 comp_status == BT_FUNC_STATUS_MEMORY_ERROR,
5badd463 231 "Unexpected returned status: status=%s",
d24d5663 232 bt_common_func_status_string(comp_status));
6ecdcca3 233 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(comp_status);
d24d5663 234 if (comp_status != BT_FUNC_STATUS_OK) {
870631a2
PP
235 if (comp_status < 0) {
236 BT_LIB_LOGW_APPEND_CAUSE(
237 "Component's \"graph is configured\" method failed: "
238 "%![comp-]+c, status=%s",
239 comp,
240 bt_common_func_status_string(
241 comp_status));
242 }
243
244 status = comp_status;
5badd463
PP
245 goto end;
246 }
247 }
248
249 comp_sink->graph_is_configured_method_called = true;
250 }
251
252 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED;
253
254end:
255 return status;
256}
257
8cc56726
SM
258static inline
259void bt_graph_make_faulty(struct bt_graph *graph)
260{
261 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_FAULTY;
3f7d4d90 262 BT_LIB_LOGI("Set graph's state to faulty: %![graph-]+g", graph);
8cc56726
SM
263}
264
6ac74c0c 265#endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.086872 seconds and 4 git commands to generate.