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