Commit | Line | Data |
---|---|---|
958f7d11 PP |
1 | /* |
2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #include <babeltrace/babeltrace-internal.h> | |
24 | #include <babeltrace/ctf-ir/clock-class.h> | |
25 | #include <babeltrace/ctf-ir/event.h> | |
26 | #include <babeltrace/graph/clock-class-priority-map.h> | |
27 | #include <babeltrace/graph/component-filter.h> | |
28 | #include <babeltrace/graph/component.h> | |
29 | #include <babeltrace/graph/notification-event.h> | |
30 | #include <babeltrace/graph/notification-inactivity.h> | |
31 | #include <babeltrace/graph/notification-iterator.h> | |
32 | #include <babeltrace/graph/notification.h> | |
33 | #include <babeltrace/graph/port.h> | |
34 | #include <babeltrace/graph/private-component-filter.h> | |
35 | #include <babeltrace/graph/private-component.h> | |
36 | #include <babeltrace/graph/private-component.h> | |
37 | #include <babeltrace/graph/private-connection.h> | |
38 | #include <babeltrace/graph/private-notification-iterator.h> | |
39 | #include <babeltrace/graph/private-port.h> | |
40 | #include <plugins-common.h> | |
41 | #include <glib.h> | |
c55a9f58 | 42 | #include <stdbool.h> |
958f7d11 PP |
43 | #include <assert.h> |
44 | ||
65ee897d PP |
45 | #define IGNORE_ABSOLUTE_PARAM_NAME "ignore-absolute" |
46 | ||
958f7d11 PP |
47 | struct muxer_comp { |
48 | /* Array of struct bt_private_notification_iterator * (weak refs) */ | |
49 | GPtrArray *muxer_notif_iters; | |
50 | ||
51 | /* Weak ref */ | |
52 | struct bt_private_component *priv_comp; | |
53 | unsigned int next_port_num; | |
54 | size_t available_input_ports; | |
55 | bool error; | |
a09c6b95 | 56 | bool initializing_muxer_notif_iter; |
65ee897d | 57 | bool ignore_absolute; |
958f7d11 PP |
58 | }; |
59 | ||
60 | struct muxer_upstream_notif_iter { | |
ab11110e | 61 | /* Owned by this, NULL if ended */ |
958f7d11 PP |
62 | struct bt_notification_iterator *notif_iter; |
63 | ||
06a2cb0d | 64 | /* Weak */ |
958f7d11 | 65 | struct bt_private_port *priv_port; |
089717de PP |
66 | |
67 | /* | |
68 | * This flag is true if the upstream notification iterator's | |
69 | * current notification must be considered for the multiplexing | |
70 | * operations. If the upstream iterator returns | |
71 | * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN, then this object | |
72 | * is considered invalid, because its current notification is | |
73 | * still the previous one, but we already took it into account. | |
74 | * | |
75 | * The value of this flag is not important if notif_iter above | |
76 | * is NULL (which means the upstream iterator is finished). | |
77 | */ | |
78 | bool is_valid; | |
958f7d11 PP |
79 | }; |
80 | ||
958f7d11 | 81 | struct muxer_notif_iter { |
ab11110e PP |
82 | /* |
83 | * Array of struct muxer_upstream_notif_iter * (owned by this). | |
84 | * | |
85 | * NOTE: This array is searched in linearly to find the youngest | |
86 | * current notification. Keep this until benchmarks confirm that | |
87 | * another data structure is faster than this for our typical | |
88 | * use cases. | |
89 | */ | |
958f7d11 PP |
90 | GPtrArray *muxer_upstream_notif_iters; |
91 | ||
ab11110e | 92 | /* |
06a2cb0d | 93 | * List of "recently" connected input ports (weak) to |
ab11110e PP |
94 | * handle by this muxer notification iterator. |
95 | * muxer_port_connected() adds entries to this list, and the | |
96 | * entries are removed when a notification iterator is created | |
97 | * on the port's connection and put into | |
98 | * muxer_upstream_notif_iters above by | |
99 | * muxer_notif_iter_handle_newly_connected_ports(). | |
100 | */ | |
101 | GList *newly_connected_priv_ports; | |
958f7d11 PP |
102 | |
103 | /* Next thing to return by the "next" method */ | |
104 | struct bt_notification_iterator_next_return next_next_return; | |
958f7d11 PP |
105 | |
106 | /* Last time returned in a notification */ | |
107 | int64_t last_returned_ts_ns; | |
108 | }; | |
109 | ||
ab11110e PP |
110 | static |
111 | void destroy_muxer_upstream_notif_iter( | |
112 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
113 | { | |
114 | if (!muxer_upstream_notif_iter) { | |
115 | return; | |
116 | } | |
117 | ||
118 | bt_put(muxer_upstream_notif_iter->notif_iter); | |
ab11110e PP |
119 | g_free(muxer_upstream_notif_iter); |
120 | } | |
121 | ||
958f7d11 PP |
122 | static |
123 | struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter( | |
124 | struct muxer_notif_iter *muxer_notif_iter, | |
125 | struct bt_notification_iterator *notif_iter, | |
126 | struct bt_private_port *priv_port) | |
127 | { | |
128 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
129 | g_new0(struct muxer_upstream_notif_iter, 1); | |
130 | ||
131 | if (!muxer_upstream_notif_iter) { | |
132 | goto end; | |
133 | } | |
134 | ||
135 | muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter); | |
06a2cb0d | 136 | muxer_upstream_notif_iter->priv_port = priv_port; |
089717de | 137 | muxer_upstream_notif_iter->is_valid = false; |
958f7d11 PP |
138 | g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters, |
139 | muxer_upstream_notif_iter); | |
140 | ||
141 | end: | |
142 | return muxer_upstream_notif_iter; | |
143 | } | |
144 | ||
958f7d11 PP |
145 | static |
146 | int ensure_available_input_port(struct bt_private_component *priv_comp) | |
147 | { | |
148 | struct muxer_comp *muxer_comp = | |
149 | bt_private_component_get_user_data(priv_comp); | |
150 | int ret = 0; | |
151 | GString *port_name = NULL; | |
152 | void *priv_port = NULL; | |
153 | ||
154 | assert(muxer_comp); | |
155 | ||
156 | if (muxer_comp->available_input_ports >= 1) { | |
157 | goto end; | |
158 | } | |
159 | ||
160 | port_name = g_string_new("in"); | |
161 | if (!port_name) { | |
162 | ret = -1; | |
163 | goto end; | |
164 | } | |
165 | ||
166 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
167 | priv_port = bt_private_component_filter_add_input_private_port( | |
3e9b0023 | 168 | priv_comp, port_name->str, NULL); |
958f7d11 PP |
169 | if (!priv_port) { |
170 | ret = -1; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | muxer_comp->available_input_ports++; | |
175 | muxer_comp->next_port_num++; | |
176 | ||
177 | end: | |
178 | if (port_name) { | |
179 | g_string_free(port_name, TRUE); | |
180 | } | |
181 | ||
06a2cb0d | 182 | bt_put(priv_port); |
958f7d11 PP |
183 | return ret; |
184 | } | |
185 | ||
958f7d11 PP |
186 | static |
187 | int create_output_port(struct bt_private_component *priv_comp) | |
188 | { | |
189 | void *priv_port; | |
190 | int ret = 0; | |
191 | ||
192 | priv_port = bt_private_component_filter_add_output_private_port( | |
3e9b0023 | 193 | priv_comp, "out", NULL); |
958f7d11 PP |
194 | if (!priv_port) { |
195 | ret = -1; | |
196 | } | |
197 | ||
198 | bt_put(priv_port); | |
199 | return ret; | |
200 | } | |
201 | ||
202 | static | |
203 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
204 | { | |
205 | if (!muxer_comp) { | |
206 | return; | |
207 | } | |
208 | ||
209 | if (muxer_comp->muxer_notif_iters) { | |
210 | g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE); | |
211 | } | |
212 | ||
213 | g_free(muxer_comp); | |
214 | } | |
215 | ||
65ee897d PP |
216 | static |
217 | struct bt_value *get_default_params(void) | |
218 | { | |
219 | struct bt_value *params; | |
220 | int ret; | |
221 | ||
222 | params = bt_value_map_create(); | |
223 | if (!params) { | |
224 | goto error; | |
225 | } | |
226 | ||
227 | ret = bt_value_map_insert_bool(params, IGNORE_ABSOLUTE_PARAM_NAME, | |
228 | false); | |
229 | if (ret) { | |
230 | goto error; | |
231 | } | |
232 | ||
233 | goto end; | |
234 | ||
235 | error: | |
236 | BT_PUT(params); | |
237 | ||
238 | end: | |
239 | return params; | |
240 | } | |
241 | ||
242 | static | |
243 | int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params) | |
244 | { | |
245 | struct bt_value *default_params = NULL; | |
246 | struct bt_value *real_params = NULL; | |
247 | struct bt_value *ignore_absolute = NULL; | |
248 | int ret = 0; | |
c55a9f58 | 249 | bt_bool bool_val; |
65ee897d PP |
250 | |
251 | default_params = get_default_params(); | |
252 | if (!default_params) { | |
253 | goto error; | |
254 | } | |
255 | ||
256 | real_params = bt_value_map_extend(default_params, params); | |
257 | if (!real_params) { | |
258 | goto error; | |
259 | } | |
260 | ||
261 | ignore_absolute = bt_value_map_get(real_params, | |
262 | IGNORE_ABSOLUTE_PARAM_NAME); | |
263 | if (!bt_value_is_bool(ignore_absolute)) { | |
264 | goto error; | |
265 | } | |
266 | ||
c55a9f58 | 267 | if (bt_value_bool_get(ignore_absolute, &bool_val)) { |
65ee897d PP |
268 | goto error; |
269 | } | |
270 | ||
c55a9f58 PP |
271 | muxer_comp->ignore_absolute = (bool) bool_val; |
272 | ||
65ee897d PP |
273 | goto end; |
274 | ||
275 | error: | |
276 | ret = -1; | |
277 | ||
278 | end: | |
279 | bt_put(default_params); | |
280 | bt_put(real_params); | |
281 | bt_put(ignore_absolute); | |
282 | return ret; | |
283 | } | |
284 | ||
958f7d11 PP |
285 | BT_HIDDEN |
286 | enum bt_component_status muxer_init( | |
287 | struct bt_private_component *priv_comp, | |
288 | struct bt_value *params, void *init_data) | |
289 | { | |
290 | int ret; | |
291 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; | |
292 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); | |
293 | ||
294 | if (!muxer_comp) { | |
295 | goto error; | |
296 | } | |
297 | ||
65ee897d PP |
298 | ret = configure_muxer_comp(muxer_comp, params); |
299 | if (ret) { | |
300 | goto error; | |
301 | } | |
302 | ||
958f7d11 PP |
303 | muxer_comp->muxer_notif_iters = g_ptr_array_new(); |
304 | if (!muxer_comp->muxer_notif_iters) { | |
305 | goto error; | |
306 | } | |
307 | ||
308 | muxer_comp->priv_comp = priv_comp; | |
309 | ret = bt_private_component_set_user_data(priv_comp, muxer_comp); | |
310 | assert(ret == 0); | |
958f7d11 PP |
311 | ret = ensure_available_input_port(priv_comp); |
312 | if (ret) { | |
313 | goto error; | |
314 | } | |
315 | ||
316 | ret = create_output_port(priv_comp); | |
317 | if (ret) { | |
318 | goto error; | |
319 | } | |
320 | ||
321 | goto end; | |
322 | ||
323 | error: | |
324 | destroy_muxer_comp(muxer_comp); | |
325 | ret = bt_private_component_set_user_data(priv_comp, NULL); | |
326 | assert(ret == 0); | |
327 | status = BT_COMPONENT_STATUS_ERROR; | |
328 | ||
329 | end: | |
330 | return status; | |
331 | } | |
332 | ||
333 | BT_HIDDEN | |
334 | void muxer_finalize(struct bt_private_component *priv_comp) | |
335 | { | |
336 | struct muxer_comp *muxer_comp = | |
337 | bt_private_component_get_user_data(priv_comp); | |
338 | ||
339 | destroy_muxer_comp(muxer_comp); | |
340 | } | |
341 | ||
342 | static | |
343 | struct bt_notification_iterator *create_notif_iter_on_input_port( | |
344 | struct bt_private_port *priv_port, int *ret) | |
345 | { | |
346 | struct bt_port *port = bt_port_from_private_port(priv_port); | |
347 | struct bt_notification_iterator *notif_iter = NULL; | |
348 | struct bt_private_connection *priv_conn = NULL; | |
349 | ||
350 | assert(ret); | |
351 | *ret = 0; | |
352 | assert(port); | |
353 | ||
354 | assert(bt_port_is_connected(port)); | |
355 | priv_conn = bt_private_port_get_private_connection(priv_port); | |
356 | if (!priv_conn) { | |
357 | *ret = -1; | |
358 | goto end; | |
359 | } | |
360 | ||
ab11110e PP |
361 | // TODO: Advance the iterator to >= the time of the latest |
362 | // returned notification by the muxer notification | |
363 | // iterator which creates it. | |
958f7d11 | 364 | notif_iter = bt_private_connection_create_notification_iterator( |
fa054faf | 365 | priv_conn, NULL); |
958f7d11 PP |
366 | if (!notif_iter) { |
367 | *ret = -1; | |
368 | goto end; | |
369 | } | |
370 | ||
371 | end: | |
372 | bt_put(port); | |
373 | bt_put(priv_conn); | |
374 | return notif_iter; | |
375 | } | |
376 | ||
ab11110e | 377 | static |
089717de | 378 | enum bt_notification_iterator_status muxer_upstream_notif_iter_next( |
ab11110e PP |
379 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) |
380 | { | |
089717de | 381 | enum bt_notification_iterator_status status; |
ab11110e | 382 | |
089717de | 383 | status = bt_notification_iterator_next( |
ab11110e PP |
384 | muxer_upstream_notif_iter->notif_iter); |
385 | ||
089717de | 386 | switch (status) { |
ab11110e | 387 | case BT_NOTIFICATION_ITERATOR_STATUS_OK: |
089717de PP |
388 | /* |
389 | * Notification iterator's current notification is valid: | |
390 | * it must be considered for muxing operations. | |
391 | */ | |
392 | muxer_upstream_notif_iter->is_valid = true; | |
ab11110e PP |
393 | break; |
394 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
089717de PP |
395 | /* |
396 | * Notification iterator's current notification is not | |
397 | * valid anymore. Return | |
398 | * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN | |
399 | * immediately. | |
400 | */ | |
401 | muxer_upstream_notif_iter->is_valid = false; | |
ab11110e PP |
402 | break; |
403 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
404 | /* | |
405 | * Notification iterator reached the end: release it. It | |
406 | * won't be considered again to find the youngest | |
407 | * notification. | |
408 | */ | |
409 | BT_PUT(muxer_upstream_notif_iter->notif_iter); | |
089717de PP |
410 | muxer_upstream_notif_iter->is_valid = false; |
411 | status = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
412 | break; | |
ab11110e PP |
413 | default: |
414 | /* Error or unsupported status code */ | |
089717de PP |
415 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
416 | break; | |
ab11110e PP |
417 | } |
418 | ||
089717de | 419 | return status; |
ab11110e PP |
420 | } |
421 | ||
422 | static | |
089717de | 423 | int muxer_notif_iter_handle_newly_connected_ports( |
ab11110e PP |
424 | struct muxer_notif_iter *muxer_notif_iter) |
425 | { | |
ab11110e PP |
426 | int ret = 0; |
427 | ||
ab11110e PP |
428 | /* |
429 | * Here we create one upstream notification iterator for each | |
089717de PP |
430 | * newly connected port. We do not perform an initial "next" on |
431 | * those new upstream notification iterators: they are | |
432 | * invalidated, to be validated later. The list of newly | |
433 | * connected ports to handle here is updated by | |
434 | * muxer_port_connected(). | |
ab11110e PP |
435 | */ |
436 | while (true) { | |
437 | GList *node = muxer_notif_iter->newly_connected_priv_ports; | |
438 | struct bt_private_port *priv_port; | |
439 | struct bt_port *port; | |
440 | struct bt_notification_iterator *upstream_notif_iter = NULL; | |
441 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter; | |
442 | ||
443 | if (!node) { | |
444 | break; | |
445 | } | |
446 | ||
447 | priv_port = node->data; | |
448 | port = bt_port_from_private_port(priv_port); | |
449 | assert(port); | |
450 | ||
451 | if (!bt_port_is_connected(port)) { | |
452 | /* | |
453 | * Looks like this port is not connected | |
454 | * anymore: we can't create an upstream | |
089717de PP |
455 | * notification iterator on its (non-existing) |
456 | * connection in this case. | |
ab11110e PP |
457 | */ |
458 | goto remove_node; | |
459 | } | |
460 | ||
461 | BT_PUT(port); | |
462 | upstream_notif_iter = create_notif_iter_on_input_port(priv_port, | |
463 | &ret); | |
464 | if (ret) { | |
465 | assert(!upstream_notif_iter); | |
ab11110e PP |
466 | goto error; |
467 | } | |
468 | ||
469 | muxer_upstream_notif_iter = | |
470 | muxer_notif_iter_add_upstream_notif_iter( | |
471 | muxer_notif_iter, upstream_notif_iter, | |
472 | priv_port); | |
ab11110e PP |
473 | BT_PUT(upstream_notif_iter); |
474 | if (!muxer_upstream_notif_iter) { | |
475 | goto error; | |
476 | } | |
477 | ||
ab11110e PP |
478 | remove_node: |
479 | bt_put(upstream_notif_iter); | |
480 | bt_put(port); | |
ab11110e PP |
481 | muxer_notif_iter->newly_connected_priv_ports = |
482 | g_list_delete_link( | |
483 | muxer_notif_iter->newly_connected_priv_ports, | |
484 | node); | |
485 | } | |
486 | ||
487 | goto end; | |
488 | ||
489 | error: | |
089717de | 490 | if (ret >= 0) { |
ab11110e PP |
491 | ret = -1; |
492 | } | |
493 | ||
494 | end: | |
ab11110e PP |
495 | return ret; |
496 | } | |
497 | ||
958f7d11 PP |
498 | static |
499 | int get_notif_ts_ns(struct muxer_comp *muxer_comp, | |
500 | struct bt_notification *notif, int64_t last_returned_ts_ns, | |
501 | int64_t *ts_ns) | |
502 | { | |
503 | struct bt_clock_class_priority_map *cc_prio_map = NULL; | |
504 | struct bt_ctf_clock_class *clock_class = NULL; | |
505 | struct bt_ctf_clock_value *clock_value = NULL; | |
506 | struct bt_ctf_event *event = NULL; | |
507 | int ret = 0; | |
508 | ||
509 | assert(notif); | |
510 | assert(ts_ns); | |
511 | ||
512 | switch (bt_notification_get_type(notif)) { | |
513 | case BT_NOTIFICATION_TYPE_EVENT: | |
514 | cc_prio_map = | |
515 | bt_notification_event_get_clock_class_priority_map( | |
516 | notif); | |
517 | break; | |
518 | ||
519 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
520 | cc_prio_map = | |
a09c6b95 | 521 | bt_notification_inactivity_get_clock_class_priority_map( |
958f7d11 PP |
522 | notif); |
523 | break; | |
524 | default: | |
089717de | 525 | /* All the other notifications have a higher priority */ |
958f7d11 PP |
526 | *ts_ns = last_returned_ts_ns; |
527 | goto end; | |
528 | } | |
529 | ||
530 | if (!cc_prio_map) { | |
531 | goto error; | |
532 | } | |
533 | ||
534 | /* | |
535 | * If the clock class priority map is empty, then we consider | |
536 | * that this notification has no time. In this case it's always | |
537 | * the youngest. | |
538 | */ | |
539 | if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) { | |
540 | *ts_ns = last_returned_ts_ns; | |
541 | goto end; | |
542 | } | |
543 | ||
544 | clock_class = | |
545 | bt_clock_class_priority_map_get_highest_priority_clock_class( | |
546 | cc_prio_map); | |
547 | if (!clock_class) { | |
548 | goto error; | |
549 | } | |
550 | ||
65ee897d PP |
551 | if (!muxer_comp->ignore_absolute && |
552 | !bt_ctf_clock_class_is_absolute(clock_class)) { | |
958f7d11 PP |
553 | goto error; |
554 | } | |
555 | ||
556 | switch (bt_notification_get_type(notif)) { | |
557 | case BT_NOTIFICATION_TYPE_EVENT: | |
558 | event = bt_notification_event_get_event(notif); | |
ab11110e | 559 | assert(event); |
958f7d11 PP |
560 | clock_value = bt_ctf_event_get_clock_value(event, |
561 | clock_class); | |
562 | break; | |
563 | case BT_NOTIFICATION_TYPE_INACTIVITY: | |
564 | clock_value = bt_notification_inactivity_get_clock_value( | |
565 | notif, clock_class); | |
566 | break; | |
567 | default: | |
568 | assert(false); | |
569 | } | |
570 | ||
571 | if (!clock_value) { | |
572 | goto error; | |
573 | } | |
574 | ||
575 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ts_ns); | |
576 | if (ret) { | |
577 | goto error; | |
578 | } | |
579 | ||
580 | goto end; | |
581 | ||
582 | error: | |
583 | ret = -1; | |
584 | ||
585 | end: | |
586 | bt_put(cc_prio_map); | |
587 | bt_put(event); | |
588 | bt_put(clock_class); | |
589 | bt_put(clock_value); | |
590 | return ret; | |
591 | } | |
592 | ||
ab11110e PP |
593 | /* |
594 | * This function finds the youngest available notification amongst the | |
595 | * non-ended upstream notification iterators and returns the upstream | |
596 | * notification iterator which has it, or | |
597 | * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available | |
598 | * notification. | |
599 | * | |
600 | * This function does NOT: | |
601 | * | |
602 | * * Update any upstream notification iterator. | |
603 | * * Check for newly connected ports. | |
604 | * * Check the upstream notification iterators to retry. | |
605 | * | |
606 | * On sucess, this function sets *muxer_upstream_notif_iter to the | |
607 | * upstream notification iterator of which the current notification is | |
608 | * the youngest, and sets *ts_ns to its time. | |
609 | */ | |
958f7d11 PP |
610 | static |
611 | enum bt_notification_iterator_status | |
612 | muxer_notif_iter_youngest_upstream_notif_iter( | |
613 | struct muxer_comp *muxer_comp, | |
614 | struct muxer_notif_iter *muxer_notif_iter, | |
615 | struct muxer_upstream_notif_iter **muxer_upstream_notif_iter, | |
616 | int64_t *ts_ns) | |
617 | { | |
618 | size_t i; | |
619 | int ret; | |
620 | int64_t youngest_ts_ns = INT64_MAX; | |
621 | enum bt_notification_iterator_status status = | |
622 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
623 | ||
624 | assert(muxer_comp); | |
625 | assert(muxer_notif_iter); | |
626 | assert(muxer_upstream_notif_iter); | |
627 | *muxer_upstream_notif_iter = NULL; | |
628 | ||
629 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { | |
630 | struct bt_notification *notif; | |
631 | struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter = | |
632 | g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i); | |
633 | int64_t notif_ts_ns; | |
634 | ||
635 | if (!cur_muxer_upstream_notif_iter->notif_iter) { | |
ab11110e | 636 | /* This upstream notification iterator is ended */ |
958f7d11 PP |
637 | continue; |
638 | } | |
639 | ||
089717de | 640 | assert(cur_muxer_upstream_notif_iter->is_valid); |
958f7d11 PP |
641 | notif = bt_notification_iterator_get_notification( |
642 | cur_muxer_upstream_notif_iter->notif_iter); | |
643 | assert(notif); | |
644 | ret = get_notif_ts_ns(muxer_comp, notif, | |
645 | muxer_notif_iter->last_returned_ts_ns, ¬if_ts_ns); | |
646 | bt_put(notif); | |
647 | if (ret) { | |
648 | *muxer_upstream_notif_iter = NULL; | |
649 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
650 | goto end; | |
651 | } | |
652 | ||
653 | if (notif_ts_ns <= youngest_ts_ns) { | |
654 | *muxer_upstream_notif_iter = | |
655 | cur_muxer_upstream_notif_iter; | |
656 | youngest_ts_ns = notif_ts_ns; | |
657 | *ts_ns = youngest_ts_ns; | |
658 | } | |
659 | } | |
660 | ||
661 | if (!*muxer_upstream_notif_iter) { | |
662 | status = BT_NOTIFICATION_ITERATOR_STATUS_END; | |
663 | *ts_ns = INT64_MIN; | |
664 | } | |
665 | ||
666 | end: | |
667 | return status; | |
668 | } | |
669 | ||
670 | static | |
089717de PP |
671 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iter( |
672 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter) | |
958f7d11 | 673 | { |
089717de PP |
674 | enum bt_notification_iterator_status status = |
675 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
958f7d11 | 676 | |
089717de PP |
677 | if (muxer_upstream_notif_iter->is_valid || |
678 | !muxer_upstream_notif_iter->notif_iter) { | |
ab11110e PP |
679 | goto end; |
680 | } | |
681 | ||
089717de PP |
682 | status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter); |
683 | ||
684 | end: | |
685 | return status; | |
686 | } | |
687 | ||
688 | static | |
689 | enum bt_notification_iterator_status validate_muxer_upstream_notif_iters( | |
690 | struct muxer_notif_iter *muxer_notif_iter) | |
691 | { | |
692 | enum bt_notification_iterator_status status = | |
693 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
694 | size_t i; | |
695 | ||
696 | for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) { | |
697 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = | |
698 | g_ptr_array_index( | |
699 | muxer_notif_iter->muxer_upstream_notif_iters, | |
700 | i); | |
701 | ||
702 | status = validate_muxer_upstream_notif_iter( | |
703 | muxer_upstream_notif_iter); | |
704 | if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
705 | goto end; | |
706 | } | |
707 | } | |
708 | ||
709 | end: | |
710 | return status; | |
711 | } | |
712 | ||
713 | static | |
714 | struct bt_notification_iterator_next_return muxer_notif_iter_do_next( | |
715 | struct muxer_comp *muxer_comp, | |
716 | struct muxer_notif_iter *muxer_notif_iter) | |
717 | { | |
718 | struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL; | |
719 | struct bt_notification_iterator_next_return next_return = { | |
720 | .notification = NULL, | |
721 | .status = BT_NOTIFICATION_ITERATOR_STATUS_OK, | |
722 | }; | |
723 | int64_t next_return_ts; | |
724 | ||
725 | while (true) { | |
726 | int ret = muxer_notif_iter_handle_newly_connected_ports( | |
727 | muxer_notif_iter); | |
728 | ||
729 | if (ret) { | |
730 | next_return.status = | |
731 | BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
732 | goto end; | |
733 | } | |
734 | ||
735 | next_return.status = | |
736 | validate_muxer_upstream_notif_iters(muxer_notif_iter); | |
737 | if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
738 | goto end; | |
739 | } | |
ab11110e | 740 | |
958f7d11 | 741 | /* |
089717de PP |
742 | * At this point, we know that all the existing upstream |
743 | * notification iterators are valid. However the | |
744 | * operations to validate them (during | |
745 | * validate_muxer_upstream_notif_iters()) may have | |
746 | * connected new ports. If no ports were connected | |
747 | * during this operation, exit the loop. | |
958f7d11 | 748 | */ |
089717de PP |
749 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
750 | break; | |
751 | } | |
958f7d11 PP |
752 | } |
753 | ||
089717de PP |
754 | assert(!muxer_notif_iter->newly_connected_priv_ports); |
755 | ||
958f7d11 | 756 | /* |
089717de PP |
757 | * At this point we know that all the existing upstream |
758 | * notification iterators are valid. We can find the one, | |
759 | * amongst those, of which the current notification is the | |
760 | * youngest. | |
958f7d11 | 761 | */ |
089717de | 762 | next_return.status = |
958f7d11 PP |
763 | muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp, |
764 | muxer_notif_iter, &muxer_upstream_notif_iter, | |
089717de PP |
765 | &next_return_ts); |
766 | if (next_return.status < 0 || | |
767 | next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_END) { | |
958f7d11 PP |
768 | goto end; |
769 | } | |
770 | ||
089717de PP |
771 | if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) { |
772 | next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
958f7d11 PP |
773 | goto end; |
774 | } | |
775 | ||
089717de PP |
776 | assert(next_return.status == BT_NOTIFICATION_ITERATOR_STATUS_OK); |
777 | assert(muxer_upstream_notif_iter); | |
778 | next_return.notification = bt_notification_iterator_get_notification( | |
779 | muxer_upstream_notif_iter->notif_iter); | |
780 | assert(next_return.notification); | |
958f7d11 PP |
781 | |
782 | /* | |
089717de PP |
783 | * We invalidate the upstream notification iterator so that, the |
784 | * next time this function is called, | |
785 | * validate_muxer_upstream_notif_iters() will make it valid. | |
958f7d11 | 786 | */ |
089717de PP |
787 | muxer_upstream_notif_iter->is_valid = false; |
788 | muxer_notif_iter->last_returned_ts_ns = next_return_ts; | |
958f7d11 PP |
789 | |
790 | end: | |
089717de | 791 | return next_return; |
958f7d11 PP |
792 | } |
793 | ||
794 | static | |
ab11110e PP |
795 | void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter) |
796 | { | |
ab11110e PP |
797 | if (!muxer_notif_iter) { |
798 | return; | |
799 | } | |
800 | ||
801 | if (muxer_notif_iter->muxer_upstream_notif_iters) { | |
802 | g_ptr_array_free( | |
803 | muxer_notif_iter->muxer_upstream_notif_iters, TRUE); | |
804 | } | |
805 | ||
ab11110e PP |
806 | g_list_free(muxer_notif_iter->newly_connected_priv_ports); |
807 | g_free(muxer_notif_iter); | |
808 | } | |
809 | ||
810 | static | |
811 | int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp, | |
958f7d11 PP |
812 | struct muxer_notif_iter *muxer_notif_iter) |
813 | { | |
ab11110e | 814 | struct bt_component *comp; |
544d0515 PP |
815 | int64_t count; |
816 | int64_t i; | |
ab11110e | 817 | int ret = 0; |
958f7d11 | 818 | |
ab11110e PP |
819 | /* |
820 | * Add the connected input ports to this muxer notification | |
821 | * iterator's list of newly connected ports. They will be | |
822 | * handled by muxer_notif_iter_handle_newly_connected_ports(). | |
823 | */ | |
958f7d11 PP |
824 | comp = bt_component_from_private_component(muxer_comp->priv_comp); |
825 | assert(comp); | |
544d0515 PP |
826 | count = bt_component_filter_get_input_port_count(comp); |
827 | if (count < 0) { | |
ab11110e PP |
828 | goto end; |
829 | } | |
958f7d11 PP |
830 | |
831 | for (i = 0; i < count; i++) { | |
832 | struct bt_private_port *priv_port = | |
9ac68eb1 | 833 | bt_private_component_filter_get_input_private_port_by_index( |
958f7d11 PP |
834 | muxer_comp->priv_comp, i); |
835 | struct bt_port *port; | |
958f7d11 PP |
836 | |
837 | assert(priv_port); | |
958f7d11 | 838 | port = bt_port_from_private_port(priv_port); |
ab11110e | 839 | assert(port); |
958f7d11 PP |
840 | |
841 | if (!bt_port_is_connected(port)) { | |
958f7d11 | 842 | bt_put(priv_port); |
ab11110e | 843 | bt_put(port); |
958f7d11 PP |
844 | continue; |
845 | } | |
846 | ||
847 | bt_put(port); | |
06a2cb0d | 848 | bt_put(priv_port); |
ab11110e PP |
849 | muxer_notif_iter->newly_connected_priv_ports = |
850 | g_list_append( | |
851 | muxer_notif_iter->newly_connected_priv_ports, | |
958f7d11 | 852 | priv_port); |
ab11110e | 853 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
ab11110e PP |
854 | ret = -1; |
855 | goto end; | |
958f7d11 | 856 | } |
958f7d11 PP |
857 | } |
858 | ||
859 | end: | |
860 | bt_put(comp); | |
861 | return ret; | |
862 | } | |
863 | ||
958f7d11 PP |
864 | BT_HIDDEN |
865 | enum bt_notification_iterator_status muxer_notif_iter_init( | |
866 | struct bt_private_notification_iterator *priv_notif_iter, | |
867 | struct bt_private_port *output_priv_port) | |
868 | { | |
869 | struct muxer_comp *muxer_comp = NULL; | |
870 | struct muxer_notif_iter *muxer_notif_iter = NULL; | |
871 | struct bt_private_component *priv_comp = NULL; | |
872 | enum bt_notification_iterator_status status = | |
873 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
874 | int ret; | |
875 | ||
876 | priv_comp = bt_private_notification_iterator_get_private_component( | |
877 | priv_notif_iter); | |
878 | assert(priv_comp); | |
879 | muxer_comp = bt_private_component_get_user_data(priv_comp); | |
880 | assert(muxer_comp); | |
a09c6b95 PP |
881 | |
882 | if (muxer_comp->initializing_muxer_notif_iter) { | |
883 | /* | |
089717de PP |
884 | * Weird, unhandled situation detected: downstream |
885 | * creates a muxer notification iterator while creating | |
886 | * another muxer notification iterator (same component). | |
a09c6b95 | 887 | */ |
958f7d11 PP |
888 | goto error; |
889 | } | |
890 | ||
a09c6b95 PP |
891 | muxer_comp->initializing_muxer_notif_iter = true; |
892 | muxer_notif_iter = g_new0(struct muxer_notif_iter, 1); | |
893 | if (!muxer_notif_iter) { | |
ab11110e PP |
894 | goto error; |
895 | } | |
896 | ||
958f7d11 PP |
897 | muxer_notif_iter->last_returned_ts_ns = INT64_MIN; |
898 | muxer_notif_iter->muxer_upstream_notif_iters = | |
899 | g_ptr_array_new_with_free_func( | |
900 | (GDestroyNotify) destroy_muxer_upstream_notif_iter); | |
901 | if (!muxer_notif_iter->muxer_upstream_notif_iters) { | |
902 | goto error; | |
903 | } | |
904 | ||
a09c6b95 PP |
905 | /* |
906 | * Add the muxer notification iterator to the component's array | |
907 | * of muxer notification iterators here because | |
908 | * muxer_notif_iter_init_newly_connected_ports() can cause | |
909 | * muxer_port_connected() to be called, which adds the newly | |
910 | * connected port to each muxer notification iterator's list of | |
911 | * newly connected ports. | |
912 | */ | |
913 | g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter); | |
914 | ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp, | |
915 | muxer_notif_iter); | |
916 | if (ret) { | |
917 | goto error; | |
918 | } | |
919 | ||
958f7d11 PP |
920 | ret = bt_private_notification_iterator_set_user_data(priv_notif_iter, |
921 | muxer_notif_iter); | |
922 | assert(ret == 0); | |
958f7d11 PP |
923 | goto end; |
924 | ||
925 | error: | |
a09c6b95 PP |
926 | if (g_ptr_array_index(muxer_comp->muxer_notif_iters, |
927 | muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) { | |
928 | g_ptr_array_remove_index(muxer_comp->muxer_notif_iters, | |
929 | muxer_comp->muxer_notif_iters->len - 1); | |
930 | } | |
931 | ||
958f7d11 PP |
932 | destroy_muxer_notif_iter(muxer_notif_iter); |
933 | ret = bt_private_notification_iterator_set_user_data(priv_notif_iter, | |
934 | NULL); | |
935 | assert(ret == 0); | |
936 | status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
937 | ||
938 | end: | |
a09c6b95 | 939 | muxer_comp->initializing_muxer_notif_iter = false; |
958f7d11 PP |
940 | bt_put(priv_comp); |
941 | return status; | |
942 | } | |
943 | ||
944 | BT_HIDDEN | |
945 | void muxer_notif_iter_finalize( | |
946 | struct bt_private_notification_iterator *priv_notif_iter) | |
947 | { | |
948 | struct muxer_notif_iter *muxer_notif_iter = | |
949 | bt_private_notification_iterator_get_user_data(priv_notif_iter); | |
950 | struct bt_private_component *priv_comp = NULL; | |
951 | struct muxer_comp *muxer_comp = NULL; | |
952 | ||
953 | priv_comp = bt_private_notification_iterator_get_private_component( | |
954 | priv_notif_iter); | |
955 | assert(priv_comp); | |
956 | muxer_comp = bt_private_component_get_user_data(priv_comp); | |
957 | ||
958 | if (muxer_comp) { | |
959 | (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters, | |
960 | muxer_notif_iter); | |
961 | destroy_muxer_notif_iter(muxer_notif_iter); | |
962 | } | |
963 | ||
964 | bt_put(priv_comp); | |
965 | } | |
966 | ||
967 | BT_HIDDEN | |
968 | struct bt_notification_iterator_next_return muxer_notif_iter_next( | |
969 | struct bt_private_notification_iterator *priv_notif_iter) | |
970 | { | |
089717de | 971 | struct bt_notification_iterator_next_return next_ret; |
958f7d11 PP |
972 | struct muxer_notif_iter *muxer_notif_iter = |
973 | bt_private_notification_iterator_get_user_data(priv_notif_iter); | |
974 | struct bt_private_component *priv_comp = NULL; | |
975 | struct muxer_comp *muxer_comp = NULL; | |
958f7d11 PP |
976 | |
977 | assert(muxer_notif_iter); | |
978 | priv_comp = bt_private_notification_iterator_get_private_component( | |
979 | priv_notif_iter); | |
980 | assert(priv_comp); | |
981 | muxer_comp = bt_private_component_get_user_data(priv_comp); | |
982 | assert(muxer_comp); | |
983 | ||
984 | /* Are we in an error state set elsewhere? */ | |
985 | if (unlikely(muxer_comp->error)) { | |
089717de PP |
986 | next_ret.notification = NULL; |
987 | next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; | |
988 | goto end; | |
958f7d11 PP |
989 | } |
990 | ||
089717de | 991 | next_ret = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter); |
958f7d11 PP |
992 | |
993 | end: | |
994 | bt_put(priv_comp); | |
995 | return next_ret; | |
996 | } | |
997 | ||
998 | BT_HIDDEN | |
999 | void muxer_port_connected( | |
1000 | struct bt_private_component *priv_comp, | |
1001 | struct bt_private_port *self_private_port, | |
1002 | struct bt_port *other_port) | |
1003 | { | |
1004 | struct bt_port *self_port = | |
1005 | bt_port_from_private_port(self_private_port); | |
1006 | struct muxer_comp *muxer_comp = | |
1007 | bt_private_component_get_user_data(priv_comp); | |
1008 | size_t i; | |
06a2cb0d | 1009 | int ret; |
958f7d11 PP |
1010 | |
1011 | assert(self_port); | |
1012 | assert(muxer_comp); | |
1013 | ||
06a2cb0d PP |
1014 | if (bt_port_get_type(self_port) == BT_PORT_TYPE_OUTPUT) { |
1015 | goto end; | |
958f7d11 PP |
1016 | } |
1017 | ||
1018 | for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) { | |
1019 | struct muxer_notif_iter *muxer_notif_iter = | |
1020 | g_ptr_array_index(muxer_comp->muxer_notif_iters, i); | |
1021 | ||
1022 | /* | |
ab11110e PP |
1023 | * Add this port to the list of newly connected ports |
1024 | * for this muxer notification iterator. We append at | |
1025 | * the end of this list while | |
1026 | * muxer_notif_iter_handle_newly_connected_ports() | |
1027 | * removes the nodes from the beginning. | |
1028 | * | |
1029 | * The list node owns the private port. | |
958f7d11 | 1030 | */ |
ab11110e PP |
1031 | muxer_notif_iter->newly_connected_priv_ports = |
1032 | g_list_append( | |
1033 | muxer_notif_iter->newly_connected_priv_ports, | |
06a2cb0d | 1034 | self_private_port); |
ab11110e | 1035 | if (!muxer_notif_iter->newly_connected_priv_ports) { |
089717de | 1036 | /* Put reference taken by bt_get() above */ |
958f7d11 PP |
1037 | muxer_comp->error = true; |
1038 | goto end; | |
1039 | } | |
1040 | } | |
1041 | ||
06a2cb0d PP |
1042 | /* One less available input port */ |
1043 | muxer_comp->available_input_ports--; | |
1044 | ret = ensure_available_input_port(priv_comp); | |
1045 | if (ret) { | |
1046 | /* | |
1047 | * Only way to report an error later since this | |
1048 | * method does not return anything. | |
1049 | */ | |
1050 | muxer_comp->error = true; | |
1051 | goto end; | |
1052 | } | |
1053 | ||
958f7d11 PP |
1054 | end: |
1055 | bt_put(self_port); | |
1056 | } | |
1057 | ||
1058 | BT_HIDDEN | |
1059 | void muxer_port_disconnected(struct bt_private_component *priv_comp, | |
1060 | struct bt_private_port *priv_port) | |
1061 | { | |
1062 | struct bt_port *port = bt_port_from_private_port(priv_port); | |
1063 | struct muxer_comp *muxer_comp = | |
1064 | bt_private_component_get_user_data(priv_comp); | |
1065 | ||
1066 | assert(port); | |
1067 | assert(muxer_comp); | |
1068 | ||
ab11110e PP |
1069 | /* |
1070 | * There's nothing special to do when a port is disconnected | |
1071 | * because this component deals with upstream notification | |
1072 | * iterators which were already created thanks to connected | |
1073 | * ports. The fact that the port is disconnected does not cancel | |
1074 | * the upstream notification iterators created using its | |
089717de PP |
1075 | * connection: they still exist, even if the connection is dead. |
1076 | * The only way to remove an upstream notification iterator is | |
1077 | * for its "next" operation to return | |
1078 | * BT_NOTIFICATION_ITERATOR_STATUS_END. | |
ab11110e | 1079 | */ |
958f7d11 PP |
1080 | if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) { |
1081 | /* One more available input port */ | |
1082 | muxer_comp->available_input_ports++; | |
1083 | } | |
1084 | ||
1085 | bt_put(port); | |
1086 | } |