lib: update and simplify the `bt_object` API
[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 BT_ASSERT(obj);
141 g_free(obj);
142 }
143
144 static
145 void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
146 {
147 struct bt_notification_iterator_private_connection *iterator;
148
149 BT_ASSERT(obj);
150
151 /*
152 * The notification iterator's reference count is 0 if we're
153 * here. Increment it to avoid a double-destroy (possibly
154 * infinitely recursive). This could happen for example if the
155 * notification iterator's finalization function does bt_get()
156 * (or anything that causes bt_get() to be called) on itself
157 * (ref. count goes from 0 to 1), and then bt_put(): the
158 * reference count would go from 1 to 0 again and this function
159 * would be called again.
160 */
161 obj->ref_count++;
162 iterator = (void *) obj;
163 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
164 iterator);
165 bt_private_connection_notification_iterator_finalize(iterator);
166
167 if (iterator->stream_states) {
168 /*
169 * Remove our destroy listener from each stream which
170 * has a state in this iterator. Otherwise the destroy
171 * listener would be called with an invalid/other
172 * notification iterator object.
173 */
174 GHashTableIter ht_iter;
175 gpointer stream_gptr, stream_state_gptr;
176
177 g_hash_table_iter_init(&ht_iter, iterator->stream_states);
178
179 while (g_hash_table_iter_next(&ht_iter, &stream_gptr, &stream_state_gptr)) {
180 BT_ASSERT(stream_gptr);
181
182 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
183 bt_stream_common_remove_destroy_listener(
184 (void *) stream_gptr, stream_destroy_listener,
185 iterator);
186 }
187
188 g_hash_table_destroy(iterator->stream_states);
189 }
190
191 if (iterator->connection) {
192 /*
193 * Remove ourself from the originating connection so
194 * that it does not try to finalize a dangling pointer
195 * later.
196 */
197 bt_connection_remove_iterator(iterator->connection, iterator);
198 }
199
200 destroy_base_notification_iterator(obj);
201 }
202
203 BT_HIDDEN
204 void bt_private_connection_notification_iterator_finalize(
205 struct bt_notification_iterator_private_connection *iterator)
206 {
207 struct bt_component_class *comp_class = NULL;
208 bt_component_class_notification_iterator_finalize_method
209 finalize_method = NULL;
210
211 BT_ASSERT(iterator);
212
213 switch (iterator->state) {
214 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
215 /* Skip user finalization if user initialization failed */
216 BT_LOGD("Not finalizing non-initialized notification iterator: "
217 "addr=%p", iterator);
218 return;
219 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
220 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
221 /* Already finalized */
222 BT_LOGD("Not finalizing notification iterator: already finalized: "
223 "addr=%p", iterator);
224 return;
225 default:
226 break;
227 }
228
229 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
230
231 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
232 BT_LOGD("Updating notification iterator's state: "
233 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
234 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
235 } else {
236 BT_LOGD("Updating notification iterator's state: "
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
238 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
239 }
240
241 BT_ASSERT(iterator->upstream_component);
242 comp_class = iterator->upstream_component->class;
243
244 /* Call user-defined destroy method */
245 switch (comp_class->type) {
246 case BT_COMPONENT_CLASS_TYPE_SOURCE:
247 {
248 struct bt_component_class_source *source_class;
249
250 source_class = container_of(comp_class, struct bt_component_class_source, parent);
251 finalize_method = source_class->methods.iterator.finalize;
252 break;
253 }
254 case BT_COMPONENT_CLASS_TYPE_FILTER:
255 {
256 struct bt_component_class_filter *filter_class;
257
258 filter_class = container_of(comp_class, struct bt_component_class_filter, parent);
259 finalize_method = filter_class->methods.iterator.finalize;
260 break;
261 }
262 default:
263 /* Unreachable */
264 abort();
265 }
266
267 if (finalize_method) {
268 BT_LOGD("Calling user's finalization method: addr=%p",
269 iterator);
270 finalize_method(
271 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
272 }
273
274 iterator->upstream_component = NULL;
275 iterator->upstream_port = NULL;
276 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
277 }
278
279 BT_HIDDEN
280 void bt_private_connection_notification_iterator_set_connection(
281 struct bt_notification_iterator_private_connection *iterator,
282 struct bt_connection *connection)
283 {
284 BT_ASSERT(iterator);
285 iterator->connection = connection;
286 BT_LOGV("Set notification iterator's connection: "
287 "iter-addr=%p, conn-addr=%p", iterator, connection);
288 }
289
290 static
291 void init_notification_iterator(struct bt_notification_iterator *iterator,
292 enum bt_notification_iterator_type type,
293 bt_object_release_func destroy)
294 {
295 bt_object_init_shared(&iterator->base, destroy);
296 iterator->type = type;
297 }
298
299 BT_HIDDEN
300 enum bt_connection_status bt_private_connection_notification_iterator_create(
301 struct bt_component *upstream_comp,
302 struct bt_port *upstream_port,
303 struct bt_connection *connection,
304 struct bt_notification_iterator_private_connection **user_iterator)
305 {
306 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
307 enum bt_component_class_type type;
308 struct bt_notification_iterator_private_connection *iterator = NULL;
309
310 BT_ASSERT(upstream_comp);
311 BT_ASSERT(upstream_port);
312 BT_ASSERT(bt_port_is_connected(upstream_port));
313 BT_ASSERT(user_iterator);
314 BT_LOGD("Creating notification iterator on private connection: "
315 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
316 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
317 "conn-addr=%p",
318 upstream_comp, bt_component_get_name(upstream_comp),
319 upstream_port, bt_port_get_name(upstream_port),
320 connection);
321 type = bt_component_get_class_type(upstream_comp);
322 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
323 type == BT_COMPONENT_CLASS_TYPE_FILTER);
324 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
325 if (!iterator) {
326 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
327 status = BT_CONNECTION_STATUS_NOMEM;
328 goto end;
329 }
330
331 init_notification_iterator((void *) iterator,
332 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
333 bt_private_connection_notification_iterator_destroy);
334
335 iterator->stream_states = g_hash_table_new_full(g_direct_hash,
336 g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state);
337 if (!iterator->stream_states) {
338 BT_LOGE_STR("Failed to allocate a GHashTable.");
339 status = BT_CONNECTION_STATUS_NOMEM;
340 goto end;
341 }
342
343 iterator->upstream_component = upstream_comp;
344 iterator->upstream_port = upstream_port;
345 iterator->connection = connection;
346 iterator->graph = bt_component_borrow_graph(upstream_comp);
347 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
348 BT_LOGD("Created notification iterator: "
349 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
350 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
351 "conn-addr=%p, iter-addr=%p",
352 upstream_comp, bt_component_get_name(upstream_comp),
353 upstream_port, bt_port_get_name(upstream_port),
354 connection, iterator);
355
356 /* Move reference to user */
357 *user_iterator = iterator;
358 iterator = NULL;
359
360 end:
361 bt_put(iterator);
362 return status;
363 }
364
365 void *bt_private_connection_private_notification_iterator_get_user_data(
366 struct bt_private_connection_private_notification_iterator *private_iterator)
367 {
368 struct bt_notification_iterator_private_connection *iterator = (void *)
369 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
370
371 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
372 return iterator->user_data;
373 }
374
375 enum bt_notification_iterator_status
376 bt_private_connection_private_notification_iterator_set_user_data(
377 struct bt_private_connection_private_notification_iterator *private_iterator,
378 void *data)
379 {
380 struct bt_notification_iterator_private_connection *iterator = (void *)
381 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
382
383 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
384 iterator->user_data = data;
385 BT_LOGV("Set notification iterator's user data: "
386 "iter-addr=%p, user-data-addr=%p", iterator, data);
387 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
388 }
389
390 struct bt_graph *bt_private_connection_private_notification_iterator_borrow_graph(
391 struct bt_private_connection_private_notification_iterator *private_iterator)
392 {
393 struct bt_notification_iterator_private_connection *iterator = (void *)
394 bt_private_connection_notification_iterator_borrow_from_private(
395 private_iterator);
396
397 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
398 return iterator->graph;
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 enum bt_notification_iterator_status
631 bt_private_connection_notification_iterator_next(
632 struct bt_notification_iterator *user_iterator,
633 struct bt_notification **user_notif)
634 {
635 struct bt_notification_iterator_private_connection *iterator =
636 (void *) user_iterator;
637 struct bt_private_connection_private_notification_iterator *priv_iterator =
638 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator);
639 bt_component_class_notification_iterator_next_method next_method = NULL;
640 struct bt_notification_iterator_next_method_return next_return = {
641 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
642 .notification = NULL,
643 };
644 enum bt_notification_iterator_status status =
645 BT_NOTIFICATION_ITERATOR_STATUS_OK;
646
647 BT_ASSERT_PRE_NON_NULL(user_iterator, "Notification iterator");
648 BT_ASSERT_PRE_NON_NULL(user_notif, "Notification");
649 BT_ASSERT_PRE(user_iterator->type ==
650 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
651 "Notification iterator was not created from a private connection: "
652 "%!+i", iterator);
653 BT_LIB_LOGD("Getting next private connection notification iterator's notification: %!+i",
654 iterator);
655 BT_ASSERT_PRE(iterator->state ==
656 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE,
657 "Notification iterator's \"next\" called, but "
658 "iterator is in the wrong state: %!+i", iterator);
659 BT_ASSERT(iterator->upstream_component);
660 BT_ASSERT(iterator->upstream_component->class);
661
662 /* Pick the appropriate "next" method */
663 switch (iterator->upstream_component->class->type) {
664 case BT_COMPONENT_CLASS_TYPE_SOURCE:
665 {
666 struct bt_component_class_source *source_class =
667 container_of(iterator->upstream_component->class,
668 struct bt_component_class_source, parent);
669
670 BT_ASSERT(source_class->methods.iterator.next);
671 next_method = source_class->methods.iterator.next;
672 break;
673 }
674 case BT_COMPONENT_CLASS_TYPE_FILTER:
675 {
676 struct bt_component_class_filter *filter_class =
677 container_of(iterator->upstream_component->class,
678 struct bt_component_class_filter, parent);
679
680 BT_ASSERT(filter_class->methods.iterator.next);
681 next_method = filter_class->methods.iterator.next;
682 break;
683 }
684 default:
685 abort();
686 }
687
688 /*
689 * Call the user's "next" method to get the next notification
690 * and status.
691 */
692 BT_ASSERT(next_method);
693 BT_LOGD_STR("Calling user's \"next\" method.");
694 next_return = next_method(priv_iterator);
695 BT_LOGD("User method returned: status=%s",
696 bt_notification_iterator_status_string(next_return.status));
697 if (next_return.status < 0) {
698 BT_LOGW_STR("User method failed.");
699 status = next_return.status;
700 goto end;
701 }
702
703 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED ||
704 iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED) {
705 /*
706 * The user's "next" method, somehow, cancelled its own
707 * notification iterator. This can happen, for example,
708 * when the user's method removes the port on which
709 * there's the connection from which the iterator was
710 * created. In this case, said connection is ended, and
711 * all its notification iterators are finalized.
712 *
713 * Only bt_put() the returned notification if
714 * the status is
715 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
716 * otherwise this field could be garbage.
717 */
718 if (next_return.status ==
719 BT_NOTIFICATION_ITERATOR_STATUS_OK) {
720 bt_put(next_return.notification);
721 }
722
723 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
724 goto end;
725 }
726
727 switch (next_return.status) {
728 case BT_NOTIFICATION_ITERATOR_STATUS_END:
729 BT_ASSERT_PRE(priv_conn_notif_iter_can_end(iterator),
730 "Notification iterator cannot end at this point: "
731 "%!+i", iterator);
732 BT_ASSERT(iterator->state ==
733 BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ACTIVE);
734 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED;
735 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
736 BT_LOGD("Set new status: status=%s",
737 bt_notification_iterator_status_string(status));
738 goto end;
739 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
740 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
741 goto end;
742 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
743 BT_ASSERT_PRE(next_return.notification,
744 "User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL: "
745 "%!+i", iterator);
746 BT_ASSERT_PRE(validate_notification(iterator,
747 next_return.notification),
748 "Notification is invalid at this point: "
749 "%![notif-iter-]+i, %![notif-]+n",
750 iterator, next_return.notification);
751 *user_notif = next_return.notification;
752 break;
753 default:
754 /* Unknown non-error status */
755 abort();
756 }
757
758 end:
759 return status;
760 }
761
762 enum bt_notification_iterator_status
763 bt_output_port_notification_iterator_next(
764 struct bt_notification_iterator *iterator,
765 struct bt_notification **user_notif)
766 {
767 enum bt_notification_iterator_status status;
768 struct bt_notification_iterator_output_port *out_port_iter =
769 (void *) iterator;
770 enum bt_graph_status graph_status;
771
772 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
773 BT_ASSERT_PRE_NON_NULL(user_notif, "Notification");
774 BT_ASSERT_PRE(iterator->type ==
775 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
776 "Notification iterator was not created from an output port: "
777 "%!+i", iterator);
778 BT_LIB_LOGD("Getting next output port notification iterator's notification: %!+i",
779 iterator);
780 graph_status = bt_graph_consume_sink_no_check(
781 out_port_iter->graph, out_port_iter->colander);
782 switch (graph_status) {
783 case BT_GRAPH_STATUS_CANCELED:
784 BT_ASSERT(!out_port_iter->notif);
785 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
786 break;
787 case BT_GRAPH_STATUS_AGAIN:
788 BT_ASSERT(!out_port_iter->notif);
789 status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
790 break;
791 case BT_GRAPH_STATUS_END:
792 BT_ASSERT(!out_port_iter->notif);
793 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
794 break;
795 case BT_GRAPH_STATUS_NOMEM:
796 BT_ASSERT(!out_port_iter->notif);
797 status = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
798 break;
799 case BT_GRAPH_STATUS_OK:
800 BT_ASSERT(out_port_iter->notif);
801 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
802 *user_notif = out_port_iter->notif;
803 out_port_iter->notif = NULL;
804 break;
805 default:
806 /* Other errors */
807 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
808 }
809
810 return status;
811 }
812
813 struct bt_component *bt_private_connection_notification_iterator_get_component(
814 struct bt_notification_iterator *iterator)
815 {
816 struct bt_notification_iterator_private_connection *iter_priv_conn;
817
818 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
819 BT_ASSERT_PRE(iterator->type ==
820 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
821 "Notification iterator was not created from a private connection: "
822 "%!+i", iterator);
823 iter_priv_conn = (void *) iterator;
824 return bt_get(iter_priv_conn->upstream_component);
825 }
826
827 struct bt_private_component *
828 bt_private_connection_private_notification_iterator_get_private_component(
829 struct bt_private_connection_private_notification_iterator *private_iterator)
830 {
831 return bt_private_component_from_component(
832 bt_private_connection_notification_iterator_get_component(
833 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
834 }
835
836 static
837 void bt_output_port_notification_iterator_destroy(struct bt_object *obj)
838 {
839 struct bt_notification_iterator_output_port *iterator =
840 (void *) container_of(obj, struct bt_notification_iterator, base);
841
842 BT_LOGD("Destroying output port notification iterator object: addr=%p",
843 iterator);
844 BT_ASSERT(!iterator->notif);
845 BT_LOGD_STR("Putting graph.");
846 bt_put(iterator->graph);
847 BT_LOGD_STR("Putting colander sink component.");
848 bt_put(iterator->colander);
849 destroy_base_notification_iterator(obj);
850 }
851
852 struct bt_notification_iterator *bt_output_port_notification_iterator_create(
853 struct bt_port *output_port,
854 const char *colander_component_name)
855 {
856 struct bt_notification_iterator_output_port *iterator = NULL;
857 struct bt_component_class *colander_comp_cls = NULL;
858 struct bt_component *output_port_comp = NULL;
859 struct bt_component *colander_comp;
860 struct bt_graph *graph = NULL;
861 enum bt_graph_status graph_status;
862 const char *colander_comp_name;
863 struct bt_port *colander_in_port = NULL;
864 struct bt_component_class_sink_colander_data colander_data;
865
866 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
867 BT_ASSERT_PRE(bt_port_get_type(output_port) == BT_PORT_TYPE_OUTPUT,
868 "Port is not an output port: %!+p", output_port);
869 output_port_comp = bt_port_get_component(output_port);
870 BT_ASSERT_PRE(output_port_comp,
871 "Output port has no component: %!+p", output_port);
872 graph = bt_component_get_graph(output_port_comp);
873 BT_ASSERT(graph);
874
875 /* Create notification iterator */
876 BT_LOGD("Creating notification iterator on output port: "
877 "comp-addr=%p, comp-name\"%s\", port-addr=%p, port-name=\"%s\"",
878 output_port_comp, bt_component_get_name(output_port_comp),
879 output_port, bt_port_get_name(output_port));
880 iterator = g_new0(struct bt_notification_iterator_output_port, 1);
881 if (!iterator) {
882 BT_LOGE_STR("Failed to allocate one output port notification iterator.");
883 goto error;
884 }
885
886 init_notification_iterator((void *) iterator,
887 BT_NOTIFICATION_ITERATOR_TYPE_OUTPUT_PORT,
888 bt_output_port_notification_iterator_destroy);
889
890 /* Create colander component */
891 colander_comp_cls = bt_component_class_sink_colander_get();
892 if (!colander_comp_cls) {
893 BT_LOGW("Cannot get colander sink component class.");
894 goto error;
895 }
896
897 BT_MOVE(iterator->graph, graph);
898 colander_comp_name =
899 colander_component_name ? colander_component_name : "colander";
900 colander_data.notification = &iterator->notif;
901 graph_status = bt_graph_add_component_with_init_method_data(
902 iterator->graph, colander_comp_cls, colander_comp_name,
903 NULL, &colander_data, &iterator->colander);
904 if (graph_status != BT_GRAPH_STATUS_OK) {
905 BT_LOGW("Cannot add colander sink component to graph: "
906 "graph-addr=%p, name=\"%s\", graph-status=%s",
907 iterator->graph, colander_comp_name,
908 bt_graph_status_string(graph_status));
909 goto error;
910 }
911
912 /*
913 * Connect provided output port to the colander component's
914 * input port.
915 */
916 colander_in_port = bt_component_sink_get_input_port_by_index(
917 iterator->colander, 0);
918 BT_ASSERT(colander_in_port);
919 graph_status = bt_graph_connect_ports(iterator->graph,
920 output_port, colander_in_port, NULL);
921 if (graph_status != BT_GRAPH_STATUS_OK) {
922 BT_LOGW("Cannot add colander sink component to graph: "
923 "graph-addr=%p, name=\"%s\", graph-status=%s",
924 iterator->graph, colander_comp_name,
925 bt_graph_status_string(graph_status));
926 goto error;
927 }
928
929 /*
930 * At this point everything went fine. Make the graph
931 * nonconsumable forever so that only this notification iterator
932 * can consume (thanks to bt_graph_consume_sink_no_check()).
933 * This avoids leaking the notification created by the colander
934 * sink and moved to the notification iterator's notification
935 * member.
936 */
937 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
938 goto end;
939
940 error:
941 if (iterator && iterator->graph && iterator->colander) {
942 int ret;
943
944 /* Remove created colander component from graph if any */
945 colander_comp = iterator->colander;
946 BT_PUT(iterator->colander);
947
948 /*
949 * At this point the colander component's reference
950 * count is 0 because iterator->colander was the only
951 * owner. We also know that it is not connected because
952 * this is the last operation before this function
953 * succeeds.
954 *
955 * Since we honor the preconditions here,
956 * bt_graph_remove_unconnected_component() always
957 * succeeds.
958 */
959 ret = bt_graph_remove_unconnected_component(iterator->graph,
960 colander_comp);
961 BT_ASSERT(ret == 0);
962 }
963
964 BT_PUT(iterator);
965
966 end:
967 bt_put(colander_in_port);
968 bt_put(colander_comp_cls);
969 bt_put(output_port_comp);
970 bt_put(graph);
971 return (void *) iterator;
972 }
973
974 struct bt_notification_iterator *
975 bt_private_connection_notification_iterator_borrow_from_private(
976 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
977 {
978 return (void *) private_notification_iterator;
979 }
This page took 0.04898 seconds and 4 git commands to generate.