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 ret
= 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
);
503 bt_self_component_set_data(
504 bt_self_component_filter_as_self_component(self_comp
),
509 if (status
== BT_SELF_COMPONENT_STATUS_OK
) {
510 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
514 destroy_trimmer_comp(trimmer_comp
);
522 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
524 BT_ASSERT(trimmer_it
);
525 bt_self_component_port_input_message_iterator_put_ref(
526 trimmer_it
->upstream_iter
);
528 if (trimmer_it
->output_messages
) {
529 g_queue_free(trimmer_it
->output_messages
);
532 if (trimmer_it
->stream_states
) {
533 g_hash_table_destroy(trimmer_it
->stream_states
);
540 void destroy_trimmer_iterator_stream_state(
541 struct trimmer_iterator_stream_state
*sstate
)
544 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
545 BT_MESSAGE_PUT_REF_AND_RESET(sstate
->stream_beginning_msg
);
550 bt_self_message_iterator_status
trimmer_msg_iter_init(
551 bt_self_message_iterator
*self_msg_iter
,
552 bt_self_component_filter
*self_comp
,
553 bt_self_component_port_output
*port
)
555 bt_self_message_iterator_status status
=
556 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
557 struct trimmer_iterator
*trimmer_it
;
559 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
561 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
565 trimmer_it
->trimmer_comp
= bt_self_component_get_data(
566 bt_self_component_filter_as_self_component(self_comp
));
567 BT_ASSERT(trimmer_it
->trimmer_comp
);
569 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
570 trimmer_it
->trimmer_comp
->end
.is_set
) {
572 * Both trimming time range's bounds are set, so skip
574 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
577 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
580 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
581 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
582 trimmer_it
->upstream_iter
=
583 bt_self_component_port_input_message_iterator_create(
584 bt_self_component_filter_borrow_input_port_by_name(
585 self_comp
, in_port_name
));
586 if (!trimmer_it
->upstream_iter
) {
587 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
591 trimmer_it
->output_messages
= g_queue_new();
592 if (!trimmer_it
->output_messages
) {
593 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
597 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
598 g_direct_equal
, NULL
,
599 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
600 if (!trimmer_it
->stream_states
) {
601 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
605 trimmer_it
->self_msg_iter
= self_msg_iter
;
606 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
609 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
&& trimmer_it
) {
610 destroy_trimmer_iterator(trimmer_it
);
617 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
620 const bt_clock_class
*clock_class
= NULL
;
621 const bt_clock_snapshot
*clock_snapshot
= NULL
;
622 bt_clock_snapshot_state cs_state
= BT_CLOCK_SNAPSHOT_STATE_KNOWN
;
623 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
627 BT_ASSERT(ns_from_origin
);
630 switch (bt_message_get_type(msg
)) {
631 case BT_MESSAGE_TYPE_EVENT
:
633 bt_message_event_borrow_stream_class_default_clock_class_const(
635 if (unlikely(!clock_class
)) {
639 cs_state
= bt_message_event_borrow_default_clock_snapshot_const(
640 msg
, &clock_snapshot
);
642 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
644 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
646 if (unlikely(!clock_class
)) {
650 cs_state
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
651 msg
, &clock_snapshot
);
653 case BT_MESSAGE_TYPE_PACKET_END
:
655 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
657 if (unlikely(!clock_class
)) {
661 cs_state
= bt_message_packet_end_borrow_default_clock_snapshot_const(
662 msg
, &clock_snapshot
);
664 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
666 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
668 if (unlikely(!clock_class
)) {
672 cs_state
= bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
673 msg
, &clock_snapshot
);
675 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
677 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
679 if (unlikely(!clock_class
)) {
683 cs_state
= bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
684 msg
, &clock_snapshot
);
686 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
688 bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const(
690 if (unlikely(!clock_class
)) {
694 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
695 msg
, &clock_snapshot
);
696 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
||
697 sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
) {
698 /* Lowest possible time to always include them */
699 *ns_from_origin
= INT64_MIN
;
700 goto no_clock_snapshot
;
704 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
706 bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const(
708 if (unlikely(!clock_class
)) {
712 sa_cs_state
= bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
713 msg
, &clock_snapshot
);
714 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN
) {
715 /* Lowest time to always include it */
716 *ns_from_origin
= INT64_MIN
;
717 goto no_clock_snapshot
;
718 } else if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
) {
719 /* Greatest time to always exclude it */
720 *ns_from_origin
= INT64_MAX
;
721 goto no_clock_snapshot
;
725 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
727 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
728 msg
, &clock_snapshot
);
731 goto no_clock_snapshot
;
734 if (unlikely(cs_state
!= BT_CLOCK_SNAPSHOT_STATE_KNOWN
)) {
735 BT_LOGE_STR("Unsupported unknown clock snapshot.");
740 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
,
760 void put_messages(bt_message_array_const msgs
, uint64_t count
)
764 for (i
= 0; i
< count
; i
++) {
765 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
770 int set_trimmer_iterator_bound(struct trimmer_bound
*bound
,
771 int64_t ns_from_origin
, bool is_gmt
)
774 time_t time_seconds
= (time_t) (ns_from_origin
/ NS_PER_S
);
777 BT_ASSERT(!bound
->is_set
);
780 /* We only need to extract the date from this time */
782 bt_gmtime_r(&time_seconds
, &tm
);
784 bt_localtime_r(&time_seconds
, &tm
);
788 BT_LOGE_ERRNO("Cannot convert timestamp to date and time",
789 "ts=%" PRId64
, (int64_t) time_seconds
);
794 ret
= set_bound_ns_from_origin(bound
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
795 tm
.tm_mday
, bound
->time
.hour
, bound
->time
.minute
,
796 bound
->time
.second
, bound
->time
.ns
, is_gmt
);
803 bt_self_message_iterator_status
state_set_trimmer_iterator_bounds(
804 struct trimmer_iterator
*trimmer_it
)
806 bt_message_iterator_status upstream_iter_status
=
807 BT_MESSAGE_ITERATOR_STATUS_OK
;
808 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
809 bt_message_array_const msgs
;
811 int64_t ns_from_origin
= INT64_MIN
;
815 BT_ASSERT(!trimmer_it
->begin
.is_set
||
816 !trimmer_it
->end
.is_set
);
819 upstream_iter_status
=
820 bt_self_component_port_input_message_iterator_next(
821 trimmer_it
->upstream_iter
, &msgs
, &count
);
822 if (upstream_iter_status
!= BT_MESSAGE_ITERATOR_STATUS_OK
) {
826 for (i
= 0; i
< count
; i
++) {
827 const bt_message
*msg
= msgs
[i
];
831 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
,
841 BT_ASSERT(ns_from_origin
!= INT64_MIN
&&
842 ns_from_origin
!= INT64_MAX
);
843 put_messages(msgs
, count
);
847 put_messages(msgs
, count
);
851 if (!trimmer_it
->begin
.is_set
) {
852 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
853 ret
= set_trimmer_iterator_bound(&trimmer_it
->begin
,
854 ns_from_origin
, trimmer_comp
->is_gmt
);
860 if (!trimmer_it
->end
.is_set
) {
861 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
862 ret
= set_trimmer_iterator_bound(&trimmer_it
->end
,
863 ns_from_origin
, trimmer_comp
->is_gmt
);
869 ret
= validate_trimmer_bounds(&trimmer_it
->begin
,
878 put_messages(msgs
, count
);
879 upstream_iter_status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
882 return (int) upstream_iter_status
;
886 bt_self_message_iterator_status
state_seek_initially(
887 struct trimmer_iterator
*trimmer_it
)
889 bt_self_message_iterator_status status
=
890 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
892 BT_ASSERT(trimmer_it
->begin
.is_set
);
894 if (trimmer_it
->begin
.is_infinite
) {
895 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
896 trimmer_it
->upstream_iter
)) {
897 BT_LOGE_STR("Cannot make upstream message iterator initially seek its beginning.");
898 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
902 status
= (int) bt_self_component_port_input_message_iterator_seek_beginning(
903 trimmer_it
->upstream_iter
);
905 if (!bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
906 trimmer_it
->upstream_iter
,
907 trimmer_it
->begin
.ns_from_origin
)) {
908 BT_LOGE("Cannot make upstream message iterator initially seek: "
909 "seek-ns-from-origin=%" PRId64
,
910 trimmer_it
->begin
.ns_from_origin
);
911 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
915 status
= (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin(
916 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
919 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
920 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_TRIM
;
928 void push_message(struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
)
930 g_queue_push_head(trimmer_it
->output_messages
, (void *) msg
);
934 const bt_message
*pop_message(struct trimmer_iterator
*trimmer_it
)
936 return g_queue_pop_tail(trimmer_it
->output_messages
);
940 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
941 int64_t ns_from_origin
, uint64_t *raw_value
)
945 uint64_t cc_offset_cycles
;
948 bt_clock_class_get_offset(clock_class
, &cc_offset_s
, &cc_offset_cycles
);
949 cc_freq
= bt_clock_class_get_frequency(clock_class
);
950 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
951 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
955 bt_self_message_iterator_status
end_stream(struct trimmer_iterator
*trimmer_it
,
956 struct trimmer_iterator_stream_state
*sstate
)
958 bt_self_message_iterator_status status
=
959 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
961 const bt_clock_class
*clock_class
;
963 bt_message
*msg
= NULL
;
965 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
967 if (!sstate
->stream
) {
971 if (sstate
->cur_packet
) {
973 * The last message could not have been a stream
974 * activity end message if we have a current packet.
976 BT_ASSERT(!sstate
->last_msg_is_stream_activity_end
);
979 * Create and push a packet end message, making its time
980 * the trimming range's end time.
982 clock_class
= bt_stream_class_borrow_default_clock_class_const(
983 bt_stream_borrow_class_const(sstate
->stream
));
984 BT_ASSERT(clock_class
);
985 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
986 trimmer_it
->end
.ns_from_origin
, &raw_value
);
988 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
992 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
993 trimmer_it
->self_msg_iter
, sstate
->cur_packet
,
996 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1000 push_message(trimmer_it
, msg
);
1002 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1005 * Because we generated a packet end message, set the
1006 * stream activity end message's time to use to the
1007 * trimming range's end time (this packet end message's
1010 sstate
->stream_act_end_ns_from_origin
=
1011 trimmer_it
->end
.ns_from_origin
;
1014 if (!sstate
->last_msg_is_stream_activity_end
) {
1015 /* Create and push a stream activity end message */
1016 msg
= bt_message_stream_activity_end_create(
1017 trimmer_it
->self_msg_iter
, sstate
->stream
);
1019 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1023 clock_class
= bt_stream_class_borrow_default_clock_class_const(
1024 bt_stream_borrow_class_const(sstate
->stream
));
1025 BT_ASSERT(clock_class
);
1026 BT_ASSERT(sstate
->stream_act_end_ns_from_origin
!= INT64_MIN
);
1027 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1028 sstate
->stream_act_end_ns_from_origin
, &raw_value
);
1030 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1034 bt_message_stream_activity_end_set_default_clock_snapshot(
1036 push_message(trimmer_it
, msg
);
1040 /* Create and push a stream end message */
1041 msg
= bt_message_stream_end_create(trimmer_it
->self_msg_iter
,
1044 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1048 push_message(trimmer_it
, msg
);
1052 * Just to make sure that we don't use this stream state again
1053 * in the future without an obvious error.
1055 sstate
->stream
= NULL
;
1058 bt_message_put_ref(msg
);
1063 bt_self_message_iterator_status
end_iterator_streams(
1064 struct trimmer_iterator
*trimmer_it
)
1066 bt_self_message_iterator_status status
=
1067 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1068 GHashTableIter iter
;
1069 gpointer key
, sstate
;
1071 if (trimmer_it
->end
.is_infinite
) {
1073 * An infinite trimming range's end time guarantees that
1074 * we received (and pushed) all the appropriate end
1081 * End each stream and then remove them from the hash table of
1082 * stream states to release unneeded references.
1084 g_hash_table_iter_init(&iter
, trimmer_it
->stream_states
);
1086 while (g_hash_table_iter_next(&iter
, &key
, &sstate
)) {
1087 status
= end_stream(trimmer_it
, sstate
);
1094 g_hash_table_remove_all(trimmer_it
->stream_states
);
1101 bt_self_message_iterator_status
create_stream_beginning_activity_message(
1102 struct trimmer_iterator
*trimmer_it
,
1103 const bt_stream
*stream
,
1104 const bt_clock_class
*clock_class
, bt_message
**msg
)
1106 bt_self_message_iterator_status status
=
1107 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1110 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1112 *msg
= bt_message_stream_activity_beginning_create(
1113 trimmer_it
->self_msg_iter
, stream
);
1115 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1123 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1124 trimmer_it
->begin
.ns_from_origin
, &raw_value
);
1126 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1127 bt_message_put_ref(*msg
);
1131 bt_message_stream_activity_beginning_set_default_clock_snapshot(
1140 * Makes sure to initialize a stream state, pushing the appropriate
1143 * `stream_act_beginning_msg` is an initial stream activity beginning
1144 * message to potentially use, depending on its clock snapshot state.
1145 * This function consumes `stream_act_beginning_msg` unconditionally.
1148 bt_self_message_iterator_status
ensure_stream_state_is_inited(
1149 struct trimmer_iterator
*trimmer_it
,
1150 struct trimmer_iterator_stream_state
*sstate
,
1151 const bt_message
*stream_act_beginning_msg
)
1153 bt_self_message_iterator_status status
=
1154 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1155 bt_message
*new_msg
= NULL
;
1156 const bt_clock_class
*clock_class
=
1157 bt_stream_class_borrow_default_clock_class_const(
1158 bt_stream_borrow_class_const(sstate
->stream
));
1160 BT_ASSERT(!sstate
->inited
);
1162 if (!sstate
->stream_beginning_msg
) {
1163 /* No initial stream beginning message: create one */
1164 sstate
->stream_beginning_msg
=
1165 bt_message_stream_beginning_create(
1166 trimmer_it
->self_msg_iter
, sstate
->stream
);
1167 if (!sstate
->stream_beginning_msg
) {
1168 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1173 /* Push initial stream beginning message */
1174 BT_ASSERT(sstate
->stream_beginning_msg
);
1175 push_message(trimmer_it
, sstate
->stream_beginning_msg
);
1176 sstate
->stream_beginning_msg
= NULL
;
1178 if (stream_act_beginning_msg
) {
1180 * Initial stream activity beginning message exists: if
1181 * its time is -inf, then create and push a new one
1182 * having the trimming range's beginning time. Otherwise
1183 * push it as is (known and unknown).
1185 const bt_clock_snapshot
*cs
;
1186 bt_message_stream_activity_clock_snapshot_state sa_cs_state
;
1188 sa_cs_state
= bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
1189 stream_act_beginning_msg
, &cs
);
1190 if (sa_cs_state
== BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE
&&
1191 !trimmer_it
->begin
.is_infinite
) {
1193 * -inf time: use trimming range's beginning
1194 * time (which is not -inf).
1196 status
= create_stream_beginning_activity_message(
1197 trimmer_it
, sstate
->stream
, clock_class
,
1199 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1203 push_message(trimmer_it
, new_msg
);
1206 /* Known/unknown: push as is */
1207 push_message(trimmer_it
, stream_act_beginning_msg
);
1208 stream_act_beginning_msg
= NULL
;
1211 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1214 * No stream beginning activity message: create and push
1217 status
= create_stream_beginning_activity_message(
1218 trimmer_it
, sstate
->stream
, clock_class
, &new_msg
);
1219 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1223 push_message(trimmer_it
, new_msg
);
1227 sstate
->inited
= true;
1230 bt_message_put_ref(new_msg
);
1231 bt_message_put_ref(stream_act_beginning_msg
);
1236 bt_self_message_iterator_status
ensure_cur_packet_exists(
1237 struct trimmer_iterator
*trimmer_it
,
1238 struct trimmer_iterator_stream_state
*sstate
,
1239 const bt_packet
*packet
)
1241 bt_self_message_iterator_status status
=
1242 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1244 const bt_clock_class
*clock_class
=
1245 bt_stream_class_borrow_default_clock_class_const(
1246 bt_stream_borrow_class_const(sstate
->stream
));
1247 bt_message
*msg
= NULL
;
1250 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1251 BT_ASSERT(!sstate
->cur_packet
);
1254 * Create and push an initial packet beginning message,
1255 * making its time the trimming range's beginning time.
1257 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1258 trimmer_it
->begin
.ns_from_origin
, &raw_value
);
1260 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1264 msg
= bt_message_packet_beginning_create_with_default_clock_snapshot(
1265 trimmer_it
->self_msg_iter
, packet
, raw_value
);
1267 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1271 push_message(trimmer_it
, msg
);
1274 /* Set packet as this stream's current packet */
1275 sstate
->cur_packet
= packet
;
1276 bt_packet_get_ref(sstate
->cur_packet
);
1279 bt_message_put_ref(msg
);
1284 * Handles a message which is associated to a given stream state. This
1285 * _could_ make the iterator's output message queue grow; this could
1286 * also consume the message without pushing anything to this queue, only
1287 * modifying the stream state.
1289 * This function consumes the `msg` reference, _whatever the outcome_.
1291 * `ns_from_origin` is the message's time, as given by
1292 * get_msg_ns_from_origin().
1294 * This function sets `reached_end` if handling this message made the
1295 * iterator reach the end of the trimming range. Note that the output
1296 * message queue could contain messages even if this function sets
1300 bt_self_message_iterator_status
handle_message_with_stream_state(
1301 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1302 struct trimmer_iterator_stream_state
*sstate
,
1303 int64_t ns_from_origin
, bool *reached_end
)
1305 bt_self_message_iterator_status status
=
1306 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1307 bt_message_type msg_type
= bt_message_get_type(msg
);
1311 case BT_MESSAGE_TYPE_EVENT
:
1312 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1313 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1314 status
= end_iterator_streams(trimmer_it
);
1315 *reached_end
= true;
1319 if (unlikely(!sstate
->inited
)) {
1320 status
= ensure_stream_state_is_inited(trimmer_it
,
1322 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1327 if (unlikely(!sstate
->cur_packet
)) {
1328 const bt_event
*event
=
1329 bt_message_event_borrow_event_const(msg
);
1330 const bt_packet
*packet
= bt_event_borrow_packet_const(
1333 status
= ensure_cur_packet_exists(trimmer_it
, sstate
,
1335 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1340 BT_ASSERT(sstate
->cur_packet
);
1341 push_message(trimmer_it
, msg
);
1344 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1345 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1346 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1347 status
= end_iterator_streams(trimmer_it
);
1348 *reached_end
= true;
1352 if (unlikely(!sstate
->inited
)) {
1353 status
= ensure_stream_state_is_inited(trimmer_it
,
1355 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1360 BT_ASSERT(!sstate
->cur_packet
);
1361 sstate
->cur_packet
=
1362 bt_message_packet_beginning_borrow_packet_const(msg
);
1363 bt_packet_get_ref(sstate
->cur_packet
);
1364 push_message(trimmer_it
, msg
);
1367 case BT_MESSAGE_TYPE_PACKET_END
:
1368 sstate
->stream_act_end_ns_from_origin
= ns_from_origin
;
1370 if (unlikely(!trimmer_it
->end
.is_infinite
&&
1371 ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1372 status
= end_iterator_streams(trimmer_it
);
1373 *reached_end
= true;
1377 if (unlikely(!sstate
->inited
)) {
1378 status
= ensure_stream_state_is_inited(trimmer_it
,
1380 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1385 if (unlikely(!sstate
->cur_packet
)) {
1386 const bt_packet
*packet
=
1387 bt_message_packet_end_borrow_packet_const(msg
);
1389 status
= ensure_cur_packet_exists(trimmer_it
, sstate
,
1391 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1396 BT_ASSERT(sstate
->cur_packet
);
1397 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1398 push_message(trimmer_it
, msg
);
1401 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1402 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1405 * `ns_from_origin` is the message's time range's
1406 * beginning time here.
1408 int64_t end_ns_from_origin
;
1409 const bt_clock_snapshot
*end_cs
;
1411 if (bt_message_get_type(msg
) ==
1412 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1414 * Safe to ignore the return value because we
1415 * know there's a default clock and it's always
1418 (void) bt_message_discarded_events_borrow_default_end_clock_snapshot_const(
1422 * Safe to ignore the return value because we
1423 * know there's a default clock and it's always
1426 (void) bt_message_discarded_packets_borrow_default_end_clock_snapshot_const(
1430 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1431 &end_ns_from_origin
)) {
1432 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1436 sstate
->stream_act_end_ns_from_origin
= end_ns_from_origin
;
1438 if (!trimmer_it
->end
.is_infinite
&&
1439 ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1440 status
= end_iterator_streams(trimmer_it
);
1441 *reached_end
= true;
1445 if (!trimmer_it
->end
.is_infinite
&&
1446 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1448 * This message's end time is outside the
1449 * trimming time range: replace it with a new
1450 * message having an end time equal to the
1451 * trimming time range's end and without a
1454 const bt_clock_class
*clock_class
=
1455 bt_clock_snapshot_borrow_clock_class_const(
1457 const bt_clock_snapshot
*begin_cs
;
1458 bt_message
*new_msg
;
1459 uint64_t end_raw_value
;
1461 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1462 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1464 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1468 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1469 (void) bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
1471 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1472 trimmer_it
->self_msg_iter
,
1474 bt_clock_snapshot_get_value(begin_cs
),
1477 (void) bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
1479 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1480 trimmer_it
->self_msg_iter
,
1482 bt_clock_snapshot_get_value(begin_cs
),
1487 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1491 /* Replace the original message */
1492 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1495 if (unlikely(!sstate
->inited
)) {
1496 status
= ensure_stream_state_is_inited(trimmer_it
,
1498 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1503 push_message(trimmer_it
, msg
);
1507 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1508 if (!trimmer_it
->end
.is_infinite
&&
1509 ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1511 * This only happens when the message's time is
1512 * known and is greater than the trimming
1513 * range's end time. Unknown and -inf times are
1515 * `trimmer_it->end.ns_from_origin`.
1517 status
= end_iterator_streams(trimmer_it
);
1518 *reached_end
= true;
1522 if (!sstate
->inited
) {
1523 status
= ensure_stream_state_is_inited(trimmer_it
,
1526 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1530 push_message(trimmer_it
, msg
);
1535 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1536 if (trimmer_it
->end
.is_infinite
) {
1537 push_message(trimmer_it
, msg
);
1542 if (ns_from_origin
== INT64_MIN
) {
1543 /* Unknown: push as is if stream state is inited */
1544 if (sstate
->inited
) {
1545 push_message(trimmer_it
, msg
);
1547 sstate
->last_msg_is_stream_activity_end
= true;
1549 } else if (ns_from_origin
== INT64_MAX
) {
1550 /* Infinite: use trimming range's end time */
1551 sstate
->stream_act_end_ns_from_origin
=
1552 trimmer_it
->end
.ns_from_origin
;
1554 /* Known: check if outside of trimming range */
1555 if (ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1556 sstate
->stream_act_end_ns_from_origin
=
1557 trimmer_it
->end
.ns_from_origin
;
1558 status
= end_iterator_streams(trimmer_it
);
1559 *reached_end
= true;
1563 if (!sstate
->inited
) {
1565 * First message for this stream is a
1566 * stream activity end: we can't deduce
1567 * anything about the stream activity
1568 * beginning's time, and using this
1569 * message's time would make a useless
1570 * pair of stream activity beginning/end
1571 * with the same time. Just skip this
1572 * message and wait for something
1578 push_message(trimmer_it
, msg
);
1580 sstate
->last_msg_is_stream_activity_end
= true;
1581 sstate
->stream_act_end_ns_from_origin
= ns_from_origin
;
1585 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1587 * We don't know what follows at this point, so just
1588 * keep this message until we know what to do with it
1589 * (it will be used in ensure_stream_state_is_inited()).
1591 BT_ASSERT(!sstate
->inited
);
1592 BT_MESSAGE_MOVE_REF(sstate
->stream_beginning_msg
, msg
);
1594 case BT_MESSAGE_TYPE_STREAM_END
:
1595 if (sstate
->inited
) {
1597 * This is the end of an inited stream: end this
1598 * stream if its stream activity end message
1599 * time is not the trimming range's end time
1600 * (which means the final stream activity end
1601 * message had an infinite time). end_stream()
1602 * will generate its own stream end message.
1604 if (trimmer_it
->end
.is_infinite
) {
1605 push_message(trimmer_it
, msg
);
1607 g_hash_table_remove(trimmer_it
->stream_states
,
1609 } else if (sstate
->stream_act_end_ns_from_origin
<
1610 trimmer_it
->end
.ns_from_origin
) {
1611 status
= end_stream(trimmer_it
, sstate
);
1612 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1616 /* We won't need this stream state again */
1617 g_hash_table_remove(trimmer_it
->stream_states
,
1621 /* We dont't need this stream state anymore */
1622 g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1631 /* We release the message's reference whatever the outcome */
1632 bt_message_put_ref(msg
);
1633 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1637 * Handles an input message. This _could_ make the iterator's output
1638 * message queue grow; this could also consume the message without
1639 * pushing anything to this queue, only modifying the stream state.
1641 * This function consumes the `msg` reference, _whatever the outcome_.
1643 * This function sets `reached_end` if handling this message made the
1644 * iterator reach the end of the trimming range. Note that the output
1645 * message queue could contain messages even if this function sets
1649 bt_self_message_iterator_status
handle_message(
1650 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1653 bt_self_message_iterator_status status
;
1654 const bt_stream
*stream
= NULL
;
1655 int64_t ns_from_origin
= INT64_MIN
;
1658 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1660 /* Find message's associated stream */
1661 switch (bt_message_get_type(msg
)) {
1662 case BT_MESSAGE_TYPE_EVENT
:
1663 stream
= bt_event_borrow_stream_const(
1664 bt_message_event_borrow_event_const(msg
));
1666 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1667 stream
= bt_packet_borrow_stream_const(
1668 bt_message_packet_beginning_borrow_packet_const(msg
));
1670 case BT_MESSAGE_TYPE_PACKET_END
:
1671 stream
= bt_packet_borrow_stream_const(
1672 bt_message_packet_end_borrow_packet_const(msg
));
1674 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1675 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1677 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1678 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1680 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING
:
1681 stream
= bt_message_stream_activity_beginning_borrow_stream_const(msg
);
1683 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END
:
1684 stream
= bt_message_stream_activity_end_borrow_stream_const(msg
);
1686 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1687 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1689 case BT_MESSAGE_TYPE_STREAM_END
:
1690 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1696 if (likely(stream
)) {
1697 /* Find stream state */
1698 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
,
1700 if (unlikely(!sstate
)) {
1701 /* No stream state yet: create one now */
1702 const bt_stream_class
*sc
;
1705 * Validate right now that the stream's class
1706 * has a registered default clock class so that
1707 * an existing stream state guarantees existing
1708 * default clock snapshots for its associated
1711 * Also check that clock snapshots are always
1714 sc
= bt_stream_borrow_class_const(stream
);
1715 if (!bt_stream_class_borrow_default_clock_class_const(sc
)) {
1716 BT_LOGE("Unsupported stream: stream class does "
1717 "not have a default clock class: "
1719 "stream-id=%" PRIu64
", "
1720 "stream-name=\"%s\"",
1721 stream
, bt_stream_get_id(stream
),
1722 bt_stream_get_name(stream
));
1723 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1727 if (!bt_stream_class_default_clock_is_always_known(sc
)) {
1728 BT_LOGE("Unsupported stream: clock does not "
1729 "always have a known value: "
1731 "stream-id=%" PRIu64
", "
1732 "stream-name=\"%s\"",
1733 stream
, bt_stream_get_id(stream
),
1734 bt_stream_get_name(stream
));
1735 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1739 sstate
= g_new0(struct trimmer_iterator_stream_state
,
1742 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
1746 sstate
->stream
= stream
;
1747 sstate
->stream_act_end_ns_from_origin
= INT64_MIN
;
1748 g_hash_table_insert(trimmer_it
->stream_states
,
1749 (void *) stream
, sstate
);
1753 /* Retrieve the message's time */
1754 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &skip
);
1755 if (unlikely(ret
)) {
1756 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
1760 if (likely(sstate
)) {
1761 /* Message associated to a stream */
1762 status
= handle_message_with_stream_state(trimmer_it
, msg
,
1763 sstate
, ns_from_origin
, reached_end
);
1766 * handle_message_with_stream_state() unconditionally
1772 * Message not associated to a stream (message iterator
1775 if (unlikely(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1776 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1777 status
= end_iterator_streams(trimmer_it
);
1778 *reached_end
= true;
1780 push_message(trimmer_it
, msg
);
1786 /* We release the message's reference whatever the outcome */
1787 bt_message_put_ref(msg
);
1792 void fill_message_array_from_output_messages(
1793 struct trimmer_iterator
*trimmer_it
,
1794 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1799 * Move auto-seek messages to the output array (which is this
1800 * iterator's base message array).
1802 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1803 msgs
[*count
] = pop_message(trimmer_it
);
1808 BT_ASSERT(*count
> 0);
1812 bt_self_message_iterator_status
state_ending(
1813 struct trimmer_iterator
*trimmer_it
,
1814 bt_message_array_const msgs
, uint64_t capacity
,
1817 bt_self_message_iterator_status status
=
1818 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1820 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1821 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1822 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1826 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1834 bt_self_message_iterator_status
state_trim(struct trimmer_iterator
*trimmer_it
,
1835 bt_message_array_const msgs
, uint64_t capacity
,
1838 bt_self_message_iterator_status status
=
1839 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1840 bt_message_array_const my_msgs
;
1843 bool reached_end
= false;
1845 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1846 status
= (int) bt_self_component_port_input_message_iterator_next(
1847 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1848 if (unlikely(status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
)) {
1849 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_END
) {
1850 status
= end_iterator_streams(trimmer_it
);
1851 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1856 TRIMMER_ITERATOR_STATE_ENDING
;
1857 status
= state_ending(trimmer_it
, msgs
,
1864 BT_ASSERT(my_count
> 0);
1866 for (i
= 0; i
< my_count
; i
++) {
1867 status
= handle_message(trimmer_it
, my_msgs
[i
],
1871 * handle_message() unconditionally consumes the
1872 * message reference.
1876 if (unlikely(status
!=
1877 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
)) {
1878 put_messages(my_msgs
, my_count
);
1882 if (unlikely(reached_end
)) {
1884 * This message's time was passed the
1885 * trimming time range's end time: we
1886 * are done. Their might still be
1887 * messages in the output message queue,
1888 * so move to the "ending" state and
1889 * apply it immediately since
1890 * state_trim() is called within the
1893 put_messages(my_msgs
, my_count
);
1895 TRIMMER_ITERATOR_STATE_ENDING
;
1896 status
= state_ending(trimmer_it
, msgs
,
1904 * There's at least one message in the output message queue:
1905 * move the messages to the output message array.
1907 BT_ASSERT(!g_queue_is_empty(trimmer_it
->output_messages
));
1908 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1916 bt_self_message_iterator_status
trimmer_msg_iter_next(
1917 bt_self_message_iterator
*self_msg_iter
,
1918 bt_message_array_const msgs
, uint64_t capacity
,
1921 struct trimmer_iterator
*trimmer_it
=
1922 bt_self_message_iterator_get_data(self_msg_iter
);
1923 bt_self_message_iterator_status status
=
1924 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
1926 BT_ASSERT(trimmer_it
);
1928 if (likely(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1929 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1930 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1934 switch (trimmer_it
->state
) {
1935 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1936 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1937 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1941 status
= state_seek_initially(trimmer_it
);
1942 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1946 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1947 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1952 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1953 status
= state_seek_initially(trimmer_it
);
1954 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1958 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1959 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1964 case TRIMMER_ITERATOR_STATE_ENDING
:
1965 status
= state_ending(trimmer_it
, msgs
, capacity
,
1967 if (status
!= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
1972 case TRIMMER_ITERATOR_STATE_ENDED
:
1973 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
1985 void trimmer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1987 struct trimmer_iterator
*trimmer_it
=
1988 bt_self_message_iterator_get_data(self_msg_iter
);
1990 BT_ASSERT(trimmer_it
);
1991 destroy_trimmer_iterator(trimmer_it
);