lib/graph/iterator.c: add logging
[babeltrace.git] / lib / graph / connection.c
CommitLineData
784cdc68 1/*
7d55361f 2 * connection.c
784cdc68
JG
3 *
4 * Babeltrace Connection
5 *
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
a36bfb16
PP
29#define BT_LOG_TAG "CONNECTION"
30#include <babeltrace/lib-logging-internal.h>
31
b2e0c907
PP
32#include <babeltrace/graph/notification-iterator-internal.h>
33#include <babeltrace/graph/component-internal.h>
34#include <babeltrace/graph/component-source-internal.h>
35#include <babeltrace/graph/component-filter-internal.h>
36#include <babeltrace/graph/connection-internal.h>
37#include <babeltrace/graph/private-connection.h>
38#include <babeltrace/graph/graph-internal.h>
39#include <babeltrace/graph/port-internal.h>
784cdc68 40#include <babeltrace/object-internal.h>
3d9990ac 41#include <babeltrace/compiler-internal.h>
784cdc68
JG
42#include <glib.h>
43
44static
45void bt_connection_destroy(struct bt_object *obj)
46{
47 struct bt_connection *connection = container_of(obj,
48 struct bt_connection, base);
bd14d768
PP
49 size_t i;
50
a36bfb16
PP
51 BT_LOGD("Destroying connection: addr=%p", connection);
52
bd14d768
PP
53 /*
54 * Make sure that each notification iterator which was created
55 * for this connection is finalized before we destroy it. Once a
56 * notification iterator is finalized, all its method return
57 * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status.
58 *
59 * Because connections are destroyed before components within a
60 * graph, this ensures that notification iterators are always
61 * finalized before their upstream component.
62 */
63 if (connection->iterators) {
64 for (i = 0; i < connection->iterators->len; i++) {
65 struct bt_notification_iterator *iterator =
66 g_ptr_array_index(connection->iterators, i);
67
a36bfb16
PP
68 BT_LOGD("Finalizing notification iterator created by this connection: "
69 "iter-addr=%p", iterator);
bd14d768
PP
70 bt_notification_iterator_finalize(iterator);
71
72 /*
73 * Make sure this iterator does not try to
74 * remove itself from this connection's
75 * iterators on destruction because this
76 * connection won't exist anymore.
77 */
78 bt_notification_iterator_set_connection(iterator,
79 NULL);
80 }
81
82 g_ptr_array_free(connection->iterators, TRUE);
83 }
784cdc68
JG
84
85 /*
86 * No bt_put on ports as a connection only holds _weak_ references
87 * to them.
88 */
89 g_free(connection);
90}
91
f167d3c0
PP
92static
93void bt_connection_try_remove_from_graph(struct bt_connection *connection)
94{
95 void *graph = bt_object_borrow_parent(&connection->base);
96
97 if (connection->base.ref_count.count > 0 ||
98 connection->downstream_port ||
99 connection->upstream_port ||
100 connection->iterators->len > 0) {
101 return;
102 }
103
104 /*
105 * At this point we know that:
106 *
a36bfb16 107 * 1. The connection is ended (ports were disconnected).
f167d3c0
PP
108 * 2. All the notification iterators that this connection
109 * created, if any, are finalized.
110 * 3. The connection's reference count is 0, so only the
111 * parent (graph) owns this connection after this call.
112 *
113 * In other words, no other object than the graph knows this
114 * connection.
115 *
116 * It is safe to remove the connection from the graph, therefore
117 * destroying it.
118 */
a36bfb16
PP
119 BT_LOGD("Removing self from graph's connections: "
120 "graph-addr=%p, conn-addr=%p", graph, connection);
f167d3c0
PP
121 bt_graph_remove_connection(graph, connection);
122}
123
124static
125void bt_connection_parent_is_owner(struct bt_object *obj)
126{
127 struct bt_connection *connection = container_of(obj,
128 struct bt_connection, base);
129
130 bt_connection_try_remove_from_graph(connection);
131}
132
890882ef
PP
133struct bt_connection *bt_connection_from_private_connection(
134 struct bt_private_connection *private_connection)
135{
136 return bt_get(bt_connection_from_private(private_connection));
137}
138
784cdc68
JG
139BT_HIDDEN
140struct bt_connection *bt_connection_create(
141 struct bt_graph *graph,
72b913fb
PP
142 struct bt_port *upstream_port,
143 struct bt_port *downstream_port)
784cdc68
JG
144{
145 struct bt_connection *connection = NULL;
146
72b913fb 147 if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
a36bfb16 148 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
784cdc68
JG
149 goto end;
150 }
72b913fb 151 if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
a36bfb16 152 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
784cdc68
JG
153 goto end;
154 }
155
a36bfb16
PP
156 BT_LOGD("Creating connection: "
157 "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", "
158 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
159 graph, upstream_port, bt_port_get_name(upstream_port),
160 downstream_port, bt_port_get_name(downstream_port));
784cdc68
JG
161 connection = g_new0(struct bt_connection, 1);
162 if (!connection) {
a36bfb16 163 BT_LOGE_STR("Failed to allocate one connection.");
784cdc68
JG
164 goto end;
165 }
166
167 bt_object_init(connection, bt_connection_destroy);
f167d3c0
PP
168 bt_object_set_parent_is_owner_listener(connection,
169 bt_connection_parent_is_owner);
bd14d768
PP
170 connection->iterators = g_ptr_array_new();
171 if (!connection->iterators) {
a36bfb16 172 BT_LOGE_STR("Failed to allocate a GPtrArray.");
bd14d768
PP
173 BT_PUT(connection);
174 goto end;
175 }
176
784cdc68 177 /* Weak references are taken, see comment in header. */
72b913fb
PP
178 connection->upstream_port = upstream_port;
179 connection->downstream_port = downstream_port;
a36bfb16 180 BT_LOGD_STR("Setting upstream port's connection.");
72b913fb 181 bt_port_set_connection(upstream_port, connection);
a36bfb16 182 BT_LOGD_STR("Setting downstream port's connection.");
72b913fb 183 bt_port_set_connection(downstream_port, connection);
784cdc68 184 bt_object_set_parent(connection, &graph->base);
a36bfb16
PP
185 BT_LOGD("Created connection: "
186 "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", "
187 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
188 "conn-addr=%p",
189 graph, upstream_port, bt_port_get_name(upstream_port),
190 downstream_port, bt_port_get_name(downstream_port),
191 connection);
192
784cdc68
JG
193end:
194 return connection;
195}
196
72b913fb 197BT_HIDDEN
2038affb 198void bt_connection_disconnect_ports(struct bt_connection *conn)
72b913fb
PP
199{
200 struct bt_component *downstream_comp = NULL;
201 struct bt_component *upstream_comp = NULL;
202 struct bt_port *downstream_port = conn->downstream_port;
203 struct bt_port *upstream_port = conn->upstream_port;
f167d3c0
PP
204 struct bt_graph *graph = (void *) bt_object_borrow_parent(conn);
205 size_t i;
72b913fb
PP
206
207 if (downstream_port) {
208 downstream_comp = bt_port_get_component(downstream_port);
209 bt_port_set_connection(downstream_port, NULL);
210 conn->downstream_port = NULL;
211 }
212
213 if (upstream_port) {
214 upstream_comp = bt_port_get_component(upstream_port);
215 bt_port_set_connection(upstream_port, NULL);
216 conn->upstream_port = NULL;
217 }
218
2038affb 219 if (downstream_comp) {
a36bfb16 220 /* bt_component_port_disconnected() logs details */
72b913fb
PP
221 bt_component_port_disconnected(downstream_comp,
222 downstream_port);
223 }
224
2038affb 225 if (upstream_comp) {
a36bfb16 226 /* bt_component_port_disconnected() logs details */
72b913fb
PP
227 bt_component_port_disconnected(upstream_comp, upstream_port);
228 }
229
f345f8bb 230 assert(graph);
a36bfb16 231 /* bt_graph_notify_ports_disconnected() logs details */
f345f8bb
PP
232 bt_graph_notify_ports_disconnected(graph, upstream_comp,
233 downstream_comp, upstream_port, downstream_port);
72b913fb
PP
234 bt_put(downstream_comp);
235 bt_put(upstream_comp);
f167d3c0
PP
236
237 /*
a36bfb16 238 * Because this connection is ended, finalize (cancel) each
f167d3c0
PP
239 * notification iterator created from it.
240 */
241 for (i = 0; i < conn->iterators->len; i++) {
242 struct bt_notification_iterator *iterator =
243 g_ptr_array_index(conn->iterators, i);
244
a36bfb16
PP
245 BT_LOGD("Finalizing notification iterator created by this ended connection: "
246 "conn-addr=%p, iter-addr=%p", conn, iterator);
f167d3c0
PP
247 bt_notification_iterator_finalize(iterator);
248
249 /*
250 * Make sure this iterator does not try to remove itself
251 * from this connection's iterators on destruction
252 * because this connection won't exist anymore.
253 */
254 bt_notification_iterator_set_connection(iterator,
255 NULL);
256 }
257
258 g_ptr_array_set_size(conn->iterators, 0);
259 bt_connection_try_remove_from_graph(conn);
72b913fb
PP
260}
261
262struct bt_port *bt_connection_get_upstream_port(
784cdc68
JG
263 struct bt_connection *connection)
264{
72b913fb 265 return connection ? bt_get(connection->upstream_port) : NULL;
784cdc68
JG
266}
267
72b913fb 268struct bt_port *bt_connection_get_downstream_port(
784cdc68
JG
269 struct bt_connection *connection)
270{
72b913fb 271 return connection ? bt_get(connection->downstream_port) : NULL;
784cdc68
JG
272}
273
274struct bt_notification_iterator *
890882ef 275bt_private_connection_create_notification_iterator(
fa054faf
PP
276 struct bt_private_connection *private_connection,
277 const enum bt_notification_type *notification_types)
784cdc68 278{
890882ef
PP
279 enum bt_component_class_type upstream_comp_class_type;
280 struct bt_notification_iterator *iterator = NULL;
281 struct bt_port *upstream_port = NULL;
784cdc68 282 struct bt_component *upstream_component = NULL;
890882ef
PP
283 struct bt_component_class *upstream_comp_class = NULL;
284 struct bt_connection *connection = NULL;
285 bt_component_class_notification_iterator_init_method init_method = NULL;
fa054faf
PP
286 static const enum bt_notification_type all_notif_types[] = {
287 BT_NOTIFICATION_TYPE_ALL,
288 BT_NOTIFICATION_TYPE_SENTINEL,
289 };
784cdc68 290
890882ef 291 if (!private_connection) {
a36bfb16 292 BT_LOGW_STR("Invalid parameter: private connection is NULL.");
890882ef 293 goto error;
784cdc68
JG
294 }
295
fa054faf 296 if (!notification_types) {
a36bfb16 297 BT_LOGD_STR("No notification types: subscribing to all notifications.");
fa054faf
PP
298 notification_types = all_notif_types;
299 }
300
890882ef 301 connection = bt_connection_from_private(private_connection);
72b913fb 302 if (!connection->upstream_port || !connection->downstream_port) {
a36bfb16
PP
303 BT_LOGW("Invalid parameter: connection is ended: "
304 "conn-addr=%p", connection);
890882ef 305 goto error;
72b913fb
PP
306 }
307
890882ef
PP
308 upstream_port = connection->upstream_port;
309 assert(upstream_port);
310 upstream_component = bt_port_get_component(upstream_port);
784cdc68 311 assert(upstream_component);
890882ef 312 upstream_comp_class = upstream_component->class;
a36bfb16
PP
313 BT_LOGD("Creating notification iterator from connection: "
314 "conn-addr=%p, upstream-port-addr=%p, "
315 "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
316 "upstream-comp-name=\"%s\"",
317 connection, connection->upstream_port,
318 bt_port_get_name(connection->upstream_port),
319 upstream_component, bt_component_get_name(upstream_component));
890882ef
PP
320 upstream_comp_class_type =
321 bt_component_get_class_type(upstream_component);
322 if (upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_SOURCE &&
323 upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_FILTER) {
324 /* Unsupported operation. */
a36bfb16
PP
325 BT_LOGW("Upstream component's class is not a source or filter component class: "
326 "comp-class-type=%s",
327 bt_component_class_type_string(upstream_comp_class_type));
890882ef
PP
328 goto error;
329 }
784cdc68 330
3230ee6b 331 iterator = bt_notification_iterator_create(upstream_component,
bd14d768 332 upstream_port, notification_types, connection);
890882ef 333 if (!iterator) {
a36bfb16 334 BT_LOGW("Cannot create notification iterator from connection.");
890882ef
PP
335 goto error;
336 }
337
338 switch (upstream_comp_class_type) {
784cdc68 339 case BT_COMPONENT_CLASS_TYPE_SOURCE:
890882ef
PP
340 {
341 struct bt_component_class_source *source_class =
342 container_of(upstream_comp_class,
343 struct bt_component_class_source, parent);
344 init_method = source_class->methods.iterator.init;
784cdc68 345 break;
890882ef 346 }
784cdc68 347 case BT_COMPONENT_CLASS_TYPE_FILTER:
890882ef
PP
348 {
349 struct bt_component_class_filter *filter_class =
350 container_of(upstream_comp_class,
351 struct bt_component_class_filter, parent);
352 init_method = filter_class->methods.iterator.init;
784cdc68 353 break;
890882ef 354 }
784cdc68 355 default:
890882ef
PP
356 /* Unreachable. */
357 assert(0);
358 }
359
360 if (init_method) {
a36bfb16
PP
361 enum bt_notification_iterator_status status;
362
363 BT_LOGD("Calling user's initialization method: iter-addr=%p",
364 iterator);
365 status = init_method(
91457551
PP
366 bt_private_notification_iterator_from_notification_iterator(iterator),
367 bt_private_port_from_port(upstream_port));
a36bfb16
PP
368 BT_LOGD("User method returned: status=%s",
369 bt_notification_iterator_status_string(status));
890882ef 370 if (status < 0) {
a36bfb16 371 BT_LOGW_STR("Initialization method failed.");
890882ef
PP
372 goto error;
373 }
374 }
375
bd14d768 376 g_ptr_array_add(connection->iterators, iterator);
a36bfb16
PP
377 BT_LOGD("Created notification iterator from connection: "
378 "conn-addr=%p, upstream-port-addr=%p, "
379 "upstream-port-name=\"%s\", upstream-comp-addr=%p, "
380 "upstream-comp-name=\"%s\", iter-addr=%p",
381 connection, connection->upstream_port,
382 bt_port_get_name(connection->upstream_port),
383 upstream_component, bt_component_get_name(upstream_component),
384 iterator);
890882ef
PP
385 goto end;
386
387error:
388 BT_PUT(iterator);
389
784cdc68
JG
390end:
391 bt_put(upstream_component);
890882ef 392 return iterator;
784cdc68 393}
bd14d768
PP
394
395BT_HIDDEN
396void bt_connection_remove_iterator(struct bt_connection *conn,
397 struct bt_notification_iterator *iterator)
398{
399 g_ptr_array_remove(conn->iterators, iterator);
a36bfb16
PP
400 BT_LOGV("Removed notification iterator from connection: "
401 "conn-addr=%p, iter-addr=%p", conn, iterator);
f167d3c0 402 bt_connection_try_remove_from_graph(conn);
bd14d768 403}
This page took 0.043785 seconds and 4 git commands to generate.