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