lib: graph: remove useless checks, make functions inline on fast path
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032
JG
1/*
2 * iterator.c
3 *
4 * Babeltrace Notification Iterator
5 *
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3230ee6b 7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
47e5a032
JG
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
5af447e5
PP
28#define BT_LOG_TAG "NOTIF-ITER"
29#include <babeltrace/lib-logging-internal.h>
30
3d9990ac 31#include <babeltrace/compiler-internal.h>
b8a06801 32#include <babeltrace/ref.h>
2ec84d26
PP
33#include <babeltrace/ctf-ir/fields.h>
34#include <babeltrace/ctf-ir/field-types.h>
35#include <babeltrace/ctf-ir/field-types-internal.h>
3230ee6b
PP
36#include <babeltrace/ctf-ir/event-internal.h>
37#include <babeltrace/ctf-ir/packet-internal.h>
38#include <babeltrace/ctf-ir/stream-internal.h>
73d5c1ad 39#include <babeltrace/graph/connection.h>
bd14d768 40#include <babeltrace/graph/connection-internal.h>
b2e0c907
PP
41#include <babeltrace/graph/component.h>
42#include <babeltrace/graph/component-source-internal.h>
43#include <babeltrace/graph/component-class-internal.h>
8ed535b5
PP
44#include <babeltrace/graph/component-class-sink-colander-internal.h>
45#include <babeltrace/graph/component-sink.h>
fa054faf 46#include <babeltrace/graph/notification.h>
b2e0c907
PP
47#include <babeltrace/graph/notification-iterator.h>
48#include <babeltrace/graph/notification-iterator-internal.h>
e7fa96c3 49#include <babeltrace/graph/notification-internal.h>
3230ee6b
PP
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>
2ec84d26 56#include <babeltrace/graph/notification-discarded-elements-internal.h>
3230ee6b 57#include <babeltrace/graph/port.h>
8ed535b5 58#include <babeltrace/graph/graph-internal.h>
c55a9f58 59#include <babeltrace/types.h>
f6ccaed9 60#include <babeltrace/assert-internal.h>
f42867e2 61#include <babeltrace/assert-pre-internal.h>
fa054faf 62#include <stdint.h>
2ec84d26 63#include <inttypes.h>
0fbb9a9f 64#include <stdlib.h>
3230ee6b 65
2ec84d26 66struct discarded_elements_state {
50842bdc 67 struct bt_clock_value *cur_begin;
2ec84d26
PP
68 uint64_t cur_count;
69};
70
3230ee6b 71struct stream_state {
50842bdc
PP
72 struct bt_stream *stream; /* owned by this */
73 struct bt_packet *cur_packet; /* owned by this */
2ec84d26
PP
74 struct discarded_elements_state discarded_packets_state;
75 struct discarded_elements_state discarded_events_state;
f42867e2 76 uint64_t expected_notif_seq_num;
c55a9f58 77 bt_bool is_ended;
3230ee6b
PP
78};
79
3230ee6b 80static
3dca2276 81void stream_destroy_listener(struct bt_stream_common *stream, void *data)
3230ee6b 82{
90157d89 83 struct bt_notification_iterator_private_connection *iterator = data;
3230ee6b
PP
84
85 /* Remove associated stream state */
86 g_hash_table_remove(iterator->stream_states, stream);
87}
88
89static
90void destroy_stream_state(struct stream_state *stream_state)
91{
92 if (!stream_state) {
93 return;
94 }
95
5af447e5
PP
96 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state);
97 BT_LOGV_STR("Putting stream state's current packet.");
3230ee6b 98 bt_put(stream_state->cur_packet);
5af447e5 99 BT_LOGV_STR("Putting stream state's stream.");
3230ee6b 100 bt_put(stream_state->stream);
2ec84d26
PP
101 bt_put(stream_state->discarded_packets_state.cur_begin);
102 bt_put(stream_state->discarded_events_state.cur_begin);
3230ee6b
PP
103 g_free(stream_state);
104}
105
3230ee6b 106static
50842bdc 107struct stream_state *create_stream_state(struct bt_stream *stream)
3230ee6b
PP
108{
109 struct stream_state *stream_state = g_new0(struct stream_state, 1);
110
111 if (!stream_state) {
5af447e5 112 BT_LOGE_STR("Failed to allocate one stream state.");
3230ee6b
PP
113 goto end;
114 }
115
126215b4
MD
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
f42867e2 121 * the sequence number 0.
126215b4
MD
122 */
123 stream_state->discarded_packets_state.cur_count = -1ULL;
124
3230ee6b 125 /*
f42867e2 126 * We keep a reference to the stream until we know it's ended.
3230ee6b
PP
127 */
128 stream_state->stream = bt_get(stream);
5af447e5
PP
129 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
130 "stream-state-addr=%p",
50842bdc 131 stream, bt_stream_get_name(stream), stream_state);
3230ee6b
PP
132
133end:
134 return stream_state;
135}
47e5a032 136
8ed535b5
PP
137static
138void destroy_base_notification_iterator(struct bt_object *obj)
139{
07245ac2
PP
140 BT_ASSERT(obj);
141 g_free(obj);
8ed535b5
PP
142}
143
47e5a032 144static
90157d89 145void bt_private_connection_notification_iterator_destroy(struct bt_object *obj)
47e5a032 146{
90157d89 147 struct bt_notification_iterator_private_connection *iterator;
8738a040 148
f6ccaed9 149 BT_ASSERT(obj);
d3eb6e8f 150
bd14d768
PP
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 */
3fea54f6 161 obj->ref_count++;
07245ac2 162 iterator = (void *) obj;
8ed535b5 163 BT_LOGD("Destroying private connection notification iterator object: addr=%p",
5af447e5 164 iterator);
90157d89 165 bt_private_connection_notification_iterator_finalize(iterator);
d3eb6e8f 166
3230ee6b
PP
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)) {
f6ccaed9 180 BT_ASSERT(stream_gptr);
5af447e5
PP
181
182 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
3dca2276 183 bt_stream_common_remove_destroy_listener(
3230ee6b
PP
184 (void *) stream_gptr, stream_destroy_listener,
185 iterator);
186 }
187
188 g_hash_table_destroy(iterator->stream_states);
189 }
190
bd14d768
PP
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
8ed535b5 200 destroy_base_notification_iterator(obj);
47e5a032
JG
201}
202
bd14d768 203BT_HIDDEN
90157d89
PP
204void bt_private_connection_notification_iterator_finalize(
205 struct bt_notification_iterator_private_connection *iterator)
bd14d768
PP
206{
207 struct bt_component_class *comp_class = NULL;
208 bt_component_class_notification_iterator_finalize_method
209 finalize_method = NULL;
210
f6ccaed9 211 BT_ASSERT(iterator);
bd14d768
PP
212
213 switch (iterator->state) {
90157d89 214 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED:
088d4023
PP
215 /* Skip user finalization if user initialization failed */
216 BT_LOGD("Not finalizing non-initialized notification iterator: "
217 "addr=%p", iterator);
218 return;
90157d89
PP
219 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED:
220 case BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED:
bd14d768 221 /* Already finalized */
5af447e5
PP
222 BT_LOGD("Not finalizing notification iterator: already finalized: "
223 "addr=%p", iterator);
bd14d768
PP
224 return;
225 default:
226 break;
227 }
228
5af447e5
PP
229 BT_LOGD("Finalizing notification iterator: addr=%p", iterator);
230
90157d89 231 if (iterator->state == BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_ENDED) {
5af447e5 232 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
233 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
234 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED;
df14f8af 235 } else {
5af447e5 236 BT_LOGD("Updating notification iterator's state: "
90157d89
PP
237 "new-state=BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED");
238 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_FINALIZED;
df14f8af
MD
239 }
240
f6ccaed9 241 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
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 */
0fbb9a9f 264 abort();
bd14d768
PP
265 }
266
267 if (finalize_method) {
5af447e5
PP
268 BT_LOGD("Calling user's finalization method: addr=%p",
269 iterator);
bd14d768 270 finalize_method(
90157d89 271 bt_private_connection_private_notification_iterator_from_notification_iterator(iterator));
bd14d768
PP
272 }
273
bd14d768
PP
274 iterator->upstream_component = NULL;
275 iterator->upstream_port = NULL;
5af447e5 276 BT_LOGD("Finalized notification iterator: addr=%p", iterator);
bd14d768
PP
277}
278
279BT_HIDDEN
90157d89
PP
280void bt_private_connection_notification_iterator_set_connection(
281 struct bt_notification_iterator_private_connection *iterator,
bd14d768
PP
282 struct bt_connection *connection)
283{
f6ccaed9 284 BT_ASSERT(iterator);
bd14d768 285 iterator->connection = connection;
5af447e5
PP
286 BT_LOGV("Set notification iterator's connection: "
287 "iter-addr=%p, conn-addr=%p", iterator, connection);
bd14d768
PP
288}
289
90157d89
PP
290static
291void init_notification_iterator(struct bt_notification_iterator *iterator,
292 enum bt_notification_iterator_type type,
293 bt_object_release_func destroy)
294{
3fea54f6 295 bt_object_init_shared(&iterator->base, destroy);
90157d89
PP
296 iterator->type = type;
297}
298
47e5a032 299BT_HIDDEN
90157d89 300enum bt_connection_status bt_private_connection_notification_iterator_create(
3230ee6b 301 struct bt_component *upstream_comp,
fa054faf 302 struct bt_port *upstream_port,
73d5c1ad 303 struct bt_connection *connection,
90157d89 304 struct bt_notification_iterator_private_connection **user_iterator)
47e5a032 305{
73d5c1ad 306 enum bt_connection_status status = BT_CONNECTION_STATUS_OK;
d3e4dcd8 307 enum bt_component_class_type type;
90157d89 308 struct bt_notification_iterator_private_connection *iterator = NULL;
47e5a032 309
f6ccaed9
PP
310 BT_ASSERT(upstream_comp);
311 BT_ASSERT(upstream_port);
f6ccaed9
PP
312 BT_ASSERT(bt_port_is_connected(upstream_port));
313 BT_ASSERT(user_iterator);
8ed535b5 314 BT_LOGD("Creating notification iterator on private connection: "
5af447e5
PP
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);
3230ee6b 321 type = bt_component_get_class_type(upstream_comp);
f6ccaed9 322 BT_ASSERT(type == BT_COMPONENT_CLASS_TYPE_SOURCE ||
ef2f7566 323 type == BT_COMPONENT_CLASS_TYPE_FILTER);
90157d89 324 iterator = g_new0(struct bt_notification_iterator_private_connection, 1);
47e5a032 325 if (!iterator) {
8ed535b5 326 BT_LOGE_STR("Failed to allocate one private connection notification iterator.");
73d5c1ad
PP
327 status = BT_CONNECTION_STATUS_NOMEM;
328 goto end;
47e5a032
JG
329 }
330
90157d89
PP
331 init_notification_iterator((void *) iterator,
332 BT_NOTIFICATION_ITERATOR_TYPE_PRIVATE_CONNECTION,
333 bt_private_connection_notification_iterator_destroy);
3230ee6b
PP
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) {
5af447e5 338 BT_LOGE_STR("Failed to allocate a GHashTable.");
73d5c1ad
PP
339 status = BT_CONNECTION_STATUS_NOMEM;
340 goto end;
3230ee6b
PP
341 }
342
bd14d768
PP
343 iterator->upstream_component = upstream_comp;
344 iterator->upstream_port = upstream_port;
345 iterator->connection = connection;
5c563278 346 iterator->graph = bt_component_borrow_graph(upstream_comp);
90157d89 347 iterator->state = BT_PRIVATE_CONNECTION_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED;
5af447e5
PP
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);
1a6a376a
PP
355
356 /* Move reference to user */
357 *user_iterator = iterator;
358 iterator = NULL;
3230ee6b 359
47e5a032 360end:
73d5c1ad
PP
361 bt_put(iterator);
362 return status;
47e5a032
JG
363}
364
90157d89
PP
365void *bt_private_connection_private_notification_iterator_get_user_data(
366 struct bt_private_connection_private_notification_iterator *private_iterator)
ea8d3e58 367{
5c563278 368 struct bt_notification_iterator_private_connection *iterator = (void *)
6d137876 369 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
890882ef 370
f42867e2
PP
371 BT_ASSERT_PRE_NON_NULL(private_iterator, "Notification iterator");
372 return iterator->user_data;
ea8d3e58
JG
373}
374
375enum bt_notification_iterator_status
90157d89
PP
376bt_private_connection_private_notification_iterator_set_user_data(
377 struct bt_private_connection_private_notification_iterator *private_iterator,
890882ef 378 void *data)
ea8d3e58 379{
5c563278 380 struct bt_notification_iterator_private_connection *iterator = (void *)
6d137876 381 bt_private_connection_notification_iterator_borrow_from_private(private_iterator);
ea8d3e58 382
f42867e2 383 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
ea8d3e58 384 iterator->user_data = data;
5af447e5
PP
385 BT_LOGV("Set notification iterator's user data: "
386 "iter-addr=%p, user-data-addr=%p", iterator, data);
f42867e2 387 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
8738a040 388}
413bc2c4 389
5c563278
PP
390struct 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
f42867e2
PP
401BT_ASSERT_PRE_FUNC
402static inline
403void bt_notification_borrow_packet_stream(struct bt_notification *notif,
404 struct bt_stream **stream, struct bt_packet **packet)
fa054faf 405{
f42867e2 406 BT_ASSERT(notif);
fa054faf 407
f42867e2 408 switch (notif->type) {
fa054faf 409 case BT_NOTIFICATION_TYPE_EVENT:
f42867e2
PP
410 *packet = bt_event_borrow_packet(
411 bt_notification_event_borrow_event(notif));
412 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
413 break;
414 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
f42867e2 415 *stream = bt_notification_stream_begin_borrow_stream(notif);
fa054faf
PP
416 break;
417 case BT_NOTIFICATION_TYPE_STREAM_END:
f42867e2 418 *stream = bt_notification_stream_end_borrow_stream(notif);
fa054faf
PP
419 break;
420 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
f42867e2
PP
421 *packet = bt_notification_packet_begin_borrow_packet(notif);
422 *stream = bt_packet_borrow_stream(*packet);
fa054faf
PP
423 break;
424 case BT_NOTIFICATION_TYPE_PACKET_END:
f42867e2
PP
425 *packet = bt_notification_packet_end_borrow_packet(notif);
426 *stream = bt_packet_borrow_stream(*packet);
2ec84d26 427 break;
fa054faf 428 default:
f42867e2 429 break;
fa054faf 430 }
fa054faf
PP
431}
432
f42867e2
PP
433BT_ASSERT_PRE_FUNC
434static inline
435bool validate_notification(
90157d89 436 struct bt_notification_iterator_private_connection *iterator,
f42867e2 437 struct bt_notification *notif)
3230ee6b 438{
f42867e2 439 bool is_valid = true;
3230ee6b 440 struct stream_state *stream_state;
f42867e2
PP
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 }
3230ee6b 451
f42867e2
PP
452 stream_state = g_hash_table_lookup(iterator->stream_states, stream);
453 if (!stream_state) {
3230ee6b 454 /*
f42867e2
PP
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.
3230ee6b 458 */
f42867e2
PP
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;
3230ee6b
PP
465 goto end;
466 }
467
f42867e2
PP
468 if (notif->seq_num == -1ULL) {
469 notif->seq_num = 0;
3230ee6b
PP
470 }
471
f42867e2
PP
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;
3230ee6b 480 goto end;
3230ee6b 481 }
3230ee6b 482
f42867e2
PP
483 stream_state = create_stream_state(stream);
484 if (!stream_state) {
485 abort();
486 }
fa054faf 487
f42867e2
PP
488 g_hash_table_insert(iterator->stream_states, stream,
489 stream_state);
490 stream_state->expected_notif_seq_num++;
491 goto end;
fa054faf
PP
492 }
493
f42867e2
PP
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;
fa054faf
PP
505 goto end;
506 }
507
f42867e2
PP
508 if (notif->seq_num == -1ULL) {
509 notif->seq_num = stream_state->expected_notif_seq_num;
3230ee6b
PP
510 }
511
f42867e2
PP
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;
fa054faf
PP
520 goto end;
521 }
522
f42867e2
PP
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;
3230ee6b
PP
589 }
590
3230ee6b 591end:
f42867e2 592 return is_valid;
3230ee6b
PP
593}
594
f42867e2
PP
595BT_ASSERT_PRE_FUNC
596static inline bool priv_conn_notif_iter_can_end(
597 struct bt_notification_iterator_private_connection *iterator)
3230ee6b 598{
f42867e2
PP
599 GHashTableIter iter;
600 gpointer stream_key, state_value;
601 bool ret = true;
3230ee6b 602
f42867e2
PP
603 /*
604 * Verify that this iterator received a
605 * BT_NOTIFICATION_TYPE_STREAM_END notification for each stream
606 * which has a state.
607 */
3230ee6b 608
f42867e2 609 g_hash_table_iter_init(&iter, iterator->stream_states);
3230ee6b 610
f42867e2
PP
611 while (g_hash_table_iter_next(&iter, &stream_key, &state_value)) {
612 struct stream_state *stream_state = (void *) state_value;
3230ee6b 613
f42867e2
PP
614 BT_ASSERT(stream_state);
615 BT_ASSERT(stream_key);
fa054faf 616
f42867e2
PP
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 }
3230ee6b
PP
624 }
625
3230ee6b 626end:
3230ee6b
PP
627 return ret;
628}
629
f42867e2 630enum bt_notification_iterator_status
07245ac2
PP
631bt_private_connection_notification_iterator_next(
632 struct bt_notification_iterator *user_iterator,
633 struct bt_notification **user_notif)
3230ee6b 634{
07245ac2
PP
635 struct bt_notification_iterator_private_connection *iterator =
636 (void *) user_iterator;
f42867e2
PP
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,
3230ee6b 643 };
f42867e2
PP
644 enum bt_notification_iterator_status status =
645 BT_NOTIFICATION_ITERATOR_STATUS_OK;
3230ee6b 646
07245ac2
PP
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",
f42867e2
PP
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);
d3eb6e8f 661
3230ee6b
PP
662 /* Pick the appropriate "next" method */
663 switch (iterator->upstream_component->class->type) {
d3eb6e8f
PP
664 case BT_COMPONENT_CLASS_TYPE_SOURCE:
665 {
666 struct bt_component_class_source *source_class =
3230ee6b 667 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
668 struct bt_component_class_source, parent);
669
f6ccaed9 670 BT_ASSERT(source_class->methods.iterator.next);
d3eb6e8f
PP
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 =
3230ee6b 677 container_of(iterator->upstream_component->class,
d3eb6e8f
PP
678 struct bt_component_class_filter, parent);
679
f6ccaed9 680 BT_ASSERT(filter_class->methods.iterator.next);
d3eb6e8f
PP
681 next_method = filter_class->methods.iterator.next;
682 break;
683 }
684 default:
0fbb9a9f 685 abort();
d3eb6e8f
PP
686 }
687
3230ee6b
PP
688 /*
689 * Call the user's "next" method to get the next notification
fa054faf 690 * and status.
3230ee6b 691 */
f6ccaed9 692 BT_ASSERT(next_method);
f42867e2
PP
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 }
3230ee6b 702
f42867e2
PP
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);
3230ee6b
PP
721 }
722
f42867e2
PP
723 status = BT_NOTIFICATION_ITERATOR_STATUS_CANCELED;
724 goto end;
725 }
8cf27cc5 726
f42867e2
PP
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);
07245ac2 751 *user_notif = next_return.notification;
f42867e2
PP
752 break;
753 default:
754 /* Unknown non-error status */
755 abort();
41a2b7ae
PP
756 }
757
758end:
3230ee6b
PP
759 return status;
760}
761
762enum bt_notification_iterator_status
07245ac2
PP
763bt_output_port_notification_iterator_next(
764 struct bt_notification_iterator *iterator,
765 struct bt_notification **user_notif)
3230ee6b
PP
766{
767 enum bt_notification_iterator_status status;
07245ac2
PP
768 struct bt_notification_iterator_output_port *out_port_iter =
769 (void *) iterator;
770 enum bt_graph_status graph_status;
3230ee6b 771
f42867e2 772 BT_ASSERT_PRE_NON_NULL(iterator, "Notification iterator");
07245ac2
PP
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;
8ed535b5 786 break;
07245ac2
PP
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;
90157d89 804 break;
90157d89 805 default:
07245ac2
PP
806 /* Other errors */
807 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
90157d89 808 }
3230ee6b 809
3230ee6b 810 return status;
53d45b87
JG
811}
812
90157d89 813struct bt_component *bt_private_connection_notification_iterator_get_component(
413bc2c4
JG
814 struct bt_notification_iterator *iterator)
815{
90157d89
PP
816 struct bt_notification_iterator_private_connection *iter_priv_conn;
817
f42867e2
PP
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);
90157d89 823 iter_priv_conn = (void *) iterator;
f42867e2 824 return bt_get(iter_priv_conn->upstream_component);
413bc2c4
JG
825}
826
91457551 827struct bt_private_component *
90157d89
PP
828bt_private_connection_private_notification_iterator_get_private_component(
829 struct bt_private_connection_private_notification_iterator *private_iterator)
91457551
PP
830{
831 return bt_private_component_from_component(
90157d89 832 bt_private_connection_notification_iterator_get_component(
6d137876 833 (void *) bt_private_connection_notification_iterator_borrow_from_private(private_iterator)));
91457551 834}
8ed535b5
PP
835
836static
837void 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);
07245ac2 844 BT_ASSERT(!iterator->notif);
8ed535b5
PP
845 BT_LOGD_STR("Putting graph.");
846 bt_put(iterator->graph);
8ed535b5
PP
847 BT_LOGD_STR("Putting colander sink component.");
848 bt_put(iterator->colander);
849 destroy_base_notification_iterator(obj);
850}
851
852struct bt_notification_iterator *bt_output_port_notification_iterator_create(
853 struct bt_port *output_port,
f42867e2 854 const char *colander_component_name)
8ed535b5 855{
8ed535b5
PP
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
f42867e2
PP
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);
8ed535b5 869 output_port_comp = bt_port_get_component(output_port);
f42867e2
PP
870 BT_ASSERT_PRE(output_port_comp,
871 "Output port has no component: %!+p", output_port);
8ed535b5 872 graph = bt_component_get_graph(output_port_comp);
f6ccaed9 873 BT_ASSERT(graph);
8ed535b5
PP
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);
8ed535b5
PP
898 colander_comp_name =
899 colander_component_name ? colander_component_name : "colander";
07245ac2 900 colander_data.notification = &iterator->notif;
8ed535b5
PP
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);
f6ccaed9 918 BT_ASSERT(colander_in_port);
8ed535b5
PP
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
07245ac2
PP
934 * sink and moved to the notification iterator's notification
935 * member.
8ed535b5
PP
936 */
937 bt_graph_set_can_consume(iterator->graph, BT_FALSE);
938 goto end;
939
940error:
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);
f6ccaed9 961 BT_ASSERT(ret == 0);
8ed535b5
PP
962 }
963
964 BT_PUT(iterator);
965
966end:
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}
25b68514
PP
973
974struct bt_notification_iterator *
5c563278 975bt_private_connection_notification_iterator_borrow_from_private(
25b68514
PP
976 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
977{
5c563278 978 return (void *) private_notification_iterator;
25b68514 979}
This page took 0.095784 seconds and 4 git commands to generate.