a2111746793fa71a8def54e06bbc022a37cc66d7
[babeltrace.git] / lib / graph / iterator.c
1 /*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
30
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/trace-ir/fields.h>
34 #include <babeltrace/trace-ir/field-types.h>
35 #include <babeltrace/trace-ir/field-types-internal.h>
36 #include <babeltrace/trace-ir/event-internal.h>
37 #include <babeltrace/trace-ir/packet-internal.h>
38 #include <babeltrace/trace-ir/stream-internal.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/connection-internal.h>
41 #include <babeltrace/graph/component.h>
42 #include <babeltrace/graph/component-source-internal.h>
43 #include <babeltrace/graph/component-class-internal.h>
44 #include <babeltrace/graph/component-class-sink-colander-internal.h>
45 #include <babeltrace/graph/component-sink.h>
46 #include <babeltrace/graph/notification.h>
47 #include <babeltrace/graph/notification-iterator.h>
48 #include <babeltrace/graph/notification-iterator-internal.h>
49 #include <babeltrace/graph/notification-internal.h>
50 #include <babeltrace/graph/notification-event.h>
51 #include <babeltrace/graph/notification-event-internal.h>
52 #include <babeltrace/graph/notification-packet.h>
53 #include <babeltrace/graph/notification-packet-internal.h>
54 #include <babeltrace/graph/notification-stream.h>
55 #include <babeltrace/graph/notification-stream-internal.h>
56 #include <babeltrace/graph/port.h>
57 #include <babeltrace/graph/graph-internal.h>
58 #include <babeltrace/types.h>
59 #include <babeltrace/assert-internal.h>
60 #include <babeltrace/assert-pre-internal.h>
61 #include <stdint.h>
62 #include <inttypes.h>
63 #include <stdlib.h>
64
65 /*
66 * TODO: Use graph's state (number of active iterators, etc.) and
67 * possibly system specifications to make a better guess than this.
68 */
69 #define NOTIF_BATCH_SIZE 15
70
71 struct stream_state {
72 struct bt_stream *stream; /* owned by this */
73 struct bt_packet *cur_packet; /* owned by this */
74 uint64_t expected_notif_seq_num;
75 bt_bool is_ended;
76 };
77
78 BT_ASSERT_PRE_FUNC
79 static
80 void destroy_stream_state(struct stream_state *stream_state)
81 {
82 if (!stream_state) {
83 return;
84 }
85
86 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
87 BT_LOGV_STR("Putting stream state's current packet.");
88 bt_put(stream_state->cur_packet);
89 BT_LOGV_STR("Putting stream state's stream.");
90 bt_put(stream_state->stream);
91 g_free(stream_state);
92 }
93
94 BT_ASSERT_PRE_FUNC
95 static
96 struct stream_state *create_stream_state(struct bt_stream *stream)
97 {
98 struct stream_state *stream_state = g_new0(struct stream_state, 1);
99
100 if (!stream_state) {
101 BT_LOGE_STR("Failed to allocate one stream state.");
102 goto end;
103 }
104
105 /*
106 * We keep a reference to the stream until we know it's ended.
107 */
108 stream_state->stream = bt_get(stream);
109 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
110 "stream-state-addr=%p",
111 stream, bt_stream_get_name(stream), stream_state);
112
113 end:
114 return stream_state;
115 }
116
117 static
118 void destroy_base_notification_iterator(struct bt_object *obj)
119 {
120 struct bt_notification_iterator *iterator = (void *) obj;
121
122 BT_ASSERT(iterator);
123
124 if (iterator->notifs) {
125 g_ptr_array_free(iterator->notifs, TRUE);
126 }
127
128 g_free(iterator);
129 }
130
131 static
132 void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
133 {
134 struct bt_notification_iterator_private_connection *iterator;
135
136 BT_ASSERT(obj);
137
138 /*
139 * The notification iterator's reference count is 0 if we're
140 * here. Increment it to avoid a double-destroy (possibly
141 * infinitely recursive). This could happen for example if the
142 * notification iterator's finalization function does bt_get()
143 * (or anything that causes bt_get() to be called) on itself
144 * (ref. count goes from 0 to 1), and then bt_put(): the
145 * reference count would go from 1 to 0 again and this function
146 * would be called again.
147 */
148 obj->ref_count++;
149 iterator = (void *) obj;
150 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
151 iterator);
152 bt_private_connection_notification_iterator_finalize(iterator);
153
154 if (iterator->stream_states) {
155 /*
156 * Remove our destroy listener from each stream which
157 * has a state in this iterator. Otherwise the destroy
158 * listener would be called with an invalid/other
159 * notification iterator object.
160 */
161 g_hash_table_destroy(iterator->stream_states);
162 }
163
164 if (iterator->connection) {
165 /*
166 * Remove ourself from the originating connection so
167 * that it does not try to finalize a dangling pointer
168 * later.
169 */
170 bt_connection_remove_iterator(iterator->connection, iterator);
171 }
172
173 destroy_base_notification_iterator(obj);
174 }
175
176 BT_HIDDEN
177 void bt_private_connection_notification_iterator_finalize(
178 struct bt_notification_iterator_private_connection *iterator)
179 {
180 struct bt_component_class *comp_class = NULL;
181 bt_component_class_notification_iterator_finalize_method
182 finalize_method = NULL;
183
184 BT_ASSERT(iterator);
185
186 switch (iterator->state) {
187 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
188 /* Skip user finalization if user initialization failed */
189 BT_LOGD("Not finalizing non-initialized notification iterator: "
190 "addr=%p", iterator);
191 return;
192 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
193 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
194 /* Already finalized */
195 BT_LOGD("Not finalizing notification iterator: already finalized: "
196 "addr=%p", iterator);
197 return;
198 default:
199 break;
200 }
201
202 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
203
204 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
205 BT_LOGD("Updating notification iterator's state: "
206 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
207 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
208 } else {
209 BT_LOGD("Updating notification iterator's state: "
210 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
211 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
212 }
213
214 BT_ASSERT(iterator->upstream_component);
215 comp_class = iterator->upstream_component->class;
216
217 /* Call user-defined destroy method */
218 switch (comp_class->type) {
219 case BT_COMPONENT_CLASS_TYPE_SOURCE:
220 {
221 struct bt_component_class_source *source_class;
222
223 source_class = container_of(comp_class, struct bt_component_class_source, parent);
224 finalize_method = source_class->methods.iterator.finalize;
225 break;
226 }
227 case BT_COMPONENT_CLASS_TYPE_FILTER:
228 {
229 struct bt_component_class_filter *filter_class;
230
231 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
232 finalize_method = filter_class->methods.iterator.finalize;
233 break;
234 }
235 default:
236 /* Unreachable */
237 abort();
238 }
239
240 if (finalize_method) {
241 BT_LOGD("Calling user's finalization method: addr=%p",
242 iterator);
243 finalize_method(
244 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
245 }
246
247 iterator->upstream_component = NULL;
248 iterator->upstream_port = NULL;
249 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
250 }
251
252 BT_HIDDEN
253 void bt_private_connection_notification_iterator_set_connection(
254 struct bt_notification_iterator_private_connection *iterator,
255 struct bt_connection *connection)
256 {
257 BT_ASSERT(iterator);
258 iterator->connection = connection;
259 BT_LOGV("Set notification iterator's connection: "
260 "iter-addr=%p, conn-addr=%p", iterator, connection);
261 }
262
263 static
264 int init_notification_iterator(struct bt_notification_iterator *iterator,
265 enum bt_notification_iterator_type type,
266 bt_object_release_func destroy)
267 {
268 int ret = 0;
269
270 bt_object_init_shared(&iterator->base, destroy);
271 iterator->type = type;
272 iterator->notifs = g_ptr_array_new();
273 if (!iterator->notifs) {
274 BT_LOGE_STR("Failed to allocate a GPtrArray.");
275 ret = -1;
276 goto end;
277 }
278
279 g_ptr_array_set_size(iterator->notifs, NOTIF_BATCH_SIZE);
280
281 end:
282 return ret;
283 }
284
285 BT_HIDDEN
286 enum bt_connection_status bt_private_connection_notification_iterator_create(
287 struct bt_component *upstream_comp,
288 struct bt_port *upstream_port,
289 struct bt_connection *connection,
290 struct bt_notification_iterator_private_connection **user_iterator)
291 {
292 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
293 enum bt_component_class_type type;
294 struct bt_notification_iterator_private_connection *iterator = NULL;
295 int ret;
296
297 BT_ASSERT(upstream_comp);
298 BT_ASSERT(upstream_port);
299 BT_ASSERT(bt_port_is_connected(upstream_port));
300 BT_ASSERT(user_iterator);
301 BT_LOGD("Creating notification iterator on private connection: "
302 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
303 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
304 "conn-addr=%p",
305 upstream_comp, bt_component_get_name(upstream_comp),
306 upstream_port, bt_port_get_name(upstream_port),
307 connection);
308 type = bt_component_get_class_type(upstream_comp);
309 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
310 type == BT_COMPONENT_CLASS_TYPE_FILTER);
311 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
312 if (!iterator) {
313 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
314 status = BT_CONNECTION_STATUS_NOMEM;
315 goto end;
316 }
317
318 ret = init_notification_iterator((void *) iterator,
319 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
320 bt_private_connection_notification_iterator_destroy);
321 if (ret) {
322 /* init_notification_iterator() logs errors */
323 status = BT_CONNECTION_STATUS_NOMEM;
324 goto end;
325 }
326
327 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
328 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
329 if (!iterator->stream_states) {
330 BT_LOGE_STR("Failed to allocate a GHashTable.");
331 status = BT_CONNECTION_STATUS_NOMEM;
332 goto end;
333 }
334
335 iterator->upstream_component = upstream_comp;
336 iterator->upstream_port = upstream_port;
337 iterator->connection = connection;
338 iterator->graph = bt_component_borrow_graph(upstream_comp);
339 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
340 BT_LOGD("Created notification iterator: "
341 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
342 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
343 "conn-addr=%p, iter-addr=%p",
344 upstream_comp, bt_component_get_name(upstream_comp),
345 upstream_port, bt_port_get_name(upstream_port),
346 connection, iterator);
347
348 /* Move reference to user */
349 *user_iterator = iterator;
350 iterator = NULL;
351
352 end:
353 bt_put(iterator);
354 return status;
355 }
356
357 void *bt_private_connection_private_notification_iterator_get_user_data(
358 struct bt_private_connection_private_notification_iterator *private_iterator)
359 {
360 struct bt_notification_iterator_private_connection *iterator = (void *)
361 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
362
363 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
364 return iterator->user_data;
365 }
366
367 enum bt_notification_iterator_status
368 bt_private_connection_private_notification_iterator_set_user_data(
369 struct bt_private_connection_private_notification_iterator *private_iterator,
370 void *data)
371 {
372 struct bt_notification_iterator_private_connection *iterator = (void *)
373 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
374
375 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
376 iterator->user_data = data;
377 BT_LOGV("Set notification iterator's user data: "
378 "iter-addr=%p, user-data-addr=%p", iterator, data);
379 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
380 }
381
382 struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph(
383 struct bt_private_connection_private_notification_iterator *private_iterator)
384 {
385 struct bt_notification_iterator_private_connection *iterator = (void *)
386 bt_private_connection_notification_iterator_borrow_from_private(
387 private_iterator);
388
389 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
390 return iterator->graph;
391 }
392
393 BT_ASSERT_PRE_FUNC
394 static inline
395 void bt_notification_borrow_packet_stream(struct bt_notification *notif,
396 struct bt_stream **stream, struct bt_packet **packet)
397 {
398 BT_ASSERT(notif);
399
400 switch (notif->type) {
401 case BT_NOTIFICATION_TYPE_EVENT:
402 *packet = bt_event_borrow_packet(
403 bt_notification_event_borrow_event(notif));
404 *stream = bt_packet_borrow_stream(*packet);
405 break;
406 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
407 *stream = bt_notification_stream_begin_borrow_stream(notif);
408 break;
409 case BT_NOTIFICATION_TYPE_STREAM_END:
410 *stream = bt_notification_stream_end_borrow_stream(notif);
411 break;
412 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
413 *packet = bt_notification_packet_begin_borrow_packet(notif);
414 *stream = bt_packet_borrow_stream(*packet);
415 break;
416 case BT_NOTIFICATION_TYPE_PACKET_END:
417 *packet = bt_notification_packet_end_borrow_packet(notif);
418 *stream = bt_packet_borrow_stream(*packet);
419 break;
420 default:
421 break;
422 }
423 }
424
425 BT_ASSERT_PRE_FUNC
426 static inline
427 bool validate_notification(
428 struct bt_notification_iterator_private_connection *iterator,
429 struct bt_notification *notif)
430 {
431 bool is_valid = true;
432 struct stream_state *stream_state;
433 struct bt_stream *stream = NULL;
434 struct bt_packet *packet = NULL;
435
436 BT_ASSERT(notif);
437 bt_notification_borrow_packet_stream(notif, &stream, &packet);
438
439 if (!stream) {
440 /* we don't care about notifications not attached to streams */
441 goto end;
442 }
443
444 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
445 if (!stream_state) {
446 /*
447 * No stream state for this stream: this notification
448 * MUST be a BT_NOTIFICATION_TYPE_STREAM_BEGIN notification
449 * and its sequence number must be 0.
450 */
451 if (notif->type != BT_NOTIFICATION_TYPE_STREAM_BEGIN) {
452 BT_ASSERT_PRE_MSG("Unexpected notification: missing a "
453 "BT_NOTIFICATION_TYPE_STREAM_BEGIN "
454 "notification prior to this notification: "
455 "%![stream-]+s", stream);
456 is_valid = false;
457 goto end;
458 }
459
460 if (notif->seq_num == -1ULL) {
461 notif->seq_num = 0;
462 }
463
464 if (notif->seq_num != 0) {
465 BT_ASSERT_PRE_MSG("Unexpected notification sequence "
466 "number for this notification iterator: "
467 "this is the first notification for this "
468 "stream, expecting sequence number 0: "
469 "seq-num=%" PRIu64 ", %![stream-]+s",
470 notif->seq_num, stream);
471 is_valid = false;
472 goto end;
473 }
474
475 stream_state = create_stream_state(stream);
476 if (!stream_state) {
477 abort();
478 }
479
480 g_hash_table_insert(iterator->stream_states, stream,
481 stream_state);
482 stream_state->expected_notif_seq_num++;
483 goto end;
484 }
485
486 if (stream_state->is_ended) {
487 /*
488 * There's a new notification which has a reference to a
489 * stream which, from this iterator's point of view, is
490 * ended ("end of stream" notification was returned).
491 * This is bad: the API guarantees that it can never
492 * happen.
493 */
494 BT_ASSERT_PRE_MSG("Stream is already ended: %![stream-]+s",
495 stream);
496 is_valid = false;
497 goto end;
498 }
499
500 if (notif->seq_num == -1ULL) {
501 notif->seq_num = stream_state->expected_notif_seq_num;
502 }
503
504 if (notif->seq_num != -1ULL &&
505 notif->seq_num != stream_state->expected_notif_seq_num) {
506 BT_ASSERT_PRE_MSG("Unexpected notification sequence number: "
507 "seq-num=%" PRIu64 ", "
508 "expected-seq-num=%" PRIu64 ", %![stream-]+s",
509 notif->seq_num, stream_state->expected_notif_seq_num,
510 stream);
511 is_valid = false;
512 goto end;
513 }
514
515 switch (notif->type) {
516 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
517 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_BEGIN "
518 "notification at this point: notif-seq-num=%" PRIu64 ", "
519 "%![stream-]+s", notif->seq_num, stream);
520 is_valid = false;
521 goto end;
522 case BT_NOTIFICATION_TYPE_STREAM_END:
523 if (stream_state->cur_packet) {
524 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_STREAM_END "
525 "notification: missing a "
526 "BT_NOTIFICATION_TYPE_PACKET_END notification "
527 "prior to this notification: "
528 "notif-seq-num=%" PRIu64 ", "
529 "%![stream-]+s", notif->seq_num, stream);
530 is_valid = false;
531 goto end;
532 }
533 stream_state->expected_notif_seq_num++;
534 stream_state->is_ended = true;
535 goto end;
536 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
537 if (stream_state->cur_packet) {
538 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_BEGIN "
539 "notification at this point: missing a "
540 "BT_NOTIFICATION_TYPE_PACKET_END notification "
541 "prior to this notification: "
542 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
543 "%![packet-]+a", notif->seq_num, stream,
544 packet);
545 is_valid = false;
546 goto end;
547 }
548 stream_state->expected_notif_seq_num++;
549 stream_state->cur_packet = bt_get(packet);
550 goto end;
551 case BT_NOTIFICATION_TYPE_PACKET_END:
552 if (!stream_state->cur_packet) {
553 BT_ASSERT_PRE_MSG("Unexpected BT_NOTIFICATION_TYPE_PACKET_END "
554 "notification at this point: missing a "
555 "BT_NOTIFICATION_TYPE_PACKET_BEGIN notification "
556 "prior to this notification: "
557 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
558 "%![packet-]+a", notif->seq_num, stream,
559 packet);
560 is_valid = false;
561 goto end;
562 }
563 stream_state->expected_notif_seq_num++;
564 BT_PUT(stream_state->cur_packet);
565 goto end;
566 case BT_NOTIFICATION_TYPE_EVENT:
567 if (packet != stream_state->cur_packet) {
568 BT_ASSERT_PRE_MSG("Unexpected packet for "
569 "BT_NOTIFICATION_TYPE_EVENT notification: "
570 "notif-seq-num=%" PRIu64 ", %![stream-]+s, "
571 "%![notif-packet-]+a, %![expected-packet-]+a",
572 notif->seq_num, stream,
573 stream_state->cur_packet, packet);
574 is_valid = false;
575 goto end;
576 }
577 stream_state->expected_notif_seq_num++;
578 goto end;
579 default:
580 break;
581 }
582
583 end:
584 return is_valid;
585 }
586
587 BT_ASSERT_PRE_FUNC
588 static inline
589 bool validate_notifications(
590 struct bt_notification_iterator_private_connection *iterator,
591 uint64_t count)
592 {
593 bool ret = true;
594 bt_notification_array notifs = (void *) iterator->base.notifs->pdata;
595 uint64_t i;
596
597 for (i = 0; i < count; i++) {
598 ret = validate_notification(iterator, notifs[i]);
599 if (!ret) {
600 break;
601 }
602 }
603
604 return ret;
605 }
606
607 BT_ASSERT_PRE_FUNC
608 static inline bool priv_conn_notif_iter_can_end(
609 struct bt_notification_iterator_private_connection *iterator)
610 {
611 GHashTableIter iter;
612 gpointer stream_key, state_value;
613 bool ret = true;
614
615 /*
616 * Verify that this iterator received a
617 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
618 * which has a state.
619 */
620
621 g_hash_table_iter_init(&iter, iterator->stream_states);
622
623 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
624 struct stream_state *stream_state = (void *) state_value;
625
626 BT_ASSERT(stream_state);
627 BT_ASSERT(stream_key);
628
629 if (!stream_state->is_ended) {
630 BT_ASSERT_PRE_MSG("Ending notification iterator, "
631 "but stream is not ended: "
632 "%![stream-]s", stream_key);
633 ret = false;
634 goto end;
635 }
636 }
637
638 end:
639 return ret;
640 }
641
642 enum bt_notification_iterator_status
643 bt_private_connection_notification_iterator_next(
644 struct bt_notification_iterator *user_iterator,
645 struct bt_notification ***user_notifs, uint64_t *user_count)
646 {
647 struct bt_notification_iterator_private_connection *iterator =
648 (void *) user_iterator;
649 struct bt_private_connection_private_notification_iterator *priv_iterator =
650 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator);
651 bt_component_class_notification_iterator_next_method next_method = NULL;
652 enum bt_notification_iterator_status status =
653 BT_NOTIFICATION_ITERATOR_STATUS_OK;
654
655 BT_ASSERT_PRE_NON_NULL(user_iterator, "Notification iterator");
656 BT_ASSERT_PRE_NON_NULL(user_notifs, "Notification array");
657 BT_ASSERT_PRE_NON_NULL(user_count, "Notification count");
658 BT_ASSERT_PRE(user_iterator->type ==
659 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
660 "Notification iterator was not created from a private connection: "
661 "%!+i", iterator);
662 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
663 iterator);
664 BT_ASSERT_PRE(iterator->state ==
665 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
666 "Notification iterator's \"next\" called, but "
667 "iterator is in the wrong state: %!+i", iterator);
668 BT_ASSERT(iterator->upstream_component);
669 BT_ASSERT(iterator->upstream_component->class);
670
671 /* Pick the appropriate "next" method */
672 switch (iterator->upstream_component->class->type) {
673 case BT_COMPONENT_CLASS_TYPE_SOURCE:
674 {
675 struct bt_component_class_source *source_class =
676 container_of(iterator->upstream_component->class,
677 struct bt_component_class_source, parent);
678
679 BT_ASSERT(source_class->methods.iterator.next);
680 next_method = source_class->methods.iterator.next;
681 break;
682 }
683 case BT_COMPONENT_CLASS_TYPE_FILTER:
684 {
685 struct bt_component_class_filter *filter_class =
686 container_of(iterator->upstream_component->class,
687 struct bt_component_class_filter, parent);
688
689 BT_ASSERT(filter_class->methods.iterator.next);
690 next_method = filter_class->methods.iterator.next;
691 break;
692 }
693 default:
694 abort();
695 }
696
697 /*
698 * Call the user's "next" method to get the next notification
699 * and status.
700 */
701 BT_ASSERT(next_method);
702 BT_LOGD_STR("Calling user's \"next\" method.");
703 status = next_method(priv_iterator,
704 (void *) user_iterator->notifs->pdata,
705 NOTIF_BATCH_SIZE, user_count);
706 BT_LOGD("User method returned: status=%s",
707 bt_notification_iterator_status_string(status));
708 if (status < 0) {
709 BT_LOGW_STR("User method failed.");
710 goto end;
711 }
712
713 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
714 iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
715 /*
716 * The user's "next" method, somehow, cancelled its own
717 * notification iterator. This can happen, for example,
718 * when the user's method removes the port on which
719 * there's the connection from which the iterator was
720 * created. In this case, said connection is ended, and
721 * all its notification iterators are finalized.
722 *
723 * Only bt_put() the returned notification if
724 * the status is
725 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
726 * otherwise this field could be garbage.
727 */
728 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
729 uint64_t i;
730 bt_notification_array notifs =
731 (void *) user_iterator->notifs->pdata;
732
733 for (i = 0; i < *user_count; i++) {
734 bt_put(notifs[i]);
735 }
736 }
737
738 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
739 goto end;
740 }
741
742 switch (status) {
743 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
744 BT_ASSERT_PRE(validate_notifications(iterator, *user_count),
745 "Notifications are invalid at this point: "
746 "%![notif-iter-]+i, count=%" PRIu64,
747 iterator, *user_count);
748 *user_notifs = (void *) user_iterator->notifs->pdata;
749 break;
750 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
751 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
752 goto end;
753 case BT_NOTIFICATION_ITERATOR_STATUS_END:
754 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
755 "Notification iterator cannot end at this point: "
756 "%!+i", iterator);
757 BT_ASSERT(iterator->state ==
758 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE);
759 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED;
760 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
761 BT_LOGD("Set new status: status=%s",
762 bt_notification_iterator_status_string(status));
763 goto end;
764 default:
765 /* Unknown non-error status */
766 abort();
767 }
768
769 end:
770 return status;
771 }
772
773 enum bt_notification_iterator_status
774 bt_output_port_notification_iterator_next(
775 struct bt_notification_iterator *iterator,
776 bt_notification_array *notifs_to_user,
777 uint64_t *count_to_user)
778 {
779 enum bt_notification_iterator_status status;
780 struct bt_notification_iterator_output_port *out_port_iter =
781 (void *) iterator;
782 enum bt_graph_status graph_status;
783
784 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
785 BT_ASSERT_PRE_NON_NULL(notifs_to_user, "Notification array");
786 BT_ASSERT_PRE_NON_NULL(count_to_user, "Notification count");
787 BT_ASSERT_PRE(iterator->type ==
788 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
789 "Notification iterator was not created from an output port: "
790 "%!+i", iterator);
791 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
792 iterator);
793
794 graph_status = bt_graph_consume_sink_no_check(
795 out_port_iter->graph, out_port_iter->colander);
796 switch (graph_status) {
797 case BT_GRAPH_STATUS_CANCELED:
798 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
799 break;
800 case BT_GRAPH_STATUS_AGAIN:
801 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
802 break;
803 case BT_GRAPH_STATUS_END:
804 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
805 break;
806 case BT_GRAPH_STATUS_NOMEM:
807 status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
808 break;
809 case BT_GRAPH_STATUS_OK:
810 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
811
812 /*
813 * On success, the colander sink moves the notifications
814 * to this iterator's array and sets this iterator's
815 * notification count: move them to the user.
816 */
817 *notifs_to_user = (void *) iterator->notifs->pdata;
818 *count_to_user = out_port_iter->count;
819 break;
820 default:
821 /* Other errors */
822 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
823 }
824
825 return status;
826 }
827
828 struct bt_component *bt_private_connection_notification_iterator_get_component(
829 struct bt_notification_iterator *iterator)
830 {
831 struct bt_notification_iterator_private_connection *iter_priv_conn;
832
833 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
834 BT_ASSERT_PRE(iterator->type ==
835 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
836 "Notification iterator was not created from a private connection: "
837 "%!+i", iterator);
838 iter_priv_conn = (void *) iterator;
839 return bt_get(iter_priv_conn->upstream_component);
840 }
841
842 struct bt_private_component *
843 bt_private_connection_private_notification_iterator_get_private_component(
844 struct bt_private_connection_private_notification_iterator *private_iterator)
845 {
846 return bt_private_component_from_component(
847 bt_private_connection_notification_iterator_get_component(
848 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
849 }
850
851 static
852 void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
853 {
854 struct bt_notification_iterator_output_port *iterator =
855 (void *) container_of(obj, struct bt_notification_iterator, base);
856
857 BT_LOGD("Destroying output port notification iterator object: addr=%p",
858 iterator);
859 BT_LOGD_STR("Putting graph.");
860 bt_put(iterator->graph);
861 BT_LOGD_STR("Putting colander sink component.");
862 bt_put(iterator->colander);
863 destroy_base_notification_iterator(obj);
864 }
865
866 struct bt_notification_iterator *bt_output_port_notification_iterator_create(
867 struct bt_port *output_port,
868 const char *colander_component_name)
869 {
870 struct bt_notification_iterator_output_port *iterator = NULL;
871 struct bt_component_class *colander_comp_cls = NULL;
872 struct bt_component *output_port_comp = NULL;
873 struct bt_component *colander_comp;
874 struct bt_graph *graph = NULL;
875 enum bt_graph_status graph_status;
876 const char *colander_comp_name;
877 struct bt_port *colander_in_port = NULL;
878 struct bt_component_class_sink_colander_data colander_data;
879 int ret;
880
881 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
882 BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
883 "Port is not an output port: %!+p", output_port);
884 output_port_comp = bt_port_get_component(output_port);
885 BT_ASSERT_PRE(output_port_comp,
886 "Output port has no component: %!+p", output_port);
887 graph = bt_component_get_graph(output_port_comp);
888 BT_ASSERT(graph);
889
890 /* Create notification iterator */
891 BT_LOGD("Creating notification iterator on output port: "
892 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
893 output_port_comp, bt_component_get_name(output_port_comp),
894 output_port, bt_port_get_name(output_port));
895 iterator = g_new0(struct bt_notification_iterator_output_port, 1);
896 if (!iterator) {
897 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
898 goto error;
899 }
900
901 ret = init_notification_iterator((void *) iterator,
902 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
903 bt_output_port_notification_iterator_destroy);
904 if (ret) {
905 /* init_notification_iterator() logs errors */
906 BT_PUT(iterator);
907 goto end;
908 }
909
910 /* Create colander component */
911 colander_comp_cls = bt_component_class_sink_colander_get();
912 if (!colander_comp_cls) {
913 BT_LOGW("Cannot get colander sink component class.");
914 goto error;
915 }
916
917 BT_MOVE(iterator->graph, graph);
918 colander_comp_name =
919 colander_component_name ? colander_component_name : "colander";
920 colander_data.notifs = (void *) iterator->base.notifs->pdata;
921 colander_data.count_addr = &iterator->count;
922
923 graph_status = bt_graph_add_component_with_init_method_data(
924 iterator->graph, colander_comp_cls, colander_comp_name,
925 NULL, &colander_data, &iterator->colander);
926 if (graph_status != BT_GRAPH_STATUS_OK) {
927 BT_LOGW("Cannot add colander sink component to graph: "
928 "graph-addr=%p, name=\"%s\", graph-status=%s",
929 iterator->graph, colander_comp_name,
930 bt_graph_status_string(graph_status));
931 goto error;
932 }
933
934 /*
935 * Connect provided output port to the colander component's
936 * input port.
937 */
938 colander_in_port = bt_component_sink_get_input_port_by_index(
939 iterator->colander, 0);
940 BT_ASSERT(colander_in_port);
941 graph_status = bt_graph_connect_ports(iterator->graph,
942 output_port, colander_in_port, NULL);
943 if (graph_status != BT_GRAPH_STATUS_OK) {
944 BT_LOGW("Cannot add colander sink component to graph: "
945 "graph-addr=%p, name=\"%s\", graph-status=%s",
946 iterator->graph, colander_comp_name,
947 bt_graph_status_string(graph_status));
948 goto error;
949 }
950
951 /*
952 * At this point everything went fine. Make the graph
953 * nonconsumable forever so that only this notification iterator
954 * can consume (thanks to bt_graph_consume_sink_no_check()).
955 * This avoids leaking the notification created by the colander
956 * sink and moved to the notification iterator's notification
957 * member.
958 */
959 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
960 goto end;
961
962 error:
963 if (iterator && iterator->graph && iterator->colander) {
964 int ret;
965
966 /* Remove created colander component from graph if any */
967 colander_comp = iterator->colander;
968 BT_PUT(iterator->colander);
969
970 /*
971 * At this point the colander component's reference
972 * count is 0 because iterator->colander was the only
973 * owner. We also know that it is not connected because
974 * this is the last operation before this function
975 * succeeds.
976 *
977 * Since we honor the preconditions here,
978 * bt_graph_remove_unconnected_component() always
979 * succeeds.
980 */
981 ret = bt_graph_remove_unconnected_component(iterator->graph,
982 colander_comp);
983 BT_ASSERT(ret == 0);
984 }
985
986 BT_PUT(iterator);
987
988 end:
989 bt_put(colander_in_port);
990 bt_put(colander_comp_cls);
991 bt_put(output_port_comp);
992 bt_put(graph);
993 return (void *) iterator;
994 }
995
996 struct bt_notification_iterator *
997 bt_private_connection_notification_iterator_borrow_from_private(
998 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
999 {
1000 return (void *) private_notification_iterator;
1001 }
This page took 0.049494 seconds and 4 git commands to generate.