4 * Babeltrace Notification Iterator
6 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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
28 #define BT_LOG_TAG "NOTIF-ITER"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/compiler-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf-ir/fields.h>
34 #include <babeltrace/ctf-ir/field-types.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/packet-internal.h>
38 #include <babeltrace/ctf-ir/stream-internal.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/connection-internal.h>
41 #include <babeltrace/graph/component.h>
42 #include <babeltrace/graph/component-source-internal.h>
43 #include <babeltrace/graph/component-class-internal.h>
44 #include <babeltrace/graph/notification.h>
45 #include <babeltrace/graph/notification-iterator.h>
46 #include <babeltrace/graph/notification-iterator-internal.h>
47 #include <babeltrace/graph/notification-internal.h>
48 #include <babeltrace/graph/notification-event.h>
49 #include <babeltrace/graph/notification-event-internal.h>
50 #include <babeltrace/graph/notification-packet.h>
51 #include <babeltrace/graph/notification-packet-internal.h>
52 #include <babeltrace/graph/notification-stream.h>
53 #include <babeltrace/graph/notification-stream-internal.h>
54 #include <babeltrace/graph/notification-discarded-elements-internal.h>
55 #include <babeltrace/graph/port.h>
56 #include <babeltrace/types.h>
61 struct discarded_elements_state
{
62 struct bt_ctf_clock_value
*cur_begin
;
67 struct bt_ctf_stream
*stream
; /* owned by this */
68 struct bt_ctf_packet
*cur_packet
; /* owned by this */
69 struct discarded_elements_state discarded_packets_state
;
70 struct discarded_elements_state discarded_events_state
;
75 ACTION_TYPE_PUSH_NOTIF
,
76 ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
,
77 ACTION_TYPE_ADD_STREAM_STATE
,
78 ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
,
79 ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
,
80 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
,
81 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
,
85 enum action_type type
;
87 /* ACTION_TYPE_PUSH_NOTIF */
89 struct bt_notification
*notif
; /* owned by this */
92 /* ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM */
94 struct bt_ctf_stream
*stream
; /* owned by this */
95 struct bt_component
*component
; /* owned by this */
96 struct bt_port
*port
; /* owned by this */
97 } map_port_to_comp_in_stream
;
99 /* ACTION_TYPE_ADD_STREAM_STATE */
101 struct bt_ctf_stream
*stream
; /* owned by this */
102 struct stream_state
*stream_state
; /* owned by this */
105 /* ACTION_TYPE_SET_STREAM_STATE_IS_ENDED */
107 struct stream_state
*stream_state
; /* weak */
108 } set_stream_state_is_ended
;
110 /* ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET */
112 struct stream_state
*stream_state
; /* weak */
113 struct bt_ctf_packet
*packet
; /* owned by this */
114 } set_stream_state_cur_packet
;
116 /* ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS */
117 /* ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS */
119 struct stream_state
*stream_state
; /* weak */
120 struct bt_ctf_clock_value
*cur_begin
; /* owned by this */
122 } update_stream_state_discarded_elements
;
127 void stream_destroy_listener(struct bt_ctf_stream
*stream
, void *data
)
129 struct bt_notification_iterator
*iterator
= data
;
131 /* Remove associated stream state */
132 g_hash_table_remove(iterator
->stream_states
, stream
);
136 void destroy_stream_state(struct stream_state
*stream_state
)
142 BT_LOGV("Destroying stream state: stream-state-addr=%p", stream_state
);
143 BT_LOGV_STR("Putting stream state's current packet.");
144 bt_put(stream_state
->cur_packet
);
145 BT_LOGV_STR("Putting stream state's stream.");
146 bt_put(stream_state
->stream
);
147 bt_put(stream_state
->discarded_packets_state
.cur_begin
);
148 bt_put(stream_state
->discarded_events_state
.cur_begin
);
149 g_free(stream_state
);
153 void destroy_action(struct action
*action
)
157 switch (action
->type
) {
158 case ACTION_TYPE_PUSH_NOTIF
:
159 BT_PUT(action
->payload
.push_notif
.notif
);
161 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
:
162 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.stream
);
163 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.component
);
164 BT_PUT(action
->payload
.map_port_to_comp_in_stream
.port
);
166 case ACTION_TYPE_ADD_STREAM_STATE
:
167 BT_PUT(action
->payload
.add_stream_state
.stream
);
168 destroy_stream_state(
169 action
->payload
.add_stream_state
.stream_state
);
170 action
->payload
.add_stream_state
.stream_state
= NULL
;
172 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
:
173 BT_PUT(action
->payload
.set_stream_state_cur_packet
.packet
);
175 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
:
177 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
:
178 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
:
179 BT_PUT(action
->payload
.update_stream_state_discarded_elements
.cur_begin
);
182 BT_LOGF("Unexpected action's type: type=%d", action
->type
);
188 void add_action(struct bt_notification_iterator
*iterator
,
189 struct action
*action
)
191 g_array_append_val(iterator
->actions
, *action
);
195 void clear_actions(struct bt_notification_iterator
*iterator
)
199 for (i
= 0; i
< iterator
->actions
->len
; i
++) {
200 struct action
*action
= &g_array_index(iterator
->actions
,
203 destroy_action(action
);
206 g_array_set_size(iterator
->actions
, 0);
210 const char *action_type_string(enum action_type type
)
213 case ACTION_TYPE_PUSH_NOTIF
:
214 return "ACTION_TYPE_PUSH_NOTIF";
215 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
:
216 return "ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM";
217 case ACTION_TYPE_ADD_STREAM_STATE
:
218 return "ACTION_TYPE_ADD_STREAM_STATE";
219 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
:
220 return "ACTION_TYPE_SET_STREAM_STATE_IS_ENDED";
221 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
:
222 return "ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET";
223 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
:
224 return "ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS";
225 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
:
226 return "ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS";
233 void apply_actions(struct bt_notification_iterator
*iterator
)
237 BT_LOGV("Applying notification's iterator current actions: "
238 "count=%u", iterator
->actions
->len
);
240 for (i
= 0; i
< iterator
->actions
->len
; i
++) {
241 struct action
*action
= &g_array_index(iterator
->actions
,
244 BT_LOGV("Applying action: index=%zu, type=%s",
245 i
, action_type_string(action
->type
));
247 switch (action
->type
) {
248 case ACTION_TYPE_PUSH_NOTIF
:
249 /* Move notification to queue */
250 g_queue_push_head(iterator
->queue
,
251 action
->payload
.push_notif
.notif
);
252 bt_notification_freeze(
253 action
->payload
.push_notif
.notif
);
254 action
->payload
.push_notif
.notif
= NULL
;
256 case ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
:
257 bt_ctf_stream_map_component_to_port(
258 action
->payload
.map_port_to_comp_in_stream
.stream
,
259 action
->payload
.map_port_to_comp_in_stream
.component
,
260 action
->payload
.map_port_to_comp_in_stream
.port
);
262 case ACTION_TYPE_ADD_STREAM_STATE
:
263 /* Move stream state to hash table */
264 g_hash_table_insert(iterator
->stream_states
,
265 action
->payload
.add_stream_state
.stream
,
266 action
->payload
.add_stream_state
.stream_state
);
268 action
->payload
.add_stream_state
.stream_state
= NULL
;
270 case ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
:
272 * We know that this stream is ended. We need to
273 * remember this as long as the stream exists to
274 * enforce that the same stream does not end
277 * Here we add a destroy listener to the stream
278 * which we put after (becomes weak as the hash
279 * table key). If we were the last object to own
280 * this stream, the destroy listener is called
281 * when we call bt_put() which removes this
282 * stream state completely. This is important
283 * because the memory used by this stream object
284 * could be reused for another stream, and they
285 * must have different states.
287 bt_ctf_stream_add_destroy_listener(
288 action
->payload
.set_stream_state_is_ended
.stream_state
->stream
,
289 stream_destroy_listener
, iterator
);
290 action
->payload
.set_stream_state_is_ended
.stream_state
->is_ended
= BT_TRUE
;
291 BT_PUT(action
->payload
.set_stream_state_is_ended
.stream_state
->stream
);
293 case ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
:
294 /* Move packet to stream state's current packet */
295 BT_MOVE(action
->payload
.set_stream_state_cur_packet
.stream_state
->cur_packet
,
296 action
->payload
.set_stream_state_cur_packet
.packet
);
298 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
:
299 case ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
:
301 struct discarded_elements_state
*state
;
303 if (action
->type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
) {
304 state
= &action
->payload
.update_stream_state_discarded_elements
.stream_state
->discarded_packets_state
;
306 state
= &action
->payload
.update_stream_state_discarded_elements
.stream_state
->discarded_events_state
;
309 BT_MOVE(state
->cur_begin
,
310 action
->payload
.update_stream_state_discarded_elements
.cur_begin
);
311 state
->cur_count
= action
->payload
.update_stream_state_discarded_elements
.cur_count
;
315 BT_LOGF("Unexpected action's type: type=%d",
321 clear_actions(iterator
);
325 struct stream_state
*create_stream_state(struct bt_ctf_stream
*stream
)
327 struct stream_state
*stream_state
= g_new0(struct stream_state
, 1);
330 BT_LOGE_STR("Failed to allocate one stream state.");
335 * We keep a reference to the stream until we know it's ended
336 * because we need to be able to create an automatic "stream
337 * end" notification when the user's "next" method returns
338 * BT_NOTIFICATION_ITERATOR_STATUS_END.
340 * We put this reference when the stream is marked as ended.
342 stream_state
->stream
= bt_get(stream
);
343 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
344 "stream-state-addr=%p",
345 stream
, bt_ctf_stream_get_name(stream
), stream_state
);
352 void bt_notification_iterator_destroy(struct bt_object
*obj
)
354 struct bt_notification_iterator
*iterator
;
359 * The notification iterator's reference count is 0 if we're
360 * here. Increment it to avoid a double-destroy (possibly
361 * infinitely recursive). This could happen for example if the
362 * notification iterator's finalization function does bt_get()
363 * (or anything that causes bt_get() to be called) on itself
364 * (ref. count goes from 0 to 1), and then bt_put(): the
365 * reference count would go from 1 to 0 again and this function
366 * would be called again.
368 obj
->ref_count
.count
++;
369 iterator
= container_of(obj
, struct bt_notification_iterator
, base
);
370 BT_LOGD("Destroying notification iterator object: addr=%p",
372 bt_notification_iterator_finalize(iterator
);
374 if (iterator
->queue
) {
375 struct bt_notification
*notif
;
377 BT_LOGD("Putting notifications in queue.");
379 while ((notif
= g_queue_pop_tail(iterator
->queue
))) {
383 g_queue_free(iterator
->queue
);
386 if (iterator
->stream_states
) {
388 * Remove our destroy listener from each stream which
389 * has a state in this iterator. Otherwise the destroy
390 * listener would be called with an invalid/other
391 * notification iterator object.
393 GHashTableIter ht_iter
;
394 gpointer stream_gptr
, stream_state_gptr
;
396 g_hash_table_iter_init(&ht_iter
, iterator
->stream_states
);
398 while (g_hash_table_iter_next(&ht_iter
, &stream_gptr
, &stream_state_gptr
)) {
401 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
402 bt_ctf_stream_remove_destroy_listener(
403 (void *) stream_gptr
, stream_destroy_listener
,
407 g_hash_table_destroy(iterator
->stream_states
);
410 if (iterator
->actions
) {
411 g_array_free(iterator
->actions
, TRUE
);
414 if (iterator
->connection
) {
416 * Remove ourself from the originating connection so
417 * that it does not try to finalize a dangling pointer
420 bt_connection_remove_iterator(iterator
->connection
, iterator
);
423 BT_LOGD_STR("Putting current notification.");
424 bt_put(iterator
->current_notification
);
429 void bt_notification_iterator_finalize(
430 struct bt_notification_iterator
*iterator
)
432 struct bt_component_class
*comp_class
= NULL
;
433 bt_component_class_notification_iterator_finalize_method
434 finalize_method
= NULL
;
438 switch (iterator
->state
) {
439 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
440 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
441 /* Already finalized */
442 BT_LOGD("Not finalizing notification iterator: already finalized: "
443 "addr=%p", iterator
);
449 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
451 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_ENDED
) {
452 BT_LOGD("Updating notification iterator's state: "
453 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
454 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
456 BT_LOGD("Updating notification iterator's state: "
457 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED");
458 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
461 assert(iterator
->upstream_component
);
462 comp_class
= iterator
->upstream_component
->class;
464 /* Call user-defined destroy method */
465 switch (comp_class
->type
) {
466 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
468 struct bt_component_class_source
*source_class
;
470 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
471 finalize_method
= source_class
->methods
.iterator
.finalize
;
474 case BT_COMPONENT_CLASS_TYPE_FILTER
:
476 struct bt_component_class_filter
*filter_class
;
478 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
479 finalize_method
= filter_class
->methods
.iterator
.finalize
;
487 if (finalize_method
) {
488 BT_LOGD("Calling user's finalization method: addr=%p",
491 bt_private_notification_iterator_from_notification_iterator(iterator
));
494 iterator
->upstream_component
= NULL
;
495 iterator
->upstream_port
= NULL
;
496 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
500 void bt_notification_iterator_set_connection(
501 struct bt_notification_iterator
*iterator
,
502 struct bt_connection
*connection
)
505 iterator
->connection
= connection
;
506 BT_LOGV("Set notification iterator's connection: "
507 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
511 int create_subscription_mask_from_notification_types(
512 struct bt_notification_iterator
*iterator
,
513 const enum bt_notification_type
*notif_types
)
515 const enum bt_notification_type
*notif_type
;
519 iterator
->subscription_mask
= 0;
521 for (notif_type
= notif_types
;
522 *notif_type
!= BT_NOTIFICATION_TYPE_SENTINEL
;
524 switch (*notif_type
) {
525 case BT_NOTIFICATION_TYPE_ALL
:
526 iterator
->subscription_mask
|=
527 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
|
528 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
|
529 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
|
530 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
|
531 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
|
532 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
|
533 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
|
534 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
536 case BT_NOTIFICATION_TYPE_EVENT
:
537 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
539 case BT_NOTIFICATION_TYPE_INACTIVITY
:
540 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
542 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
543 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
545 case BT_NOTIFICATION_TYPE_STREAM_END
:
546 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
548 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
549 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
551 case BT_NOTIFICATION_TYPE_PACKET_END
:
552 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
554 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
555 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
;
557 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
558 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
565 BT_LOGV("Added notification type to subscription mask: "
567 bt_notification_type_string(*notif_type
),
568 iterator
->subscription_mask
);
576 enum bt_connection_status
bt_notification_iterator_create(
577 struct bt_component
*upstream_comp
,
578 struct bt_port
*upstream_port
,
579 const enum bt_notification_type
*notification_types
,
580 struct bt_connection
*connection
,
581 struct bt_notification_iterator
**user_iterator
)
583 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
584 enum bt_component_class_type type
;
585 struct bt_notification_iterator
*iterator
= NULL
;
587 assert(upstream_comp
);
588 assert(upstream_port
);
589 assert(notification_types
);
590 assert(bt_port_is_connected(upstream_port
));
591 assert(user_iterator
);
592 BT_LOGD("Creating notification iterator: "
593 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
594 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
596 upstream_comp
, bt_component_get_name(upstream_comp
),
597 upstream_port
, bt_port_get_name(upstream_port
),
599 type
= bt_component_get_class_type(upstream_comp
);
600 assert(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
601 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
602 iterator
= g_new0(struct bt_notification_iterator
, 1);
604 BT_LOGE_STR("Failed to allocate one notification iterator.");
605 status
= BT_CONNECTION_STATUS_NOMEM
;
609 bt_object_init(iterator
, bt_notification_iterator_destroy
);
611 if (create_subscription_mask_from_notification_types(iterator
,
612 notification_types
)) {
613 BT_LOGW_STR("Cannot create subscription mask from notification types.");
614 status
= BT_CONNECTION_STATUS_INVALID
;
618 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
619 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
620 if (!iterator
->stream_states
) {
621 BT_LOGE_STR("Failed to allocate a GHashTable.");
622 status
= BT_CONNECTION_STATUS_NOMEM
;
626 iterator
->queue
= g_queue_new();
627 if (!iterator
->queue
) {
628 BT_LOGE_STR("Failed to allocate a GQueue.");
629 status
= BT_CONNECTION_STATUS_NOMEM
;
633 iterator
->actions
= g_array_new(FALSE
, FALSE
, sizeof(struct action
));
634 if (!iterator
->actions
) {
635 BT_LOGE_STR("Failed to allocate a GArray.");
636 status
= BT_CONNECTION_STATUS_NOMEM
;
640 iterator
->upstream_component
= upstream_comp
;
641 iterator
->upstream_port
= upstream_port
;
642 iterator
->connection
= connection
;
643 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_ACTIVE
;
644 BT_LOGD("Created notification iterator: "
645 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
646 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
647 "conn-addr=%p, iter-addr=%p",
648 upstream_comp
, bt_component_get_name(upstream_comp
),
649 upstream_port
, bt_port_get_name(upstream_port
),
650 connection
, iterator
);
652 /* Move reference to user */
653 *user_iterator
= iterator
;
661 void *bt_private_notification_iterator_get_user_data(
662 struct bt_private_notification_iterator
*private_iterator
)
664 struct bt_notification_iterator
*iterator
=
665 bt_notification_iterator_from_private(private_iterator
);
667 return iterator
? iterator
->user_data
: NULL
;
670 enum bt_notification_iterator_status
671 bt_private_notification_iterator_set_user_data(
672 struct bt_private_notification_iterator
*private_iterator
,
675 enum bt_notification_iterator_status ret
=
676 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
677 struct bt_notification_iterator
*iterator
=
678 bt_notification_iterator_from_private(private_iterator
);
681 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
682 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
686 iterator
->user_data
= data
;
687 BT_LOGV("Set notification iterator's user data: "
688 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
694 struct bt_notification
*bt_notification_iterator_get_notification(
695 struct bt_notification_iterator
*iterator
)
697 struct bt_notification
*notification
= NULL
;
700 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
704 notification
= bt_get(iterator
->current_notification
);
711 enum bt_notification_iterator_notif_type
712 bt_notification_iterator_notif_type_from_notif_type(
713 enum bt_notification_type notif_type
)
715 enum bt_notification_iterator_notif_type iter_notif_type
;
717 switch (notif_type
) {
718 case BT_NOTIFICATION_TYPE_EVENT
:
719 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
721 case BT_NOTIFICATION_TYPE_INACTIVITY
:
722 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
724 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
725 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
727 case BT_NOTIFICATION_TYPE_STREAM_END
:
728 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
730 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
731 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
733 case BT_NOTIFICATION_TYPE_PACKET_END
:
734 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
736 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
737 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
;
739 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
740 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
746 return iter_notif_type
;
750 bt_bool
validate_notification(struct bt_notification_iterator
*iterator
,
751 struct bt_notification
*notif
,
752 struct bt_ctf_stream
*notif_stream
,
753 struct bt_ctf_packet
*notif_packet
)
755 bt_bool is_valid
= BT_TRUE
;
756 struct stream_state
*stream_state
;
757 struct bt_port
*stream_comp_cur_port
;
759 assert(notif_stream
);
760 stream_comp_cur_port
=
761 bt_ctf_stream_port_for_component(notif_stream
,
762 iterator
->upstream_component
);
763 if (!stream_comp_cur_port
) {
765 * This is the first time this notification iterator
766 * bumps into this stream. Add an action to map the
767 * iterator's upstream component to the iterator's
768 * upstream port in this stream.
770 struct action action
= {
771 .type
= ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
,
772 .payload
.map_port_to_comp_in_stream
= {
773 .stream
= bt_get(notif_stream
),
774 .component
= bt_get(iterator
->upstream_component
),
775 .port
= bt_get(iterator
->upstream_port
),
779 add_action(iterator
, &action
);
781 if (stream_comp_cur_port
!= iterator
->upstream_port
) {
783 * It looks like two different ports of the same
784 * component are emitting notifications which
785 * have references to the same stream. This is
786 * bad: the API guarantees that it can never
789 BT_LOGW("Two different ports of the same component are emitting notifications which refer to the same stream: "
790 "stream-addr=%p, stream-name=\"%s\", "
791 "stream-comp-cur-port-addr=%p, "
792 "stream-comp-cur-port-name=%p, "
793 "iter-upstream-port-addr=%p, "
794 "iter-upstream-port-name=%s",
796 bt_ctf_stream_get_name(notif_stream
),
797 stream_comp_cur_port
,
798 bt_port_get_name(stream_comp_cur_port
),
799 iterator
->upstream_port
,
800 bt_port_get_name(iterator
->upstream_port
));
807 stream_state
= g_hash_table_lookup(iterator
->stream_states
,
810 BT_LOGV("Stream state already exists: "
811 "stream-addr=%p, stream-name=\"%s\", "
812 "stream-state-addr=%p",
814 bt_ctf_stream_get_name(notif_stream
), stream_state
);
816 if (stream_state
->is_ended
) {
818 * There's a new notification which has a
819 * reference to a stream which, from this
820 * iterator's point of view, is ended ("end of
821 * stream" notification was returned). This is
822 * bad: the API guarantees that it can never
825 BT_LOGW("Stream is already ended: "
826 "stream-addr=%p, stream-name=\"%s\"",
828 bt_ctf_stream_get_name(notif_stream
));
833 switch (notif
->type
) {
834 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
836 * We already have a stream state, which means
837 * we already returned a "stream begin"
838 * notification: this is an invalid duplicate.
840 BT_LOGW("Duplicate stream beginning notification: "
841 "stream-addr=%p, stream-name=\"%s\"",
843 bt_ctf_stream_get_name(notif_stream
));
846 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
847 if (notif_packet
== stream_state
->cur_packet
) {
848 /* Duplicate "packet begin" notification */
849 BT_LOGW("Duplicate stream beginning notification: "
850 "stream-addr=%p, stream-name=\"%s\", "
853 bt_ctf_stream_get_name(notif_stream
),
869 bt_bool
is_subscribed_to_notification_type(struct bt_notification_iterator
*iterator
,
870 enum bt_notification_type notif_type
)
872 uint32_t iter_notif_type
=
873 (uint32_t) bt_notification_iterator_notif_type_from_notif_type(
876 return (iter_notif_type
& iterator
->subscription_mask
) ? BT_TRUE
: BT_FALSE
;
880 void add_action_push_notif(struct bt_notification_iterator
*iterator
,
881 struct bt_notification
*notif
)
883 struct action action
= {
884 .type
= ACTION_TYPE_PUSH_NOTIF
,
889 if (!is_subscribed_to_notification_type(iterator
, notif
->type
)) {
893 action
.payload
.push_notif
.notif
= bt_get(notif
);
894 add_action(iterator
, &action
);
895 BT_LOGV("Added \"push notification\" action: notif-addr=%p", notif
);
899 int add_action_push_notif_stream_begin(
900 struct bt_notification_iterator
*iterator
,
901 struct bt_ctf_stream
*stream
)
904 struct bt_notification
*stream_begin_notif
= NULL
;
906 if (!is_subscribed_to_notification_type(iterator
,
907 BT_NOTIFICATION_TYPE_STREAM_BEGIN
)) {
908 BT_LOGV("Not adding \"push stream beginning notification\" action: "
909 "notification iterator is not subscribed: addr=%p",
915 stream_begin_notif
= bt_notification_stream_begin_create(stream
);
916 if (!stream_begin_notif
) {
917 BT_LOGE_STR("Cannot create stream beginning notification.");
921 add_action_push_notif(iterator
, stream_begin_notif
);
922 BT_LOGV("Added \"push stream beginning notification\" action: "
923 "stream-addr=%p, stream-name=\"%s\"",
924 stream
, bt_ctf_stream_get_name(stream
));
931 bt_put(stream_begin_notif
);
936 int add_action_push_notif_stream_end(
937 struct bt_notification_iterator
*iterator
,
938 struct bt_ctf_stream
*stream
)
941 struct bt_notification
*stream_end_notif
= NULL
;
943 if (!is_subscribed_to_notification_type(iterator
,
944 BT_NOTIFICATION_TYPE_STREAM_END
)) {
945 BT_LOGV("Not adding \"push stream end notification\" action: "
946 "notification iterator is not subscribed: addr=%p",
952 stream_end_notif
= bt_notification_stream_end_create(stream
);
953 if (!stream_end_notif
) {
954 BT_LOGE_STR("Cannot create stream end notification.");
958 add_action_push_notif(iterator
, stream_end_notif
);
959 BT_LOGV("Added \"push stream end notification\" action: "
960 "stream-addr=%p, stream-name=\"%s\"",
961 stream
, bt_ctf_stream_get_name(stream
));
968 bt_put(stream_end_notif
);
973 int add_action_push_notif_packet_begin(
974 struct bt_notification_iterator
*iterator
,
975 struct bt_ctf_packet
*packet
)
978 struct bt_notification
*packet_begin_notif
= NULL
;
980 if (!is_subscribed_to_notification_type(iterator
,
981 BT_NOTIFICATION_TYPE_PACKET_BEGIN
)) {
982 BT_LOGV("Not adding \"push packet beginning notification\" action: "
983 "notification iterator is not subscribed: addr=%p",
989 packet_begin_notif
= bt_notification_packet_begin_create(packet
);
990 if (!packet_begin_notif
) {
991 BT_LOGE_STR("Cannot create packet beginning notification.");
995 add_action_push_notif(iterator
, packet_begin_notif
);
996 BT_LOGV("Added \"push packet beginning notification\" action: "
997 "packet-addr=%p", packet
);
1004 bt_put(packet_begin_notif
);
1009 int add_action_push_notif_packet_end(
1010 struct bt_notification_iterator
*iterator
,
1011 struct bt_ctf_packet
*packet
)
1014 struct bt_notification
*packet_end_notif
= NULL
;
1016 if (!is_subscribed_to_notification_type(iterator
,
1017 BT_NOTIFICATION_TYPE_PACKET_END
)) {
1018 BT_LOGV("Not adding \"push packet end notification\" action: "
1019 "notification iterator is not subscribed: addr=%p",
1025 packet_end_notif
= bt_notification_packet_end_create(packet
);
1026 if (!packet_end_notif
) {
1027 BT_LOGE_STR("Cannot create packet end notification.");
1031 add_action_push_notif(iterator
, packet_end_notif
);
1032 BT_LOGV("Added \"push packet end notification\" action: "
1033 "packet-addr=%p", packet
);
1040 bt_put(packet_end_notif
);
1045 void add_action_set_stream_state_is_ended(
1046 struct bt_notification_iterator
*iterator
,
1047 struct stream_state
*stream_state
)
1049 struct action action
= {
1050 .type
= ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
,
1051 .payload
.set_stream_state_is_ended
= {
1052 .stream_state
= stream_state
,
1056 assert(stream_state
);
1057 add_action(iterator
, &action
);
1058 BT_LOGV("Added \"set stream state's ended\" action: "
1059 "stream-state-addr=%p", stream_state
);
1063 void add_action_set_stream_state_cur_packet(
1064 struct bt_notification_iterator
*iterator
,
1065 struct stream_state
*stream_state
,
1066 struct bt_ctf_packet
*packet
)
1068 struct action action
= {
1069 .type
= ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
,
1070 .payload
.set_stream_state_cur_packet
= {
1071 .stream_state
= stream_state
,
1072 .packet
= bt_get(packet
),
1076 assert(stream_state
);
1077 add_action(iterator
, &action
);
1078 BT_LOGV("Added \"set stream state's current packet\" action: "
1079 "stream-state-addr=%p, packet-addr=%p",
1080 stream_state
, packet
);
1084 void add_action_update_stream_state_discarded_elements(
1085 struct bt_notification_iterator
*iterator
,
1086 enum action_type type
,
1087 struct stream_state
*stream_state
,
1088 struct bt_ctf_clock_value
*cur_begin
,
1091 struct action action
= {
1093 .payload
.update_stream_state_discarded_elements
= {
1094 .stream_state
= stream_state
,
1095 .cur_begin
= bt_get(cur_begin
),
1096 .cur_count
= cur_count
,
1100 assert(stream_state
);
1101 assert(type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
||
1102 type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
);
1103 add_action(iterator
, &action
);
1104 if (type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
) {
1105 BT_LOGV("Added \"update stream state's discarded packets\" action: "
1106 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64
,
1107 stream_state
, cur_begin
, cur_count
);
1108 } else if (type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
) {
1109 BT_LOGV("Added \"update stream state's discarded events\" action: "
1110 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64
,
1111 stream_state
, cur_begin
, cur_count
);
1116 int ensure_stream_state_exists(struct bt_notification_iterator
*iterator
,
1117 struct bt_notification
*stream_begin_notif
,
1118 struct bt_ctf_stream
*notif_stream
,
1119 struct stream_state
**stream_state
)
1123 if (!notif_stream
) {
1125 * The notification does not reference any stream: no
1126 * need to get or create a stream state.
1131 *stream_state
= g_hash_table_lookup(iterator
->stream_states
,
1133 if (!*stream_state
) {
1135 * This iterator did not bump into this stream yet:
1136 * create a stream state and a "stream begin"
1139 struct action action
= {
1140 .type
= ACTION_TYPE_ADD_STREAM_STATE
,
1141 .payload
.add_stream_state
= {
1142 .stream
= bt_get(notif_stream
),
1143 .stream_state
= NULL
,
1147 *stream_state
= create_stream_state(notif_stream
);
1148 if (!stream_state
) {
1149 BT_LOGE_STR("Cannot create stream state.");
1153 action
.payload
.add_stream_state
.stream_state
=
1155 add_action(iterator
, &action
);
1157 if (stream_begin_notif
) {
1158 add_action_push_notif(iterator
, stream_begin_notif
);
1160 ret
= add_action_push_notif_stream_begin(iterator
,
1163 BT_LOGE_STR("Cannot add \"push stream beginning notification\" action.");
1172 destroy_stream_state(*stream_state
);
1180 struct bt_ctf_field
*get_struct_field_uint(struct bt_ctf_field
*struct_field
,
1181 const char *field_name
)
1183 struct bt_ctf_field
*field
= NULL
;
1184 struct bt_ctf_field_type
*ft
= NULL
;
1186 field
= bt_ctf_field_structure_get_field_by_name(struct_field
,
1189 BT_LOGV_STR("`%s` field does not exist.");
1193 if (!bt_ctf_field_is_integer(field
)) {
1194 BT_LOGV("Skipping `%s` field because its type is not an integer field type: "
1195 "field-addr=%p, ft-addr=%p, ft-id=%s", field_name
,
1196 field
, ft
, bt_ctf_field_type_id_string(
1197 bt_ctf_field_type_get_type_id(ft
)));
1202 ft
= bt_ctf_field_get_type(field
);
1205 if (bt_ctf_field_type_integer_is_signed(ft
)) {
1206 BT_LOGV("Skipping `%s` integer field because its type is signed: "
1207 "field-addr=%p, ft-addr=%p", field_name
, field
, ft
);
1218 uint64_t get_packet_context_events_discarded(struct bt_ctf_packet
*packet
)
1220 struct bt_ctf_field
*packet_context
= NULL
;
1221 struct bt_ctf_field
*field
= NULL
;
1222 uint64_t retval
= -1ULL;
1225 packet_context
= bt_ctf_packet_get_context(packet
);
1226 if (!packet_context
) {
1230 field
= get_struct_field_uint(packet_context
, "events_discarded");
1232 BT_LOGV("`events_discarded` field does not exist in packet's context field: "
1233 "packet-addr=%p, packet-context-field-addr=%p",
1234 packet
, packet_context
);
1238 assert(bt_ctf_field_is_integer(field
));
1239 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &retval
);
1241 BT_LOGV("Cannot get raw value of packet's context field's `events_discarded` integer field: "
1242 "packet-addr=%p, field-addr=%p",
1249 bt_put(packet_context
);
1255 uint64_t get_packet_header_packet_seq_num(struct bt_ctf_packet
*packet
)
1257 struct bt_ctf_field
*packet_header
= NULL
;
1258 struct bt_ctf_field
*field
= NULL
;
1259 uint64_t retval
= -1ULL;
1262 packet_header
= bt_ctf_packet_get_header(packet
);
1263 if (!packet_header
) {
1267 field
= get_struct_field_uint(packet_header
, "packet_seq_num");
1269 BT_LOGV("`packet_seq_num` field does not exist in packet's header field: "
1270 "packet-addr=%p, packet-header-field-addr=%p",
1271 packet
, packet_header
);
1275 assert(bt_ctf_field_is_integer(field
));
1276 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &retval
);
1278 BT_LOGV("Cannot get raw value of packet's header field's `packet_seq_num` integer field: "
1279 "packet-addr=%p, field-addr=%p",
1286 bt_put(packet_header
);
1292 int handle_discarded_packets(struct bt_notification_iterator
*iterator
,
1293 struct bt_ctf_packet
*packet
,
1294 struct bt_ctf_clock_value
*ts_begin
,
1295 struct bt_ctf_clock_value
*ts_end
,
1296 struct stream_state
*stream_state
)
1298 struct bt_notification
*notif
= NULL
;
1300 uint64_t next_count
;
1303 next_count
= get_packet_header_packet_seq_num(packet
);
1304 if (next_count
== -1ULL) {
1305 next_count
= stream_state
->discarded_packets_state
.cur_count
;
1309 if (next_count
< stream_state
->discarded_packets_state
.cur_count
) {
1310 BT_LOGW("Current value of packet's header field's `packet_seq_num` field is lesser than the previous value for the same stream: "
1311 "not updating the stream state's current value: "
1312 "packet-addr=%p, prev-count=%" PRIu64
", "
1313 "cur-count=%" PRIu64
,
1314 packet
, stream_state
->discarded_packets_state
.cur_count
,
1319 diff
= next_count
- stream_state
->discarded_packets_state
.cur_count
;
1322 * Add a discarded packets notification. The packets
1323 * are considered to be lost betweem the state's last time
1324 * and the current packet's beginning time.
1326 notif
= bt_notification_discarded_elements_create(
1327 BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
,
1328 stream_state
->stream
,
1329 stream_state
->discarded_packets_state
.cur_begin
,
1332 BT_LOGE_STR("Cannot create discarded packets notification.");
1337 add_action_push_notif(iterator
, notif
);
1341 add_action_update_stream_state_discarded_elements(iterator
,
1342 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
,
1343 stream_state
, ts_end
, next_count
);
1351 int handle_discarded_events(struct bt_notification_iterator
*iterator
,
1352 struct bt_ctf_packet
*packet
,
1353 struct bt_ctf_clock_value
*ts_begin
,
1354 struct bt_ctf_clock_value
*ts_end
,
1355 struct stream_state
*stream_state
)
1357 struct bt_notification
*notif
= NULL
;
1359 uint64_t next_count
;
1362 next_count
= get_packet_context_events_discarded(packet
);
1363 if (next_count
== -1ULL) {
1364 next_count
= stream_state
->discarded_events_state
.cur_count
;
1368 if (next_count
< stream_state
->discarded_events_state
.cur_count
) {
1369 BT_LOGW("Current value of packet's context field's `events_discarded` field is lesser than the previous value for the same stream: "
1370 "not updating the stream state's current value: "
1371 "packet-addr=%p, prev-count=%" PRIu64
", "
1372 "cur-count=%" PRIu64
,
1373 packet
, stream_state
->discarded_events_state
.cur_count
,
1378 diff
= next_count
- stream_state
->discarded_events_state
.cur_count
;
1381 * Add a discarded events notification. The events are
1382 * considered to be lost betweem the state's last time
1383 * and the current packet's end time.
1385 notif
= bt_notification_discarded_elements_create(
1386 BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
,
1387 stream_state
->stream
,
1388 stream_state
->discarded_events_state
.cur_begin
,
1391 BT_LOGE_STR("Cannot create discarded events notification.");
1396 add_action_push_notif(iterator
, notif
);
1400 add_action_update_stream_state_discarded_elements(iterator
,
1401 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
,
1402 stream_state
, ts_end
, next_count
);
1410 int get_field_clock_value(struct bt_ctf_field
*root_field
,
1411 const char *field_name
,
1412 struct bt_ctf_clock_value
**user_clock_val
)
1414 struct bt_ctf_field
*field
;
1415 struct bt_ctf_field_type
*ft
= NULL
;
1416 struct bt_ctf_clock_class
*clock_class
= NULL
;
1417 struct bt_ctf_clock_value
*clock_value
= NULL
;
1421 field
= get_struct_field_uint(root_field
, field_name
);
1423 /* Not an error: skip this */
1427 ft
= bt_ctf_field_get_type(field
);
1429 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
1431 BT_LOGW("Integer field type has no mapped clock class but it's expected to have one: "
1437 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &val
);
1439 BT_LOGW("Cannot get integer field's raw value: "
1440 "field-addr=%p", field
);
1445 clock_value
= bt_ctf_clock_value_create(clock_class
, val
);
1447 BT_LOGE_STR("Cannot create clock value from clock class.");
1452 /* Move clock value to user */
1453 *user_clock_val
= clock_value
;
1459 bt_put(clock_class
);
1460 bt_put(clock_value
);
1465 int get_ts_begin_ts_end_from_packet(struct bt_ctf_packet
*packet
,
1466 struct bt_ctf_clock_value
**user_ts_begin
,
1467 struct bt_ctf_clock_value
**user_ts_end
)
1469 struct bt_ctf_field
*packet_context
= NULL
;
1470 struct bt_ctf_clock_value
*ts_begin
= NULL
;
1471 struct bt_ctf_clock_value
*ts_end
= NULL
;
1474 packet_context
= bt_ctf_packet_get_context(packet
);
1475 if (!packet_context
) {
1479 ret
= get_field_clock_value(packet_context
, "timestamp_begin",
1482 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1483 "packet-addr=%p, packet-context-field-addr=%p",
1484 packet
, packet_context
);
1488 ret
= get_field_clock_value(packet_context
, "timestamp_end",
1491 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1492 "packet-addr=%p, packet-context-field-addr=%p",
1493 packet
, packet_context
);
1497 /* Move clock values to user */
1498 *user_ts_begin
= ts_begin
;
1500 *user_ts_end
= ts_end
;
1504 bt_put(packet_context
);
1511 int handle_discarded_elements(struct bt_notification_iterator
*iterator
,
1512 struct bt_ctf_packet
*packet
, struct stream_state
*stream_state
)
1514 struct bt_ctf_clock_value
*ts_begin
= NULL
;
1515 struct bt_ctf_clock_value
*ts_end
= NULL
;
1518 ret
= get_ts_begin_ts_end_from_packet(packet
, &ts_begin
, &ts_end
);
1520 BT_LOGW("Cannot get packet's beginning or end clock values: "
1521 "packet-addr=%p, ret=%d", packet
, ret
);
1526 ret
= handle_discarded_packets(iterator
, packet
, ts_begin
, ts_end
,
1529 BT_LOGW("Cannot handle discarded packets for packet: "
1530 "packet-addr=%p, ret=%d", packet
, ret
);
1535 ret
= handle_discarded_events(iterator
, packet
, ts_begin
, ts_end
,
1538 BT_LOGW("Cannot handle discarded events for packet: "
1539 "packet-addr=%p, ret=%d", packet
, ret
);
1551 int handle_packet_switch(struct bt_notification_iterator
*iterator
,
1552 struct bt_notification
*packet_begin_notif
,
1553 struct bt_ctf_packet
*new_packet
,
1554 struct stream_state
*stream_state
)
1558 if (stream_state
->cur_packet
== new_packet
) {
1562 BT_LOGV("Handling packet switch: "
1563 "cur-packet-addr=%p, new-packet-addr=%p",
1564 stream_state
->cur_packet
, new_packet
);
1566 if (stream_state
->cur_packet
) {
1567 /* End of the current packet */
1568 ret
= add_action_push_notif_packet_end(iterator
,
1569 stream_state
->cur_packet
);
1571 BT_LOGE_STR("Cannot add \"push packet end notification\" action.");
1577 * Check the new packet's header and context fields for
1578 * discarded packets and events to emit those automatic
1581 ret
= handle_discarded_elements(iterator
, new_packet
, stream_state
);
1583 BT_LOGE_STR("Cannot handle discarded elements for new packet.");
1587 /* Beginning of the new packet */
1588 if (packet_begin_notif
) {
1589 add_action_push_notif(iterator
, packet_begin_notif
);
1590 } else if (new_packet
) {
1591 ret
= add_action_push_notif_packet_begin(iterator
,
1594 BT_LOGE_STR("Cannot add \"push packet beginning notification\" action.");
1599 add_action_set_stream_state_cur_packet(iterator
, stream_state
,
1611 int handle_notif_stream_begin(
1612 struct bt_notification_iterator
*iterator
,
1613 struct bt_notification
*notif
,
1614 struct bt_ctf_stream
*notif_stream
)
1617 struct stream_state
*stream_state
;
1619 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_BEGIN
);
1620 assert(notif_stream
);
1621 ret
= ensure_stream_state_exists(iterator
, notif
, notif_stream
,
1624 BT_LOGE_STR("Cannot ensure that stream state exists.");
1638 int handle_notif_stream_end(
1639 struct bt_notification_iterator
*iterator
,
1640 struct bt_notification
*notif
,
1641 struct bt_ctf_stream
*notif_stream
)
1644 struct stream_state
*stream_state
;
1646 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_END
);
1647 assert(notif_stream
);
1648 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1651 BT_LOGE_STR("Cannot ensure that stream state exists.");
1655 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1657 BT_LOGE_STR("Cannot handle packet switch.");
1661 add_action_push_notif(iterator
, notif
);
1662 add_action_set_stream_state_is_ended(iterator
, stream_state
);
1673 int handle_notif_discarded_elements(
1674 struct bt_notification_iterator
*iterator
,
1675 struct bt_notification
*notif
,
1676 struct bt_ctf_stream
*notif_stream
)
1679 struct stream_state
*stream_state
;
1681 assert(notif
->type
== BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
||
1682 notif
->type
== BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
);
1683 assert(notif_stream
);
1684 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1687 BT_LOGE_STR("Cannot ensure that stream state exists.");
1691 add_action_push_notif(iterator
, notif
);
1702 int handle_notif_packet_begin(
1703 struct bt_notification_iterator
*iterator
,
1704 struct bt_notification
*notif
,
1705 struct bt_ctf_stream
*notif_stream
,
1706 struct bt_ctf_packet
*notif_packet
)
1709 struct stream_state
*stream_state
;
1711 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_BEGIN
);
1712 assert(notif_packet
);
1713 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1716 BT_LOGE_STR("Cannot ensure that stream state exists.");
1720 ret
= handle_packet_switch(iterator
, notif
, notif_packet
, stream_state
);
1722 BT_LOGE_STR("Cannot handle packet switch.");
1736 int handle_notif_packet_end(
1737 struct bt_notification_iterator
*iterator
,
1738 struct bt_notification
*notif
,
1739 struct bt_ctf_stream
*notif_stream
,
1740 struct bt_ctf_packet
*notif_packet
)
1743 struct stream_state
*stream_state
;
1745 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_END
);
1746 assert(notif_packet
);
1747 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1750 BT_LOGE_STR("Cannot ensure that stream state exists.");
1754 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1756 BT_LOGE_STR("Cannot handle packet switch.");
1760 /* End of the current packet */
1761 add_action_push_notif(iterator
, notif
);
1762 add_action_set_stream_state_cur_packet(iterator
, stream_state
, NULL
);
1773 int handle_notif_event(
1774 struct bt_notification_iterator
*iterator
,
1775 struct bt_notification
*notif
,
1776 struct bt_ctf_stream
*notif_stream
,
1777 struct bt_ctf_packet
*notif_packet
)
1780 struct stream_state
*stream_state
;
1782 assert(notif
->type
== BT_NOTIFICATION_TYPE_EVENT
);
1783 assert(notif_packet
);
1784 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1787 BT_LOGE_STR("Cannot ensure that stream state exists.");
1791 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1793 BT_LOGE_STR("Cannot handle packet switch.");
1797 add_action_push_notif(iterator
, notif
);
1808 int enqueue_notification_and_automatic(
1809 struct bt_notification_iterator
*iterator
,
1810 struct bt_notification
*notif
)
1813 struct bt_ctf_event
*notif_event
= NULL
;
1814 struct bt_ctf_stream
*notif_stream
= NULL
;
1815 struct bt_ctf_packet
*notif_packet
= NULL
;
1819 BT_LOGV("Enqueuing user notification and automatic notifications: "
1820 "iter-addr=%p, notif-addr=%p", iterator
, notif
);
1822 // TODO: Skip most of this if the iterator is only subscribed
1823 // to event/inactivity notifications.
1825 /* Get the stream and packet referred by the notification */
1826 switch (notif
->type
) {
1827 case BT_NOTIFICATION_TYPE_EVENT
:
1828 notif_event
= bt_notification_event_borrow_event(notif
);
1829 assert(notif_event
);
1830 notif_packet
= bt_ctf_event_borrow_packet(notif_event
);
1831 assert(notif_packet
);
1833 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1835 bt_notification_stream_begin_borrow_stream(notif
);
1836 assert(notif_stream
);
1838 case BT_NOTIFICATION_TYPE_STREAM_END
:
1839 notif_stream
= bt_notification_stream_end_borrow_stream(notif
);
1840 assert(notif_stream
);
1842 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1844 bt_notification_packet_begin_borrow_packet(notif
);
1845 assert(notif_packet
);
1847 case BT_NOTIFICATION_TYPE_PACKET_END
:
1848 notif_packet
= bt_notification_packet_end_borrow_packet(notif
);
1849 assert(notif_packet
);
1851 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1856 * Invalid type of notification. Only the notification
1857 * types above are allowed to be returned by a user
1860 BT_LOGF("Unexpected notification type at this point: "
1861 "notif-addr=%p, notif-type=%s", notif
,
1862 bt_notification_type_string(notif
->type
));
1867 notif_stream
= bt_ctf_packet_borrow_stream(notif_packet
);
1868 assert(notif_stream
);
1871 if (!notif_stream
) {
1873 * The notification has no reference to a stream: it
1874 * cannot cause the creation of automatic notifications.
1876 BT_LOGV_STR("Notification has no reference to any stream: skipping automatic notification generation.");
1880 if (!validate_notification(iterator
, notif
, notif_stream
,
1882 BT_LOGW_STR("Invalid notification.");
1887 switch (notif
->type
) {
1888 case BT_NOTIFICATION_TYPE_EVENT
:
1889 ret
= handle_notif_event(iterator
, notif
, notif_stream
,
1892 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1893 ret
= handle_notif_stream_begin(iterator
, notif
, notif_stream
);
1895 case BT_NOTIFICATION_TYPE_STREAM_END
:
1896 ret
= handle_notif_stream_end(iterator
, notif
, notif_stream
);
1898 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1899 ret
= handle_notif_packet_begin(iterator
, notif
, notif_stream
,
1902 case BT_NOTIFICATION_TYPE_PACKET_END
:
1903 ret
= handle_notif_packet_end(iterator
, notif
, notif_stream
,
1906 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
1907 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
1908 ret
= handle_notif_discarded_elements(iterator
, notif
,
1911 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1912 add_action_push_notif(iterator
, notif
);
1919 BT_LOGW_STR("Failed to handle notification for automatic notification generation.");
1923 apply_actions(iterator
);
1924 BT_LOGV("Enqueued user notification and automatic notifications: "
1925 "iter-addr=%p, notif-addr=%p", iterator
, notif
);
1936 int handle_end(struct bt_notification_iterator
*iterator
)
1938 GHashTableIter stream_state_iter
;
1939 gpointer stream_gptr
, stream_state_gptr
;
1942 BT_LOGV("Handling end of iteration: addr=%p", iterator
);
1945 * Emit a "stream end" notification for each non-ended stream
1946 * known by this iterator and mark them as ended.
1948 g_hash_table_iter_init(&stream_state_iter
, iterator
->stream_states
);
1950 while (g_hash_table_iter_next(&stream_state_iter
, &stream_gptr
,
1951 &stream_state_gptr
)) {
1952 struct stream_state
*stream_state
= stream_state_gptr
;
1954 assert(stream_state_gptr
);
1956 if (stream_state
->is_ended
) {
1960 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1962 BT_LOGE_STR("Cannot handle packet switch.");
1966 ret
= add_action_push_notif_stream_end(iterator
, stream_gptr
);
1968 BT_LOGE_STR("Cannot add \"push stream end notification\" action.");
1972 add_action_set_stream_state_is_ended(iterator
, stream_state
);
1975 apply_actions(iterator
);
1976 BT_LOGV("Handled end of iteration: addr=%p", iterator
);
1987 enum bt_notification_iterator_status
ensure_queue_has_notifications(
1988 struct bt_notification_iterator
*iterator
)
1990 struct bt_private_notification_iterator
*priv_iterator
=
1991 bt_private_notification_iterator_from_notification_iterator(iterator
);
1992 bt_component_class_notification_iterator_next_method next_method
= NULL
;
1993 struct bt_notification_iterator_next_return next_return
= {
1994 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
1995 .notification
= NULL
,
1997 enum bt_notification_iterator_status status
=
1998 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
2002 BT_LOGD("Ensuring that notification iterator's queue has at least one notification: "
2003 "iter-addr=%p, queue-size=%u, iter-state=%s",
2004 iterator
, iterator
->queue
->length
,
2005 bt_notification_iterator_state_string(iterator
->state
));
2007 if (iterator
->queue
->length
> 0) {
2009 * We already have enough. Even if this notification
2010 * iterator is finalized, its user can still flush its
2011 * current queue's content by calling its "next" method
2012 * since this content is local and has no impact on what
2013 * used to be the iterator's upstream component.
2015 BT_LOGD_STR("Queue already has at least one notification.");
2019 switch (iterator
->state
) {
2020 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
2021 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
2022 BT_LOGD_STR("Notification iterator's \"next\" called, but it is finalized.");
2023 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
2025 case BT_NOTIFICATION_ITERATOR_STATE_ENDED
:
2026 BT_LOGD_STR("Notification iterator is ended.");
2027 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
2033 assert(iterator
->upstream_component
);
2034 assert(iterator
->upstream_component
->class);
2036 /* Pick the appropriate "next" method */
2037 switch (iterator
->upstream_component
->class->type
) {
2038 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2040 struct bt_component_class_source
*source_class
=
2041 container_of(iterator
->upstream_component
->class,
2042 struct bt_component_class_source
, parent
);
2044 assert(source_class
->methods
.iterator
.next
);
2045 next_method
= source_class
->methods
.iterator
.next
;
2048 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2050 struct bt_component_class_filter
*filter_class
=
2051 container_of(iterator
->upstream_component
->class,
2052 struct bt_component_class_filter
, parent
);
2054 assert(filter_class
->methods
.iterator
.next
);
2055 next_method
= filter_class
->methods
.iterator
.next
;
2063 * Call the user's "next" method to get the next notification
2066 assert(next_method
);
2068 while (iterator
->queue
->length
== 0) {
2069 BT_LOGD_STR("Calling user's \"next\" method.");
2070 next_return
= next_method(priv_iterator
);
2071 BT_LOGD("User method returned: status=%s",
2072 bt_notification_iterator_status_string(next_return
.status
));
2073 if (next_return
.status
< 0) {
2074 BT_LOGW_STR("User method failed.");
2075 status
= next_return
.status
;
2079 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
2080 iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
2082 * The user's "next" method, somehow, cancelled
2083 * its own notification iterator. This can
2084 * happen, for example, when the user's method
2085 * removes the port on which there's the
2086 * connection from which the iterator was
2087 * created. In this case, said connection is
2088 * ended, and all its notification iterators are
2091 * Only bt_put() the returned notification if
2093 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
2094 * otherwise this field could be garbage.
2096 if (next_return
.status
==
2097 BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
2098 bt_put(next_return
.notification
);
2101 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
2105 switch (next_return
.status
) {
2106 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
2107 ret
= handle_end(iterator
);
2109 BT_LOGW_STR("Cannot handle end of iteration.");
2110 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2114 assert(iterator
->state
==
2115 BT_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
2116 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_ENDED
;
2118 if (iterator
->queue
->length
== 0) {
2119 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
2122 BT_LOGD("Set new status: status=%s",
2123 bt_notification_iterator_status_string(status
));
2125 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
2126 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
2128 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
2129 if (!next_return
.notification
) {
2130 BT_LOGW_STR("User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL.");
2131 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2136 * Ignore some notifications which are always
2137 * automatically generated by the notification
2138 * iterator to make sure they have valid values.
2140 switch (next_return
.notification
->type
) {
2141 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
2142 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
2143 BT_LOGV("Ignoring discarded elements notification returned by notification iterator's \"next\" method: "
2145 bt_notification_type_string(next_return
.notification
->type
));
2146 BT_PUT(next_return
.notification
);
2153 * We know the notification is valid. Before we
2154 * push it to the head of the queue, push the
2155 * appropriate automatic notifications if any.
2157 ret
= enqueue_notification_and_automatic(iterator
,
2158 next_return
.notification
);
2159 BT_PUT(next_return
.notification
);
2161 BT_LOGW("Cannot enqueue notification and automatic notifications.");
2162 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2167 /* Unknown non-error status */
2176 enum bt_notification_iterator_status
2177 bt_notification_iterator_next(struct bt_notification_iterator
*iterator
)
2179 enum bt_notification_iterator_status status
;
2182 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
2183 status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
2187 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator
);
2190 * Make sure that the iterator's queue contains at least one
2193 status
= ensure_queue_has_notifications(iterator
);
2194 if (status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
2200 * Move the notification at the tail of the queue to the
2201 * iterator's current notification.
2203 assert(iterator
->queue
->length
> 0);
2204 bt_put(iterator
->current_notification
);
2205 iterator
->current_notification
= g_queue_pop_tail(iterator
->queue
);
2206 assert(iterator
->current_notification
);
2212 struct bt_component
*bt_notification_iterator_get_component(
2213 struct bt_notification_iterator
*iterator
)
2215 return bt_get(iterator
->upstream_component
);
2218 struct bt_private_component
*
2219 bt_private_notification_iterator_get_private_component(
2220 struct bt_private_notification_iterator
*private_iterator
)
2222 return bt_private_component_from_component(
2223 bt_notification_iterator_get_component(
2224 bt_notification_iterator_from_private(private_iterator
)));
2227 enum bt_notification_iterator_status
bt_notification_iterator_seek_time(
2228 struct bt_notification_iterator
*iterator
,
2229 enum bt_notification_iterator_seek_origin seek_origin
,
2232 enum bt_notification_iterator_status ret
=
2233 BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED
;