2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT"
27 #include <babeltrace/compat/utc-internal.h>
28 #include <babeltrace/compat/time-internal.h>
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/common-internal.h>
31 #include <plugins-common.h>
32 #include <babeltrace/assert-internal.h>
39 #define NS_PER_S INT64_C(1000000000)
41 static const char * const in_port_name
= "in";
44 unsigned int hour
, minute
, second
, ns
;
47 struct trimmer_bound
{
49 * Nanoseconds from origin, valid if `is_set` is set and
50 * `is_infinite` is false.
52 int64_t ns_from_origin
;
54 /* True if this bound's full time (`ns_from_origin`) is set */
58 * True if this bound represents the infinity (negative or
59 * positive depending on which bound it is). If this is true,
60 * then we don't care about `ns_from_origin` above.
65 * This bound's time without the date; this time is used to set
66 * `ns_from_origin` once we know the date.
68 struct trimmer_time time
;
72 struct trimmer_bound begin
, end
;
76 enum trimmer_iterator_state
{
78 * Find the first message's date and set the bounds's times
81 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
,
84 * Initially seek to the trimming range's beginning time.
86 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
,
89 * Fill the output message queue for as long as received input
90 * messages are within the trimming time range.
92 TRIMMER_ITERATOR_STATE_TRIM
,
94 /* Flush the remaining messages in the output message queue */
95 TRIMMER_ITERATOR_STATE_ENDING
,
97 /* Trimming operation and message iterator is ended */
98 TRIMMER_ITERATOR_STATE_ENDED
,
101 struct trimmer_iterator
{
103 struct trimmer_comp
*trimmer_comp
;
106 bt_self_message_iterator
*self_msg_iter
;
108 enum trimmer_iterator_state state
;
111 bt_self_component_port_input_message_iterator
*upstream_iter
;
112 struct trimmer_bound begin
, end
;
115 * Queue of `const bt_message *` (owned by the queue).
117 * This is where the trimming operation pushes the messages to
118 * output by this message iterator.
120 GQueue
*output_messages
;
123 * Hash table of `bt_stream *` (weak) to
124 * `struct trimmer_iterator_stream_state *` (owned by the HT).
126 GHashTable
*stream_states
;
129 struct trimmer_iterator_stream_state
{
131 * True if both stream beginning and initial stream activity
132 * beginning messages were pushed for this stream.
137 * True if the last pushed message for this stream was a stream
138 * activity end message.
140 bool last_msg_is_stream_activity_end
;
143 * Time to use for a generated stream end activity message when
146 int64_t stream_act_end_ns_from_origin
;
149 const bt_stream
*stream
;
151 /* Owned by this (`NULL` initially and between packets) */
152 const bt_packet
*cur_packet
;
155 const bt_message
*stream_beginning_msg
;
159 void destroy_trimmer_comp(struct trimmer_comp
*trimmer_comp
)
161 BT_ASSERT(trimmer_comp
);
162 g_free(trimmer_comp
);
166 struct trimmer_comp
*create_trimmer_comp(void)
168 return g_new0(struct trimmer_comp
, 1);
172 void trimmer_finalize(bt_self_component_filter
*self_comp
)
174 struct trimmer_comp
*trimmer_comp
=
175 bt_self_component_get_data(
176 bt_self_component_filter_as_self_component(self_comp
));
179 destroy_trimmer_comp(trimmer_comp
);
184 * Sets the time (in ns from origin) of a trimmer bound from date and
187 * Returns a negative value if anything goes wrong.
190 int set_bound_ns_from_origin(struct trimmer_bound
*bound
,
191 unsigned int year
, unsigned int month
, unsigned int day
,
192 unsigned int hour
, unsigned int minute
, unsigned int second
,
193 unsigned int ns
, bool is_gmt
)
203 .tm_year
= year
- 1900,
208 result
= bt_timegm(&tm
);
210 result
= mktime(&tm
);
219 bound
->ns_from_origin
= (int64_t) result
;
220 bound
->ns_from_origin
*= NS_PER_S
;
221 bound
->ns_from_origin
+= ns
;
222 bound
->is_set
= true;
229 * Parses a timestamp, figuring out its format.
231 * Returns a negative value if anything goes wrong.
235 * YYYY-MM-DD hh:mm[:ss[.ns]]
239 * TODO: Check overflows.
242 int set_bound_from_str(const char *str
, struct trimmer_bound
*bound
,
247 unsigned int year
, month
, day
, hour
, minute
, second
, ns
;
250 /* Try `YYYY-MM-DD hh:mm:ss.ns` format */
251 s_ret
= sscanf(str
, "%u-%u-%u %u:%u:%u.%u%c", &year
, &month
, &day
,
252 &hour
, &minute
, &second
, &ns
, &dummy
);
254 ret
= set_bound_ns_from_origin(bound
, year
, month
, day
,
255 hour
, minute
, second
, ns
, is_gmt
);
259 /* Try `YYYY-MM-DD hh:mm:ss` format */
260 s_ret
= sscanf(str
, "%u-%u-%u %u:%u:%u%c", &year
, &month
, &day
,
261 &hour
, &minute
, &second
, &dummy
);
263 ret
= set_bound_ns_from_origin(bound
, year
, month
, day
,
264 hour
, minute
, second
, 0, is_gmt
);
268 /* Try `YYYY-MM-DD hh:mm` format */
269 s_ret
= sscanf(str
, "%u-%u-%u %u:%u%c", &year
, &month
, &day
,
270 &hour
, &minute
, &dummy
);
272 ret
= set_bound_ns_from_origin(bound
, year
, month
, day
,
273 hour
, minute
, 0, 0, is_gmt
);
277 /* Try `YYYY-MM-DD` format */
278 s_ret
= sscanf(str
, "%u-%u-%u%c", &year
, &month
, &day
, &dummy
);
280 ret
= set_bound_ns_from_origin(bound
, year
, month
, day
,
285 /* Try `hh:mm:ss.ns` format */
286 s_ret
= sscanf(str
, "%u:%u:%u.%u%c", &hour
, &minute
, &second
, &ns
,
289 bound
->time
.hour
= hour
;
290 bound
->time
.minute
= minute
;
291 bound
->time
.second
= second
;
296 /* Try `hh:mm:ss` format */
297 s_ret
= sscanf(str
, "%u:%u:%u%c", &hour
, &minute
, &second
, &dummy
);
299 bound
->time
.hour
= hour
;
300 bound
->time
.minute
= minute
;
301 bound
->time
.second
= second
;
306 /* Try `-s.ns` format */
307 s_ret
= sscanf(str
, "-%u.%u%c", &second
, &ns
, &dummy
);
309 bound
->ns_from_origin
= -((int64_t) second
) * NS_PER_S
;
310 bound
->ns_from_origin
-= (int64_t) ns
;
311 bound
->is_set
= true;
315 /* Try `s.ns` format */
316 s_ret
= sscanf(str
, "%u.%u%c", &second
, &ns
, &dummy
);
318 bound
->ns_from_origin
= ((int64_t) second
) * NS_PER_S
;
319 bound
->ns_from_origin
+= (int64_t) ns
;
320 bound
->is_set
= true;
324 /* Try `-s` format */
325 s_ret
= sscanf(str
, "-%u%c", &second
, &dummy
);
327 bound
->ns_from_origin
= -((int64_t) second
) * NS_PER_S
;
328 bound
->is_set
= true;
333 s_ret
= sscanf(str
, "%u%c", &second
, &dummy
);
335 bound
->ns_from_origin
= (int64_t) second
* NS_PER_S
;
336 bound
->is_set
= true;
340 BT_LOGE("Invalid date/time format: param=\"%s\"", str
);
348 * Sets a trimmer bound's properties from a parameter string/integer
351 * Returns a negative value if anything goes wrong.
354 int set_bound_from_param(const char *param_name
, const bt_value
*param
,
355 struct trimmer_bound
*bound
, bool is_gmt
)
361 if (bt_value_is_integer(param
)) {
362 int64_t value
= bt_value_integer_get(param
);
365 * Just convert it to a temporary string to handle
366 * everything the same way.
368 sprintf(tmp_arg
, "%" PRId64
, value
);
370 } else if (bt_value_is_string(param
)) {
371 arg
= bt_value_string_get(param
);
373 BT_LOGE("`%s` parameter must be an integer or a string value.",
379 ret
= set_bound_from_str(arg
, bound
, is_gmt
);
386 int validate_trimmer_bounds(struct trimmer_bound
*begin
,
387 struct trimmer_bound
*end
)
391 BT_ASSERT(begin
->is_set
);
392 BT_ASSERT(end
->is_set
);
394 if (!begin
->is_infinite
&& !end
->is_infinite
&&
395 begin
->ns_from_origin
> end
->ns_from_origin
) {
396 BT_LOGE("Trimming time range's beginning time is greater than end time: "
397 "begin-ns-from-origin=%" PRId64
", "
398 "end-ns-from-origin=%" PRId64
,
399 begin
->ns_from_origin
,
400 end
->ns_from_origin
);
405 if (!begin
->is_infinite
&& begin
->ns_from_origin
== INT64_MIN
) {
406 BT_LOGE("Invalid trimming time range's beginning time: "
407 "ns-from-origin=%" PRId64
,
408 begin
->ns_from_origin
);
413 if (!end
->is_infinite
&& end
->ns_from_origin
== INT64_MIN
) {
414 BT_LOGE("Invalid trimming time range's end time: "
415 "ns-from-origin=%" PRId64
,
416 end
->ns_from_origin
);
426 int init_trimmer_comp_from_params(struct trimmer_comp
*trimmer_comp
,
427 const bt_value
*params
)
429 const bt_value
*value
;
433 value
= bt_value_map_borrow_entry_value_const(params
, "gmt");
435 trimmer_comp
->is_gmt
= (bool) bt_value_bool_get(value
);
438 value
= bt_value_map_borrow_entry_value_const(params
, "begin");
440 if (set_bound_from_param("begin", value
,
441 &trimmer_comp
->begin
, trimmer_comp
->is_gmt
)) {
442 /* set_bound_from_param() logs errors */
443 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
447 trimmer_comp
->begin
.is_infinite
= true;
448 trimmer_comp
->begin
.is_set
= true;
451 value
= bt_value_map_borrow_entry_value_const(params
, "end");
453 if (set_bound_from_param("end", value
,
454 &trimmer_comp
->end
, trimmer_comp
->is_gmt
)) {
455 /* set_bound_from_param() logs errors */
456 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
460 trimmer_comp
->end
.is_infinite
= true;
461 trimmer_comp
->end
.is_set
= true;
465 if (trimmer_comp
->begin
.is_set
&& trimmer_comp
->end
.is_set
) {
466 /* validate_trimmer_bounds() logs errors */
467 ret
= validate_trimmer_bounds(&trimmer_comp
->begin
,
474 bt_self_component_status
trimmer_init(bt_self_component_filter
*self_comp
,
475 const bt_value
*params
, void *init_data
)
478 bt_self_component_status status
;
479 struct trimmer_comp
*trimmer_comp
= create_trimmer_comp();
482 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
486 status
= bt_self_component_filter_add_input_port(
487 self_comp
, in_port_name
, NULL
, NULL
);
488 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
492 status
= bt_self_component_filter_add_output_port(
493 self_comp
, "out", NULL
, NULL
);
494 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
498 ret
= init_trimmer_comp_from_params(trimmer_comp
, params
);
500 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
504 bt_self_component_set_data(
505 bt_self_component_filter_as_self_component(self_comp
),
510 if (status
== BT_SELF_COMPONENT_STATUS_OK
) {
511 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
515 destroy_trimmer_comp(trimmer_comp
);
523 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
525 BT_ASSERT(trimmer_it
);
526 bt_self_component_port_input_message_iterator_put_ref(
527 trimmer_it
->upstream_iter
);
529 if (trimmer_it
->output_messages
) {
530 g_queue_free(trimmer_it
->output_messages
);
533 if (trimmer_it
->stream_states
) {
534 g_hash_table_destroy(trimmer_it
->stream_states
);
541 void destroy_trimmer_iterator_stream_state(
542 struct trimmer_iterator_stream_state
*sstate
)
545 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
546 BT_MESSAGE_PUT_REF_AND_RESET(sstate
->stream_beginning_msg
);
551 bt_self_message_iterator_status
trimmer_msg_iter_init(
552 bt_self_message_iterator
*self_msg_iter
,
553 bt_self_component_filter
*self_comp
,
554 bt_self_component_port_output
*port
)
556 bt_self_message_iterator_status status
=
557 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
558 struct trimmer_iterator
*trimmer_it
;
560 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
562 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
566 trimmer_it
->trimmer_comp
= bt_self_component_get_data(
567 bt_self_component_filter_as_self_component(self_comp
));
568 BT_ASSERT(trimmer_it
->trimmer_comp
);
570 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
571 trimmer_it
->trimmer_comp
->end
.is_set
) {
573 * Both trimming time range's bounds are set, so skip
575 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
578 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
581 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
582 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
583 trimmer_it
->upstream_iter
=
584 bt_self_component_port_input_message_iterator_create(
585 bt_self_component_filter_borrow_input_port_by_name(
586 self_comp
, in_port_name
));
587 if (!trimmer_it
->upstream_iter
) {
588 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
592 trimmer_it
->output_messages
= g_queue_new();
593 if (!trimmer_it
->output_messages
) {
594 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
598 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
599 g_direct_equal
, NULL
,
600 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
601 if (!trimmer_it
->stream_states
) {
602 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
606 trimmer_it
->self_msg_iter
= self_msg_iter
;
607 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
610 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
&& trimmer_it
) {
611 destroy_trimmer_iterator(trimmer_it
);
618 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
621 const bt_clock_class
*clock_class
= NULL
;
622 const bt_clock_snapshot
*clock_snapshot
= NULL
;
623 bt_clock_snapshot_state cs_state
= BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
624 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
628 BT_ASSERT(ns_from_origin
);
631 switch (bt_message_get_type(msg
)) {
632 case BT_MESSAGE_TYPE_EVENT
:
634 bt_message_event_borrow_stream_class_default_clock_class_const(
636 if (unlikely(!clock_class
)) {
640 cs_state
= bt_message_event_borrow_default_clock_snapshot_const(
641 msg
, &clock_snapshot
);
643 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
645 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
647 if (unlikely(!clock_class
)) {
651 cs_state
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
652 msg
, &clock_snapshot
);
654 case BT_MESSAGE_TYPE_PACKET_END
:
656 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
658 if (unlikely(!clock_class
)) {
662 cs_state
= bt_message_packet_end_borrow_default_clock_snapshot_const(
663 msg
, &clock_snapshot
);
665 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
667 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
669 if (unlikely(!clock_class
)) {
673 cs_state
= bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
674 msg
, &clock_snapshot
);
676 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
678 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
680 if (unlikely(!clock_class
)) {
684 cs_state
= bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
685 msg
, &clock_snapshot
);
687 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
689 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
691 if (unlikely(!clock_class
)) {
695 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
696 msg
, &clock_snapshot
);
697 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
||
698 sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
) {
699 /* Lowest possible time to always include them */
700 *ns_from_origin
= INT64_MIN
;
701 goto no_clock_snapshot
;
705 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
707 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
709 if (unlikely(!clock_class
)) {
713 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
714 msg
, &clock_snapshot
);
715 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
716 /* Lowest time to always include it */
717 *ns_from_origin
= INT64_MIN
;
718 goto no_clock_snapshot
;
719 } else if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
) {
720 /* Greatest time to always exclude it */
721 *ns_from_origin
= INT64_MAX
;
722 goto no_clock_snapshot
;
726 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
728 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
729 msg
, &clock_snapshot
);
732 goto no_clock_snapshot
;
735 if (unlikely(cs_state
!= BT_CLOCK_SNAPSHOT_STATE_KNOWN
)) {
736 BT_LOGE_STR("Unsupported unknown clock snapshot.");
741 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
,
761 void put_messages(bt_message_array_const msgs
, uint64_t count
)
765 for (i
= 0; i
< count
; i
++) {
766 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
771 int set_trimmer_iterator_bound(struct trimmer_bound
*bound
,
772 int64_t ns_from_origin
, bool is_gmt
)
775 time_t time_seconds
= (time_t) (ns_from_origin
/ NS_PER_S
);
778 BT_ASSERT(!bound
->is_set
);
781 /* We only need to extract the date from this time */
783 bt_gmtime_r(&time_seconds
, &tm
);
785 bt_localtime_r(&time_seconds
, &tm
);
789 BT_LOGE_ERRNO("Cannot convert timestamp to date and time",
790 "ts=%" PRId64
, (int64_t) time_seconds
);
795 ret
= set_bound_ns_from_origin(bound
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
796 tm
.tm_mday
, bound
->time
.hour
, bound
->time
.minute
,
797 bound
->time
.second
, bound
->time
.ns
, is_gmt
);
804 bt_self_message_iterator_status
state_set_trimmer_iterator_bounds(
805 struct trimmer_iterator
*trimmer_it
)
807 bt_message_iterator_status upstream_iter_status
=
808 BT_MESSAGE_ITERATOR_STATUS_OK
;
809 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
810 bt_message_array_const msgs
;
812 int64_t ns_from_origin
= INT64_MIN
;
816 BT_ASSERT(!trimmer_it
->begin
.is_set
||
817 !trimmer_it
->end
.is_set
);
820 upstream_iter_status
=
821 bt_self_component_port_input_message_iterator_next(
822 trimmer_it
->upstream_iter
, &msgs
, &count
);
823 if (upstream_iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
827 for (i
= 0; i
< count
; i
++) {
828 const bt_message
*msg
= msgs
[i
];
832 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
,
842 BT_ASSERT(ns_from_origin
!= INT64_MIN
&&
843 ns_from_origin
!= INT64_MAX
);
844 put_messages(msgs
, count
);
848 put_messages(msgs
, count
);
852 if (!trimmer_it
->begin
.is_set
) {
853 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
854 ret
= set_trimmer_iterator_bound(&trimmer_it
->begin
,
855 ns_from_origin
, trimmer_comp
->is_gmt
);
861 if (!trimmer_it
->end
.is_set
) {
862 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
863 ret
= set_trimmer_iterator_bound(&trimmer_it
->end
,
864 ns_from_origin
, trimmer_comp
->is_gmt
);
870 ret
= validate_trimmer_bounds(&trimmer_it
->begin
,
879 put_messages(msgs
, count
);
880 upstream_iter_status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
883 return (int) upstream_iter_status
;
887 bt_self_message_iterator_status
state_seek_initially(
888 struct trimmer_iterator
*trimmer_it
)
890 bt_self_message_iterator_status status
=
891 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
893 BT_ASSERT(trimmer_it
->begin
.is_set
);
895 if (trimmer_it
->begin
.is_infinite
) {
896 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
897 trimmer_it
->upstream_iter
)) {
898 BT_LOGE_STR("Cannot make upstream message iterator initially seek its beginning.");
899 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
903 status
= (int) bt_self_component_port_input_message_iterator_seek_beginning(
904 trimmer_it
->upstream_iter
);
906 if (!bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
907 trimmer_it
->upstream_iter
,
908 trimmer_it
->begin
.ns_from_origin
)) {
909 BT_LOGE("Cannot make upstream message iterator initially seek: "
910 "seek-ns-from-origin=%" PRId64
,
911 trimmer_it
->begin
.ns_from_origin
);
912 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
916 status
= (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin(
917 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
920 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
921 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_TRIM
;
929 void push_message(struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
)
931 g_queue_push_head(trimmer_it
->output_messages
, (void *) msg
);
935 const bt_message
*pop_message(struct trimmer_iterator
*trimmer_it
)
937 return g_queue_pop_tail(trimmer_it
->output_messages
);
941 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
942 int64_t ns_from_origin
, uint64_t *raw_value
)
946 uint64_t cc_offset_cycles
;
949 bt_clock_class_get_offset(clock_class
, &cc_offset_s
, &cc_offset_cycles
);
950 cc_freq
= bt_clock_class_get_frequency(clock_class
);
951 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
952 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
956 bt_self_message_iterator_status
end_stream(struct trimmer_iterator
*trimmer_it
,
957 struct trimmer_iterator_stream_state
*sstate
)
959 bt_self_message_iterator_status status
=
960 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
962 const bt_clock_class
*clock_class
;
964 bt_message
*msg
= NULL
;
966 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
968 if (!sstate
->stream
) {
972 if (sstate
->cur_packet
) {
974 * The last message could not have been a stream
975 * activity end message if we have a current packet.
977 BT_ASSERT(!sstate
->last_msg_is_stream_activity_end
);
980 * Create and push a packet end message, making its time
981 * the trimming range's end time.
983 clock_class
= bt_stream_class_borrow_default_clock_class_const(
984 bt_stream_borrow_class_const(sstate
->stream
));
985 BT_ASSERT(clock_class
);
986 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
987 trimmer_it
->end
.ns_from_origin
, &raw_value
);
989 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
993 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
994 trimmer_it
->self_msg_iter
, sstate
->cur_packet
,
997 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1001 push_message(trimmer_it
, msg
);
1003 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1006 * Because we generated a packet end message, set the
1007 * stream activity end message's time to use to the
1008 * trimming range's end time (this packet end message's
1011 sstate
->stream_act_end_ns_from_origin
=
1012 trimmer_it
->end
.ns_from_origin
;
1015 if (!sstate
->last_msg_is_stream_activity_end
) {
1016 /* Create and push a stream activity end message */
1017 msg
= bt_message_stream_activity_end_create(
1018 trimmer_it
->self_msg_iter
, sstate
->stream
);
1020 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1024 clock_class
= bt_stream_class_borrow_default_clock_class_const(
1025 bt_stream_borrow_class_const(sstate
->stream
));
1026 BT_ASSERT(clock_class
);
1027 BT_ASSERT(sstate
->stream_act_end_ns_from_origin
!= INT64_MIN
);
1028 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1029 sstate
->stream_act_end_ns_from_origin
, &raw_value
);
1031 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1035 bt_message_stream_activity_end_set_default_clock_snapshot(
1037 push_message(trimmer_it
, msg
);
1041 /* Create and push a stream end message */
1042 msg
= bt_message_stream_end_create(trimmer_it
->self_msg_iter
,
1045 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1049 push_message(trimmer_it
, msg
);
1053 * Just to make sure that we don't use this stream state again
1054 * in the future without an obvious error.
1056 sstate
->stream
= NULL
;
1059 bt_message_put_ref(msg
);
1064 bt_self_message_iterator_status
end_iterator_streams(
1065 struct trimmer_iterator
*trimmer_it
)
1067 bt_self_message_iterator_status status
=
1068 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1069 GHashTableIter iter
;
1070 gpointer key
, sstate
;
1072 if (trimmer_it
->end
.is_infinite
) {
1074 * An infinite trimming range's end time guarantees that
1075 * we received (and pushed) all the appropriate end
1082 * End each stream and then remove them from the hash table of
1083 * stream states to release unneeded references.
1085 g_hash_table_iter_init(&iter
, trimmer_it
->stream_states
);
1087 while (g_hash_table_iter_next(&iter
, &key
, &sstate
)) {
1088 status
= end_stream(trimmer_it
, sstate
);
1095 g_hash_table_remove_all(trimmer_it
->stream_states
);
1102 bt_self_message_iterator_status
create_stream_beginning_activity_message(
1103 struct trimmer_iterator
*trimmer_it
,
1104 const bt_stream
*stream
,
1105 const bt_clock_class
*clock_class
, bt_message
**msg
)
1107 bt_self_message_iterator_status status
=
1108 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1111 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1113 *msg
= bt_message_stream_activity_beginning_create(
1114 trimmer_it
->self_msg_iter
, stream
);
1116 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1124 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1125 trimmer_it
->begin
.ns_from_origin
, &raw_value
);
1127 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1128 bt_message_put_ref(*msg
);
1132 bt_message_stream_activity_beginning_set_default_clock_snapshot(
1141 * Makes sure to initialize a stream state, pushing the appropriate
1144 * `stream_act_beginning_msg` is an initial stream activity beginning
1145 * message to potentially use, depending on its clock snapshot state.
1146 * This function consumes `stream_act_beginning_msg` unconditionally.
1149 bt_self_message_iterator_status
ensure_stream_state_is_inited(
1150 struct trimmer_iterator
*trimmer_it
,
1151 struct trimmer_iterator_stream_state
*sstate
,
1152 const bt_message
*stream_act_beginning_msg
)
1154 bt_self_message_iterator_status status
=
1155 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1156 bt_message
*new_msg
= NULL
;
1157 const bt_clock_class
*clock_class
=
1158 bt_stream_class_borrow_default_clock_class_const(
1159 bt_stream_borrow_class_const(sstate
->stream
));
1161 BT_ASSERT(!sstate
->inited
);
1163 if (!sstate
->stream_beginning_msg
) {
1164 /* No initial stream beginning message: create one */
1165 sstate
->stream_beginning_msg
=
1166 bt_message_stream_beginning_create(
1167 trimmer_it
->self_msg_iter
, sstate
->stream
);
1168 if (!sstate
->stream_beginning_msg
) {
1169 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1174 /* Push initial stream beginning message */
1175 BT_ASSERT(sstate
->stream_beginning_msg
);
1176 push_message(trimmer_it
, sstate
->stream_beginning_msg
);
1177 sstate
->stream_beginning_msg
= NULL
;
1179 if (stream_act_beginning_msg
) {
1181 * Initial stream activity beginning message exists: if
1182 * its time is -inf, then create and push a new one
1183 * having the trimming range's beginning time. Otherwise
1184 * push it as is (known and unknown).
1186 const bt_clock_snapshot
*cs
;
1187 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
1189 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
1190 stream_act_beginning_msg
, &cs
);
1191 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
&&
1192 !trimmer_it
->begin
.is_infinite
) {
1194 * -inf time: use trimming range's beginning
1195 * time (which is not -inf).
1197 status
= create_stream_beginning_activity_message(
1198 trimmer_it
, sstate
->stream
, clock_class
,
1200 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1204 push_message(trimmer_it
, new_msg
);
1207 /* Known/unknown: push as is */
1208 push_message(trimmer_it
, stream_act_beginning_msg
);
1209 stream_act_beginning_msg
= NULL
;
1212 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1215 * No stream beginning activity message: create and push
1218 status
= create_stream_beginning_activity_message(
1219 trimmer_it
, sstate
->stream
, clock_class
, &new_msg
);
1220 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1224 push_message(trimmer_it
, new_msg
);
1228 sstate
->inited
= true;
1231 bt_message_put_ref(new_msg
);
1232 bt_message_put_ref(stream_act_beginning_msg
);
1237 bt_self_message_iterator_status
ensure_cur_packet_exists(
1238 struct trimmer_iterator
*trimmer_it
,
1239 struct trimmer_iterator_stream_state
*sstate
,
1240 const bt_packet
*packet
)
1242 bt_self_message_iterator_status status
=
1243 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1245 const bt_clock_class
*clock_class
=
1246 bt_stream_class_borrow_default_clock_class_const(
1247 bt_stream_borrow_class_const(sstate
->stream
));
1248 bt_message
*msg
= NULL
;
1251 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1252 BT_ASSERT(!sstate
->cur_packet
);
1255 * Create and push an initial packet beginning message,
1256 * making its time the trimming range's beginning time.
1258 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1259 trimmer_it
->begin
.ns_from_origin
, &raw_value
);
1261 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1265 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1266 trimmer_it
->self_msg_iter
, packet
, raw_value
);
1268 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1272 push_message(trimmer_it
, msg
);
1275 /* Set packet as this stream's current packet */
1276 sstate
->cur_packet
= packet
;
1277 bt_packet_get_ref(sstate
->cur_packet
);
1280 bt_message_put_ref(msg
);
1285 * Handles a message which is associated to a given stream state. This
1286 * _could_ make the iterator's output message queue grow; this could
1287 * also consume the message without pushing anything to this queue, only
1288 * modifying the stream state.
1290 * This function consumes the `msg` reference, _whatever the outcome_.
1292 * `ns_from_origin` is the message's time, as given by
1293 * get_msg_ns_from_origin().
1295 * This function sets `reached_end` if handling this message made the
1296 * iterator reach the end of the trimming range. Note that the output
1297 * message queue could contain messages even if this function sets
1301 bt_self_message_iterator_status
handle_message_with_stream_state(
1302 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1303 struct trimmer_iterator_stream_state
*sstate
,
1304 int64_t ns_from_origin
, bool *reached_end
)
1306 bt_self_message_iterator_status status
=
1307 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1308 bt_message_type msg_type
= bt_message_get_type(msg
);
1312 case BT_MESSAGE_TYPE_EVENT
:
1313 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1314 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1315 status
= end_iterator_streams(trimmer_it
);
1316 *reached_end
= true;
1320 if (unlikely(!sstate
->inited
)) {
1321 status
= ensure_stream_state_is_inited(trimmer_it
,
1323 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1328 if (unlikely(!sstate
->cur_packet
)) {
1329 const bt_event
*event
=
1330 bt_message_event_borrow_event_const(msg
);
1331 const bt_packet
*packet
= bt_event_borrow_packet_const(
1334 status
= ensure_cur_packet_exists(trimmer_it
, sstate
,
1336 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1341 BT_ASSERT(sstate
->cur_packet
);
1342 push_message(trimmer_it
, msg
);
1345 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1346 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1347 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1348 status
= end_iterator_streams(trimmer_it
);
1349 *reached_end
= true;
1353 if (unlikely(!sstate
->inited
)) {
1354 status
= ensure_stream_state_is_inited(trimmer_it
,
1356 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1361 BT_ASSERT(!sstate
->cur_packet
);
1362 sstate
->cur_packet
=
1363 bt_message_packet_beginning_borrow_packet_const(msg
);
1364 bt_packet_get_ref(sstate
->cur_packet
);
1365 push_message(trimmer_it
, msg
);
1368 case BT_MESSAGE_TYPE_PACKET_END
:
1369 sstate
->stream_act_end_ns_from_origin
= ns_from_origin
;
1371 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1372 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1373 status
= end_iterator_streams(trimmer_it
);
1374 *reached_end
= true;
1378 if (unlikely(!sstate
->inited
)) {
1379 status
= ensure_stream_state_is_inited(trimmer_it
,
1381 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1386 if (unlikely(!sstate
->cur_packet
)) {
1387 const bt_packet
*packet
=
1388 bt_message_packet_end_borrow_packet_const(msg
);
1390 status
= ensure_cur_packet_exists(trimmer_it
, sstate
,
1392 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1397 BT_ASSERT(sstate
->cur_packet
);
1398 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1399 push_message(trimmer_it
, msg
);
1402 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1403 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1406 * `ns_from_origin` is the message's time range's
1407 * beginning time here.
1409 int64_t end_ns_from_origin
;
1410 const bt_clock_snapshot
*end_cs
;
1412 if (bt_message_get_type(msg
) ==
1413 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1415 * Safe to ignore the return value because we
1416 * know there's a default clock and it's always
1419 (void) bt_message_discarded_events_borrow_default_end_clock_snapshot_const(
1423 * Safe to ignore the return value because we
1424 * know there's a default clock and it's always
1427 (void) bt_message_discarded_packets_borrow_default_end_clock_snapshot_const(
1431 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1432 &end_ns_from_origin
)) {
1433 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1437 sstate
->stream_act_end_ns_from_origin
= end_ns_from_origin
;
1439 if (!trimmer_it
->end
.is_infinite
&&
1440 ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1441 status
= end_iterator_streams(trimmer_it
);
1442 *reached_end
= true;
1446 if (!trimmer_it
->end
.is_infinite
&&
1447 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1449 * This message's end time is outside the
1450 * trimming time range: replace it with a new
1451 * message having an end time equal to the
1452 * trimming time range's end and without a
1455 const bt_clock_class
*clock_class
=
1456 bt_clock_snapshot_borrow_clock_class_const(
1458 const bt_clock_snapshot
*begin_cs
;
1459 bt_message
*new_msg
;
1460 uint64_t end_raw_value
;
1462 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1463 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1465 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1469 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1470 (void) bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
1472 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1473 trimmer_it
->self_msg_iter
,
1475 bt_clock_snapshot_get_value(begin_cs
),
1478 (void) bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
1480 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1481 trimmer_it
->self_msg_iter
,
1483 bt_clock_snapshot_get_value(begin_cs
),
1488 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1492 /* Replace the original message */
1493 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1496 if (unlikely(!sstate
->inited
)) {
1497 status
= ensure_stream_state_is_inited(trimmer_it
,
1499 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1504 push_message(trimmer_it
, msg
);
1508 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1509 if (!trimmer_it
->end
.is_infinite
&&
1510 ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1512 * This only happens when the message's time is
1513 * known and is greater than the trimming
1514 * range's end time. Unknown and -inf times are
1516 * `trimmer_it->end.ns_from_origin`.
1518 status
= end_iterator_streams(trimmer_it
);
1519 *reached_end
= true;
1523 if (!sstate
->inited
) {
1524 status
= ensure_stream_state_is_inited(trimmer_it
,
1527 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1531 push_message(trimmer_it
, msg
);
1536 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1537 if (trimmer_it
->end
.is_infinite
) {
1538 push_message(trimmer_it
, msg
);
1543 if (ns_from_origin
== INT64_MIN
) {
1544 /* Unknown: push as is if stream state is inited */
1545 if (sstate
->inited
) {
1546 push_message(trimmer_it
, msg
);
1548 sstate
->last_msg_is_stream_activity_end
= true;
1550 } else if (ns_from_origin
== INT64_MAX
) {
1551 /* Infinite: use trimming range's end time */
1552 sstate
->stream_act_end_ns_from_origin
=
1553 trimmer_it
->end
.ns_from_origin
;
1555 /* Known: check if outside of trimming range */
1556 if (ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1557 sstate
->stream_act_end_ns_from_origin
=
1558 trimmer_it
->end
.ns_from_origin
;
1559 status
= end_iterator_streams(trimmer_it
);
1560 *reached_end
= true;
1564 if (!sstate
->inited
) {
1566 * First message for this stream is a
1567 * stream activity end: we can't deduce
1568 * anything about the stream activity
1569 * beginning's time, and using this
1570 * message's time would make a useless
1571 * pair of stream activity beginning/end
1572 * with the same time. Just skip this
1573 * message and wait for something
1579 push_message(trimmer_it
, msg
);
1581 sstate
->last_msg_is_stream_activity_end
= true;
1582 sstate
->stream_act_end_ns_from_origin
= ns_from_origin
;
1586 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1588 * We don't know what follows at this point, so just
1589 * keep this message until we know what to do with it
1590 * (it will be used in ensure_stream_state_is_inited()).
1592 BT_ASSERT(!sstate
->inited
);
1593 BT_MESSAGE_MOVE_REF(sstate
->stream_beginning_msg
, msg
);
1595 case BT_MESSAGE_TYPE_STREAM_END
:
1596 if (sstate
->inited
) {
1598 * This is the end of an inited stream: end this
1599 * stream if its stream activity end message
1600 * time is not the trimming range's end time
1601 * (which means the final stream activity end
1602 * message had an infinite time). end_stream()
1603 * will generate its own stream end message.
1605 if (trimmer_it
->end
.is_infinite
) {
1606 push_message(trimmer_it
, msg
);
1608 g_hash_table_remove(trimmer_it
->stream_states
,
1610 } else if (sstate
->stream_act_end_ns_from_origin
<
1611 trimmer_it
->end
.ns_from_origin
) {
1612 status
= end_stream(trimmer_it
, sstate
);
1613 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1617 /* We won't need this stream state again */
1618 g_hash_table_remove(trimmer_it
->stream_states
,
1622 /* We dont't need this stream state anymore */
1623 g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1632 /* We release the message's reference whatever the outcome */
1633 bt_message_put_ref(msg
);
1634 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1638 * Handles an input message. This _could_ make the iterator's output
1639 * message queue grow; this could also consume the message without
1640 * pushing anything to this queue, only modifying the stream state.
1642 * This function consumes the `msg` reference, _whatever the outcome_.
1644 * This function sets `reached_end` if handling this message made the
1645 * iterator reach the end of the trimming range. Note that the output
1646 * message queue could contain messages even if this function sets
1650 bt_self_message_iterator_status
handle_message(
1651 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1654 bt_self_message_iterator_status status
;
1655 const bt_stream
*stream
= NULL
;
1656 int64_t ns_from_origin
= INT64_MIN
;
1659 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1661 /* Find message's associated stream */
1662 switch (bt_message_get_type(msg
)) {
1663 case BT_MESSAGE_TYPE_EVENT
:
1664 stream
= bt_event_borrow_stream_const(
1665 bt_message_event_borrow_event_const(msg
));
1667 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1668 stream
= bt_packet_borrow_stream_const(
1669 bt_message_packet_beginning_borrow_packet_const(msg
));
1671 case BT_MESSAGE_TYPE_PACKET_END
:
1672 stream
= bt_packet_borrow_stream_const(
1673 bt_message_packet_end_borrow_packet_const(msg
));
1675 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1676 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1678 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1679 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1681 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1682 stream
= bt_message_stream_activity_beginning_borrow_stream_const(msg
);
1684 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1685 stream
= bt_message_stream_activity_end_borrow_stream_const(msg
);
1687 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1688 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1690 case BT_MESSAGE_TYPE_STREAM_END
:
1691 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1697 if (likely(stream
)) {
1698 /* Find stream state */
1699 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
,
1701 if (unlikely(!sstate
)) {
1702 /* No stream state yet: create one now */
1703 const bt_stream_class
*sc
;
1706 * Validate right now that the stream's class
1707 * has a registered default clock class so that
1708 * an existing stream state guarantees existing
1709 * default clock snapshots for its associated
1712 * Also check that clock snapshots are always
1715 sc
= bt_stream_borrow_class_const(stream
);
1716 if (!bt_stream_class_borrow_default_clock_class_const(sc
)) {
1717 BT_LOGE("Unsupported stream: stream class does "
1718 "not have a default clock class: "
1720 "stream-id=%" PRIu64
", "
1721 "stream-name=\"%s\"",
1722 stream
, bt_stream_get_id(stream
),
1723 bt_stream_get_name(stream
));
1724 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1728 if (!bt_stream_class_default_clock_is_always_known(sc
)) {
1729 BT_LOGE("Unsupported stream: clock does not "
1730 "always have a known value: "
1732 "stream-id=%" PRIu64
", "
1733 "stream-name=\"%s\"",
1734 stream
, bt_stream_get_id(stream
),
1735 bt_stream_get_name(stream
));
1736 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1740 sstate
= g_new0(struct trimmer_iterator_stream_state
,
1743 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1747 sstate
->stream
= stream
;
1748 sstate
->stream_act_end_ns_from_origin
= INT64_MIN
;
1749 g_hash_table_insert(trimmer_it
->stream_states
,
1750 (void *) stream
, sstate
);
1754 /* Retrieve the message's time */
1755 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &skip
);
1756 if (unlikely(ret
)) {
1757 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1761 if (likely(sstate
)) {
1762 /* Message associated to a stream */
1763 status
= handle_message_with_stream_state(trimmer_it
, msg
,
1764 sstate
, ns_from_origin
, reached_end
);
1767 * handle_message_with_stream_state() unconditionally
1773 * Message not associated to a stream (message iterator
1776 if (unlikely(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1777 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1778 status
= end_iterator_streams(trimmer_it
);
1779 *reached_end
= true;
1781 push_message(trimmer_it
, msg
);
1782 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1788 /* We release the message's reference whatever the outcome */
1789 bt_message_put_ref(msg
);
1794 void fill_message_array_from_output_messages(
1795 struct trimmer_iterator
*trimmer_it
,
1796 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1801 * Move auto-seek messages to the output array (which is this
1802 * iterator's base message array).
1804 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1805 msgs
[*count
] = pop_message(trimmer_it
);
1810 BT_ASSERT(*count
> 0);
1814 bt_self_message_iterator_status
state_ending(
1815 struct trimmer_iterator
*trimmer_it
,
1816 bt_message_array_const msgs
, uint64_t capacity
,
1819 bt_self_message_iterator_status status
=
1820 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1822 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1823 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1824 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1828 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1836 bt_self_message_iterator_status
state_trim(struct trimmer_iterator
*trimmer_it
,
1837 bt_message_array_const msgs
, uint64_t capacity
,
1840 bt_self_message_iterator_status status
=
1841 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1842 bt_message_array_const my_msgs
;
1845 bool reached_end
= false;
1847 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1848 status
= (int) bt_self_component_port_input_message_iterator_next(
1849 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1850 if (unlikely(status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
)) {
1851 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_END
) {
1852 status
= end_iterator_streams(trimmer_it
);
1853 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1858 TRIMMER_ITERATOR_STATE_ENDING
;
1859 status
= state_ending(trimmer_it
, msgs
,
1866 BT_ASSERT(my_count
> 0);
1868 for (i
= 0; i
< my_count
; i
++) {
1869 status
= handle_message(trimmer_it
, my_msgs
[i
],
1873 * handle_message() unconditionally consumes the
1874 * message reference.
1878 if (unlikely(status
!=
1879 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
)) {
1880 put_messages(my_msgs
, my_count
);
1884 if (unlikely(reached_end
)) {
1886 * This message's time was passed the
1887 * trimming time range's end time: we
1888 * are done. Their might still be
1889 * messages in the output message queue,
1890 * so move to the "ending" state and
1891 * apply it immediately since
1892 * state_trim() is called within the
1895 put_messages(my_msgs
, my_count
);
1897 TRIMMER_ITERATOR_STATE_ENDING
;
1898 status
= state_ending(trimmer_it
, msgs
,
1906 * There's at least one message in the output message queue:
1907 * move the messages to the output message array.
1909 BT_ASSERT(!g_queue_is_empty(trimmer_it
->output_messages
));
1910 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1918 bt_self_message_iterator_status
trimmer_msg_iter_next(
1919 bt_self_message_iterator
*self_msg_iter
,
1920 bt_message_array_const msgs
, uint64_t capacity
,
1923 struct trimmer_iterator
*trimmer_it
=
1924 bt_self_message_iterator_get_data(self_msg_iter
);
1925 bt_self_message_iterator_status status
=
1926 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1928 BT_ASSERT(trimmer_it
);
1930 if (likely(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1931 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1932 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1936 switch (trimmer_it
->state
) {
1937 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1938 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1939 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1943 status
= state_seek_initially(trimmer_it
);
1944 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1948 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1949 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1954 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1955 status
= state_seek_initially(trimmer_it
);
1956 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1960 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1961 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1966 case TRIMMER_ITERATOR_STATE_ENDING
:
1967 status
= state_ending(trimmer_it
, msgs
, capacity
,
1969 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1974 case TRIMMER_ITERATOR_STATE_ENDED
:
1975 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1987 void trimmer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1989 struct trimmer_iterator
*trimmer_it
=
1990 bt_self_message_iterator_get_data(self_msg_iter
);
1992 BT_ASSERT(trimmer_it
);
1993 destroy_trimmer_iterator(trimmer_it
);