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_OUTPUT_LEVEL (trimmer_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER"
26 #include "logging/comp-logging.h"
28 #include "compat/utc.h"
29 #include "compat/time.h"
30 #include <babeltrace2/babeltrace.h>
31 #include "common/common.h"
32 #include "common/assert.h"
37 #include "compat/glib.h"
38 #include "plugins/common/param-validation/param-validation.h"
42 #define NS_PER_S INT64_C(1000000000)
44 static const char * const in_port_name
= "in";
47 unsigned int hour
, minute
, second
, ns
;
50 struct trimmer_bound
{
52 * Nanoseconds from origin, valid if `is_set` is set and
53 * `is_infinite` is false.
55 int64_t ns_from_origin
;
57 /* True if this bound's full time (`ns_from_origin`) is set */
61 * True if this bound represents the infinity (negative or
62 * positive depending on which bound it is). If this is true,
63 * then we don't care about `ns_from_origin` above.
68 * This bound's time without the date; this time is used to set
69 * `ns_from_origin` once we know the date.
71 struct trimmer_time time
;
75 struct trimmer_bound begin
, end
;
77 bt_logging_level log_level
;
78 bt_self_component
*self_comp
;
81 enum trimmer_iterator_state
{
83 * Find the first message's date and set the bounds's times
86 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
,
89 * Initially seek to the trimming range's beginning time.
91 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
,
94 * Fill the output message queue for as long as received input
95 * messages are within the trimming time range.
97 TRIMMER_ITERATOR_STATE_TRIM
,
99 /* Flush the remaining messages in the output message queue */
100 TRIMMER_ITERATOR_STATE_ENDING
,
102 /* Trimming operation and message iterator is ended */
103 TRIMMER_ITERATOR_STATE_ENDED
,
106 struct trimmer_iterator
{
108 struct trimmer_comp
*trimmer_comp
;
111 bt_self_message_iterator
*self_msg_iter
;
113 enum trimmer_iterator_state state
;
116 bt_self_component_port_input_message_iterator
*upstream_iter
;
117 struct trimmer_bound begin
, end
;
120 * Queue of `const bt_message *` (owned by the queue).
122 * This is where the trimming operation pushes the messages to
123 * output by this message iterator.
125 GQueue
*output_messages
;
128 * Hash table of `bt_stream *` (weak) to
129 * `struct trimmer_iterator_stream_state *` (owned by the HT).
131 GHashTable
*stream_states
;
134 struct trimmer_iterator_stream_state
{
136 const bt_stream
*stream
;
138 /* Have we seen a message with clock_snapshot going through this stream? */
139 bool seen_clock_snapshot
;
141 /* Owned by this (`NULL` initially and between packets) */
142 const bt_packet
*cur_packet
;
146 void destroy_trimmer_comp(struct trimmer_comp
*trimmer_comp
)
148 BT_ASSERT(trimmer_comp
);
149 g_free(trimmer_comp
);
153 struct trimmer_comp
*create_trimmer_comp(void)
155 return g_new0(struct trimmer_comp
, 1);
159 void trimmer_finalize(bt_self_component_filter
*self_comp
)
161 struct trimmer_comp
*trimmer_comp
=
162 bt_self_component_get_data(
163 bt_self_component_filter_as_self_component(self_comp
));
166 destroy_trimmer_comp(trimmer_comp
);
171 * Compile regex in `pattern`, and try to match `string`. If there's a match,
172 * return true and set `*match_info` to the list of matches. The list of
173 * matches must be freed by the caller. If there's no match, return false and
174 * set `*match_info` to NULL;
177 bool compile_and_match(const char *pattern
, const char *string
, GMatchInfo
**match_info
) {
178 bool matches
= false;
179 GError
*regex_error
= NULL
;
182 regex
= g_regex_new(pattern
, 0, 0, ®ex_error
);
187 matches
= g_regex_match(regex
, string
, 0, match_info
);
190 * g_regex_match allocates `*match_info` even if it returns
191 * FALSE. If there's no match, we have no use for it, so free
192 * it immediatly and don't return it to the caller.
194 g_match_info_free(*match_info
);
198 g_regex_unref(regex
);
203 g_error_free(regex_error
);
210 * Convert the captured text in match number `match_num` in `match_info`
211 * to an unsigned integer.
214 guint64
match_to_uint(const GMatchInfo
*match_info
, gint match_num
) {
215 gchar
*text
, *endptr
;
218 text
= g_match_info_fetch(match_info
, match_num
);
222 * Because the input is carefully sanitized with regexes by the caller,
223 * we assume that g_ascii_strtoull cannot fail.
226 result
= g_ascii_strtoull(text
, &endptr
, 10);
227 BT_ASSERT(endptr
> text
);
228 BT_ASSERT(errno
== 0);
236 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
237 * This function is like match_to_uint, but multiplies the parsed number to get
238 * the expected result.
241 guint64
match_to_uint_ns(const GMatchInfo
*match_info
, gint match_num
) {
244 gint start_pos
, end_pos
, power
;
245 static int pow10
[] = {
246 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
249 nanoseconds
= match_to_uint(match_info
, match_num
);
251 /* Multiply by 10 as many times as there are omitted digits. */
252 ret
= g_match_info_fetch_pos(match_info
, match_num
, &start_pos
, &end_pos
);
255 power
= 9 - (end_pos
- start_pos
);
256 BT_ASSERT(power
>= 0 && power
<= 8);
258 nanoseconds
*= pow10
[power
];
264 * Sets the time (in ns from origin) of a trimmer bound from date and
267 * Returns a negative value if anything goes wrong.
270 int set_bound_ns_from_origin(struct trimmer_bound
*bound
,
271 unsigned int year
, unsigned int month
, unsigned int day
,
272 unsigned int hour
, unsigned int minute
, unsigned int second
,
273 unsigned int ns
, bool is_gmt
)
283 .tm_year
= year
- 1900,
288 result
= bt_timegm(&tm
);
290 result
= mktime(&tm
);
299 bound
->ns_from_origin
= (int64_t) result
;
300 bound
->ns_from_origin
*= NS_PER_S
;
301 bound
->ns_from_origin
+= ns
;
302 bound
->is_set
= true;
309 * Parses a timestamp, figuring out its format.
311 * Returns a negative value if anything goes wrong.
315 * YYYY-MM-DD hh:mm[:ss[.ns]]
319 * TODO: Check overflows.
322 int set_bound_from_str(struct trimmer_comp
*trimmer_comp
,
323 const char *str
, struct trimmer_bound
*bound
, bool is_gmt
)
325 /* Matches YYYY-MM-DD */
326 #define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
328 /* Matches HH:MM[:SS[.NS]] */
329 #define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
331 /* Matches [-]SS[.NS] */
332 #define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
334 GMatchInfo
*match_info
;
337 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
338 if (compile_and_match("^" DATE_RE
" " TIME_RE
"$", str
, &match_info
)) {
339 unsigned int year
= 0, month
= 0, day
= 0, hours
= 0, minutes
= 0, seconds
= 0, nanoseconds
= 0;
340 gint match_count
= g_match_info_get_match_count(match_info
);
342 BT_ASSERT(match_count
>= 6 && match_count
<= 8);
344 year
= match_to_uint(match_info
, 1);
345 month
= match_to_uint(match_info
, 2);
346 day
= match_to_uint(match_info
, 3);
347 hours
= match_to_uint(match_info
, 4);
348 minutes
= match_to_uint(match_info
, 5);
350 if (match_count
>= 7) {
351 seconds
= match_to_uint(match_info
, 6);
354 if (match_count
>= 8) {
355 nanoseconds
= match_to_uint_ns(match_info
, 7);
358 set_bound_ns_from_origin(bound
, year
, month
, day
, hours
, minutes
, seconds
, nanoseconds
, is_gmt
);
363 if (compile_and_match("^" DATE_RE
"$", str
, &match_info
)) {
364 unsigned int year
= 0, month
= 0, day
= 0;
366 BT_ASSERT(g_match_info_get_match_count(match_info
) == 4);
368 year
= match_to_uint(match_info
, 1);
369 month
= match_to_uint(match_info
, 2);
370 day
= match_to_uint(match_info
, 3);
372 set_bound_ns_from_origin(bound
, year
, month
, day
, 0, 0, 0, 0, is_gmt
);
377 /* Try `hh:mm[:ss[.ns]]` format */
378 if (compile_and_match("^" TIME_RE
"$", str
, &match_info
)) {
379 gint match_count
= g_match_info_get_match_count(match_info
);
380 BT_ASSERT(match_count
>= 3 && match_count
<= 5);
381 bound
->time
.hour
= match_to_uint(match_info
, 1);
382 bound
->time
.minute
= match_to_uint(match_info
, 2);
384 if (match_count
>= 4) {
385 bound
->time
.second
= match_to_uint(match_info
, 3);
388 if (match_count
>= 5) {
389 bound
->time
.ns
= match_to_uint_ns(match_info
, 4);
395 /* Try `[-]s[.ns]` format */
396 if (compile_and_match("^" S_NS_RE
"$", str
, &match_info
)) {
397 gboolean is_neg
, fetch_pos_ret
;
398 gint start_pos
, end_pos
, match_count
;
399 guint64 seconds
, nanoseconds
= 0;
401 match_count
= g_match_info_get_match_count(match_info
);
402 BT_ASSERT(match_count
>= 3 && match_count
<= 4);
404 /* Check for presence of negation sign. */
405 fetch_pos_ret
= g_match_info_fetch_pos(match_info
, 1, &start_pos
, &end_pos
);
406 BT_ASSERT(fetch_pos_ret
);
407 is_neg
= (end_pos
- start_pos
) > 0;
409 seconds
= match_to_uint(match_info
, 2);
411 if (match_count
>= 4) {
412 nanoseconds
= match_to_uint_ns(match_info
, 3);
415 bound
->ns_from_origin
= seconds
* NS_PER_S
+ nanoseconds
;
418 bound
->ns_from_origin
= -bound
->ns_from_origin
;
421 bound
->is_set
= true;
426 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
427 "Invalid date/time format: param=\"%s\"", str
);
435 * Sets a trimmer bound's properties from a parameter string/integer
438 * Returns a negative value if anything goes wrong.
441 int set_bound_from_param(struct trimmer_comp
*trimmer_comp
,
442 const char *param_name
, const bt_value
*param
,
443 struct trimmer_bound
*bound
, bool is_gmt
)
449 if (bt_value_is_signed_integer(param
)) {
450 int64_t value
= bt_value_integer_signed_get(param
);
453 * Just convert it to a temporary string to handle
454 * everything the same way.
456 sprintf(tmp_arg
, "%" PRId64
, value
);
459 BT_ASSERT(bt_value_is_string(param
));
460 arg
= bt_value_string_get(param
);
463 ret
= set_bound_from_str(trimmer_comp
, arg
, bound
, is_gmt
);
469 int validate_trimmer_bounds(struct trimmer_comp
*trimmer_comp
,
470 struct trimmer_bound
*begin
, struct trimmer_bound
*end
)
474 BT_ASSERT(begin
->is_set
);
475 BT_ASSERT(end
->is_set
);
477 if (!begin
->is_infinite
&& !end
->is_infinite
&&
478 begin
->ns_from_origin
> end
->ns_from_origin
) {
479 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
480 "Trimming time range's beginning time is greater than end time: "
481 "begin-ns-from-origin=%" PRId64
", "
482 "end-ns-from-origin=%" PRId64
,
483 begin
->ns_from_origin
,
484 end
->ns_from_origin
);
489 if (!begin
->is_infinite
&& begin
->ns_from_origin
== INT64_MIN
) {
490 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
491 "Invalid trimming time range's beginning time: "
492 "ns-from-origin=%" PRId64
,
493 begin
->ns_from_origin
);
498 if (!end
->is_infinite
&& end
->ns_from_origin
== INT64_MIN
) {
499 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
500 "Invalid trimming time range's end time: "
501 "ns-from-origin=%" PRId64
,
502 end
->ns_from_origin
);
512 enum bt_param_validation_status
validate_bound_type(
513 const bt_value
*value
,
514 struct bt_param_validation_context
*context
)
516 enum bt_param_validation_status status
= BT_PARAM_VALIDATION_STATUS_OK
;
518 if (!bt_value_is_signed_integer(value
) &&
519 !bt_value_is_string(value
)) {
520 status
= bt_param_validation_error(context
,
521 "unexpected type: expected-types=[%s, %s], actual-type=%s",
522 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
523 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
524 bt_common_value_type_string(bt_value_get_type(value
)));
530 struct bt_param_validation_map_value_entry_descr trimmer_params
[] = {
531 { "gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
532 { "begin", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
533 { "end", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
534 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
538 bt_component_class_initialize_method_status
init_trimmer_comp_from_params(
539 struct trimmer_comp
*trimmer_comp
,
540 const bt_value
*params
)
542 const bt_value
*value
;
543 bt_component_class_initialize_method_status status
;
544 enum bt_param_validation_status validation_status
;
545 gchar
*validate_error
= NULL
;
547 validation_status
= bt_param_validation_validate(params
,
548 trimmer_params
, &validate_error
);
549 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
550 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
552 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
553 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
554 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
, "%s",
560 value
= bt_value_map_borrow_entry_value_const(params
, "gmt");
562 trimmer_comp
->is_gmt
= (bool) bt_value_bool_get(value
);
565 value
= bt_value_map_borrow_entry_value_const(params
, "begin");
567 if (set_bound_from_param(trimmer_comp
, "begin", value
,
568 &trimmer_comp
->begin
, trimmer_comp
->is_gmt
)) {
569 /* set_bound_from_param() logs errors */
570 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
574 trimmer_comp
->begin
.is_infinite
= true;
575 trimmer_comp
->begin
.is_set
= true;
578 value
= bt_value_map_borrow_entry_value_const(params
, "end");
580 if (set_bound_from_param(trimmer_comp
, "end", value
,
581 &trimmer_comp
->end
, trimmer_comp
->is_gmt
)) {
582 /* set_bound_from_param() logs errors */
583 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
587 trimmer_comp
->end
.is_infinite
= true;
588 trimmer_comp
->end
.is_set
= true;
591 if (trimmer_comp
->begin
.is_set
&& trimmer_comp
->end
.is_set
) {
592 /* validate_trimmer_bounds() logs errors */
593 if (validate_trimmer_bounds(trimmer_comp
,
594 &trimmer_comp
->begin
, &trimmer_comp
->end
)) {
595 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
600 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
603 g_free(validate_error
);
608 bt_component_class_initialize_method_status
trimmer_init(
609 bt_self_component_filter
*self_comp_flt
,
610 bt_self_component_filter_configuration
*config
,
611 const bt_value
*params
, void *init_data
)
613 bt_component_class_initialize_method_status status
;
614 bt_self_component_add_port_status add_port_status
;
615 struct trimmer_comp
*trimmer_comp
= create_trimmer_comp();
616 bt_self_component
*self_comp
=
617 bt_self_component_filter_as_self_component(self_comp_flt
);
620 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
624 trimmer_comp
->log_level
= bt_component_get_logging_level(
625 bt_self_component_as_component(self_comp
));
626 trimmer_comp
->self_comp
= self_comp
;
628 add_port_status
= bt_self_component_filter_add_input_port(
629 self_comp_flt
, in_port_name
, NULL
, NULL
);
630 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
631 status
= (int) add_port_status
;
635 add_port_status
= bt_self_component_filter_add_output_port(
636 self_comp_flt
, "out", NULL
, NULL
);
637 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
638 status
= (int) add_port_status
;
642 status
= init_trimmer_comp_from_params(trimmer_comp
, params
);
643 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
647 bt_self_component_set_data(self_comp
, trimmer_comp
);
649 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
654 destroy_trimmer_comp(trimmer_comp
);
662 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
668 bt_self_component_port_input_message_iterator_put_ref(
669 trimmer_it
->upstream_iter
);
671 if (trimmer_it
->output_messages
) {
672 g_queue_free(trimmer_it
->output_messages
);
675 if (trimmer_it
->stream_states
) {
676 g_hash_table_destroy(trimmer_it
->stream_states
);
685 void destroy_trimmer_iterator_stream_state(
686 struct trimmer_iterator_stream_state
*sstate
)
689 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
694 bt_component_class_message_iterator_initialize_method_status
trimmer_msg_iter_init(
695 bt_self_message_iterator
*self_msg_iter
,
696 bt_self_message_iterator_configuration
*config
,
697 bt_self_component_filter
*self_comp
,
698 bt_self_component_port_output
*port
)
700 bt_component_class_message_iterator_initialize_method_status status
;
701 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
703 struct trimmer_iterator
*trimmer_it
;
705 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
707 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
711 trimmer_it
->trimmer_comp
= bt_self_component_get_data(
712 bt_self_component_filter_as_self_component(self_comp
));
713 BT_ASSERT(trimmer_it
->trimmer_comp
);
715 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
716 trimmer_it
->trimmer_comp
->end
.is_set
) {
718 * Both trimming time range's bounds are set, so skip
720 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
723 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
726 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
727 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
729 bt_self_component_port_input_message_iterator_create_from_message_iterator(
731 bt_self_component_filter_borrow_input_port_by_name(
732 self_comp
, in_port_name
), &trimmer_it
->upstream_iter
);
733 if (msg_iter_status
!= BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
734 status
= (int) msg_iter_status
;
738 trimmer_it
->output_messages
= g_queue_new();
739 if (!trimmer_it
->output_messages
) {
740 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
744 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
745 g_direct_equal
, NULL
,
746 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
747 if (!trimmer_it
->stream_states
) {
748 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
753 * The trimmer requires upstream messages to have times, so it can
754 * always seek forward.
756 bt_self_message_iterator_configuration_set_can_seek_forward(
759 trimmer_it
->self_msg_iter
= self_msg_iter
;
760 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
762 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK
;
766 destroy_trimmer_iterator(trimmer_it
);
773 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
774 bool *has_clock_snapshot
)
776 const bt_clock_class
*clock_class
= NULL
;
777 const bt_clock_snapshot
*clock_snapshot
= NULL
;
781 BT_ASSERT_DBG(ns_from_origin
);
782 BT_ASSERT_DBG(has_clock_snapshot
);
784 switch (bt_message_get_type(msg
)) {
785 case BT_MESSAGE_TYPE_EVENT
:
787 bt_message_event_borrow_stream_class_default_clock_class_const(
789 if (G_UNLIKELY(!clock_class
)) {
793 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
796 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
798 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
800 if (G_UNLIKELY(!clock_class
)) {
804 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
807 case BT_MESSAGE_TYPE_PACKET_END
:
809 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
811 if (G_UNLIKELY(!clock_class
)) {
815 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
818 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
820 enum bt_message_stream_clock_snapshot_state cs_state
;
823 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg
);
824 if (G_UNLIKELY(!clock_class
)) {
828 cs_state
= bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
829 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
830 goto no_clock_snapshot
;
835 case BT_MESSAGE_TYPE_STREAM_END
:
837 enum bt_message_stream_clock_snapshot_state cs_state
;
840 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg
);
841 if (G_UNLIKELY(!clock_class
)) {
845 cs_state
= bt_message_stream_end_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
846 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
847 goto no_clock_snapshot
;
852 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
854 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
856 if (G_UNLIKELY(!clock_class
)) {
860 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
863 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
865 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
867 if (G_UNLIKELY(!clock_class
)) {
871 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
874 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
876 bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const(
880 goto no_clock_snapshot
;
883 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
,
885 if (G_UNLIKELY(ret
)) {
889 *has_clock_snapshot
= true;
893 *has_clock_snapshot
= false;
904 void put_messages(bt_message_array_const msgs
, uint64_t count
)
908 for (i
= 0; i
< count
; i
++) {
909 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
914 int set_trimmer_iterator_bound(struct trimmer_iterator
*trimmer_it
,
915 struct trimmer_bound
*bound
, int64_t ns_from_origin
,
918 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
921 time_t time_seconds
= (time_t) (ns_from_origin
/ NS_PER_S
);
924 BT_ASSERT(!bound
->is_set
);
927 /* We only need to extract the date from this time */
929 res
= bt_gmtime_r(&time_seconds
, &tm
);
931 res
= bt_localtime_r(&time_seconds
, &tm
);
935 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(trimmer_comp
->self_comp
,
936 "Cannot convert timestamp to date and time",
937 ": ts=%" PRId64
, (int64_t) time_seconds
);
942 ret
= set_bound_ns_from_origin(bound
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
943 tm
.tm_mday
, bound
->time
.hour
, bound
->time
.minute
,
944 bound
->time
.second
, bound
->time
.ns
, is_gmt
);
951 bt_component_class_message_iterator_next_method_status
952 state_set_trimmer_iterator_bounds(
953 struct trimmer_iterator
*trimmer_it
)
955 bt_message_iterator_next_status upstream_iter_status
=
956 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
;
957 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
958 bt_message_array_const msgs
;
960 int64_t ns_from_origin
= INT64_MIN
;
964 BT_ASSERT(!trimmer_it
->begin
.is_set
||
965 !trimmer_it
->end
.is_set
);
968 upstream_iter_status
=
969 bt_self_component_port_input_message_iterator_next(
970 trimmer_it
->upstream_iter
, &msgs
, &count
);
971 if (upstream_iter_status
!= BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
975 for (i
= 0; i
< count
; i
++) {
976 const bt_message
*msg
= msgs
[i
];
977 bool has_ns_from_origin
;
978 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
,
979 &has_ns_from_origin
);
984 if (!has_ns_from_origin
) {
988 BT_ASSERT_DBG(ns_from_origin
!= INT64_MIN
&&
989 ns_from_origin
!= INT64_MAX
);
990 put_messages(msgs
, count
);
994 put_messages(msgs
, count
);
998 if (!trimmer_it
->begin
.is_set
) {
999 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1000 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->begin
,
1001 ns_from_origin
, trimmer_comp
->is_gmt
);
1007 if (!trimmer_it
->end
.is_set
) {
1008 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
1009 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->end
,
1010 ns_from_origin
, trimmer_comp
->is_gmt
);
1016 ret
= validate_trimmer_bounds(trimmer_it
->trimmer_comp
,
1017 &trimmer_it
->begin
, &trimmer_it
->end
);
1025 put_messages(msgs
, count
);
1026 upstream_iter_status
= BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
;
1029 return (int) upstream_iter_status
;
1033 bt_component_class_message_iterator_next_method_status
state_seek_initially(
1034 struct trimmer_iterator
*trimmer_it
)
1036 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1037 bt_component_class_message_iterator_next_method_status status
;
1039 BT_ASSERT(trimmer_it
->begin
.is_set
);
1041 if (trimmer_it
->begin
.is_infinite
) {
1044 status
= (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1045 trimmer_it
->upstream_iter
, &can_seek
);
1046 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1048 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1049 "Cannot make upstream message iterator initially seek its beginning.");
1056 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1057 "Cannot make upstream message iterator initially seek its beginning.");
1058 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1062 status
= (int) bt_self_component_port_input_message_iterator_seek_beginning(
1063 trimmer_it
->upstream_iter
);
1067 status
= (int) bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1068 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
,
1071 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1073 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1074 "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64
,
1075 trimmer_it
->begin
.ns_from_origin
);
1082 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1083 "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64
,
1084 trimmer_it
->begin
.ns_from_origin
);
1085 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1089 status
= (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1090 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
1093 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1094 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_TRIM
;
1102 void push_message(struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
)
1104 g_queue_push_head(trimmer_it
->output_messages
, (void *) msg
);
1108 const bt_message
*pop_message(struct trimmer_iterator
*trimmer_it
)
1110 return g_queue_pop_tail(trimmer_it
->output_messages
);
1114 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1115 int64_t ns_from_origin
, uint64_t *raw_value
)
1118 int64_t cc_offset_s
;
1119 uint64_t cc_offset_cycles
;
1122 bt_clock_class_get_offset(clock_class
, &cc_offset_s
, &cc_offset_cycles
);
1123 cc_freq
= bt_clock_class_get_frequency(clock_class
);
1124 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1125 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1129 bt_component_class_message_iterator_next_method_status
1130 end_stream(struct trimmer_iterator
*trimmer_it
,
1131 struct trimmer_iterator_stream_state
*sstate
)
1133 bt_component_class_message_iterator_next_method_status status
=
1134 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1135 /* Initialize to silence maybe-uninitialized warning. */
1136 uint64_t raw_value
= 0;
1137 bt_message
*msg
= NULL
;
1139 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
1140 BT_ASSERT(sstate
->stream
);
1143 * If we haven't seen a message with a clock snapshot, we don't know if the trimmer's end bound is within
1144 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1146 * Also, it would be a bit of a lie to generate a stream end message with the end bound as its
1147 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1148 * seen a message with a clock snapshot and the stream is cut short by another message with a
1149 * clock snapshot, then we are sure that the the end bound time is not below the clock range,
1150 * and we know the stream was active at that time (and that we cut it short).
1152 if (sstate
->seen_clock_snapshot
) {
1153 const bt_clock_class
*clock_class
;
1156 clock_class
= bt_stream_class_borrow_default_clock_class_const(
1157 bt_stream_borrow_class_const(sstate
->stream
));
1158 BT_ASSERT(clock_class
);
1159 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1160 trimmer_it
->end
.ns_from_origin
, &raw_value
);
1162 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1167 if (sstate
->cur_packet
) {
1169 * Create and push a packet end message, making its time
1170 * the trimming range's end time.
1172 * We know that we must have seen a clock snapshot, the one in
1173 * the packet beginning message, since trimmer currently
1174 * requires packet messages to have clock snapshots (see comment
1175 * in create_stream_state_entry).
1177 BT_ASSERT(sstate
->seen_clock_snapshot
);
1179 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
1180 trimmer_it
->self_msg_iter
, sstate
->cur_packet
,
1183 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1187 push_message(trimmer_it
, msg
);
1189 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1192 /* Create and push a stream end message. */
1193 msg
= bt_message_stream_end_create(trimmer_it
->self_msg_iter
,
1196 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1200 if (sstate
->seen_clock_snapshot
) {
1201 bt_message_stream_end_set_default_clock_snapshot(msg
, raw_value
);
1204 push_message(trimmer_it
, msg
);
1208 * Just to make sure that we don't use this stream state again
1209 * in the future without an obvious error.
1211 sstate
->stream
= NULL
;
1214 bt_message_put_ref(msg
);
1219 bt_component_class_message_iterator_next_method_status
end_iterator_streams(
1220 struct trimmer_iterator
*trimmer_it
)
1222 bt_component_class_message_iterator_next_method_status status
=
1223 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1224 GHashTableIter iter
;
1225 gpointer key
, sstate
;
1227 if (trimmer_it
->end
.is_infinite
) {
1229 * An infinite trimming range's end time guarantees that
1230 * we received (and pushed) all the appropriate end
1237 * End each stream and then remove them from the hash table of
1238 * stream states to release unneeded references.
1240 g_hash_table_iter_init(&iter
, trimmer_it
->stream_states
);
1242 while (g_hash_table_iter_next(&iter
, &key
, &sstate
)) {
1243 status
= end_stream(trimmer_it
, sstate
);
1250 g_hash_table_remove_all(trimmer_it
->stream_states
);
1257 bt_component_class_message_iterator_next_method_status
1258 create_stream_state_entry(
1259 struct trimmer_iterator
*trimmer_it
,
1260 const struct bt_stream
*stream
,
1261 struct trimmer_iterator_stream_state
**stream_state
)
1263 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1264 bt_component_class_message_iterator_next_method_status status
;
1265 struct trimmer_iterator_stream_state
*sstate
;
1266 const bt_stream_class
*sc
;
1268 BT_ASSERT(!bt_g_hash_table_contains(trimmer_it
->stream_states
, stream
));
1271 * Validate right now that the stream's class
1272 * has a registered default clock class so that
1273 * an existing stream state guarantees existing
1274 * default clock snapshots for its associated
1277 * Also check that clock snapshots are always
1280 sc
= bt_stream_borrow_class_const(stream
);
1281 if (!bt_stream_class_borrow_default_clock_class_const(sc
)) {
1282 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1283 "Unsupported stream: stream class does "
1284 "not have a default clock class: "
1286 "stream-id=%" PRIu64
", "
1287 "stream-name=\"%s\"",
1288 stream
, bt_stream_get_id(stream
),
1289 bt_stream_get_name(stream
));
1290 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1295 * Temporary: make sure packet beginning, packet
1296 * end, discarded events, and discarded packets
1297 * messages have default clock snapshots until
1298 * the support for not having them is
1301 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1303 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1304 "Unsupported stream: packets have no beginning clock snapshot: "
1306 "stream-id=%" PRIu64
", "
1307 "stream-name=\"%s\"",
1308 stream
, bt_stream_get_id(stream
),
1309 bt_stream_get_name(stream
));
1310 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1314 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1316 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1317 "Unsupported stream: packets have no end clock snapshot: "
1319 "stream-id=%" PRIu64
", "
1320 "stream-name=\"%s\"",
1321 stream
, bt_stream_get_id(stream
),
1322 bt_stream_get_name(stream
));
1323 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1327 if (bt_stream_class_supports_discarded_events(sc
) &&
1328 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc
)) {
1329 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1330 "Unsupported stream: discarded events have no clock snapshots: "
1332 "stream-id=%" PRIu64
", "
1333 "stream-name=\"%s\"",
1334 stream
, bt_stream_get_id(stream
),
1335 bt_stream_get_name(stream
));
1336 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1340 if (bt_stream_class_supports_discarded_packets(sc
) &&
1341 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
)) {
1342 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1343 "Unsupported stream: discarded packets "
1344 "have no clock snapshots: "
1346 "stream-id=%" PRIu64
", "
1347 "stream-name=\"%s\"",
1348 stream
, bt_stream_get_id(stream
),
1349 bt_stream_get_name(stream
));
1350 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1354 sstate
= g_new0(struct trimmer_iterator_stream_state
, 1);
1356 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1360 sstate
->stream
= stream
;
1362 g_hash_table_insert(trimmer_it
->stream_states
, (void *) stream
, sstate
);
1364 *stream_state
= sstate
;
1366 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1373 struct trimmer_iterator_stream_state
*get_stream_state_entry(
1374 struct trimmer_iterator
*trimmer_it
,
1375 const struct bt_stream
*stream
)
1377 struct trimmer_iterator_stream_state
*sstate
;
1379 BT_ASSERT_DBG(stream
);
1380 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
, stream
);
1381 BT_ASSERT_DBG(sstate
);
1387 * Handles a message which is associated to a given stream state. This
1388 * _could_ make the iterator's output message queue grow; this could
1389 * also consume the message without pushing anything to this queue, only
1390 * modifying the stream state.
1392 * This function consumes the `msg` reference, _whatever the outcome_.
1394 * If non-NULL, `ns_from_origin` is the message's time, as given by
1395 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
1397 * This function sets `reached_end` if handling this message made the
1398 * iterator reach the end of the trimming range. Note that the output
1399 * message queue could contain messages even if this function sets
1403 bt_component_class_message_iterator_next_method_status
1404 handle_message_with_stream(
1405 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1406 const struct bt_stream
*stream
, const int64_t *ns_from_origin
,
1409 bt_component_class_message_iterator_next_method_status status
=
1410 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1411 bt_message_type msg_type
= bt_message_get_type(msg
);
1413 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1416 * Retrieve the stream's state - except if the message is stream
1417 * beginning, in which case we don't know about about this stream yet.
1419 if (msg_type
!= BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
1420 sstate
= get_stream_state_entry(trimmer_it
, stream
);
1424 case BT_MESSAGE_TYPE_EVENT
:
1426 * Event messages always have a clock snapshot if the stream
1427 * class has a clock class. And we know it has, otherwise we
1428 * couldn't be using the trimmer component.
1430 BT_ASSERT_DBG(ns_from_origin
);
1432 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1433 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1434 status
= end_iterator_streams(trimmer_it
);
1435 *reached_end
= true;
1439 sstate
->seen_clock_snapshot
= true;
1441 push_message(trimmer_it
, msg
);
1445 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1447 * Packet beginning messages won't have a clock snapshot if
1448 * stream_class->packets_have_beginning_default_clock_snapshot
1449 * is false. But for now, assume they always do.
1451 BT_ASSERT(ns_from_origin
);
1452 BT_ASSERT(!sstate
->cur_packet
);
1454 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1455 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1456 status
= end_iterator_streams(trimmer_it
);
1457 *reached_end
= true;
1461 sstate
->cur_packet
=
1462 bt_message_packet_beginning_borrow_packet_const(msg
);
1463 bt_packet_get_ref(sstate
->cur_packet
);
1465 sstate
->seen_clock_snapshot
= true;
1467 push_message(trimmer_it
, msg
);
1471 case BT_MESSAGE_TYPE_PACKET_END
:
1473 * Packet end messages won't have a clock snapshot if
1474 * stream_class->packets_have_end_default_clock_snapshot
1475 * is false. But for now, assume they always do.
1477 BT_ASSERT(ns_from_origin
);
1478 BT_ASSERT(sstate
->cur_packet
);
1480 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1481 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1482 status
= end_iterator_streams(trimmer_it
);
1483 *reached_end
= true;
1487 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1489 sstate
->seen_clock_snapshot
= true;
1491 push_message(trimmer_it
, msg
);
1495 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1496 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1499 * `ns_from_origin` is the message's time range's
1500 * beginning time here.
1502 int64_t end_ns_from_origin
;
1503 const bt_clock_snapshot
*end_cs
;
1505 BT_ASSERT(ns_from_origin
);
1507 sstate
->seen_clock_snapshot
= true;
1509 if (bt_message_get_type(msg
) ==
1510 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1512 * Safe to ignore the return value because we
1513 * know there's a default clock and it's always
1516 end_cs
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1520 * Safe to ignore the return value because we
1521 * know there's a default clock and it's always
1524 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1528 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1529 &end_ns_from_origin
)) {
1530 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1534 if (!trimmer_it
->end
.is_infinite
&&
1535 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1536 status
= end_iterator_streams(trimmer_it
);
1537 *reached_end
= true;
1541 if (!trimmer_it
->end
.is_infinite
&&
1542 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1544 * This message's end time is outside the
1545 * trimming time range: replace it with a new
1546 * message having an end time equal to the
1547 * trimming time range's end and without a
1550 const bt_clock_class
*clock_class
=
1551 bt_clock_snapshot_borrow_clock_class_const(
1553 const bt_clock_snapshot
*begin_cs
;
1554 bt_message
*new_msg
;
1555 uint64_t end_raw_value
;
1557 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1558 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1560 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1564 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1565 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1567 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1568 trimmer_it
->self_msg_iter
,
1570 bt_clock_snapshot_get_value(begin_cs
),
1573 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1575 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1576 trimmer_it
->self_msg_iter
,
1578 bt_clock_snapshot_get_value(begin_cs
),
1583 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1587 /* Replace the original message */
1588 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1591 push_message(trimmer_it
, msg
);
1596 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1598 * If this message has a time and this time is greater than the
1599 * trimmer's end bound, it triggers the end of the trim window.
1601 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1602 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1603 status
= end_iterator_streams(trimmer_it
);
1604 *reached_end
= true;
1608 /* Learn about this stream. */
1609 status
= create_stream_state_entry(trimmer_it
, stream
, &sstate
);
1610 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1614 if (ns_from_origin
) {
1615 sstate
->seen_clock_snapshot
= true;
1618 push_message(trimmer_it
, msg
);
1621 case BT_MESSAGE_TYPE_STREAM_END
:
1626 * If this message has a time and this time is greater than the
1627 * trimmer's end bound, it triggers the end of the trim window.
1629 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1630 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1631 status
= end_iterator_streams(trimmer_it
);
1632 *reached_end
= true;
1637 * Either the stream end message's time is within the trimmer's
1638 * bounds, or it doesn't have a time. In both cases, pass
1639 * the message unmodified.
1641 push_message(trimmer_it
, msg
);
1644 /* Forget about this stream. */
1645 removed
= g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1654 /* We release the message's reference whatever the outcome */
1655 bt_message_put_ref(msg
);
1660 * Handles an input message. This _could_ make the iterator's output
1661 * message queue grow; this could also consume the message without
1662 * pushing anything to this queue, only modifying the stream state.
1664 * This function consumes the `msg` reference, _whatever the outcome_.
1666 * This function sets `reached_end` if handling this message made the
1667 * iterator reach the end of the trimming range. Note that the output
1668 * message queue could contain messages even if this function sets
1672 bt_component_class_message_iterator_next_method_status
handle_message(
1673 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1676 bt_component_class_message_iterator_next_method_status status
;
1677 const bt_stream
*stream
= NULL
;
1678 int64_t ns_from_origin
= INT64_MIN
;
1679 bool has_ns_from_origin
= false;
1682 /* Find message's associated stream */
1683 switch (bt_message_get_type(msg
)) {
1684 case BT_MESSAGE_TYPE_EVENT
:
1685 stream
= bt_event_borrow_stream_const(
1686 bt_message_event_borrow_event_const(msg
));
1688 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1689 stream
= bt_packet_borrow_stream_const(
1690 bt_message_packet_beginning_borrow_packet_const(msg
));
1692 case BT_MESSAGE_TYPE_PACKET_END
:
1693 stream
= bt_packet_borrow_stream_const(
1694 bt_message_packet_end_borrow_packet_const(msg
));
1696 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1697 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1699 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1700 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1702 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1703 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1705 case BT_MESSAGE_TYPE_STREAM_END
:
1706 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1712 /* Retrieve the message's time */
1713 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &has_ns_from_origin
);
1714 if (G_UNLIKELY(ret
)) {
1715 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
1719 if (G_LIKELY(stream
)) {
1720 /* Message associated to a stream */
1721 status
= handle_message_with_stream(trimmer_it
, msg
,
1722 stream
, has_ns_from_origin
? &ns_from_origin
: NULL
, reached_end
);
1725 * handle_message_with_stream_state() unconditionally
1731 * Message not associated to a stream (message iterator
1734 if (G_UNLIKELY(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1735 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1736 status
= end_iterator_streams(trimmer_it
);
1737 *reached_end
= true;
1739 push_message(trimmer_it
, msg
);
1740 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1746 /* We release the message's reference whatever the outcome */
1747 bt_message_put_ref(msg
);
1752 void fill_message_array_from_output_messages(
1753 struct trimmer_iterator
*trimmer_it
,
1754 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1759 * Move auto-seek messages to the output array (which is this
1760 * iterator's base message array).
1762 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1763 msgs
[*count
] = pop_message(trimmer_it
);
1768 BT_ASSERT_DBG(*count
> 0);
1772 bt_component_class_message_iterator_next_method_status
state_ending(
1773 struct trimmer_iterator
*trimmer_it
,
1774 bt_message_array_const msgs
, uint64_t capacity
,
1777 bt_component_class_message_iterator_next_method_status status
=
1778 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1780 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1781 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1782 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1786 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1794 bt_component_class_message_iterator_next_method_status
1795 state_trim(struct trimmer_iterator
*trimmer_it
,
1796 bt_message_array_const msgs
, uint64_t capacity
,
1799 bt_component_class_message_iterator_next_method_status status
=
1800 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1801 bt_message_array_const my_msgs
;
1804 bool reached_end
= false;
1806 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1807 status
= (int) bt_self_component_port_input_message_iterator_next(
1808 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1809 if (G_UNLIKELY(status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
)) {
1810 if (status
== BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
) {
1811 status
= end_iterator_streams(trimmer_it
);
1812 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1817 TRIMMER_ITERATOR_STATE_ENDING
;
1818 status
= state_ending(trimmer_it
, msgs
,
1825 BT_ASSERT_DBG(my_count
> 0);
1827 for (i
= 0; i
< my_count
; i
++) {
1828 status
= handle_message(trimmer_it
, my_msgs
[i
],
1832 * handle_message() unconditionally consumes the
1833 * message reference.
1837 if (G_UNLIKELY(status
!=
1838 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
)) {
1839 put_messages(my_msgs
, my_count
);
1843 if (G_UNLIKELY(reached_end
)) {
1845 * This message's time was passed the
1846 * trimming time range's end time: we
1847 * are done. Their might still be
1848 * messages in the output message queue,
1849 * so move to the "ending" state and
1850 * apply it immediately since
1851 * state_trim() is called within the
1854 put_messages(my_msgs
, my_count
);
1856 TRIMMER_ITERATOR_STATE_ENDING
;
1857 status
= state_ending(trimmer_it
, msgs
,
1865 * There's at least one message in the output message queue:
1866 * move the messages to the output message array.
1868 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it
->output_messages
));
1869 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1877 bt_component_class_message_iterator_next_method_status
trimmer_msg_iter_next(
1878 bt_self_message_iterator
*self_msg_iter
,
1879 bt_message_array_const msgs
, uint64_t capacity
,
1882 struct trimmer_iterator
*trimmer_it
=
1883 bt_self_message_iterator_get_data(self_msg_iter
);
1884 bt_component_class_message_iterator_next_method_status status
=
1885 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
1887 BT_ASSERT_DBG(trimmer_it
);
1889 if (G_LIKELY(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1890 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1891 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1895 switch (trimmer_it
->state
) {
1896 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1897 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1898 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1902 status
= state_seek_initially(trimmer_it
);
1903 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1907 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1908 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1913 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1914 status
= state_seek_initially(trimmer_it
);
1915 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1919 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1920 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1925 case TRIMMER_ITERATOR_STATE_ENDING
:
1926 status
= state_ending(trimmer_it
, msgs
, capacity
,
1928 if (status
!= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
) {
1933 case TRIMMER_ITERATOR_STATE_ENDED
:
1934 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
1946 void trimmer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1948 struct trimmer_iterator
*trimmer_it
=
1949 bt_self_message_iterator_get_data(self_msg_iter
);
1951 BT_ASSERT(trimmer_it
);
1952 destroy_trimmer_iterator(trimmer_it
);