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 * The packet index is a monotonic counter which may not start
336 * at 0 at the beginning of the stream. We therefore need to
337 * have an internal object initial state of -1ULL to distinguish
338 * between initial state and having seen a packet with
341 stream_state
->discarded_packets_state
.cur_count
= -1ULL;
344 * We keep a reference to the stream until we know it's ended
345 * because we need to be able to create an automatic "stream
346 * end" notification when the user's "next" method returns
347 * BT_NOTIFICATION_ITERATOR_STATUS_END.
349 * We put this reference when the stream is marked as ended.
351 stream_state
->stream
= bt_get(stream
);
352 BT_LOGV("Created stream state: stream-addr=%p, stream-name=\"%s\", "
353 "stream-state-addr=%p",
354 stream
, bt_ctf_stream_get_name(stream
), stream_state
);
361 void bt_notification_iterator_destroy(struct bt_object
*obj
)
363 struct bt_notification_iterator
*iterator
;
368 * The notification iterator's reference count is 0 if we're
369 * here. Increment it to avoid a double-destroy (possibly
370 * infinitely recursive). This could happen for example if the
371 * notification iterator's finalization function does bt_get()
372 * (or anything that causes bt_get() to be called) on itself
373 * (ref. count goes from 0 to 1), and then bt_put(): the
374 * reference count would go from 1 to 0 again and this function
375 * would be called again.
377 obj
->ref_count
.count
++;
378 iterator
= container_of(obj
, struct bt_notification_iterator
, base
);
379 BT_LOGD("Destroying notification iterator object: addr=%p",
381 bt_notification_iterator_finalize(iterator
);
383 if (iterator
->queue
) {
384 struct bt_notification
*notif
;
386 BT_LOGD("Putting notifications in queue.");
388 while ((notif
= g_queue_pop_tail(iterator
->queue
))) {
392 g_queue_free(iterator
->queue
);
395 if (iterator
->stream_states
) {
397 * Remove our destroy listener from each stream which
398 * has a state in this iterator. Otherwise the destroy
399 * listener would be called with an invalid/other
400 * notification iterator object.
402 GHashTableIter ht_iter
;
403 gpointer stream_gptr
, stream_state_gptr
;
405 g_hash_table_iter_init(&ht_iter
, iterator
->stream_states
);
407 while (g_hash_table_iter_next(&ht_iter
, &stream_gptr
, &stream_state_gptr
)) {
410 BT_LOGD_STR("Removing stream's destroy listener for notification iterator.");
411 bt_ctf_stream_remove_destroy_listener(
412 (void *) stream_gptr
, stream_destroy_listener
,
416 g_hash_table_destroy(iterator
->stream_states
);
419 if (iterator
->actions
) {
420 g_array_free(iterator
->actions
, TRUE
);
423 if (iterator
->connection
) {
425 * Remove ourself from the originating connection so
426 * that it does not try to finalize a dangling pointer
429 bt_connection_remove_iterator(iterator
->connection
, iterator
);
432 BT_LOGD_STR("Putting current notification.");
433 bt_put(iterator
->current_notification
);
438 void bt_notification_iterator_finalize(
439 struct bt_notification_iterator
*iterator
)
441 struct bt_component_class
*comp_class
= NULL
;
442 bt_component_class_notification_iterator_finalize_method
443 finalize_method
= NULL
;
447 switch (iterator
->state
) {
448 case BT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
:
449 /* Skip user finalization if user initialization failed */
450 BT_LOGD("Not finalizing non-initialized notification iterator: "
451 "addr=%p", iterator
);
453 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
454 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
455 /* Already finalized */
456 BT_LOGD("Not finalizing notification iterator: already finalized: "
457 "addr=%p", iterator
);
463 BT_LOGD("Finalizing notification iterator: addr=%p", iterator
);
465 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_ENDED
) {
466 BT_LOGD("Updating notification iterator's state: "
467 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED");
468 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
;
470 BT_LOGD("Updating notification iterator's state: "
471 "new-state=BT_NOTIFICATION_ITERATOR_STATE_FINALIZED");
472 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
;
475 assert(iterator
->upstream_component
);
476 comp_class
= iterator
->upstream_component
->class;
478 /* Call user-defined destroy method */
479 switch (comp_class
->type
) {
480 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
482 struct bt_component_class_source
*source_class
;
484 source_class
= container_of(comp_class
, struct bt_component_class_source
, parent
);
485 finalize_method
= source_class
->methods
.iterator
.finalize
;
488 case BT_COMPONENT_CLASS_TYPE_FILTER
:
490 struct bt_component_class_filter
*filter_class
;
492 filter_class
= container_of(comp_class
, struct bt_component_class_filter
, parent
);
493 finalize_method
= filter_class
->methods
.iterator
.finalize
;
501 if (finalize_method
) {
502 BT_LOGD("Calling user's finalization method: addr=%p",
505 bt_private_notification_iterator_from_notification_iterator(iterator
));
508 iterator
->upstream_component
= NULL
;
509 iterator
->upstream_port
= NULL
;
510 BT_LOGD("Finalized notification iterator: addr=%p", iterator
);
514 void bt_notification_iterator_set_connection(
515 struct bt_notification_iterator
*iterator
,
516 struct bt_connection
*connection
)
519 iterator
->connection
= connection
;
520 BT_LOGV("Set notification iterator's connection: "
521 "iter-addr=%p, conn-addr=%p", iterator
, connection
);
525 int create_subscription_mask_from_notification_types(
526 struct bt_notification_iterator
*iterator
,
527 const enum bt_notification_type
*notif_types
)
529 const enum bt_notification_type
*notif_type
;
533 iterator
->subscription_mask
= 0;
535 for (notif_type
= notif_types
;
536 *notif_type
!= BT_NOTIFICATION_TYPE_SENTINEL
;
538 switch (*notif_type
) {
539 case BT_NOTIFICATION_TYPE_ALL
:
540 iterator
->subscription_mask
|=
541 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
|
542 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
|
543 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
|
544 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
|
545 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
|
546 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
|
547 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
|
548 BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
550 case BT_NOTIFICATION_TYPE_EVENT
:
551 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
553 case BT_NOTIFICATION_TYPE_INACTIVITY
:
554 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
556 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
557 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
559 case BT_NOTIFICATION_TYPE_STREAM_END
:
560 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
562 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
563 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
565 case BT_NOTIFICATION_TYPE_PACKET_END
:
566 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
568 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
569 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
;
571 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
572 iterator
->subscription_mask
|= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
579 BT_LOGV("Added notification type to subscription mask: "
581 bt_notification_type_string(*notif_type
),
582 iterator
->subscription_mask
);
590 enum bt_connection_status
bt_notification_iterator_create(
591 struct bt_component
*upstream_comp
,
592 struct bt_port
*upstream_port
,
593 const enum bt_notification_type
*notification_types
,
594 struct bt_connection
*connection
,
595 struct bt_notification_iterator
**user_iterator
)
597 enum bt_connection_status status
= BT_CONNECTION_STATUS_OK
;
598 enum bt_component_class_type type
;
599 struct bt_notification_iterator
*iterator
= NULL
;
601 assert(upstream_comp
);
602 assert(upstream_port
);
603 assert(notification_types
);
604 assert(bt_port_is_connected(upstream_port
));
605 assert(user_iterator
);
606 BT_LOGD("Creating notification iterator: "
607 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
608 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
610 upstream_comp
, bt_component_get_name(upstream_comp
),
611 upstream_port
, bt_port_get_name(upstream_port
),
613 type
= bt_component_get_class_type(upstream_comp
);
614 assert(type
== BT_COMPONENT_CLASS_TYPE_SOURCE
||
615 type
== BT_COMPONENT_CLASS_TYPE_FILTER
);
616 iterator
= g_new0(struct bt_notification_iterator
, 1);
618 BT_LOGE_STR("Failed to allocate one notification iterator.");
619 status
= BT_CONNECTION_STATUS_NOMEM
;
623 bt_object_init(iterator
, bt_notification_iterator_destroy
);
625 if (create_subscription_mask_from_notification_types(iterator
,
626 notification_types
)) {
627 BT_LOGW_STR("Cannot create subscription mask from notification types.");
628 status
= BT_CONNECTION_STATUS_INVALID
;
632 iterator
->stream_states
= g_hash_table_new_full(g_direct_hash
,
633 g_direct_equal
, NULL
, (GDestroyNotify
) destroy_stream_state
);
634 if (!iterator
->stream_states
) {
635 BT_LOGE_STR("Failed to allocate a GHashTable.");
636 status
= BT_CONNECTION_STATUS_NOMEM
;
640 iterator
->queue
= g_queue_new();
641 if (!iterator
->queue
) {
642 BT_LOGE_STR("Failed to allocate a GQueue.");
643 status
= BT_CONNECTION_STATUS_NOMEM
;
647 iterator
->actions
= g_array_new(FALSE
, FALSE
, sizeof(struct action
));
648 if (!iterator
->actions
) {
649 BT_LOGE_STR("Failed to allocate a GArray.");
650 status
= BT_CONNECTION_STATUS_NOMEM
;
654 iterator
->upstream_component
= upstream_comp
;
655 iterator
->upstream_port
= upstream_port
;
656 iterator
->connection
= connection
;
657 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_NON_INITIALIZED
;
658 BT_LOGD("Created notification iterator: "
659 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
660 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
661 "conn-addr=%p, iter-addr=%p",
662 upstream_comp
, bt_component_get_name(upstream_comp
),
663 upstream_port
, bt_port_get_name(upstream_port
),
664 connection
, iterator
);
666 /* Move reference to user */
667 *user_iterator
= iterator
;
675 void *bt_private_notification_iterator_get_user_data(
676 struct bt_private_notification_iterator
*private_iterator
)
678 struct bt_notification_iterator
*iterator
=
679 bt_notification_iterator_from_private(private_iterator
);
681 return iterator
? iterator
->user_data
: NULL
;
684 enum bt_notification_iterator_status
685 bt_private_notification_iterator_set_user_data(
686 struct bt_private_notification_iterator
*private_iterator
,
689 enum bt_notification_iterator_status ret
=
690 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
691 struct bt_notification_iterator
*iterator
=
692 bt_notification_iterator_from_private(private_iterator
);
695 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
696 ret
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
700 iterator
->user_data
= data
;
701 BT_LOGV("Set notification iterator's user data: "
702 "iter-addr=%p, user-data-addr=%p", iterator
, data
);
708 struct bt_notification
*bt_notification_iterator_get_notification(
709 struct bt_notification_iterator
*iterator
)
711 struct bt_notification
*notification
= NULL
;
714 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
718 notification
= bt_get(iterator
->current_notification
);
725 enum bt_notification_iterator_notif_type
726 bt_notification_iterator_notif_type_from_notif_type(
727 enum bt_notification_type notif_type
)
729 enum bt_notification_iterator_notif_type iter_notif_type
;
731 switch (notif_type
) {
732 case BT_NOTIFICATION_TYPE_EVENT
:
733 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_EVENT
;
735 case BT_NOTIFICATION_TYPE_INACTIVITY
:
736 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_INACTIVITY
;
738 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
739 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_BEGIN
;
741 case BT_NOTIFICATION_TYPE_STREAM_END
:
742 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_STREAM_END
;
744 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
745 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_BEGIN
;
747 case BT_NOTIFICATION_TYPE_PACKET_END
:
748 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_PACKET_END
;
750 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
751 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_EVENTS
;
753 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
754 iter_notif_type
= BT_NOTIFICATION_ITERATOR_NOTIF_TYPE_DISCARDED_PACKETS
;
760 return iter_notif_type
;
764 bt_bool
validate_notification(struct bt_notification_iterator
*iterator
,
765 struct bt_notification
*notif
,
766 struct bt_ctf_stream
*notif_stream
,
767 struct bt_ctf_packet
*notif_packet
)
769 bt_bool is_valid
= BT_TRUE
;
770 struct stream_state
*stream_state
;
771 struct bt_port
*stream_comp_cur_port
;
773 assert(notif_stream
);
774 stream_comp_cur_port
=
775 bt_ctf_stream_port_for_component(notif_stream
,
776 iterator
->upstream_component
);
777 if (!stream_comp_cur_port
) {
779 * This is the first time this notification iterator
780 * bumps into this stream. Add an action to map the
781 * iterator's upstream component to the iterator's
782 * upstream port in this stream.
784 struct action action
= {
785 .type
= ACTION_TYPE_MAP_PORT_TO_COMP_IN_STREAM
,
786 .payload
.map_port_to_comp_in_stream
= {
787 .stream
= bt_get(notif_stream
),
788 .component
= bt_get(iterator
->upstream_component
),
789 .port
= bt_get(iterator
->upstream_port
),
793 add_action(iterator
, &action
);
795 if (stream_comp_cur_port
!= iterator
->upstream_port
) {
797 * It looks like two different ports of the same
798 * component are emitting notifications which
799 * have references to the same stream. This is
800 * bad: the API guarantees that it can never
803 BT_LOGW("Two different ports of the same component are emitting notifications which refer to the same stream: "
804 "stream-addr=%p, stream-name=\"%s\", "
805 "stream-comp-cur-port-addr=%p, "
806 "stream-comp-cur-port-name=%p, "
807 "iter-upstream-port-addr=%p, "
808 "iter-upstream-port-name=%s",
810 bt_ctf_stream_get_name(notif_stream
),
811 stream_comp_cur_port
,
812 bt_port_get_name(stream_comp_cur_port
),
813 iterator
->upstream_port
,
814 bt_port_get_name(iterator
->upstream_port
));
821 stream_state
= g_hash_table_lookup(iterator
->stream_states
,
824 BT_LOGV("Stream state already exists: "
825 "stream-addr=%p, stream-name=\"%s\", "
826 "stream-state-addr=%p",
828 bt_ctf_stream_get_name(notif_stream
), stream_state
);
830 if (stream_state
->is_ended
) {
832 * There's a new notification which has a
833 * reference to a stream which, from this
834 * iterator's point of view, is ended ("end of
835 * stream" notification was returned). This is
836 * bad: the API guarantees that it can never
839 BT_LOGW("Stream is already ended: "
840 "stream-addr=%p, stream-name=\"%s\"",
842 bt_ctf_stream_get_name(notif_stream
));
847 switch (notif
->type
) {
848 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
850 * We already have a stream state, which means
851 * we already returned a "stream begin"
852 * notification: this is an invalid duplicate.
854 BT_LOGW("Duplicate stream beginning notification: "
855 "stream-addr=%p, stream-name=\"%s\"",
857 bt_ctf_stream_get_name(notif_stream
));
860 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
861 if (notif_packet
== stream_state
->cur_packet
) {
862 /* Duplicate "packet begin" notification */
863 BT_LOGW("Duplicate stream beginning notification: "
864 "stream-addr=%p, stream-name=\"%s\", "
867 bt_ctf_stream_get_name(notif_stream
),
883 bt_bool
is_subscribed_to_notification_type(struct bt_notification_iterator
*iterator
,
884 enum bt_notification_type notif_type
)
886 uint32_t iter_notif_type
=
887 (uint32_t) bt_notification_iterator_notif_type_from_notif_type(
890 return (iter_notif_type
& iterator
->subscription_mask
) ? BT_TRUE
: BT_FALSE
;
894 void add_action_push_notif(struct bt_notification_iterator
*iterator
,
895 struct bt_notification
*notif
)
897 struct action action
= {
898 .type
= ACTION_TYPE_PUSH_NOTIF
,
903 if (!is_subscribed_to_notification_type(iterator
, notif
->type
)) {
907 action
.payload
.push_notif
.notif
= bt_get(notif
);
908 add_action(iterator
, &action
);
909 BT_LOGV("Added \"push notification\" action: notif-addr=%p", notif
);
913 int add_action_push_notif_stream_begin(
914 struct bt_notification_iterator
*iterator
,
915 struct bt_ctf_stream
*stream
)
918 struct bt_notification
*stream_begin_notif
= NULL
;
920 if (!is_subscribed_to_notification_type(iterator
,
921 BT_NOTIFICATION_TYPE_STREAM_BEGIN
)) {
922 BT_LOGV("Not adding \"push stream beginning notification\" action: "
923 "notification iterator is not subscribed: addr=%p",
929 stream_begin_notif
= bt_notification_stream_begin_create(stream
);
930 if (!stream_begin_notif
) {
931 BT_LOGE_STR("Cannot create stream beginning notification.");
935 add_action_push_notif(iterator
, stream_begin_notif
);
936 BT_LOGV("Added \"push stream beginning notification\" action: "
937 "stream-addr=%p, stream-name=\"%s\"",
938 stream
, bt_ctf_stream_get_name(stream
));
945 bt_put(stream_begin_notif
);
950 int add_action_push_notif_stream_end(
951 struct bt_notification_iterator
*iterator
,
952 struct bt_ctf_stream
*stream
)
955 struct bt_notification
*stream_end_notif
= NULL
;
957 if (!is_subscribed_to_notification_type(iterator
,
958 BT_NOTIFICATION_TYPE_STREAM_END
)) {
959 BT_LOGV("Not adding \"push stream end notification\" action: "
960 "notification iterator is not subscribed: addr=%p",
966 stream_end_notif
= bt_notification_stream_end_create(stream
);
967 if (!stream_end_notif
) {
968 BT_LOGE_STR("Cannot create stream end notification.");
972 add_action_push_notif(iterator
, stream_end_notif
);
973 BT_LOGV("Added \"push stream end notification\" action: "
974 "stream-addr=%p, stream-name=\"%s\"",
975 stream
, bt_ctf_stream_get_name(stream
));
982 bt_put(stream_end_notif
);
987 int add_action_push_notif_packet_begin(
988 struct bt_notification_iterator
*iterator
,
989 struct bt_ctf_packet
*packet
)
992 struct bt_notification
*packet_begin_notif
= NULL
;
994 if (!is_subscribed_to_notification_type(iterator
,
995 BT_NOTIFICATION_TYPE_PACKET_BEGIN
)) {
996 BT_LOGV("Not adding \"push packet beginning notification\" action: "
997 "notification iterator is not subscribed: addr=%p",
1003 packet_begin_notif
= bt_notification_packet_begin_create(packet
);
1004 if (!packet_begin_notif
) {
1005 BT_LOGE_STR("Cannot create packet beginning notification.");
1009 add_action_push_notif(iterator
, packet_begin_notif
);
1010 BT_LOGV("Added \"push packet beginning notification\" action: "
1011 "packet-addr=%p", packet
);
1018 bt_put(packet_begin_notif
);
1023 int add_action_push_notif_packet_end(
1024 struct bt_notification_iterator
*iterator
,
1025 struct bt_ctf_packet
*packet
)
1028 struct bt_notification
*packet_end_notif
= NULL
;
1030 if (!is_subscribed_to_notification_type(iterator
,
1031 BT_NOTIFICATION_TYPE_PACKET_END
)) {
1032 BT_LOGV("Not adding \"push packet end notification\" action: "
1033 "notification iterator is not subscribed: addr=%p",
1039 packet_end_notif
= bt_notification_packet_end_create(packet
);
1040 if (!packet_end_notif
) {
1041 BT_LOGE_STR("Cannot create packet end notification.");
1045 add_action_push_notif(iterator
, packet_end_notif
);
1046 BT_LOGV("Added \"push packet end notification\" action: "
1047 "packet-addr=%p", packet
);
1054 bt_put(packet_end_notif
);
1059 void add_action_set_stream_state_is_ended(
1060 struct bt_notification_iterator
*iterator
,
1061 struct stream_state
*stream_state
)
1063 struct action action
= {
1064 .type
= ACTION_TYPE_SET_STREAM_STATE_IS_ENDED
,
1065 .payload
.set_stream_state_is_ended
= {
1066 .stream_state
= stream_state
,
1070 assert(stream_state
);
1071 add_action(iterator
, &action
);
1072 BT_LOGV("Added \"set stream state's ended\" action: "
1073 "stream-state-addr=%p", stream_state
);
1077 void add_action_set_stream_state_cur_packet(
1078 struct bt_notification_iterator
*iterator
,
1079 struct stream_state
*stream_state
,
1080 struct bt_ctf_packet
*packet
)
1082 struct action action
= {
1083 .type
= ACTION_TYPE_SET_STREAM_STATE_CUR_PACKET
,
1084 .payload
.set_stream_state_cur_packet
= {
1085 .stream_state
= stream_state
,
1086 .packet
= bt_get(packet
),
1090 assert(stream_state
);
1091 add_action(iterator
, &action
);
1092 BT_LOGV("Added \"set stream state's current packet\" action: "
1093 "stream-state-addr=%p, packet-addr=%p",
1094 stream_state
, packet
);
1098 void add_action_update_stream_state_discarded_elements(
1099 struct bt_notification_iterator
*iterator
,
1100 enum action_type type
,
1101 struct stream_state
*stream_state
,
1102 struct bt_ctf_clock_value
*cur_begin
,
1105 struct action action
= {
1107 .payload
.update_stream_state_discarded_elements
= {
1108 .stream_state
= stream_state
,
1109 .cur_begin
= bt_get(cur_begin
),
1110 .cur_count
= cur_count
,
1114 assert(stream_state
);
1115 assert(type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
||
1116 type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
);
1117 add_action(iterator
, &action
);
1118 if (type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
) {
1119 BT_LOGV("Added \"update stream state's discarded packets\" action: "
1120 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64
,
1121 stream_state
, cur_begin
, cur_count
);
1122 } else if (type
== ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
) {
1123 BT_LOGV("Added \"update stream state's discarded events\" action: "
1124 "stream-state-addr=%p, cur-begin-addr=%p, cur-count=%" PRIu64
,
1125 stream_state
, cur_begin
, cur_count
);
1130 int ensure_stream_state_exists(struct bt_notification_iterator
*iterator
,
1131 struct bt_notification
*stream_begin_notif
,
1132 struct bt_ctf_stream
*notif_stream
,
1133 struct stream_state
**_stream_state
)
1136 struct stream_state
*stream_state
= NULL
;
1138 if (!notif_stream
) {
1140 * The notification does not reference any stream: no
1141 * need to get or create a stream state.
1146 stream_state
= g_hash_table_lookup(iterator
->stream_states
,
1148 if (!stream_state
) {
1150 * This iterator did not bump into this stream yet:
1151 * create a stream state and a "stream begin"
1154 struct action action
= {
1155 .type
= ACTION_TYPE_ADD_STREAM_STATE
,
1156 .payload
.add_stream_state
= {
1157 .stream
= bt_get(notif_stream
),
1158 .stream_state
= NULL
,
1162 stream_state
= create_stream_state(notif_stream
);
1163 if (!stream_state
) {
1164 BT_LOGE_STR("Cannot create stream state.");
1168 action
.payload
.add_stream_state
.stream_state
= stream_state
;
1169 add_action(iterator
, &action
);
1171 if (stream_begin_notif
) {
1172 add_action_push_notif(iterator
, stream_begin_notif
);
1174 ret
= add_action_push_notif_stream_begin(iterator
,
1177 BT_LOGE_STR("Cannot add \"push stream beginning notification\" action.");
1185 destroy_stream_state(stream_state
);
1186 stream_state
= NULL
;
1190 *_stream_state
= stream_state
;
1195 struct bt_ctf_field
*get_struct_field_uint(struct bt_ctf_field
*struct_field
,
1196 const char *field_name
)
1198 struct bt_ctf_field
*field
= NULL
;
1199 struct bt_ctf_field_type
*ft
= NULL
;
1201 field
= bt_ctf_field_structure_get_field_by_name(struct_field
,
1204 BT_LOGV_STR("`%s` field does not exist.");
1208 if (!bt_ctf_field_is_integer(field
)) {
1209 BT_LOGV("Skipping `%s` field because its type is not an integer field type: "
1210 "field-addr=%p, ft-addr=%p, ft-id=%s", field_name
,
1211 field
, ft
, bt_ctf_field_type_id_string(
1212 bt_ctf_field_type_get_type_id(ft
)));
1217 ft
= bt_ctf_field_get_type(field
);
1220 if (bt_ctf_field_type_integer_is_signed(ft
)) {
1221 BT_LOGV("Skipping `%s` integer field because its type is signed: "
1222 "field-addr=%p, ft-addr=%p", field_name
, field
, ft
);
1233 uint64_t get_packet_context_events_discarded(struct bt_ctf_packet
*packet
)
1235 struct bt_ctf_field
*packet_context
= NULL
;
1236 struct bt_ctf_field
*field
= NULL
;
1237 uint64_t retval
= -1ULL;
1240 packet_context
= bt_ctf_packet_get_context(packet
);
1241 if (!packet_context
) {
1245 field
= get_struct_field_uint(packet_context
, "events_discarded");
1247 BT_LOGV("`events_discarded` field does not exist in packet's context field: "
1248 "packet-addr=%p, packet-context-field-addr=%p",
1249 packet
, packet_context
);
1253 assert(bt_ctf_field_is_integer(field
));
1254 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &retval
);
1256 BT_LOGV("Cannot get raw value of packet's context field's `events_discarded` integer field: "
1257 "packet-addr=%p, field-addr=%p",
1264 bt_put(packet_context
);
1270 uint64_t get_packet_context_packet_seq_num(struct bt_ctf_packet
*packet
)
1272 struct bt_ctf_field
*packet_context
= NULL
;
1273 struct bt_ctf_field
*field
= NULL
;
1274 uint64_t retval
= -1ULL;
1277 packet_context
= bt_ctf_packet_get_context(packet
);
1278 if (!packet_context
) {
1282 field
= get_struct_field_uint(packet_context
, "packet_seq_num");
1284 BT_LOGV("`packet_seq_num` field does not exist in packet's context field: "
1285 "packet-addr=%p, packet-context-field-addr=%p",
1286 packet
, packet_context
);
1290 assert(bt_ctf_field_is_integer(field
));
1291 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &retval
);
1293 BT_LOGV("Cannot get raw value of packet's context field's `packet_seq_num` integer field: "
1294 "packet-addr=%p, field-addr=%p",
1301 bt_put(packet_context
);
1307 int handle_discarded_packets(struct bt_notification_iterator
*iterator
,
1308 struct bt_ctf_packet
*packet
,
1309 struct bt_ctf_clock_value
*ts_begin
,
1310 struct bt_ctf_clock_value
*ts_end
,
1311 struct stream_state
*stream_state
)
1313 struct bt_notification
*notif
= NULL
;
1315 uint64_t prev_count
, next_count
;
1318 next_count
= get_packet_context_packet_seq_num(packet
);
1319 if (next_count
== -1ULL) {
1321 * Stream does not have seqnum field, skip discarded
1326 prev_count
= stream_state
->discarded_packets_state
.cur_count
;
1328 if (prev_count
!= -1ULL) {
1329 if (next_count
< prev_count
) {
1330 BT_LOGW("Current value of packet's context field's `packet_seq_num` field is lesser than the previous value for the same stream: "
1331 "not updating the stream state's current value: "
1332 "packet-addr=%p, prev-count=%" PRIu64
", "
1333 "cur-count=%" PRIu64
,
1334 packet
, prev_count
, next_count
);
1337 if (next_count
== prev_count
) {
1338 BT_LOGW("Current value of packet's context field's `packet_seq_num` field is equal to the previous value for the same stream: "
1339 "not updating the stream state's current value: "
1340 "packet-addr=%p, prev-count=%" PRIu64
", "
1341 "cur-count=%" PRIu64
,
1342 packet
, prev_count
, next_count
);
1346 diff
= next_count
- prev_count
;
1349 * Add a discarded packets notification. The packets
1350 * are considered to be lost between the state's last time
1351 * and the current packet's beginning time.
1352 * The counter is expected to monotonically increase of
1353 * 1 for each packet. Therefore, the number of missing
1354 * packets is 'diff - 1'.
1356 notif
= bt_notification_discarded_elements_create(
1357 BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
,
1358 stream_state
->stream
,
1359 stream_state
->discarded_packets_state
.cur_begin
,
1360 ts_begin
, diff
- 1);
1362 BT_LOGE_STR("Cannot create discarded packets notification.");
1367 add_action_push_notif(iterator
, notif
);
1371 add_action_update_stream_state_discarded_elements(iterator
,
1372 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_PACKETS
,
1373 stream_state
, ts_end
, next_count
);
1381 int handle_discarded_events(struct bt_notification_iterator
*iterator
,
1382 struct bt_ctf_packet
*packet
,
1383 struct bt_ctf_clock_value
*ts_begin
,
1384 struct bt_ctf_clock_value
*ts_end
,
1385 struct stream_state
*stream_state
)
1387 struct bt_notification
*notif
= NULL
;
1389 uint64_t next_count
;
1392 next_count
= get_packet_context_events_discarded(packet
);
1393 if (next_count
== -1ULL) {
1394 next_count
= stream_state
->discarded_events_state
.cur_count
;
1398 if (next_count
< stream_state
->discarded_events_state
.cur_count
) {
1399 BT_LOGW("Current value of packet's context field's `events_discarded` field is lesser than the previous value for the same stream: "
1400 "not updating the stream state's current value: "
1401 "packet-addr=%p, prev-count=%" PRIu64
", "
1402 "cur-count=%" PRIu64
,
1403 packet
, stream_state
->discarded_events_state
.cur_count
,
1408 diff
= next_count
- stream_state
->discarded_events_state
.cur_count
;
1411 * Add a discarded events notification. The events are
1412 * considered to be lost betweem the state's last time
1413 * and the current packet's end time.
1415 notif
= bt_notification_discarded_elements_create(
1416 BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
,
1417 stream_state
->stream
,
1418 stream_state
->discarded_events_state
.cur_begin
,
1421 BT_LOGE_STR("Cannot create discarded events notification.");
1426 add_action_push_notif(iterator
, notif
);
1430 add_action_update_stream_state_discarded_elements(iterator
,
1431 ACTION_TYPE_UPDATE_STREAM_STATE_DISCARDED_EVENTS
,
1432 stream_state
, ts_end
, next_count
);
1440 int get_field_clock_value(struct bt_ctf_field
*root_field
,
1441 const char *field_name
,
1442 struct bt_ctf_clock_value
**user_clock_val
)
1444 struct bt_ctf_field
*field
;
1445 struct bt_ctf_field_type
*ft
= NULL
;
1446 struct bt_ctf_clock_class
*clock_class
= NULL
;
1447 struct bt_ctf_clock_value
*clock_value
= NULL
;
1451 field
= get_struct_field_uint(root_field
, field_name
);
1453 /* Not an error: skip this */
1457 ft
= bt_ctf_field_get_type(field
);
1459 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(ft
);
1461 BT_LOGW("Integer field type has no mapped clock class but it's expected to have one: "
1467 ret
= bt_ctf_field_unsigned_integer_get_value(field
, &val
);
1469 BT_LOGW("Cannot get integer field's raw value: "
1470 "field-addr=%p", field
);
1475 clock_value
= bt_ctf_clock_value_create(clock_class
, val
);
1477 BT_LOGE_STR("Cannot create clock value from clock class.");
1482 /* Move clock value to user */
1483 *user_clock_val
= clock_value
;
1489 bt_put(clock_class
);
1490 bt_put(clock_value
);
1495 int get_ts_begin_ts_end_from_packet(struct bt_ctf_packet
*packet
,
1496 struct bt_ctf_clock_value
**user_ts_begin
,
1497 struct bt_ctf_clock_value
**user_ts_end
)
1499 struct bt_ctf_field
*packet_context
= NULL
;
1500 struct bt_ctf_clock_value
*ts_begin
= NULL
;
1501 struct bt_ctf_clock_value
*ts_end
= NULL
;
1504 packet_context
= bt_ctf_packet_get_context(packet
);
1505 if (!packet_context
) {
1509 ret
= get_field_clock_value(packet_context
, "timestamp_begin",
1512 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1513 "packet-addr=%p, packet-context-field-addr=%p",
1514 packet
, packet_context
);
1518 ret
= get_field_clock_value(packet_context
, "timestamp_end",
1521 BT_LOGW("Cannot create clock value for packet context's `timestamp_begin` field: "
1522 "packet-addr=%p, packet-context-field-addr=%p",
1523 packet
, packet_context
);
1527 /* Move clock values to user */
1528 *user_ts_begin
= ts_begin
;
1530 *user_ts_end
= ts_end
;
1534 bt_put(packet_context
);
1541 int handle_discarded_elements(struct bt_notification_iterator
*iterator
,
1542 struct bt_ctf_packet
*packet
, struct stream_state
*stream_state
)
1544 struct bt_ctf_clock_value
*ts_begin
= NULL
;
1545 struct bt_ctf_clock_value
*ts_end
= NULL
;
1548 ret
= get_ts_begin_ts_end_from_packet(packet
, &ts_begin
, &ts_end
);
1550 BT_LOGW("Cannot get packet's beginning or end clock values: "
1551 "packet-addr=%p, ret=%d", packet
, ret
);
1556 ret
= handle_discarded_packets(iterator
, packet
, ts_begin
, ts_end
,
1559 BT_LOGW("Cannot handle discarded packets for packet: "
1560 "packet-addr=%p, ret=%d", packet
, ret
);
1565 ret
= handle_discarded_events(iterator
, packet
, ts_begin
, ts_end
,
1568 BT_LOGW("Cannot handle discarded events for packet: "
1569 "packet-addr=%p, ret=%d", packet
, ret
);
1581 int handle_packet_switch(struct bt_notification_iterator
*iterator
,
1582 struct bt_notification
*packet_begin_notif
,
1583 struct bt_ctf_packet
*new_packet
,
1584 struct stream_state
*stream_state
)
1588 if (stream_state
->cur_packet
== new_packet
) {
1592 BT_LOGV("Handling packet switch: "
1593 "cur-packet-addr=%p, new-packet-addr=%p",
1594 stream_state
->cur_packet
, new_packet
);
1596 if (stream_state
->cur_packet
) {
1597 /* End of the current packet */
1598 ret
= add_action_push_notif_packet_end(iterator
,
1599 stream_state
->cur_packet
);
1601 BT_LOGE_STR("Cannot add \"push packet end notification\" action.");
1607 * Check the new packet's context fields for discarded packets
1608 * and events to emit those automatic notifications.
1610 ret
= handle_discarded_elements(iterator
, new_packet
, stream_state
);
1612 BT_LOGE_STR("Cannot handle discarded elements for new packet.");
1616 /* Beginning of the new packet */
1617 if (packet_begin_notif
) {
1618 add_action_push_notif(iterator
, packet_begin_notif
);
1619 } else if (new_packet
) {
1620 ret
= add_action_push_notif_packet_begin(iterator
,
1623 BT_LOGE_STR("Cannot add \"push packet beginning notification\" action.");
1628 add_action_set_stream_state_cur_packet(iterator
, stream_state
,
1640 int handle_notif_stream_begin(
1641 struct bt_notification_iterator
*iterator
,
1642 struct bt_notification
*notif
,
1643 struct bt_ctf_stream
*notif_stream
)
1646 struct stream_state
*stream_state
;
1648 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_BEGIN
);
1649 assert(notif_stream
);
1650 ret
= ensure_stream_state_exists(iterator
, notif
, notif_stream
,
1653 BT_LOGE_STR("Cannot ensure that stream state exists.");
1667 int handle_notif_stream_end(
1668 struct bt_notification_iterator
*iterator
,
1669 struct bt_notification
*notif
,
1670 struct bt_ctf_stream
*notif_stream
)
1673 struct stream_state
*stream_state
;
1675 assert(notif
->type
== BT_NOTIFICATION_TYPE_STREAM_END
);
1676 assert(notif_stream
);
1677 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1680 BT_LOGE_STR("Cannot ensure that stream state exists.");
1684 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1686 BT_LOGE_STR("Cannot handle packet switch.");
1690 add_action_push_notif(iterator
, notif
);
1691 add_action_set_stream_state_is_ended(iterator
, stream_state
);
1702 int handle_notif_discarded_elements(
1703 struct bt_notification_iterator
*iterator
,
1704 struct bt_notification
*notif
,
1705 struct bt_ctf_stream
*notif_stream
)
1708 struct stream_state
*stream_state
;
1710 assert(notif
->type
== BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
||
1711 notif
->type
== BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
);
1712 assert(notif_stream
);
1713 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1716 BT_LOGE_STR("Cannot ensure that stream state exists.");
1720 add_action_push_notif(iterator
, notif
);
1731 int handle_notif_packet_begin(
1732 struct bt_notification_iterator
*iterator
,
1733 struct bt_notification
*notif
,
1734 struct bt_ctf_stream
*notif_stream
,
1735 struct bt_ctf_packet
*notif_packet
)
1738 struct stream_state
*stream_state
;
1740 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_BEGIN
);
1741 assert(notif_packet
);
1742 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1745 BT_LOGE_STR("Cannot ensure that stream state exists.");
1749 ret
= handle_packet_switch(iterator
, notif
, notif_packet
, stream_state
);
1751 BT_LOGE_STR("Cannot handle packet switch.");
1765 int handle_notif_packet_end(
1766 struct bt_notification_iterator
*iterator
,
1767 struct bt_notification
*notif
,
1768 struct bt_ctf_stream
*notif_stream
,
1769 struct bt_ctf_packet
*notif_packet
)
1772 struct stream_state
*stream_state
;
1774 assert(notif
->type
== BT_NOTIFICATION_TYPE_PACKET_END
);
1775 assert(notif_packet
);
1776 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1779 BT_LOGE_STR("Cannot ensure that stream state exists.");
1783 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1785 BT_LOGE_STR("Cannot handle packet switch.");
1789 /* End of the current packet */
1790 add_action_push_notif(iterator
, notif
);
1791 add_action_set_stream_state_cur_packet(iterator
, stream_state
, NULL
);
1802 int handle_notif_event(
1803 struct bt_notification_iterator
*iterator
,
1804 struct bt_notification
*notif
,
1805 struct bt_ctf_stream
*notif_stream
,
1806 struct bt_ctf_packet
*notif_packet
)
1809 struct stream_state
*stream_state
;
1811 assert(notif
->type
== BT_NOTIFICATION_TYPE_EVENT
);
1812 assert(notif_packet
);
1813 ret
= ensure_stream_state_exists(iterator
, NULL
, notif_stream
,
1816 BT_LOGE_STR("Cannot ensure that stream state exists.");
1820 ret
= handle_packet_switch(iterator
, NULL
, notif_packet
, stream_state
);
1822 BT_LOGE_STR("Cannot handle packet switch.");
1826 add_action_push_notif(iterator
, notif
);
1837 int enqueue_notification_and_automatic(
1838 struct bt_notification_iterator
*iterator
,
1839 struct bt_notification
*notif
)
1842 struct bt_ctf_event
*notif_event
= NULL
;
1843 struct bt_ctf_stream
*notif_stream
= NULL
;
1844 struct bt_ctf_packet
*notif_packet
= NULL
;
1848 BT_LOGV("Enqueuing user notification and automatic notifications: "
1849 "iter-addr=%p, notif-addr=%p", iterator
, notif
);
1851 // TODO: Skip most of this if the iterator is only subscribed
1852 // to event/inactivity notifications.
1854 /* Get the stream and packet referred by the notification */
1855 switch (notif
->type
) {
1856 case BT_NOTIFICATION_TYPE_EVENT
:
1857 notif_event
= bt_notification_event_borrow_event(notif
);
1858 assert(notif_event
);
1859 notif_packet
= bt_ctf_event_borrow_packet(notif_event
);
1860 assert(notif_packet
);
1862 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1864 bt_notification_stream_begin_borrow_stream(notif
);
1865 assert(notif_stream
);
1867 case BT_NOTIFICATION_TYPE_STREAM_END
:
1868 notif_stream
= bt_notification_stream_end_borrow_stream(notif
);
1869 assert(notif_stream
);
1871 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1873 bt_notification_packet_begin_borrow_packet(notif
);
1874 assert(notif_packet
);
1876 case BT_NOTIFICATION_TYPE_PACKET_END
:
1877 notif_packet
= bt_notification_packet_end_borrow_packet(notif
);
1878 assert(notif_packet
);
1880 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1885 * Invalid type of notification. Only the notification
1886 * types above are allowed to be returned by a user
1889 BT_LOGF("Unexpected notification type at this point: "
1890 "notif-addr=%p, notif-type=%s", notif
,
1891 bt_notification_type_string(notif
->type
));
1896 notif_stream
= bt_ctf_packet_borrow_stream(notif_packet
);
1897 assert(notif_stream
);
1900 if (!notif_stream
) {
1902 * The notification has no reference to a stream: it
1903 * cannot cause the creation of automatic notifications.
1905 BT_LOGV_STR("Notification has no reference to any stream: skipping automatic notification generation.");
1909 if (!validate_notification(iterator
, notif
, notif_stream
,
1911 BT_LOGW_STR("Invalid notification.");
1916 switch (notif
->type
) {
1917 case BT_NOTIFICATION_TYPE_EVENT
:
1918 ret
= handle_notif_event(iterator
, notif
, notif_stream
,
1921 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
1922 ret
= handle_notif_stream_begin(iterator
, notif
, notif_stream
);
1924 case BT_NOTIFICATION_TYPE_STREAM_END
:
1925 ret
= handle_notif_stream_end(iterator
, notif
, notif_stream
);
1927 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
1928 ret
= handle_notif_packet_begin(iterator
, notif
, notif_stream
,
1931 case BT_NOTIFICATION_TYPE_PACKET_END
:
1932 ret
= handle_notif_packet_end(iterator
, notif
, notif_stream
,
1935 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
1936 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
1937 ret
= handle_notif_discarded_elements(iterator
, notif
,
1940 case BT_NOTIFICATION_TYPE_INACTIVITY
:
1941 add_action_push_notif(iterator
, notif
);
1948 BT_LOGW_STR("Failed to handle notification for automatic notification generation.");
1952 apply_actions(iterator
);
1953 BT_LOGV("Enqueued user notification and automatic notifications: "
1954 "iter-addr=%p, notif-addr=%p", iterator
, notif
);
1965 int handle_end(struct bt_notification_iterator
*iterator
)
1967 GHashTableIter stream_state_iter
;
1968 gpointer stream_gptr
, stream_state_gptr
;
1971 BT_LOGV("Handling end of iteration: addr=%p", iterator
);
1974 * Emit a "stream end" notification for each non-ended stream
1975 * known by this iterator and mark them as ended.
1977 g_hash_table_iter_init(&stream_state_iter
, iterator
->stream_states
);
1979 while (g_hash_table_iter_next(&stream_state_iter
, &stream_gptr
,
1980 &stream_state_gptr
)) {
1981 struct stream_state
*stream_state
= stream_state_gptr
;
1983 assert(stream_state_gptr
);
1985 if (stream_state
->is_ended
) {
1989 ret
= handle_packet_switch(iterator
, NULL
, NULL
, stream_state
);
1991 BT_LOGE_STR("Cannot handle packet switch.");
1995 ret
= add_action_push_notif_stream_end(iterator
, stream_gptr
);
1997 BT_LOGE_STR("Cannot add \"push stream end notification\" action.");
2001 add_action_set_stream_state_is_ended(iterator
, stream_state
);
2004 apply_actions(iterator
);
2005 BT_LOGV("Handled end of iteration: addr=%p", iterator
);
2016 enum bt_notification_iterator_status
ensure_queue_has_notifications(
2017 struct bt_notification_iterator
*iterator
)
2019 struct bt_private_notification_iterator
*priv_iterator
=
2020 bt_private_notification_iterator_from_notification_iterator(iterator
);
2021 bt_component_class_notification_iterator_next_method next_method
= NULL
;
2022 struct bt_notification_iterator_next_return next_return
= {
2023 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
2024 .notification
= NULL
,
2026 enum bt_notification_iterator_status status
=
2027 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
2031 BT_LOGD("Ensuring that notification iterator's queue has at least one notification: "
2032 "iter-addr=%p, queue-size=%u, iter-state=%s",
2033 iterator
, iterator
->queue
->length
,
2034 bt_notification_iterator_state_string(iterator
->state
));
2036 if (iterator
->queue
->length
> 0) {
2038 * We already have enough. Even if this notification
2039 * iterator is finalized, its user can still flush its
2040 * current queue's content by calling its "next" method
2041 * since this content is local and has no impact on what
2042 * used to be the iterator's upstream component.
2044 BT_LOGD_STR("Queue already has at least one notification.");
2048 switch (iterator
->state
) {
2049 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
:
2050 case BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
:
2051 BT_LOGD_STR("Notification iterator's \"next\" called, but it is finalized.");
2052 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
2054 case BT_NOTIFICATION_ITERATOR_STATE_ENDED
:
2055 BT_LOGD_STR("Notification iterator is ended.");
2056 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
2062 assert(iterator
->upstream_component
);
2063 assert(iterator
->upstream_component
->class);
2065 /* Pick the appropriate "next" method */
2066 switch (iterator
->upstream_component
->class->type
) {
2067 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2069 struct bt_component_class_source
*source_class
=
2070 container_of(iterator
->upstream_component
->class,
2071 struct bt_component_class_source
, parent
);
2073 assert(source_class
->methods
.iterator
.next
);
2074 next_method
= source_class
->methods
.iterator
.next
;
2077 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2079 struct bt_component_class_filter
*filter_class
=
2080 container_of(iterator
->upstream_component
->class,
2081 struct bt_component_class_filter
, parent
);
2083 assert(filter_class
->methods
.iterator
.next
);
2084 next_method
= filter_class
->methods
.iterator
.next
;
2092 * Call the user's "next" method to get the next notification
2095 assert(next_method
);
2097 while (iterator
->queue
->length
== 0) {
2098 BT_LOGD_STR("Calling user's \"next\" method.");
2099 next_return
= next_method(priv_iterator
);
2100 BT_LOGD("User method returned: status=%s",
2101 bt_notification_iterator_status_string(next_return
.status
));
2102 if (next_return
.status
< 0) {
2103 BT_LOGW_STR("User method failed.");
2104 status
= next_return
.status
;
2108 if (iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_FINALIZED
||
2109 iterator
->state
== BT_NOTIFICATION_ITERATOR_STATE_FINALIZED_AND_ENDED
) {
2111 * The user's "next" method, somehow, cancelled
2112 * its own notification iterator. This can
2113 * happen, for example, when the user's method
2114 * removes the port on which there's the
2115 * connection from which the iterator was
2116 * created. In this case, said connection is
2117 * ended, and all its notification iterators are
2120 * Only bt_put() the returned notification if
2122 * BT_NOTIFICATION_ITERATOR_STATUS_OK because
2123 * otherwise this field could be garbage.
2125 if (next_return
.status
==
2126 BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
2127 bt_put(next_return
.notification
);
2130 status
= BT_NOTIFICATION_ITERATOR_STATUS_CANCELED
;
2134 switch (next_return
.status
) {
2135 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
2136 ret
= handle_end(iterator
);
2138 BT_LOGW_STR("Cannot handle end of iteration.");
2139 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2143 assert(iterator
->state
==
2144 BT_NOTIFICATION_ITERATOR_STATE_ACTIVE
);
2145 iterator
->state
= BT_NOTIFICATION_ITERATOR_STATE_ENDED
;
2147 if (iterator
->queue
->length
== 0) {
2148 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
2151 BT_LOGD("Set new status: status=%s",
2152 bt_notification_iterator_status_string(status
));
2154 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
2155 status
= BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
;
2157 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
2158 if (!next_return
.notification
) {
2159 BT_LOGW_STR("User method returned BT_NOTIFICATION_ITERATOR_STATUS_OK, but notification is NULL.");
2160 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2165 * Ignore some notifications which are always
2166 * automatically generated by the notification
2167 * iterator to make sure they have valid values.
2169 switch (next_return
.notification
->type
) {
2170 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
2171 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
2172 BT_LOGV("Ignoring discarded elements notification returned by notification iterator's \"next\" method: "
2174 bt_notification_type_string(next_return
.notification
->type
));
2175 BT_PUT(next_return
.notification
);
2182 * We know the notification is valid. Before we
2183 * push it to the head of the queue, push the
2184 * appropriate automatic notifications if any.
2186 ret
= enqueue_notification_and_automatic(iterator
,
2187 next_return
.notification
);
2188 BT_PUT(next_return
.notification
);
2190 BT_LOGW("Cannot enqueue notification and automatic notifications.");
2191 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
2196 /* Unknown non-error status */
2205 enum bt_notification_iterator_status
2206 bt_notification_iterator_next(struct bt_notification_iterator
*iterator
)
2208 enum bt_notification_iterator_status status
;
2211 BT_LOGW_STR("Invalid parameter: notification iterator is NULL.");
2212 status
= BT_NOTIFICATION_ITERATOR_STATUS_INVALID
;
2216 BT_LOGD("Notification iterator's \"next\": iter-addr=%p", iterator
);
2219 * Make sure that the iterator's queue contains at least one
2222 status
= ensure_queue_has_notifications(iterator
);
2223 if (status
!= BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
2229 * Move the notification at the tail of the queue to the
2230 * iterator's current notification.
2232 assert(iterator
->queue
->length
> 0);
2233 bt_put(iterator
->current_notification
);
2234 iterator
->current_notification
= g_queue_pop_tail(iterator
->queue
);
2235 assert(iterator
->current_notification
);
2241 struct bt_component
*bt_notification_iterator_get_component(
2242 struct bt_notification_iterator
*iterator
)
2244 return bt_get(iterator
->upstream_component
);
2247 struct bt_private_component
*
2248 bt_private_notification_iterator_get_private_component(
2249 struct bt_private_notification_iterator
*private_iterator
)
2251 return bt_private_component_from_component(
2252 bt_notification_iterator_get_component(
2253 bt_notification_iterator_from_private(private_iterator
)));