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_COMP_LOG_SELF_COMP (trimmer_comp->self_comp)
25 #define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level)
26 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER"
27 #include "plugins/comp-logging.h"
29 #include "compat/utc.h"
30 #include "compat/time.h"
31 #include <babeltrace2/babeltrace.h>
32 #include "common/common.h"
33 #include "common/assert.h"
37 #include "compat/glib.h"
41 #define NS_PER_S INT64_C(1000000000)
43 static const char * const in_port_name
= "in";
46 unsigned int hour
, minute
, second
, ns
;
49 struct trimmer_bound
{
51 * Nanoseconds from origin, valid if `is_set` is set and
52 * `is_infinite` is false.
54 int64_t ns_from_origin
;
56 /* True if this bound's full time (`ns_from_origin`) is set */
60 * True if this bound represents the infinity (negative or
61 * positive depending on which bound it is). If this is true,
62 * then we don't care about `ns_from_origin` above.
67 * This bound's time without the date; this time is used to set
68 * `ns_from_origin` once we know the date.
70 struct trimmer_time time
;
74 struct trimmer_bound begin
, end
;
76 bt_logging_level log_level
;
77 bt_self_component
*self_comp
;
80 enum trimmer_iterator_state
{
82 * Find the first message's date and set the bounds's times
85 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
,
88 * Initially seek to the trimming range's beginning time.
90 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
,
93 * Fill the output message queue for as long as received input
94 * messages are within the trimming time range.
96 TRIMMER_ITERATOR_STATE_TRIM
,
98 /* Flush the remaining messages in the output message queue */
99 TRIMMER_ITERATOR_STATE_ENDING
,
101 /* Trimming operation and message iterator is ended */
102 TRIMMER_ITERATOR_STATE_ENDED
,
105 struct trimmer_iterator
{
107 struct trimmer_comp
*trimmer_comp
;
110 bt_self_message_iterator
*self_msg_iter
;
112 enum trimmer_iterator_state state
;
115 bt_self_component_port_input_message_iterator
*upstream_iter
;
116 struct trimmer_bound begin
, end
;
119 * Queue of `const bt_message *` (owned by the queue).
121 * This is where the trimming operation pushes the messages to
122 * output by this message iterator.
124 GQueue
*output_messages
;
127 * Hash table of `bt_stream *` (weak) to
128 * `struct trimmer_iterator_stream_state *` (owned by the HT).
130 GHashTable
*stream_states
;
133 struct trimmer_iterator_stream_state
{
135 const bt_stream
*stream
;
137 /* Have we seen a message with clock_snapshot going through this stream? */
138 bool seen_clock_snapshot
;
140 /* Owned by this (`NULL` initially and between packets) */
141 const bt_packet
*cur_packet
;
145 void destroy_trimmer_comp(struct trimmer_comp
*trimmer_comp
)
147 BT_ASSERT(trimmer_comp
);
148 g_free(trimmer_comp
);
152 struct trimmer_comp
*create_trimmer_comp(void)
154 return g_new0(struct trimmer_comp
, 1);
158 void trimmer_finalize(bt_self_component_filter
*self_comp
)
160 struct trimmer_comp
*trimmer_comp
=
161 bt_self_component_get_data(
162 bt_self_component_filter_as_self_component(self_comp
));
165 destroy_trimmer_comp(trimmer_comp
);
170 * Compile regex in `pattern`, and try to match `string`. If there's a match,
171 * return true and set `*match_info` to the list of matches. The list of
172 * matches must be freed by the caller. If there's no match, return false and
173 * set `*match_info` to NULL;
176 bool compile_and_match(const char *pattern
, const char *string
, GMatchInfo
**match_info
) {
177 bool matches
= false;
178 GError
*regex_error
= NULL
;
181 regex
= g_regex_new(pattern
, 0, 0, ®ex_error
);
186 matches
= g_regex_match(regex
, string
, 0, match_info
);
189 * g_regex_match allocates `*match_info` even if it returns
190 * FALSE. If there's no match, we have no use for it, so free
191 * it immediatly and don't return it to the caller.
193 g_match_info_free(*match_info
);
197 g_regex_unref(regex
);
202 g_error_free(regex_error
);
209 * Convert the captured text in match number `match_num` in `match_info`
210 * to an unsigned integer.
213 guint64
match_to_uint(const GMatchInfo
*match_info
, gint match_num
) {
214 gchar
*text
, *endptr
;
217 text
= g_match_info_fetch(match_info
, match_num
);
221 * Because the input is carefully sanitized with regexes by the caller,
222 * we assume that g_ascii_strtoull cannot fail.
225 result
= g_ascii_strtoull(text
, &endptr
, 10);
226 BT_ASSERT(endptr
> text
);
227 BT_ASSERT(errno
== 0);
235 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
236 * This function is like match_to_uint, but multiplies the parsed number to get
237 * the expected result.
240 guint64
match_to_uint_ns(const GMatchInfo
*match_info
, gint match_num
) {
243 gint start_pos
, end_pos
, power
;
244 static int pow10
[] = {
245 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
248 nanoseconds
= match_to_uint(match_info
, match_num
);
250 /* Multiply by 10 as many times as there are omitted digits. */
251 ret
= g_match_info_fetch_pos(match_info
, match_num
, &start_pos
, &end_pos
);
254 power
= 9 - (end_pos
- start_pos
);
255 BT_ASSERT(power
>= 0 && power
<= 8);
257 nanoseconds
*= pow10
[power
];
263 * Sets the time (in ns from origin) of a trimmer bound from date and
266 * Returns a negative value if anything goes wrong.
269 int set_bound_ns_from_origin(struct trimmer_bound
*bound
,
270 unsigned int year
, unsigned int month
, unsigned int day
,
271 unsigned int hour
, unsigned int minute
, unsigned int second
,
272 unsigned int ns
, bool is_gmt
)
282 .tm_year
= year
- 1900,
287 result
= bt_timegm(&tm
);
289 result
= mktime(&tm
);
298 bound
->ns_from_origin
= (int64_t) result
;
299 bound
->ns_from_origin
*= NS_PER_S
;
300 bound
->ns_from_origin
+= ns
;
301 bound
->is_set
= true;
308 * Parses a timestamp, figuring out its format.
310 * Returns a negative value if anything goes wrong.
314 * YYYY-MM-DD hh:mm[:ss[.ns]]
318 * TODO: Check overflows.
321 int set_bound_from_str(struct trimmer_comp
*trimmer_comp
,
322 const char *str
, struct trimmer_bound
*bound
, bool is_gmt
)
324 /* Matches YYYY-MM-DD */
325 #define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
327 /* Matches HH:MM[:SS[.NS]] */
328 #define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
330 /* Matches [-]SS[.NS] */
331 #define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
333 GMatchInfo
*match_info
;
336 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
337 if (compile_and_match("^" DATE_RE
" " TIME_RE
"$", str
, &match_info
)) {
338 unsigned int year
= 0, month
= 0, day
= 0, hours
= 0, minutes
= 0, seconds
= 0, nanoseconds
= 0;
339 gint match_count
= g_match_info_get_match_count(match_info
);
341 BT_ASSERT(match_count
>= 6 && match_count
<= 8);
343 year
= match_to_uint(match_info
, 1);
344 month
= match_to_uint(match_info
, 2);
345 day
= match_to_uint(match_info
, 3);
346 hours
= match_to_uint(match_info
, 4);
347 minutes
= match_to_uint(match_info
, 5);
349 if (match_count
>= 7) {
350 seconds
= match_to_uint(match_info
, 6);
353 if (match_count
>= 8) {
354 nanoseconds
= match_to_uint_ns(match_info
, 7);
357 set_bound_ns_from_origin(bound
, year
, month
, day
, hours
, minutes
, seconds
, nanoseconds
, is_gmt
);
362 if (compile_and_match("^" DATE_RE
"$", str
, &match_info
)) {
363 unsigned int year
= 0, month
= 0, day
= 0;
365 BT_ASSERT(g_match_info_get_match_count(match_info
) == 4);
367 year
= match_to_uint(match_info
, 1);
368 month
= match_to_uint(match_info
, 2);
369 day
= match_to_uint(match_info
, 3);
371 set_bound_ns_from_origin(bound
, year
, month
, day
, 0, 0, 0, 0, is_gmt
);
376 /* Try `hh:mm[:ss[.ns]]` format */
377 if (compile_and_match("^" TIME_RE
"$", str
, &match_info
)) {
378 gint match_count
= g_match_info_get_match_count(match_info
);
379 BT_ASSERT(match_count
>= 3 && match_count
<= 5);
380 bound
->time
.hour
= match_to_uint(match_info
, 1);
381 bound
->time
.minute
= match_to_uint(match_info
, 2);
383 if (match_count
>= 4) {
384 bound
->time
.second
= match_to_uint(match_info
, 3);
387 if (match_count
>= 5) {
388 bound
->time
.ns
= match_to_uint_ns(match_info
, 4);
394 /* Try `[-]s[.ns]` format */
395 if (compile_and_match("^" S_NS_RE
"$", str
, &match_info
)) {
396 gboolean is_neg
, fetch_pos_ret
;
397 gint start_pos
, end_pos
, match_count
;
398 guint64 seconds
, nanoseconds
= 0;
400 match_count
= g_match_info_get_match_count(match_info
);
401 BT_ASSERT(match_count
>= 3 && match_count
<= 4);
403 /* Check for presence of negation sign. */
404 fetch_pos_ret
= g_match_info_fetch_pos(match_info
, 1, &start_pos
, &end_pos
);
405 BT_ASSERT(fetch_pos_ret
);
406 is_neg
= (end_pos
- start_pos
) > 0;
408 seconds
= match_to_uint(match_info
, 2);
410 if (match_count
>= 4) {
411 nanoseconds
= match_to_uint_ns(match_info
, 3);
414 bound
->ns_from_origin
= seconds
* NS_PER_S
+ nanoseconds
;
417 bound
->ns_from_origin
= -bound
->ns_from_origin
;
420 bound
->is_set
= true;
425 BT_COMP_LOGE("Invalid date/time format: param=\"%s\"", str
);
433 * Sets a trimmer bound's properties from a parameter string/integer
436 * Returns a negative value if anything goes wrong.
439 int set_bound_from_param(struct trimmer_comp
*trimmer_comp
,
440 const char *param_name
, const bt_value
*param
,
441 struct trimmer_bound
*bound
, bool is_gmt
)
447 if (bt_value_is_signed_integer(param
)) {
448 int64_t value
= bt_value_integer_signed_get(param
);
451 * Just convert it to a temporary string to handle
452 * everything the same way.
454 sprintf(tmp_arg
, "%" PRId64
, value
);
456 } else if (bt_value_is_string(param
)) {
457 arg
= bt_value_string_get(param
);
459 BT_COMP_LOGE("`%s` parameter must be an integer or a string value.",
465 ret
= set_bound_from_str(trimmer_comp
, arg
, bound
, is_gmt
);
472 int validate_trimmer_bounds(struct trimmer_comp
*trimmer_comp
,
473 struct trimmer_bound
*begin
, struct trimmer_bound
*end
)
477 BT_ASSERT(begin
->is_set
);
478 BT_ASSERT(end
->is_set
);
480 if (!begin
->is_infinite
&& !end
->is_infinite
&&
481 begin
->ns_from_origin
> end
->ns_from_origin
) {
482 BT_COMP_LOGE("Trimming time range's beginning time is greater than end time: "
483 "begin-ns-from-origin=%" PRId64
", "
484 "end-ns-from-origin=%" PRId64
,
485 begin
->ns_from_origin
,
486 end
->ns_from_origin
);
491 if (!begin
->is_infinite
&& begin
->ns_from_origin
== INT64_MIN
) {
492 BT_COMP_LOGE("Invalid trimming time range's beginning time: "
493 "ns-from-origin=%" PRId64
,
494 begin
->ns_from_origin
);
499 if (!end
->is_infinite
&& end
->ns_from_origin
== INT64_MIN
) {
500 BT_COMP_LOGE("Invalid trimming time range's end time: "
501 "ns-from-origin=%" PRId64
,
502 end
->ns_from_origin
);
512 int init_trimmer_comp_from_params(struct trimmer_comp
*trimmer_comp
,
513 const bt_value
*params
)
515 const bt_value
*value
;
519 value
= bt_value_map_borrow_entry_value_const(params
, "gmt");
521 trimmer_comp
->is_gmt
= (bool) bt_value_bool_get(value
);
524 value
= bt_value_map_borrow_entry_value_const(params
, "begin");
526 if (set_bound_from_param(trimmer_comp
, "begin", value
,
527 &trimmer_comp
->begin
, trimmer_comp
->is_gmt
)) {
528 /* set_bound_from_param() logs errors */
533 trimmer_comp
->begin
.is_infinite
= true;
534 trimmer_comp
->begin
.is_set
= true;
537 value
= bt_value_map_borrow_entry_value_const(params
, "end");
539 if (set_bound_from_param(trimmer_comp
, "end", value
,
540 &trimmer_comp
->end
, trimmer_comp
->is_gmt
)) {
541 /* set_bound_from_param() logs errors */
546 trimmer_comp
->end
.is_infinite
= true;
547 trimmer_comp
->end
.is_set
= true;
551 if (trimmer_comp
->begin
.is_set
&& trimmer_comp
->end
.is_set
) {
552 /* validate_trimmer_bounds() logs errors */
553 ret
= validate_trimmer_bounds(trimmer_comp
,
554 &trimmer_comp
->begin
, &trimmer_comp
->end
);
560 bt_component_class_init_method_status
trimmer_init(
561 bt_self_component_filter
*self_comp_flt
,
562 const bt_value
*params
, void *init_data
)
565 bt_component_class_init_method_status status
=
566 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
;
567 bt_self_component_add_port_status add_port_status
;
568 struct trimmer_comp
*trimmer_comp
= create_trimmer_comp();
569 bt_self_component
*self_comp
=
570 bt_self_component_filter_as_self_component(self_comp_flt
);
572 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
576 trimmer_comp
->log_level
= bt_component_get_logging_level(
577 bt_self_component_as_component(self_comp
));
578 trimmer_comp
->self_comp
= self_comp
;
579 add_port_status
= bt_self_component_filter_add_input_port(
580 self_comp_flt
, in_port_name
, NULL
, NULL
);
581 switch (add_port_status
) {
582 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
583 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
585 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
586 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
592 add_port_status
= bt_self_component_filter_add_output_port(
593 self_comp_flt
, "out", NULL
, NULL
);
594 switch (add_port_status
) {
595 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR
:
596 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
598 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR
:
599 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR
;
605 ret
= init_trimmer_comp_from_params(trimmer_comp
, params
);
607 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
611 bt_self_component_set_data(self_comp
, trimmer_comp
);
615 if (status
== BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK
) {
616 status
= BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR
;
620 destroy_trimmer_comp(trimmer_comp
);
628 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
630 BT_ASSERT(trimmer_it
);
631 bt_self_component_port_input_message_iterator_put_ref(
632 trimmer_it
->upstream_iter
);
634 if (trimmer_it
->output_messages
) {
635 g_queue_free(trimmer_it
->output_messages
);
638 if (trimmer_it
->stream_states
) {
639 g_hash_table_destroy(trimmer_it
->stream_states
);
646 void destroy_trimmer_iterator_stream_state(
647 struct trimmer_iterator_stream_state
*sstate
)
650 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
655 bt_component_class_message_iterator_init_method_status
trimmer_msg_iter_init(
656 bt_self_message_iterator
*self_msg_iter
,
657 bt_self_component_filter
*self_comp
,
658 bt_self_component_port_output
*port
)
660 bt_component_class_message_iterator_init_method_status status
=
661 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
;
662 struct trimmer_iterator
*trimmer_it
;
664 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
666 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
670 trimmer_it
->trimmer_comp
= bt_self_component_get_data(
671 bt_self_component_filter_as_self_component(self_comp
));
672 BT_ASSERT(trimmer_it
->trimmer_comp
);
674 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
675 trimmer_it
->trimmer_comp
->end
.is_set
) {
677 * Both trimming time range's bounds are set, so skip
679 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
682 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
685 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
686 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
687 trimmer_it
->upstream_iter
=
688 bt_self_component_port_input_message_iterator_create(
689 bt_self_component_filter_borrow_input_port_by_name(
690 self_comp
, in_port_name
));
691 if (!trimmer_it
->upstream_iter
) {
692 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR
;
696 trimmer_it
->output_messages
= g_queue_new();
697 if (!trimmer_it
->output_messages
) {
698 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
702 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
703 g_direct_equal
, NULL
,
704 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
705 if (!trimmer_it
->stream_states
) {
706 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR
;
710 trimmer_it
->self_msg_iter
= self_msg_iter
;
711 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
714 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK
&& trimmer_it
) {
715 destroy_trimmer_iterator(trimmer_it
);
722 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
723 bool *has_clock_snapshot
)
725 const bt_clock_class
*clock_class
= NULL
;
726 const bt_clock_snapshot
*clock_snapshot
= NULL
;
730 BT_ASSERT(ns_from_origin
);
731 BT_ASSERT(has_clock_snapshot
);
733 switch (bt_message_get_type(msg
)) {
734 case BT_MESSAGE_TYPE_EVENT
:
736 bt_message_event_borrow_stream_class_default_clock_class_const(
738 if (G_UNLIKELY(!clock_class
)) {
742 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
745 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
747 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
749 if (G_UNLIKELY(!clock_class
)) {
753 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
756 case BT_MESSAGE_TYPE_PACKET_END
:
758 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
760 if (G_UNLIKELY(!clock_class
)) {
764 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
767 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
769 enum bt_message_stream_clock_snapshot_state cs_state
;
772 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg
);
773 if (G_UNLIKELY(!clock_class
)) {
777 cs_state
= bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
778 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
779 goto no_clock_snapshot
;
784 case BT_MESSAGE_TYPE_STREAM_END
:
786 enum bt_message_stream_clock_snapshot_state cs_state
;
789 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg
);
790 if (G_UNLIKELY(!clock_class
)) {
794 cs_state
= bt_message_stream_end_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
795 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
796 goto no_clock_snapshot
;
801 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
803 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
805 if (G_UNLIKELY(!clock_class
)) {
809 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
812 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
814 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
816 if (G_UNLIKELY(!clock_class
)) {
820 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
823 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
825 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
829 goto no_clock_snapshot
;
832 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
,
834 if (G_UNLIKELY(ret
)) {
838 *has_clock_snapshot
= true;
842 *has_clock_snapshot
= false;
853 void put_messages(bt_message_array_const msgs
, uint64_t count
)
857 for (i
= 0; i
< count
; i
++) {
858 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
863 int set_trimmer_iterator_bound(struct trimmer_iterator
*trimmer_it
,
864 struct trimmer_bound
*bound
, int64_t ns_from_origin
,
867 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
870 time_t time_seconds
= (time_t) (ns_from_origin
/ NS_PER_S
);
873 BT_ASSERT(!bound
->is_set
);
876 /* We only need to extract the date from this time */
878 res
= bt_gmtime_r(&time_seconds
, &tm
);
880 res
= bt_localtime_r(&time_seconds
, &tm
);
884 BT_COMP_LOGE_ERRNO("Cannot convert timestamp to date and time",
885 ": ts=%" PRId64
, (int64_t) time_seconds
);
890 ret
= set_bound_ns_from_origin(bound
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
891 tm
.tm_mday
, bound
->time
.hour
, bound
->time
.minute
,
892 bound
->time
.second
, bound
->time
.ns
, is_gmt
);
899 bt_component_class_message_iterator_next_method_status
900 state_set_trimmer_iterator_bounds(
901 struct trimmer_iterator
*trimmer_it
)
903 bt_message_iterator_next_status upstream_iter_status
=
904 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
;
905 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
906 bt_message_array_const msgs
;
908 int64_t ns_from_origin
= INT64_MIN
;
912 BT_ASSERT(!trimmer_it
->begin
.is_set
||
913 !trimmer_it
->end
.is_set
);
916 upstream_iter_status
=
917 bt_self_component_port_input_message_iterator_next(
918 trimmer_it
->upstream_iter
, &msgs
, &count
);
919 if (upstream_iter_status
!= BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
923 for (i
= 0; i
< count
; i
++) {
924 const bt_message
*msg
= msgs
[i
];
925 bool has_ns_from_origin
;
928 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
,
929 &has_ns_from_origin
);
934 if (!has_ns_from_origin
) {
938 BT_ASSERT(ns_from_origin
!= INT64_MIN
&&
939 ns_from_origin
!= INT64_MAX
);
940 put_messages(msgs
, count
);
944 put_messages(msgs
, count
);
948 if (!trimmer_it
->begin
.is_set
) {
949 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
950 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->begin
,
951 ns_from_origin
, trimmer_comp
->is_gmt
);
957 if (!trimmer_it
->end
.is_set
) {
958 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
959 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->end
,
960 ns_from_origin
, trimmer_comp
->is_gmt
);
966 ret
= validate_trimmer_bounds(trimmer_it
->trimmer_comp
,
967 &trimmer_it
->begin
, &trimmer_it
->end
);
975 put_messages(msgs
, count
);
976 upstream_iter_status
= BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
;
979 return (int) upstream_iter_status
;
983 bt_component_class_message_iterator_next_method_status
state_seek_initially(
984 struct trimmer_iterator
*trimmer_it
)
986 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
987 bt_component_class_message_iterator_next_method_status status
=
988 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
990 BT_ASSERT(trimmer_it
->begin
.is_set
);
992 if (trimmer_it
->begin
.is_infinite
) {
993 if (!bt_self_component_port_input_message_iterator_can_seek_beginning(
994 trimmer_it
->upstream_iter
)) {
995 BT_COMP_LOGE_STR("Cannot make upstream message iterator initially seek its beginning.");
996 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1000 status
= (int) bt_self_component_port_input_message_iterator_seek_beginning(
1001 trimmer_it
->upstream_iter
);
1003 if (!bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1004 trimmer_it
->upstream_iter
,
1005 trimmer_it
->begin
.ns_from_origin
)) {
1006 BT_COMP_LOGE("Cannot make upstream message iterator initially seek: "
1007 "seek-ns-from-origin=%" PRId64
,
1008 trimmer_it
->begin
.ns_from_origin
);
1009 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1013 status
= (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1014 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
1017 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1018 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_TRIM
;
1026 void push_message(struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
)
1028 g_queue_push_head(trimmer_it
->output_messages
, (void *) msg
);
1032 const bt_message
*pop_message(struct trimmer_iterator
*trimmer_it
)
1034 return g_queue_pop_tail(trimmer_it
->output_messages
);
1038 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1039 int64_t ns_from_origin
, uint64_t *raw_value
)
1042 int64_t cc_offset_s
;
1043 uint64_t cc_offset_cycles
;
1046 bt_clock_class_get_offset(clock_class
, &cc_offset_s
, &cc_offset_cycles
);
1047 cc_freq
= bt_clock_class_get_frequency(clock_class
);
1048 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1049 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1053 bt_component_class_message_iterator_next_method_status
1054 end_stream(struct trimmer_iterator
*trimmer_it
,
1055 struct trimmer_iterator_stream_state
*sstate
)
1057 bt_component_class_message_iterator_next_method_status status
=
1058 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1059 /* Initialize to silence maybe-uninitialized warning. */
1060 uint64_t raw_value
= 0;
1061 bt_message
*msg
= NULL
;
1063 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
1064 BT_ASSERT(sstate
->stream
);
1067 * If we haven't seen a message with a clock snapshot, we don't know if the trimmer's end bound is within
1068 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1070 * Also, it would be a bit of a lie to generate a stream end message with the end bound as its
1071 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1072 * seen a message with a clock snapshot and the stream is cut short by another message with a
1073 * clock snapshot, then we are sure that the the end bound time is not below the clock range,
1074 * and we know the stream was active at that time (and that we cut it short).
1076 if (sstate
->seen_clock_snapshot
) {
1077 const bt_clock_class
*clock_class
;
1080 clock_class
= bt_stream_class_borrow_default_clock_class_const(
1081 bt_stream_borrow_class_const(sstate
->stream
));
1082 BT_ASSERT(clock_class
);
1083 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1084 trimmer_it
->end
.ns_from_origin
, &raw_value
);
1086 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1091 if (sstate
->cur_packet
) {
1093 * Create and push a packet end message, making its time
1094 * the trimming range's end time.
1096 * We know that we must have seen a clock snapshot, the one in
1097 * the packet beginning message, since trimmer currently
1098 * requires packet messages to have clock snapshots (see comment
1099 * in create_stream_state_entry).
1101 BT_ASSERT(sstate
->seen_clock_snapshot
);
1103 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
1104 trimmer_it
->self_msg_iter
, sstate
->cur_packet
,
1107 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1111 push_message(trimmer_it
, msg
);
1113 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1116 /* Create and push a stream end message. */
1117 msg
= bt_message_stream_end_create(trimmer_it
->self_msg_iter
,
1120 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1124 if (sstate
->seen_clock_snapshot
) {
1125 bt_message_stream_end_set_default_clock_snapshot(msg
, raw_value
);
1128 push_message(trimmer_it
, msg
);
1132 * Just to make sure that we don't use this stream state again
1133 * in the future without an obvious error.
1135 sstate
->stream
= NULL
;
1138 bt_message_put_ref(msg
);
1143 bt_component_class_message_iterator_next_method_status
end_iterator_streams(
1144 struct trimmer_iterator
*trimmer_it
)
1146 bt_component_class_message_iterator_next_method_status status
=
1147 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1148 GHashTableIter iter
;
1149 gpointer key
, sstate
;
1151 if (trimmer_it
->end
.is_infinite
) {
1153 * An infinite trimming range's end time guarantees that
1154 * we received (and pushed) all the appropriate end
1161 * End each stream and then remove them from the hash table of
1162 * stream states to release unneeded references.
1164 g_hash_table_iter_init(&iter
, trimmer_it
->stream_states
);
1166 while (g_hash_table_iter_next(&iter
, &key
, &sstate
)) {
1167 status
= end_stream(trimmer_it
, sstate
);
1174 g_hash_table_remove_all(trimmer_it
->stream_states
);
1181 bt_component_class_message_iterator_next_method_status
1182 create_stream_state_entry(
1183 struct trimmer_iterator
*trimmer_it
,
1184 const struct bt_stream
*stream
,
1185 struct trimmer_iterator_stream_state
**stream_state
)
1187 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1188 bt_component_class_message_iterator_next_method_status status
;
1189 struct trimmer_iterator_stream_state
*sstate
;
1190 const bt_stream_class
*sc
;
1192 BT_ASSERT(!bt_g_hash_table_contains(trimmer_it
->stream_states
, stream
));
1195 * Validate right now that the stream's class
1196 * has a registered default clock class so that
1197 * an existing stream state guarantees existing
1198 * default clock snapshots for its associated
1201 * Also check that clock snapshots are always
1204 sc
= bt_stream_borrow_class_const(stream
);
1205 if (!bt_stream_class_borrow_default_clock_class_const(sc
)) {
1206 BT_COMP_LOGE("Unsupported stream: stream class does "
1207 "not have a default clock class: "
1209 "stream-id=%" PRIu64
", "
1210 "stream-name=\"%s\"",
1211 stream
, bt_stream_get_id(stream
),
1212 bt_stream_get_name(stream
));
1213 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1218 * Temporary: make sure packet beginning, packet
1219 * end, discarded events, and discarded packets
1220 * messages have default clock snapshots until
1221 * the support for not having them is
1224 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1226 BT_COMP_LOGE("Unsupported stream: packets have "
1227 "no beginning clock snapshot: "
1229 "stream-id=%" PRIu64
", "
1230 "stream-name=\"%s\"",
1231 stream
, bt_stream_get_id(stream
),
1232 bt_stream_get_name(stream
));
1233 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1237 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1239 BT_COMP_LOGE("Unsupported stream: packets have "
1240 "no end clock snapshot: "
1242 "stream-id=%" PRIu64
", "
1243 "stream-name=\"%s\"",
1244 stream
, bt_stream_get_id(stream
),
1245 bt_stream_get_name(stream
));
1246 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1250 if (bt_stream_class_supports_discarded_events(sc
) &&
1251 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc
)) {
1252 BT_COMP_LOGE("Unsupported stream: discarded events "
1253 "have no clock snapshots: "
1255 "stream-id=%" PRIu64
", "
1256 "stream-name=\"%s\"",
1257 stream
, bt_stream_get_id(stream
),
1258 bt_stream_get_name(stream
));
1259 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1263 if (bt_stream_class_supports_discarded_packets(sc
) &&
1264 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
)) {
1265 BT_COMP_LOGE("Unsupported stream: discarded packets "
1266 "have no clock snapshots: "
1268 "stream-id=%" PRIu64
", "
1269 "stream-name=\"%s\"",
1270 stream
, bt_stream_get_id(stream
),
1271 bt_stream_get_name(stream
));
1272 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1276 sstate
= g_new0(struct trimmer_iterator_stream_state
, 1);
1278 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1282 sstate
->stream
= stream
;
1284 g_hash_table_insert(trimmer_it
->stream_states
, (void *) stream
, sstate
);
1286 *stream_state
= sstate
;
1288 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1295 struct trimmer_iterator_stream_state
*get_stream_state_entry(
1296 struct trimmer_iterator
*trimmer_it
,
1297 const struct bt_stream
*stream
)
1299 struct trimmer_iterator_stream_state
*sstate
;
1302 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
, stream
);
1309 * Handles a message which is associated to a given stream state. This
1310 * _could_ make the iterator's output message queue grow; this could
1311 * also consume the message without pushing anything to this queue, only
1312 * modifying the stream state.
1314 * This function consumes the `msg` reference, _whatever the outcome_.
1316 * If non-NULL, `ns_from_origin` is the message's time, as given by
1317 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
1319 * This function sets `reached_end` if handling this message made the
1320 * iterator reach the end of the trimming range. Note that the output
1321 * message queue could contain messages even if this function sets
1325 bt_component_class_message_iterator_next_method_status
1326 handle_message_with_stream(
1327 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1328 const struct bt_stream
*stream
, const int64_t *ns_from_origin
,
1331 bt_component_class_message_iterator_next_method_status status
=
1332 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1333 bt_message_type msg_type
= bt_message_get_type(msg
);
1335 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1338 * Retrieve the stream's state - except if the message is stream
1339 * beginning, in which case we don't know about about this stream yet.
1341 if (msg_type
!= BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
1342 sstate
= get_stream_state_entry(trimmer_it
, stream
);
1346 case BT_MESSAGE_TYPE_EVENT
:
1348 * Event messages always have a clock snapshot if the stream
1349 * class has a clock class. And we know it has, otherwise we
1350 * couldn't be using the trimmer component.
1352 BT_ASSERT(ns_from_origin
);
1354 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1355 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1356 status
= end_iterator_streams(trimmer_it
);
1357 *reached_end
= true;
1361 sstate
->seen_clock_snapshot
= true;
1363 push_message(trimmer_it
, msg
);
1367 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1369 * Packet beginning messages won't have a clock snapshot if
1370 * stream_class->packets_have_beginning_default_clock_snapshot
1371 * is false. But for now, assume they always do.
1373 BT_ASSERT(ns_from_origin
);
1374 BT_ASSERT(!sstate
->cur_packet
);
1376 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1377 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1378 status
= end_iterator_streams(trimmer_it
);
1379 *reached_end
= true;
1383 sstate
->cur_packet
=
1384 bt_message_packet_beginning_borrow_packet_const(msg
);
1385 bt_packet_get_ref(sstate
->cur_packet
);
1387 sstate
->seen_clock_snapshot
= true;
1389 push_message(trimmer_it
, msg
);
1393 case BT_MESSAGE_TYPE_PACKET_END
:
1395 * Packet end messages won't have a clock snapshot if
1396 * stream_class->packets_have_end_default_clock_snapshot
1397 * is false. But for now, assume they always do.
1399 BT_ASSERT(ns_from_origin
);
1400 BT_ASSERT(sstate
->cur_packet
);
1402 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1403 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1404 status
= end_iterator_streams(trimmer_it
);
1405 *reached_end
= true;
1409 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1411 sstate
->seen_clock_snapshot
= true;
1413 push_message(trimmer_it
, msg
);
1417 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1418 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1421 * `ns_from_origin` is the message's time range's
1422 * beginning time here.
1424 int64_t end_ns_from_origin
;
1425 const bt_clock_snapshot
*end_cs
;
1427 BT_ASSERT(ns_from_origin
);
1429 sstate
->seen_clock_snapshot
= true;
1431 if (bt_message_get_type(msg
) ==
1432 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1434 * Safe to ignore the return value because we
1435 * know there's a default clock and it's always
1438 end_cs
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1442 * Safe to ignore the return value because we
1443 * know there's a default clock and it's always
1446 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1450 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1451 &end_ns_from_origin
)) {
1452 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1456 if (!trimmer_it
->end
.is_infinite
&&
1457 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1458 status
= end_iterator_streams(trimmer_it
);
1459 *reached_end
= true;
1463 if (!trimmer_it
->end
.is_infinite
&&
1464 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1466 * This message's end time is outside the
1467 * trimming time range: replace it with a new
1468 * message having an end time equal to the
1469 * trimming time range's end and without a
1472 const bt_clock_class
*clock_class
=
1473 bt_clock_snapshot_borrow_clock_class_const(
1475 const bt_clock_snapshot
*begin_cs
;
1476 bt_message
*new_msg
;
1477 uint64_t end_raw_value
;
1479 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1480 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1482 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1486 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1487 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1489 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1490 trimmer_it
->self_msg_iter
,
1492 bt_clock_snapshot_get_value(begin_cs
),
1495 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1497 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1498 trimmer_it
->self_msg_iter
,
1500 bt_clock_snapshot_get_value(begin_cs
),
1505 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1509 /* Replace the original message */
1510 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1513 push_message(trimmer_it
, msg
);
1518 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1520 * If this message has a time and this time is greater than the
1521 * trimmer's end bound, it triggers the end of the trim window.
1523 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1524 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1525 status
= end_iterator_streams(trimmer_it
);
1526 *reached_end
= true;
1530 /* Learn about this stream. */
1531 status
= create_stream_state_entry(trimmer_it
, stream
, &sstate
);
1532 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1536 if (ns_from_origin
) {
1537 sstate
->seen_clock_snapshot
= true;
1540 push_message(trimmer_it
, msg
);
1543 case BT_MESSAGE_TYPE_STREAM_END
:
1548 * If this message has a time and this time is greater than the
1549 * trimmer's end bound, it triggers the end of the trim window.
1551 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1552 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1553 status
= end_iterator_streams(trimmer_it
);
1554 *reached_end
= true;
1559 * Either the stream end message's time is within the trimmer's
1560 * bounds, or it doesn't have a time. In both cases, pass
1561 * the message unmodified.
1563 push_message(trimmer_it
, msg
);
1566 /* Forget about this stream. */
1567 removed
= g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1576 /* We release the message's reference whatever the outcome */
1577 bt_message_put_ref(msg
);
1582 * Handles an input message. This _could_ make the iterator's output
1583 * message queue grow; this could also consume the message without
1584 * pushing anything to this queue, only modifying the stream state.
1586 * This function consumes the `msg` reference, _whatever the outcome_.
1588 * This function sets `reached_end` if handling this message made the
1589 * iterator reach the end of the trimming range. Note that the output
1590 * message queue could contain messages even if this function sets
1594 bt_component_class_message_iterator_next_method_status
handle_message(
1595 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1598 bt_component_class_message_iterator_next_method_status status
;
1599 const bt_stream
*stream
= NULL
;
1600 int64_t ns_from_origin
= INT64_MIN
;
1601 bool has_ns_from_origin
= false;
1604 /* Find message's associated stream */
1605 switch (bt_message_get_type(msg
)) {
1606 case BT_MESSAGE_TYPE_EVENT
:
1607 stream
= bt_event_borrow_stream_const(
1608 bt_message_event_borrow_event_const(msg
));
1610 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1611 stream
= bt_packet_borrow_stream_const(
1612 bt_message_packet_beginning_borrow_packet_const(msg
));
1614 case BT_MESSAGE_TYPE_PACKET_END
:
1615 stream
= bt_packet_borrow_stream_const(
1616 bt_message_packet_end_borrow_packet_const(msg
));
1618 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1619 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1621 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1622 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1624 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1625 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1627 case BT_MESSAGE_TYPE_STREAM_END
:
1628 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1634 /* Retrieve the message's time */
1635 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &has_ns_from_origin
);
1636 if (G_UNLIKELY(ret
)) {
1637 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1641 if (G_LIKELY(stream
)) {
1642 /* Message associated to a stream */
1643 status
= handle_message_with_stream(trimmer_it
, msg
,
1644 stream
, has_ns_from_origin
? &ns_from_origin
: NULL
, reached_end
);
1647 * handle_message_with_stream_state() unconditionally
1653 * Message not associated to a stream (message iterator
1656 if (G_UNLIKELY(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1657 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1658 status
= end_iterator_streams(trimmer_it
);
1659 *reached_end
= true;
1661 push_message(trimmer_it
, msg
);
1662 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1668 /* We release the message's reference whatever the outcome */
1669 bt_message_put_ref(msg
);
1674 void fill_message_array_from_output_messages(
1675 struct trimmer_iterator
*trimmer_it
,
1676 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1681 * Move auto-seek messages to the output array (which is this
1682 * iterator's base message array).
1684 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1685 msgs
[*count
] = pop_message(trimmer_it
);
1690 BT_ASSERT(*count
> 0);
1694 bt_component_class_message_iterator_next_method_status
state_ending(
1695 struct trimmer_iterator
*trimmer_it
,
1696 bt_message_array_const msgs
, uint64_t capacity
,
1699 bt_component_class_message_iterator_next_method_status status
=
1700 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1702 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1703 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1704 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1708 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1716 bt_component_class_message_iterator_next_method_status
1717 state_trim(struct trimmer_iterator
*trimmer_it
,
1718 bt_message_array_const msgs
, uint64_t capacity
,
1721 bt_component_class_message_iterator_next_method_status status
=
1722 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1723 bt_message_array_const my_msgs
;
1726 bool reached_end
= false;
1728 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1729 status
= (int) bt_self_component_port_input_message_iterator_next(
1730 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1731 if (G_UNLIKELY(status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
)) {
1732 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1733 status
= end_iterator_streams(trimmer_it
);
1734 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1739 TRIMMER_ITERATOR_STATE_ENDING
;
1740 status
= state_ending(trimmer_it
, msgs
,
1747 BT_ASSERT(my_count
> 0);
1749 for (i
= 0; i
< my_count
; i
++) {
1750 status
= handle_message(trimmer_it
, my_msgs
[i
],
1754 * handle_message() unconditionally consumes the
1755 * message reference.
1759 if (G_UNLIKELY(status
!=
1760 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
)) {
1761 put_messages(my_msgs
, my_count
);
1765 if (G_UNLIKELY(reached_end
)) {
1767 * This message's time was passed the
1768 * trimming time range's end time: we
1769 * are done. Their might still be
1770 * messages in the output message queue,
1771 * so move to the "ending" state and
1772 * apply it immediately since
1773 * state_trim() is called within the
1776 put_messages(my_msgs
, my_count
);
1778 TRIMMER_ITERATOR_STATE_ENDING
;
1779 status
= state_ending(trimmer_it
, msgs
,
1787 * There's at least one message in the output message queue:
1788 * move the messages to the output message array.
1790 BT_ASSERT(!g_queue_is_empty(trimmer_it
->output_messages
));
1791 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1799 bt_component_class_message_iterator_next_method_status
trimmer_msg_iter_next(
1800 bt_self_message_iterator
*self_msg_iter
,
1801 bt_message_array_const msgs
, uint64_t capacity
,
1804 struct trimmer_iterator
*trimmer_it
=
1805 bt_self_message_iterator_get_data(self_msg_iter
);
1806 bt_component_class_message_iterator_next_method_status status
=
1807 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1809 BT_ASSERT(trimmer_it
);
1811 if (G_LIKELY(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1812 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1813 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1817 switch (trimmer_it
->state
) {
1818 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1819 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1820 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1824 status
= state_seek_initially(trimmer_it
);
1825 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1829 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1830 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1835 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1836 status
= state_seek_initially(trimmer_it
);
1837 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1841 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1842 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1847 case TRIMMER_ITERATOR_STATE_ENDING
:
1848 status
= state_ending(trimmer_it
, msgs
, capacity
,
1850 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1855 case TRIMMER_ITERATOR_STATE_ENDED
:
1856 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1868 void trimmer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1870 struct trimmer_iterator
*trimmer_it
=
1871 bt_self_message_iterator_get_data(self_msg_iter
);
1873 BT_ASSERT(trimmer_it
);
1874 destroy_trimmer_iterator(trimmer_it
);