2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
8 #define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level)
9 #define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER"
10 #include "logging/comp-logging.h"
12 #include "compat/utc.h"
13 #include "compat/time.h"
14 #include <babeltrace2/babeltrace.h>
15 #include "common/common.h"
16 #include "common/assert.h"
21 #include "compat/glib.h"
22 #include "plugins/common/param-validation/param-validation.h"
26 #define NS_PER_S INT64_C(1000000000)
28 static const char * const in_port_name
= "in";
31 unsigned int hour
, minute
, second
, ns
;
34 struct trimmer_bound
{
36 * Nanoseconds from origin, valid if `is_set` is set and
37 * `is_infinite` is false.
39 int64_t ns_from_origin
;
41 /* True if this bound's full time (`ns_from_origin`) is set */
45 * True if this bound represents the infinity (negative or
46 * positive depending on which bound it is). If this is true,
47 * then we don't care about `ns_from_origin` above.
52 * This bound's time without the date; this time is used to set
53 * `ns_from_origin` once we know the date.
55 struct trimmer_time time
;
59 struct trimmer_bound begin
, end
;
61 bt_logging_level log_level
;
62 bt_self_component
*self_comp
;
63 bt_self_component_filter
*self_comp_filter
;
66 enum trimmer_iterator_state
{
68 * Find the first message's date and set the bounds's times
71 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
,
74 * Initially seek to the trimming range's beginning time.
76 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
,
79 * Fill the output message queue for as long as received input
80 * messages are within the trimming time range.
82 TRIMMER_ITERATOR_STATE_TRIM
,
84 /* Flush the remaining messages in the output message queue */
85 TRIMMER_ITERATOR_STATE_ENDING
,
87 /* Trimming operation and message iterator is ended */
88 TRIMMER_ITERATOR_STATE_ENDED
,
91 struct trimmer_iterator
{
93 struct trimmer_comp
*trimmer_comp
;
96 bt_self_message_iterator
*self_msg_iter
;
98 enum trimmer_iterator_state state
;
101 bt_message_iterator
*upstream_iter
;
102 struct trimmer_bound begin
, end
;
105 * Queue of `const bt_message *` (owned by the queue).
107 * This is where the trimming operation pushes the messages to
108 * output by this message iterator.
110 GQueue
*output_messages
;
113 * Hash table of `bt_stream *` (weak) to
114 * `struct trimmer_iterator_stream_state *` (owned by the HT).
116 GHashTable
*stream_states
;
119 struct trimmer_iterator_stream_state
{
121 const bt_stream
*stream
;
123 /* Have we seen a message with clock_snapshot going through this stream? */
124 bool seen_clock_snapshot
;
126 /* Owned by this (`NULL` initially and between packets) */
127 const bt_packet
*cur_packet
;
131 void destroy_trimmer_comp(struct trimmer_comp
*trimmer_comp
)
133 BT_ASSERT(trimmer_comp
);
134 g_free(trimmer_comp
);
138 struct trimmer_comp
*create_trimmer_comp(void)
140 return g_new0(struct trimmer_comp
, 1);
143 void trimmer_finalize(bt_self_component_filter
*self_comp
)
145 struct trimmer_comp
*trimmer_comp
=
146 bt_self_component_get_data(
147 bt_self_component_filter_as_self_component(self_comp
));
150 destroy_trimmer_comp(trimmer_comp
);
155 * Compile regex in `pattern`, and try to match `string`. If there's a match,
156 * return true and set `*match_info` to the list of matches. The list of
157 * matches must be freed by the caller. If there's no match, return false and
158 * set `*match_info` to NULL;
161 bool compile_and_match(const char *pattern
, const char *string
, GMatchInfo
**match_info
) {
162 bool matches
= false;
163 GError
*regex_error
= NULL
;
166 regex
= g_regex_new(pattern
, 0, 0, ®ex_error
);
171 matches
= g_regex_match(regex
, string
, 0, match_info
);
174 * g_regex_match allocates `*match_info` even if it returns
175 * FALSE. If there's no match, we have no use for it, so free
176 * it immediatly and don't return it to the caller.
178 g_match_info_free(*match_info
);
182 g_regex_unref(regex
);
187 g_error_free(regex_error
);
194 * Convert the captured text in match number `match_num` in `match_info`
195 * to an unsigned integer.
198 guint64
match_to_uint(const GMatchInfo
*match_info
, gint match_num
) {
199 gchar
*text
, *endptr
;
202 text
= g_match_info_fetch(match_info
, match_num
);
206 * Because the input is carefully sanitized with regexes by the caller,
207 * we assume that g_ascii_strtoull cannot fail.
210 result
= g_ascii_strtoull(text
, &endptr
, 10);
211 BT_ASSERT(endptr
> text
);
212 BT_ASSERT(errno
== 0);
220 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
221 * This function is like match_to_uint, but multiplies the parsed number to get
222 * the expected result.
225 guint64
match_to_uint_ns(const GMatchInfo
*match_info
, gint match_num
) {
228 gint start_pos
, end_pos
, power
;
229 static int pow10
[] = {
230 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
233 nanoseconds
= match_to_uint(match_info
, match_num
);
235 /* Multiply by 10 as many times as there are omitted digits. */
236 ret
= g_match_info_fetch_pos(match_info
, match_num
, &start_pos
, &end_pos
);
239 power
= 9 - (end_pos
- start_pos
);
240 BT_ASSERT(power
>= 0 && power
<= 8);
242 nanoseconds
*= pow10
[power
];
248 * Sets the time (in ns from origin) of a trimmer bound from date and
251 * Returns a negative value if anything goes wrong.
254 int set_bound_ns_from_origin(struct trimmer_bound
*bound
,
255 unsigned int year
, unsigned int month
, unsigned int day
,
256 unsigned int hour
, unsigned int minute
, unsigned int second
,
257 unsigned int ns
, bool is_gmt
)
267 .tm_year
= year
- 1900,
272 result
= bt_timegm(&tm
);
274 result
= mktime(&tm
);
283 bound
->ns_from_origin
= (int64_t) result
;
284 bound
->ns_from_origin
*= NS_PER_S
;
285 bound
->ns_from_origin
+= ns
;
286 bound
->is_set
= true;
293 * Parses a timestamp, figuring out its format.
295 * Returns a negative value if anything goes wrong.
299 * YYYY-MM-DD hh:mm[:ss[.ns]]
303 * TODO: Check overflows.
306 int set_bound_from_str(struct trimmer_comp
*trimmer_comp
,
307 const char *str
, struct trimmer_bound
*bound
, bool is_gmt
)
309 /* Matches YYYY-MM-DD */
310 #define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
312 /* Matches HH:MM[:SS[.NS]] */
313 #define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
315 /* Matches [-]SS[.NS] */
316 #define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
318 GMatchInfo
*match_info
;
321 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
322 if (compile_and_match("^" DATE_RE
" " TIME_RE
"$", str
, &match_info
)) {
323 unsigned int year
= 0, month
= 0, day
= 0, hours
= 0, minutes
= 0, seconds
= 0, nanoseconds
= 0;
324 gint match_count
= g_match_info_get_match_count(match_info
);
326 BT_ASSERT(match_count
>= 6 && match_count
<= 8);
328 year
= match_to_uint(match_info
, 1);
329 month
= match_to_uint(match_info
, 2);
330 day
= match_to_uint(match_info
, 3);
331 hours
= match_to_uint(match_info
, 4);
332 minutes
= match_to_uint(match_info
, 5);
334 if (match_count
>= 7) {
335 seconds
= match_to_uint(match_info
, 6);
338 if (match_count
>= 8) {
339 nanoseconds
= match_to_uint_ns(match_info
, 7);
342 set_bound_ns_from_origin(bound
, year
, month
, day
, hours
, minutes
, seconds
, nanoseconds
, is_gmt
);
347 if (compile_and_match("^" DATE_RE
"$", str
, &match_info
)) {
348 unsigned int year
= 0, month
= 0, day
= 0;
350 BT_ASSERT(g_match_info_get_match_count(match_info
) == 4);
352 year
= match_to_uint(match_info
, 1);
353 month
= match_to_uint(match_info
, 2);
354 day
= match_to_uint(match_info
, 3);
356 set_bound_ns_from_origin(bound
, year
, month
, day
, 0, 0, 0, 0, is_gmt
);
361 /* Try `hh:mm[:ss[.ns]]` format */
362 if (compile_and_match("^" TIME_RE
"$", str
, &match_info
)) {
363 gint match_count
= g_match_info_get_match_count(match_info
);
364 BT_ASSERT(match_count
>= 3 && match_count
<= 5);
365 bound
->time
.hour
= match_to_uint(match_info
, 1);
366 bound
->time
.minute
= match_to_uint(match_info
, 2);
368 if (match_count
>= 4) {
369 bound
->time
.second
= match_to_uint(match_info
, 3);
372 if (match_count
>= 5) {
373 bound
->time
.ns
= match_to_uint_ns(match_info
, 4);
379 /* Try `[-]s[.ns]` format */
380 if (compile_and_match("^" S_NS_RE
"$", str
, &match_info
)) {
381 gboolean is_neg
, fetch_pos_ret
;
382 gint start_pos
, end_pos
, match_count
;
383 guint64 seconds
, nanoseconds
= 0;
385 match_count
= g_match_info_get_match_count(match_info
);
386 BT_ASSERT(match_count
>= 3 && match_count
<= 4);
388 /* Check for presence of negation sign. */
389 fetch_pos_ret
= g_match_info_fetch_pos(match_info
, 1, &start_pos
, &end_pos
);
390 BT_ASSERT(fetch_pos_ret
);
391 is_neg
= (end_pos
- start_pos
) > 0;
393 seconds
= match_to_uint(match_info
, 2);
395 if (match_count
>= 4) {
396 nanoseconds
= match_to_uint_ns(match_info
, 3);
399 bound
->ns_from_origin
= seconds
* NS_PER_S
+ nanoseconds
;
402 bound
->ns_from_origin
= -bound
->ns_from_origin
;
405 bound
->is_set
= true;
410 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
411 "Invalid date/time format: param=\"%s\"", str
);
415 g_match_info_free(match_info
);
421 * Sets a trimmer bound's properties from a parameter string/integer
424 * Returns a negative value if anything goes wrong.
427 int set_bound_from_param(struct trimmer_comp
*trimmer_comp
,
428 const char *param_name
__attribute__((unused
)),
429 const bt_value
*param
,
430 struct trimmer_bound
*bound
, bool is_gmt
)
436 if (bt_value_is_signed_integer(param
)) {
437 int64_t value
= bt_value_integer_signed_get(param
);
440 * Just convert it to a temporary string to handle
441 * everything the same way.
443 snprintf(tmp_arg
, sizeof(tmp_arg
), "%" PRId64
, value
);
446 BT_ASSERT(bt_value_is_string(param
));
447 arg
= bt_value_string_get(param
);
450 ret
= set_bound_from_str(trimmer_comp
, arg
, bound
, is_gmt
);
456 int validate_trimmer_bounds(struct trimmer_comp
*trimmer_comp
,
457 struct trimmer_bound
*begin
, struct trimmer_bound
*end
)
461 BT_ASSERT(begin
->is_set
);
462 BT_ASSERT(end
->is_set
);
464 if (!begin
->is_infinite
&& !end
->is_infinite
&&
465 begin
->ns_from_origin
> end
->ns_from_origin
) {
466 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
467 "Trimming time range's beginning time is greater than end time: "
468 "begin-ns-from-origin=%" PRId64
", "
469 "end-ns-from-origin=%" PRId64
,
470 begin
->ns_from_origin
,
471 end
->ns_from_origin
);
476 if (!begin
->is_infinite
&& begin
->ns_from_origin
== INT64_MIN
) {
477 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
478 "Invalid trimming time range's beginning time: "
479 "ns-from-origin=%" PRId64
,
480 begin
->ns_from_origin
);
485 if (!end
->is_infinite
&& end
->ns_from_origin
== INT64_MIN
) {
486 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
487 "Invalid trimming time range's end time: "
488 "ns-from-origin=%" PRId64
,
489 end
->ns_from_origin
);
499 enum bt_param_validation_status
validate_bound_type(
500 const bt_value
*value
,
501 struct bt_param_validation_context
*context
)
503 enum bt_param_validation_status status
= BT_PARAM_VALIDATION_STATUS_OK
;
505 if (!bt_value_is_signed_integer(value
) &&
506 !bt_value_is_string(value
)) {
507 status
= bt_param_validation_error(context
,
508 "unexpected type: expected-types=[%s, %s], actual-type=%s",
509 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
510 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
511 bt_common_value_type_string(bt_value_get_type(value
)));
518 struct bt_param_validation_map_value_entry_descr trimmer_params
[] = {
519 { "gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
520 { "begin", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
521 { "end", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
522 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
526 bt_component_class_initialize_method_status
init_trimmer_comp_from_params(
527 struct trimmer_comp
*trimmer_comp
,
528 const bt_value
*params
)
530 const bt_value
*value
;
531 bt_component_class_initialize_method_status status
;
532 enum bt_param_validation_status validation_status
;
533 gchar
*validate_error
= NULL
;
535 validation_status
= bt_param_validation_validate(params
,
536 trimmer_params
, &validate_error
);
537 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
538 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
540 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
541 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
542 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
, "%s",
548 value
= bt_value_map_borrow_entry_value_const(params
, "gmt");
550 trimmer_comp
->is_gmt
= (bool) bt_value_bool_get(value
);
553 value
= bt_value_map_borrow_entry_value_const(params
, "begin");
555 if (set_bound_from_param(trimmer_comp
, "begin", value
,
556 &trimmer_comp
->begin
, trimmer_comp
->is_gmt
)) {
557 /* set_bound_from_param() logs errors */
558 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
562 trimmer_comp
->begin
.is_infinite
= true;
563 trimmer_comp
->begin
.is_set
= true;
566 value
= bt_value_map_borrow_entry_value_const(params
, "end");
568 if (set_bound_from_param(trimmer_comp
, "end", value
,
569 &trimmer_comp
->end
, trimmer_comp
->is_gmt
)) {
570 /* set_bound_from_param() logs errors */
571 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
575 trimmer_comp
->end
.is_infinite
= true;
576 trimmer_comp
->end
.is_set
= true;
579 if (trimmer_comp
->begin
.is_set
&& trimmer_comp
->end
.is_set
) {
580 /* validate_trimmer_bounds() logs errors */
581 if (validate_trimmer_bounds(trimmer_comp
,
582 &trimmer_comp
->begin
, &trimmer_comp
->end
)) {
583 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
588 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
591 g_free(validate_error
);
596 bt_component_class_initialize_method_status
trimmer_init(
597 bt_self_component_filter
*self_comp_flt
,
598 bt_self_component_filter_configuration
*config
__attribute__((unused
)),
599 const bt_value
*params
,
600 void *init_data
__attribute__((unused
)))
602 bt_component_class_initialize_method_status status
;
603 bt_self_component_add_port_status add_port_status
;
604 struct trimmer_comp
*trimmer_comp
= create_trimmer_comp();
605 bt_self_component
*self_comp
=
606 bt_self_component_filter_as_self_component(self_comp_flt
);
609 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
613 trimmer_comp
->log_level
= bt_component_get_logging_level(
614 bt_self_component_as_component(self_comp
));
615 trimmer_comp
->self_comp
= self_comp
;
616 trimmer_comp
->self_comp_filter
= self_comp_flt
;
618 add_port_status
= bt_self_component_filter_add_input_port(
619 self_comp_flt
, in_port_name
, NULL
, NULL
);
620 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
621 status
= (int) add_port_status
;
625 add_port_status
= bt_self_component_filter_add_output_port(
626 self_comp_flt
, "out", NULL
, NULL
);
627 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
628 status
= (int) add_port_status
;
632 status
= init_trimmer_comp_from_params(trimmer_comp
, params
);
633 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
637 bt_self_component_set_data(self_comp
, trimmer_comp
);
639 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
644 destroy_trimmer_comp(trimmer_comp
);
652 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
658 bt_message_iterator_put_ref(
659 trimmer_it
->upstream_iter
);
661 if (trimmer_it
->output_messages
) {
662 g_queue_free(trimmer_it
->output_messages
);
665 if (trimmer_it
->stream_states
) {
666 g_hash_table_destroy(trimmer_it
->stream_states
);
675 void destroy_trimmer_iterator_stream_state(
676 struct trimmer_iterator_stream_state
*sstate
)
679 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
683 bt_message_iterator_class_initialize_method_status
trimmer_msg_iter_init(
684 bt_self_message_iterator
*self_msg_iter
,
685 bt_self_message_iterator_configuration
*config
,
686 bt_self_component_port_output
*port
__attribute__((unused
)))
688 bt_message_iterator_class_initialize_method_status status
;
689 bt_message_iterator_create_from_message_iterator_status
691 struct trimmer_iterator
*trimmer_it
;
692 bt_self_component
*self_comp
=
693 bt_self_message_iterator_borrow_component(self_msg_iter
);
695 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
697 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
701 trimmer_it
->trimmer_comp
= bt_self_component_get_data(self_comp
);
702 BT_ASSERT(trimmer_it
->trimmer_comp
);
704 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
705 trimmer_it
->trimmer_comp
->end
.is_set
) {
707 * Both trimming time range's bounds are set, so skip
709 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
712 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
715 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
716 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
718 bt_message_iterator_create_from_message_iterator(
720 bt_self_component_filter_borrow_input_port_by_name(
721 trimmer_it
->trimmer_comp
->self_comp_filter
, in_port_name
),
722 &trimmer_it
->upstream_iter
);
723 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
724 status
= (int) msg_iter_status
;
728 trimmer_it
->output_messages
= g_queue_new();
729 if (!trimmer_it
->output_messages
) {
730 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
734 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
735 g_direct_equal
, NULL
,
736 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
737 if (!trimmer_it
->stream_states
) {
738 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
743 * The trimmer requires upstream messages to have times, so it can
744 * always seek forward.
746 bt_self_message_iterator_configuration_set_can_seek_forward(
749 trimmer_it
->self_msg_iter
= self_msg_iter
;
750 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
752 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
756 destroy_trimmer_iterator(trimmer_it
);
763 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
764 bool *has_clock_snapshot
)
766 const bt_clock_class
*clock_class
= NULL
;
767 const bt_clock_snapshot
*clock_snapshot
= NULL
;
771 BT_ASSERT_DBG(ns_from_origin
);
772 BT_ASSERT_DBG(has_clock_snapshot
);
774 switch (bt_message_get_type(msg
)) {
775 case BT_MESSAGE_TYPE_EVENT
:
777 bt_message_event_borrow_stream_class_default_clock_class_const(
779 if (G_UNLIKELY(!clock_class
)) {
783 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
786 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
788 const bt_packet
*packet
= bt_message_packet_beginning_borrow_packet_const(msg
);
789 const bt_stream
*stream
= bt_packet_borrow_stream_const(packet
);
790 const bt_stream_class
*stream_class
= bt_stream_borrow_class_const(stream
);
792 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(stream_class
)) {
793 goto no_clock_snapshot
;
796 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
800 case BT_MESSAGE_TYPE_PACKET_END
:
802 const bt_packet
*packet
= bt_message_packet_end_borrow_packet_const(msg
);
803 const bt_stream
*stream
= bt_packet_borrow_stream_const(packet
);
804 const bt_stream_class
*stream_class
= bt_stream_borrow_class_const(stream
);
806 if (!bt_stream_class_packets_have_end_default_clock_snapshot(stream_class
)) {
807 goto no_clock_snapshot
;
810 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
814 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
816 enum bt_message_stream_clock_snapshot_state cs_state
;
819 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg
);
820 if (G_UNLIKELY(!clock_class
)) {
824 cs_state
= bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
825 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
826 goto no_clock_snapshot
;
831 case BT_MESSAGE_TYPE_STREAM_END
:
833 enum bt_message_stream_clock_snapshot_state cs_state
;
836 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg
);
837 if (G_UNLIKELY(!clock_class
)) {
841 cs_state
= bt_message_stream_end_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
842 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
843 goto no_clock_snapshot
;
848 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
850 const bt_stream
*stream
= bt_message_discarded_events_borrow_stream_const(msg
);
851 const bt_stream_class
*stream_class
= bt_stream_borrow_class_const(stream
);
853 if (!bt_stream_class_discarded_events_have_default_clock_snapshots(stream_class
)) {
854 goto no_clock_snapshot
;
857 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
861 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
863 const bt_stream
*stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
864 const bt_stream_class
*stream_class
= bt_stream_borrow_class_const(stream
);
866 if (!bt_stream_class_discarded_packets_have_default_clock_snapshots(stream_class
)) {
867 goto no_clock_snapshot
;
870 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_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_message_iterator_class_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_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_message_iterator_class_next_method_status
state_seek_initially(
1034 struct trimmer_iterator
*trimmer_it
)
1036 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1037 bt_message_iterator_class_next_method_status status
;
1039 BT_ASSERT(trimmer_it
->begin
.is_set
);
1041 if (trimmer_it
->begin
.is_infinite
) {
1044 status
= (int) bt_message_iterator_can_seek_beginning(
1045 trimmer_it
->upstream_iter
, &can_seek
);
1046 if (status
!= BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1062 status
= (int) bt_message_iterator_seek_beginning(
1063 trimmer_it
->upstream_iter
);
1067 status
= (int) bt_message_iterator_can_seek_ns_from_origin(
1068 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
,
1071 if (status
!= BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1089 status
= (int) bt_message_iterator_seek_ns_from_origin(
1090 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
1093 if (status
== BT_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_next_method_status
1130 end_stream(struct trimmer_iterator
*trimmer_it
,
1131 struct trimmer_iterator_stream_state
*sstate
)
1133 bt_message_iterator_class_next_method_status status
=
1134 BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_next_method_status
end_iterator_streams(
1220 struct trimmer_iterator
*trimmer_it
)
1222 bt_message_iterator_class_next_method_status status
=
1223 BT_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_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_message_iterator_class_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_MESSAGE_ITERATOR_CLASS_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_supports_packets(sc
)) {
1302 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1304 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1305 "Unsupported stream: packets have no beginning clock snapshot: "
1307 "stream-id=%" PRIu64
", "
1308 "stream-name=\"%s\"",
1309 stream
, bt_stream_get_id(stream
),
1310 bt_stream_get_name(stream
));
1311 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1315 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1317 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1318 "Unsupported stream: packets have no end clock snapshot: "
1320 "stream-id=%" PRIu64
", "
1321 "stream-name=\"%s\"",
1322 stream
, bt_stream_get_id(stream
),
1323 bt_stream_get_name(stream
));
1324 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1328 if (bt_stream_class_supports_discarded_packets(sc
) &&
1329 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
)) {
1330 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1331 "Unsupported stream: discarded packets "
1332 "have no clock snapshots: "
1334 "stream-id=%" PRIu64
", "
1335 "stream-name=\"%s\"",
1336 stream
, bt_stream_get_id(stream
),
1337 bt_stream_get_name(stream
));
1338 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1343 if (bt_stream_class_supports_discarded_events(sc
) &&
1344 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc
)) {
1345 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1346 "Unsupported stream: discarded events have no clock snapshots: "
1348 "stream-id=%" PRIu64
", "
1349 "stream-name=\"%s\"",
1350 stream
, bt_stream_get_id(stream
),
1351 bt_stream_get_name(stream
));
1352 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1356 sstate
= g_new0(struct trimmer_iterator_stream_state
, 1);
1358 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1362 sstate
->stream
= stream
;
1364 g_hash_table_insert(trimmer_it
->stream_states
, (void *) stream
, sstate
);
1366 *stream_state
= sstate
;
1368 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1375 struct trimmer_iterator_stream_state
*get_stream_state_entry(
1376 struct trimmer_iterator
*trimmer_it
,
1377 const struct bt_stream
*stream
)
1379 struct trimmer_iterator_stream_state
*sstate
;
1381 BT_ASSERT_DBG(stream
);
1382 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
, stream
);
1383 BT_ASSERT_DBG(sstate
);
1389 * Handles a message which is associated to a given stream state. This
1390 * _could_ make the iterator's output message queue grow; this could
1391 * also consume the message without pushing anything to this queue, only
1392 * modifying the stream state.
1394 * This function consumes the `msg` reference, _whatever the outcome_.
1396 * If non-NULL, `ns_from_origin` is the message's time, as given by
1397 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
1399 * This function sets `reached_end` if handling this message made the
1400 * iterator reach the end of the trimming range. Note that the output
1401 * message queue could contain messages even if this function sets
1405 bt_message_iterator_class_next_method_status
1406 handle_message_with_stream(
1407 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1408 const struct bt_stream
*stream
, const int64_t *ns_from_origin
,
1411 bt_message_iterator_class_next_method_status status
=
1412 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1413 bt_message_type msg_type
= bt_message_get_type(msg
);
1415 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1418 * Retrieve the stream's state - except if the message is stream
1419 * beginning, in which case we don't know about about this stream yet.
1421 if (msg_type
!= BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
1422 sstate
= get_stream_state_entry(trimmer_it
, stream
);
1426 case BT_MESSAGE_TYPE_EVENT
:
1428 * Event messages always have a clock snapshot if the stream
1429 * class has a clock class. And we know it has, otherwise we
1430 * couldn't be using the trimmer component.
1432 BT_ASSERT_DBG(ns_from_origin
);
1434 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1435 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1436 status
= end_iterator_streams(trimmer_it
);
1437 *reached_end
= true;
1441 sstate
->seen_clock_snapshot
= true;
1443 push_message(trimmer_it
, msg
);
1447 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1449 * Packet beginning messages won't have a clock snapshot if
1450 * stream_class->packets_have_beginning_default_clock_snapshot
1451 * is false. But for now, assume they always do.
1453 BT_ASSERT(ns_from_origin
);
1454 BT_ASSERT(!sstate
->cur_packet
);
1456 if (G_UNLIKELY(!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 sstate
->cur_packet
=
1464 bt_message_packet_beginning_borrow_packet_const(msg
);
1465 bt_packet_get_ref(sstate
->cur_packet
);
1467 sstate
->seen_clock_snapshot
= true;
1469 push_message(trimmer_it
, msg
);
1473 case BT_MESSAGE_TYPE_PACKET_END
:
1475 * Packet end messages won't have a clock snapshot if
1476 * stream_class->packets_have_end_default_clock_snapshot
1477 * is false. But for now, assume they always do.
1479 BT_ASSERT(ns_from_origin
);
1480 BT_ASSERT(sstate
->cur_packet
);
1482 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1483 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1484 status
= end_iterator_streams(trimmer_it
);
1485 *reached_end
= true;
1489 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1491 sstate
->seen_clock_snapshot
= true;
1493 push_message(trimmer_it
, msg
);
1497 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1498 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1501 * `ns_from_origin` is the message's time range's
1502 * beginning time here.
1504 int64_t end_ns_from_origin
;
1505 const bt_clock_snapshot
*end_cs
;
1507 BT_ASSERT(ns_from_origin
);
1509 sstate
->seen_clock_snapshot
= true;
1511 if (bt_message_get_type(msg
) ==
1512 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1514 * Safe to ignore the return value because we
1515 * know there's a default clock and it's always
1518 end_cs
= bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1522 * Safe to ignore the return value because we
1523 * know there's a default clock and it's always
1526 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1530 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1531 &end_ns_from_origin
)) {
1532 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1536 if (!trimmer_it
->end
.is_infinite
&&
1537 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1538 status
= end_iterator_streams(trimmer_it
);
1539 *reached_end
= true;
1543 if (!trimmer_it
->end
.is_infinite
&&
1544 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1546 * This message's end time is outside the
1547 * trimming time range: replace it with a new
1548 * message having an end time equal to the
1549 * trimming time range's end and without a
1552 const bt_clock_class
*clock_class
=
1553 bt_clock_snapshot_borrow_clock_class_const(
1555 const bt_clock_snapshot
*begin_cs
;
1556 bt_message
*new_msg
;
1557 uint64_t end_raw_value
;
1559 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1560 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1562 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1566 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1567 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1569 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1570 trimmer_it
->self_msg_iter
,
1572 bt_clock_snapshot_get_value(begin_cs
),
1575 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1577 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1578 trimmer_it
->self_msg_iter
,
1580 bt_clock_snapshot_get_value(begin_cs
),
1585 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1589 /* Replace the original message */
1590 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1593 push_message(trimmer_it
, msg
);
1598 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1600 * If this message has a time and this time is greater than the
1601 * trimmer's end bound, it triggers the end of the trim window.
1603 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1604 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1605 status
= end_iterator_streams(trimmer_it
);
1606 *reached_end
= true;
1610 /* Learn about this stream. */
1611 status
= create_stream_state_entry(trimmer_it
, stream
, &sstate
);
1612 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1616 if (ns_from_origin
) {
1617 sstate
->seen_clock_snapshot
= true;
1620 push_message(trimmer_it
, msg
);
1623 case BT_MESSAGE_TYPE_STREAM_END
:
1628 * If this message has a time and this time is greater than the
1629 * trimmer's end bound, it triggers the end of the trim window.
1631 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1632 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1633 status
= end_iterator_streams(trimmer_it
);
1634 *reached_end
= true;
1639 * Either the stream end message's time is within the trimmer's
1640 * bounds, or it doesn't have a time. In both cases, pass
1641 * the message unmodified.
1643 push_message(trimmer_it
, msg
);
1646 /* Forget about this stream. */
1647 removed
= g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1656 /* We release the message's reference whatever the outcome */
1657 bt_message_put_ref(msg
);
1662 * Handles an input message. This _could_ make the iterator's output
1663 * message queue grow; this could also consume the message without
1664 * pushing anything to this queue, only modifying the stream state.
1666 * This function consumes the `msg` reference, _whatever the outcome_.
1668 * This function sets `reached_end` if handling this message made the
1669 * iterator reach the end of the trimming range. Note that the output
1670 * message queue could contain messages even if this function sets
1674 bt_message_iterator_class_next_method_status
handle_message(
1675 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1678 bt_message_iterator_class_next_method_status status
;
1679 const bt_stream
*stream
= NULL
;
1680 int64_t ns_from_origin
= INT64_MIN
;
1681 bool has_ns_from_origin
= false;
1684 /* Find message's associated stream */
1685 switch (bt_message_get_type(msg
)) {
1686 case BT_MESSAGE_TYPE_EVENT
:
1687 stream
= bt_event_borrow_stream_const(
1688 bt_message_event_borrow_event_const(msg
));
1690 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1691 stream
= bt_packet_borrow_stream_const(
1692 bt_message_packet_beginning_borrow_packet_const(msg
));
1694 case BT_MESSAGE_TYPE_PACKET_END
:
1695 stream
= bt_packet_borrow_stream_const(
1696 bt_message_packet_end_borrow_packet_const(msg
));
1698 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1699 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1701 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1702 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1704 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1705 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1707 case BT_MESSAGE_TYPE_STREAM_END
:
1708 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1714 /* Retrieve the message's time */
1715 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &has_ns_from_origin
);
1716 if (G_UNLIKELY(ret
)) {
1717 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1721 if (G_LIKELY(stream
)) {
1722 /* Message associated to a stream */
1723 status
= handle_message_with_stream(trimmer_it
, msg
,
1724 stream
, has_ns_from_origin
? &ns_from_origin
: NULL
, reached_end
);
1727 * handle_message_with_stream_state() unconditionally
1733 * Message not associated to a stream (message iterator
1736 if (G_UNLIKELY(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1737 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1738 status
= end_iterator_streams(trimmer_it
);
1739 *reached_end
= true;
1741 push_message(trimmer_it
, msg
);
1742 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1748 /* We release the message's reference whatever the outcome */
1749 bt_message_put_ref(msg
);
1754 void fill_message_array_from_output_messages(
1755 struct trimmer_iterator
*trimmer_it
,
1756 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1761 * Move auto-seek messages to the output array (which is this
1762 * iterator's base message array).
1764 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1765 msgs
[*count
] = pop_message(trimmer_it
);
1770 BT_ASSERT_DBG(*count
> 0);
1774 bt_message_iterator_class_next_method_status
state_ending(
1775 struct trimmer_iterator
*trimmer_it
,
1776 bt_message_array_const msgs
, uint64_t capacity
,
1779 bt_message_iterator_class_next_method_status status
=
1780 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1782 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1783 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1784 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1788 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1796 bt_message_iterator_class_next_method_status
1797 state_trim(struct trimmer_iterator
*trimmer_it
,
1798 bt_message_array_const msgs
, uint64_t capacity
,
1801 bt_message_iterator_class_next_method_status status
=
1802 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1803 bt_message_array_const my_msgs
;
1806 bool reached_end
= false;
1808 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1809 status
= (int) bt_message_iterator_next(
1810 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1811 if (G_UNLIKELY(status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
)) {
1812 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1813 status
= end_iterator_streams(trimmer_it
);
1814 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1819 TRIMMER_ITERATOR_STATE_ENDING
;
1820 status
= state_ending(trimmer_it
, msgs
,
1827 BT_ASSERT_DBG(my_count
> 0);
1829 for (i
= 0; i
< my_count
; i
++) {
1830 status
= handle_message(trimmer_it
, my_msgs
[i
],
1834 * handle_message() unconditionally consumes the
1835 * message reference.
1839 if (G_UNLIKELY(status
!=
1840 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
)) {
1841 put_messages(my_msgs
, my_count
);
1845 if (G_UNLIKELY(reached_end
)) {
1847 * This message's time was passed the
1848 * trimming time range's end time: we
1849 * are done. Their might still be
1850 * messages in the output message queue,
1851 * so move to the "ending" state and
1852 * apply it immediately since
1853 * state_trim() is called within the
1856 put_messages(my_msgs
, my_count
);
1858 TRIMMER_ITERATOR_STATE_ENDING
;
1859 status
= state_ending(trimmer_it
, msgs
,
1867 * There's at least one message in the output message queue:
1868 * move the messages to the output message array.
1870 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it
->output_messages
));
1871 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1878 bt_message_iterator_class_next_method_status
trimmer_msg_iter_next(
1879 bt_self_message_iterator
*self_msg_iter
,
1880 bt_message_array_const msgs
, uint64_t capacity
,
1883 struct trimmer_iterator
*trimmer_it
=
1884 bt_self_message_iterator_get_data(self_msg_iter
);
1885 bt_message_iterator_class_next_method_status status
=
1886 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1888 BT_ASSERT_DBG(trimmer_it
);
1890 if (G_LIKELY(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1891 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1892 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1896 switch (trimmer_it
->state
) {
1897 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1898 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1899 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1903 status
= state_seek_initially(trimmer_it
);
1904 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1908 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1909 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1914 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1915 status
= state_seek_initially(trimmer_it
);
1916 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1920 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1921 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1926 case TRIMMER_ITERATOR_STATE_ENDING
:
1927 status
= state_ending(trimmer_it
, msgs
, capacity
,
1929 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1934 case TRIMMER_ITERATOR_STATE_ENDED
:
1935 status
= BT_MESSAGE_ITERATOR_CLASS_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
);