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