Commit | Line | Data |
---|---|---|
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> |
f6ccaed9 | 42 | #include <babeltrace/assert-internal.h> |
0fbb9a9f | 43 | #include <stdlib.h> |
784cdc68 JG |
44 | #include <glib.h> |
45 | ||
46 | static | |
47 | void bt_connection_destroy(struct bt_object *obj) | |
48 | { | |
49 | struct bt_connection *connection = container_of(obj, | |
50 | struct bt_connection, base); | |
bd14d768 | 51 | |
a36bfb16 PP |
52 | BT_LOGD("Destroying connection: addr=%p", connection); |
53 | ||
bd14d768 PP |
54 | /* |
55 | * Make sure that each notification iterator which was created | |
56 | * for this connection is finalized before we destroy it. Once a | |
57 | * notification iterator is finalized, all its method return | |
58 | * NULL or the BT_NOTIFICATION_ITERATOR_STATUS_CANCELED status. | |
59 | * | |
60 | * Because connections are destroyed before components within a | |
61 | * graph, this ensures that notification iterators are always | |
62 | * finalized before their upstream component. | |
c42ea0af PP |
63 | * |
64 | * Ending the connection does exactly this. We pass `false` to | |
65 | * bt_connection_end() here to avoid removing this connection | |
66 | * from the graph: if we're here, we're already in the graph's | |
67 | * destructor. | |
bd14d768 | 68 | */ |
c42ea0af PP |
69 | bt_connection_end(connection, false); |
70 | g_ptr_array_free(connection->iterators, TRUE); | |
784cdc68 JG |
71 | |
72 | /* | |
c42ea0af PP |
73 | * No bt_put on ports as a connection only holds _weak_ |
74 | * references to them. | |
784cdc68 JG |
75 | */ |
76 | g_free(connection); | |
77 | } | |
78 | ||
f167d3c0 PP |
79 | static |
80 | void bt_connection_try_remove_from_graph(struct bt_connection *connection) | |
81 | { | |
82 | void *graph = bt_object_borrow_parent(&connection->base); | |
83 | ||
84 | if (connection->base.ref_count.count > 0 || | |
85 | connection->downstream_port || | |
86 | connection->upstream_port || | |
87 | connection->iterators->len > 0) { | |
88 | return; | |
89 | } | |
90 | ||
91 | /* | |
92 | * At this point we know that: | |
93 | * | |
a36bfb16 | 94 | * 1. The connection is ended (ports were disconnected). |
f167d3c0 PP |
95 | * 2. All the notification iterators that this connection |
96 | * created, if any, are finalized. | |
97 | * 3. The connection's reference count is 0, so only the | |
98 | * parent (graph) owns this connection after this call. | |
99 | * | |
100 | * In other words, no other object than the graph knows this | |
101 | * connection. | |
102 | * | |
103 | * It is safe to remove the connection from the graph, therefore | |
104 | * destroying it. | |
105 | */ | |
a36bfb16 PP |
106 | BT_LOGD("Removing self from graph's connections: " |
107 | "graph-addr=%p, conn-addr=%p", graph, connection); | |
f167d3c0 PP |
108 | bt_graph_remove_connection(graph, connection); |
109 | } | |
110 | ||
111 | static | |
112 | void bt_connection_parent_is_owner(struct bt_object *obj) | |
113 | { | |
114 | struct bt_connection *connection = container_of(obj, | |
115 | struct bt_connection, base); | |
116 | ||
117 | bt_connection_try_remove_from_graph(connection); | |
118 | } | |
119 | ||
6d137876 | 120 | struct bt_connection *bt_connection_from_private( |
890882ef PP |
121 | struct bt_private_connection *private_connection) |
122 | { | |
6d137876 | 123 | return bt_get(bt_connection_borrow_from_private(private_connection)); |
890882ef PP |
124 | } |
125 | ||
784cdc68 JG |
126 | BT_HIDDEN |
127 | struct bt_connection *bt_connection_create( | |
128 | struct bt_graph *graph, | |
72b913fb PP |
129 | struct bt_port *upstream_port, |
130 | struct bt_port *downstream_port) | |
784cdc68 JG |
131 | { |
132 | struct bt_connection *connection = NULL; | |
133 | ||
72b913fb | 134 | if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) { |
a36bfb16 | 135 | BT_LOGW_STR("Invalid parameter: upstream port is not an output port."); |
784cdc68 JG |
136 | goto end; |
137 | } | |
72b913fb | 138 | if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) { |
a36bfb16 | 139 | BT_LOGW_STR("Invalid parameter: downstream port is not an input port."); |
784cdc68 JG |
140 | goto end; |
141 | } | |
142 | ||
a36bfb16 PP |
143 | BT_LOGD("Creating connection: " |
144 | "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", " | |
145 | "downstream-port-addr=%p, downstream-port-name=\"%s\"", | |
146 | graph, upstream_port, bt_port_get_name(upstream_port), | |
147 | downstream_port, bt_port_get_name(downstream_port)); | |
784cdc68 JG |
148 | connection = g_new0(struct bt_connection, 1); |
149 | if (!connection) { | |
a36bfb16 | 150 | BT_LOGE_STR("Failed to allocate one connection."); |
784cdc68 JG |
151 | goto end; |
152 | } | |
153 | ||
154 | bt_object_init(connection, bt_connection_destroy); | |
f167d3c0 PP |
155 | bt_object_set_parent_is_owner_listener(connection, |
156 | bt_connection_parent_is_owner); | |
bd14d768 PP |
157 | connection->iterators = g_ptr_array_new(); |
158 | if (!connection->iterators) { | |
a36bfb16 | 159 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
bd14d768 PP |
160 | BT_PUT(connection); |
161 | goto end; | |
162 | } | |
163 | ||
784cdc68 | 164 | /* Weak references are taken, see comment in header. */ |
72b913fb PP |
165 | connection->upstream_port = upstream_port; |
166 | connection->downstream_port = downstream_port; | |
a36bfb16 | 167 | BT_LOGD_STR("Setting upstream port's connection."); |
72b913fb | 168 | bt_port_set_connection(upstream_port, connection); |
a36bfb16 | 169 | BT_LOGD_STR("Setting downstream port's connection."); |
72b913fb | 170 | bt_port_set_connection(downstream_port, connection); |
784cdc68 | 171 | bt_object_set_parent(connection, &graph->base); |
a36bfb16 PP |
172 | BT_LOGD("Created connection: " |
173 | "graph-addr=%p, upstream-port-addr=%p, uptream-port-name=\"%s\", " | |
174 | "downstream-port-addr=%p, downstream-port-name=\"%s\", " | |
175 | "conn-addr=%p", | |
176 | graph, upstream_port, bt_port_get_name(upstream_port), | |
177 | downstream_port, bt_port_get_name(downstream_port), | |
178 | connection); | |
179 | ||
784cdc68 JG |
180 | end: |
181 | return connection; | |
182 | } | |
183 | ||
72b913fb | 184 | BT_HIDDEN |
c42ea0af PP |
185 | void bt_connection_end(struct bt_connection *conn, |
186 | bool try_remove_from_graph) | |
72b913fb PP |
187 | { |
188 | struct bt_component *downstream_comp = NULL; | |
189 | struct bt_component *upstream_comp = NULL; | |
190 | struct bt_port *downstream_port = conn->downstream_port; | |
191 | struct bt_port *upstream_port = conn->upstream_port; | |
c42ea0af | 192 | struct bt_graph *graph = bt_connection_borrow_graph(conn); |
f167d3c0 | 193 | size_t i; |
72b913fb | 194 | |
c42ea0af PP |
195 | BT_LOGD("Ending connection: conn-addr=%p, try-remove-from-graph=%d", |
196 | conn, try_remove_from_graph); | |
197 | ||
72b913fb | 198 | if (downstream_port) { |
c42ea0af PP |
199 | BT_LOGD("Disconnecting connection's downstream port: " |
200 | "port-addr=%p, port-name=\"%s\"", | |
201 | downstream_port, bt_port_get_name(downstream_port)); | |
72b913fb PP |
202 | downstream_comp = bt_port_get_component(downstream_port); |
203 | bt_port_set_connection(downstream_port, NULL); | |
204 | conn->downstream_port = NULL; | |
205 | } | |
206 | ||
207 | if (upstream_port) { | |
c42ea0af PP |
208 | BT_LOGD("Disconnecting connection's upstream port: " |
209 | "port-addr=%p, port-name=\"%s\"", | |
210 | upstream_port, bt_port_get_name(upstream_port)); | |
72b913fb PP |
211 | upstream_comp = bt_port_get_component(upstream_port); |
212 | bt_port_set_connection(upstream_port, NULL); | |
213 | conn->upstream_port = NULL; | |
214 | } | |
215 | ||
2038affb | 216 | if (downstream_comp) { |
a36bfb16 | 217 | /* bt_component_port_disconnected() logs details */ |
72b913fb PP |
218 | bt_component_port_disconnected(downstream_comp, |
219 | downstream_port); | |
220 | } | |
221 | ||
2038affb | 222 | if (upstream_comp) { |
a36bfb16 | 223 | /* bt_component_port_disconnected() logs details */ |
72b913fb PP |
224 | bt_component_port_disconnected(upstream_comp, upstream_port); |
225 | } | |
226 | ||
f6ccaed9 | 227 | BT_ASSERT(graph); |
a36bfb16 | 228 | /* bt_graph_notify_ports_disconnected() logs details */ |
f345f8bb PP |
229 | bt_graph_notify_ports_disconnected(graph, upstream_comp, |
230 | downstream_comp, upstream_port, downstream_port); | |
72b913fb PP |
231 | bt_put(downstream_comp); |
232 | bt_put(upstream_comp); | |
f167d3c0 PP |
233 | |
234 | /* | |
a36bfb16 | 235 | * Because this connection is ended, finalize (cancel) each |
f167d3c0 PP |
236 | * notification iterator created from it. |
237 | */ | |
238 | for (i = 0; i < conn->iterators->len; i++) { | |
90157d89 | 239 | struct bt_notification_iterator_private_connection *iterator = |
f167d3c0 PP |
240 | g_ptr_array_index(conn->iterators, i); |
241 | ||
a36bfb16 PP |
242 | BT_LOGD("Finalizing notification iterator created by this ended connection: " |
243 | "conn-addr=%p, iter-addr=%p", conn, iterator); | |
90157d89 | 244 | bt_private_connection_notification_iterator_finalize(iterator); |
f167d3c0 PP |
245 | |
246 | /* | |
247 | * Make sure this iterator does not try to remove itself | |
248 | * from this connection's iterators on destruction | |
249 | * because this connection won't exist anymore. | |
250 | */ | |
90157d89 PP |
251 | bt_private_connection_notification_iterator_set_connection( |
252 | iterator, NULL); | |
f167d3c0 PP |
253 | } |
254 | ||
255 | g_ptr_array_set_size(conn->iterators, 0); | |
c42ea0af PP |
256 | |
257 | if (try_remove_from_graph) { | |
258 | bt_connection_try_remove_from_graph(conn); | |
259 | } | |
72b913fb PP |
260 | } |
261 | ||
262 | struct 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 | 268 | struct 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 | ||
73d5c1ad | 274 | enum bt_connection_status |
890882ef | 275 | bt_private_connection_create_notification_iterator( |
fa054faf | 276 | struct bt_private_connection *private_connection, |
73d5c1ad PP |
277 | const enum bt_notification_type *notification_types, |
278 | struct bt_notification_iterator **user_iterator) | |
784cdc68 | 279 | { |
890882ef | 280 | enum bt_component_class_type upstream_comp_class_type; |
90157d89 | 281 | struct bt_notification_iterator_private_connection *iterator = NULL; |
890882ef | 282 | struct bt_port *upstream_port = NULL; |
784cdc68 | 283 | struct bt_component *upstream_component = NULL; |
890882ef PP |
284 | struct bt_component_class *upstream_comp_class = NULL; |
285 | struct bt_connection *connection = NULL; | |
286 | bt_component_class_notification_iterator_init_method init_method = NULL; | |
73d5c1ad | 287 | enum bt_connection_status status; |
fa054faf PP |
288 | static const enum bt_notification_type all_notif_types[] = { |
289 | BT_NOTIFICATION_TYPE_ALL, | |
290 | BT_NOTIFICATION_TYPE_SENTINEL, | |
291 | }; | |
784cdc68 | 292 | |
890882ef | 293 | if (!private_connection) { |
a36bfb16 | 294 | BT_LOGW_STR("Invalid parameter: private connection is NULL."); |
73d5c1ad PP |
295 | status = BT_CONNECTION_STATUS_INVALID; |
296 | goto end; | |
297 | } | |
298 | ||
299 | if (!user_iterator) { | |
300 | BT_LOGW_STR("Invalid parameter: notification iterator pointer is NULL."); | |
301 | status = BT_CONNECTION_STATUS_INVALID; | |
302 | goto end; | |
784cdc68 JG |
303 | } |
304 | ||
6d137876 | 305 | connection = bt_connection_borrow_from_private(private_connection); |
c28d097c PP |
306 | |
307 | if (bt_graph_is_canceled(bt_connection_borrow_graph(connection))) { | |
308 | BT_LOGW("Cannot create notification iterator from connection: " | |
309 | "connection's graph is canceled: " | |
310 | "conn-addr=%p, upstream-port-addr=%p, " | |
311 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " | |
312 | "upstream-comp-name=\"%s\", graph-addr=%p", | |
313 | connection, connection->upstream_port, | |
314 | bt_port_get_name(connection->upstream_port), | |
315 | upstream_component, | |
316 | bt_component_get_name(upstream_component), | |
317 | bt_connection_borrow_graph(connection)); | |
318 | status = BT_CONNECTION_STATUS_GRAPH_IS_CANCELED; | |
319 | goto end; | |
fa054faf PP |
320 | } |
321 | ||
73d5c1ad | 322 | if (bt_connection_is_ended(connection)) { |
a36bfb16 PP |
323 | BT_LOGW("Invalid parameter: connection is ended: " |
324 | "conn-addr=%p", connection); | |
73d5c1ad PP |
325 | status = BT_CONNECTION_STATUS_IS_ENDED; |
326 | goto end; | |
72b913fb PP |
327 | } |
328 | ||
c28d097c PP |
329 | if (!notification_types) { |
330 | BT_LOGD_STR("No notification types: subscribing to all notifications."); | |
331 | notification_types = all_notif_types; | |
332 | } | |
333 | ||
890882ef | 334 | upstream_port = connection->upstream_port; |
f6ccaed9 | 335 | BT_ASSERT(upstream_port); |
890882ef | 336 | upstream_component = bt_port_get_component(upstream_port); |
f6ccaed9 | 337 | BT_ASSERT(upstream_component); |
890882ef | 338 | upstream_comp_class = upstream_component->class; |
a36bfb16 PP |
339 | BT_LOGD("Creating notification iterator from connection: " |
340 | "conn-addr=%p, upstream-port-addr=%p, " | |
341 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " | |
342 | "upstream-comp-name=\"%s\"", | |
343 | connection, connection->upstream_port, | |
344 | bt_port_get_name(connection->upstream_port), | |
345 | upstream_component, bt_component_get_name(upstream_component)); | |
890882ef PP |
346 | upstream_comp_class_type = |
347 | bt_component_get_class_type(upstream_component); | |
f6ccaed9 | 348 | BT_ASSERT(upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_SOURCE || |
73d5c1ad | 349 | upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_FILTER); |
90157d89 | 350 | status = bt_private_connection_notification_iterator_create(upstream_component, |
73d5c1ad PP |
351 | upstream_port, notification_types, connection, &iterator); |
352 | if (status != BT_CONNECTION_STATUS_OK) { | |
a36bfb16 | 353 | BT_LOGW("Cannot create notification iterator from connection."); |
73d5c1ad | 354 | goto end; |
890882ef PP |
355 | } |
356 | ||
357 | switch (upstream_comp_class_type) { | |
784cdc68 | 358 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
890882ef PP |
359 | { |
360 | struct bt_component_class_source *source_class = | |
361 | container_of(upstream_comp_class, | |
362 | struct bt_component_class_source, parent); | |
363 | init_method = source_class->methods.iterator.init; | |
784cdc68 | 364 | break; |
890882ef | 365 | } |
784cdc68 | 366 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
890882ef PP |
367 | { |
368 | struct bt_component_class_filter *filter_class = | |
369 | container_of(upstream_comp_class, | |
370 | struct bt_component_class_filter, parent); | |
371 | init_method = filter_class->methods.iterator.init; | |
784cdc68 | 372 | break; |
890882ef | 373 | } |
784cdc68 | 374 | default: |
890882ef | 375 | /* Unreachable. */ |
73d5c1ad PP |
376 | BT_LOGF("Unknown component class type: type=%d", |
377 | upstream_comp_class_type); | |
0fbb9a9f | 378 | abort(); |
890882ef PP |
379 | } |
380 | ||
381 | if (init_method) { | |
73d5c1ad | 382 | enum bt_notification_iterator_status iter_status; |
a36bfb16 PP |
383 | |
384 | BT_LOGD("Calling user's initialization method: iter-addr=%p", | |
385 | iterator); | |
73d5c1ad | 386 | iter_status = init_method( |
90157d89 | 387 | bt_private_connection_private_notification_iterator_from_notification_iterator((void *) iterator), |
91457551 | 388 | bt_private_port_from_port(upstream_port)); |
a36bfb16 | 389 | BT_LOGD("User method returned: status=%s", |
73d5c1ad PP |
390 | bt_notification_iterator_status_string(iter_status)); |
391 | if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
a36bfb16 | 392 | BT_LOGW_STR("Initialization method failed."); |
73d5c1ad PP |
393 | status = bt_connection_status_from_notification_iterator_status( |
394 | iter_status); | |
395 | goto end; | |
890882ef PP |
396 | } |
397 | } | |
398 | ||
90157d89 | 399 | iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE; |
bd14d768 | 400 | g_ptr_array_add(connection->iterators, iterator); |
a36bfb16 PP |
401 | BT_LOGD("Created notification iterator from connection: " |
402 | "conn-addr=%p, upstream-port-addr=%p, " | |
403 | "upstream-port-name=\"%s\", upstream-comp-addr=%p, " | |
404 | "upstream-comp-name=\"%s\", iter-addr=%p", | |
405 | connection, connection->upstream_port, | |
406 | bt_port_get_name(connection->upstream_port), | |
407 | upstream_component, bt_component_get_name(upstream_component), | |
408 | iterator); | |
1a6a376a PP |
409 | |
410 | /* Move reference to user */ | |
90157d89 | 411 | *user_iterator = (void *) iterator; |
1a6a376a | 412 | iterator = NULL; |
890882ef | 413 | |
784cdc68 JG |
414 | end: |
415 | bt_put(upstream_component); | |
73d5c1ad PP |
416 | bt_put(iterator); |
417 | return status; | |
784cdc68 | 418 | } |
bd14d768 PP |
419 | |
420 | BT_HIDDEN | |
421 | void bt_connection_remove_iterator(struct bt_connection *conn, | |
90157d89 | 422 | struct bt_notification_iterator_private_connection *iterator) |
bd14d768 PP |
423 | { |
424 | g_ptr_array_remove(conn->iterators, iterator); | |
a36bfb16 PP |
425 | BT_LOGV("Removed notification iterator from connection: " |
426 | "conn-addr=%p, iter-addr=%p", conn, iterator); | |
f167d3c0 | 427 | bt_connection_try_remove_from_graph(conn); |
bd14d768 | 428 | } |
090a4c0a PP |
429 | |
430 | bt_bool bt_connection_is_ended(struct bt_connection *connection) | |
431 | { | |
432 | return !connection->downstream_port && !connection->upstream_port; | |
433 | } |