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
;
79 bt_self_component_filter
*self_comp_filter
;
82 enum trimmer_iterator_state
{
84 * Find the first message's date and set the bounds's times
87 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
,
90 * Initially seek to the trimming range's beginning time.
92 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
,
95 * Fill the output message queue for as long as received input
96 * messages are within the trimming time range.
98 TRIMMER_ITERATOR_STATE_TRIM
,
100 /* Flush the remaining messages in the output message queue */
101 TRIMMER_ITERATOR_STATE_ENDING
,
103 /* Trimming operation and message iterator is ended */
104 TRIMMER_ITERATOR_STATE_ENDED
,
107 struct trimmer_iterator
{
109 struct trimmer_comp
*trimmer_comp
;
112 bt_self_message_iterator
*self_msg_iter
;
114 enum trimmer_iterator_state state
;
117 bt_message_iterator
*upstream_iter
;
118 struct trimmer_bound begin
, end
;
121 * Queue of `const bt_message *` (owned by the queue).
123 * This is where the trimming operation pushes the messages to
124 * output by this message iterator.
126 GQueue
*output_messages
;
129 * Hash table of `bt_stream *` (weak) to
130 * `struct trimmer_iterator_stream_state *` (owned by the HT).
132 GHashTable
*stream_states
;
135 struct trimmer_iterator_stream_state
{
137 const bt_stream
*stream
;
139 /* Have we seen a message with clock_snapshot going through this stream? */
140 bool seen_clock_snapshot
;
142 /* Owned by this (`NULL` initially and between packets) */
143 const bt_packet
*cur_packet
;
147 void destroy_trimmer_comp(struct trimmer_comp
*trimmer_comp
)
149 BT_ASSERT(trimmer_comp
);
150 g_free(trimmer_comp
);
154 struct trimmer_comp
*create_trimmer_comp(void)
156 return g_new0(struct trimmer_comp
, 1);
160 void trimmer_finalize(bt_self_component_filter
*self_comp
)
162 struct trimmer_comp
*trimmer_comp
=
163 bt_self_component_get_data(
164 bt_self_component_filter_as_self_component(self_comp
));
167 destroy_trimmer_comp(trimmer_comp
);
172 * Compile regex in `pattern`, and try to match `string`. If there's a match,
173 * return true and set `*match_info` to the list of matches. The list of
174 * matches must be freed by the caller. If there's no match, return false and
175 * set `*match_info` to NULL;
178 bool compile_and_match(const char *pattern
, const char *string
, GMatchInfo
**match_info
) {
179 bool matches
= false;
180 GError
*regex_error
= NULL
;
183 regex
= g_regex_new(pattern
, 0, 0, ®ex_error
);
188 matches
= g_regex_match(regex
, string
, 0, match_info
);
191 * g_regex_match allocates `*match_info` even if it returns
192 * FALSE. If there's no match, we have no use for it, so free
193 * it immediatly and don't return it to the caller.
195 g_match_info_free(*match_info
);
199 g_regex_unref(regex
);
204 g_error_free(regex_error
);
211 * Convert the captured text in match number `match_num` in `match_info`
212 * to an unsigned integer.
215 guint64
match_to_uint(const GMatchInfo
*match_info
, gint match_num
) {
216 gchar
*text
, *endptr
;
219 text
= g_match_info_fetch(match_info
, match_num
);
223 * Because the input is carefully sanitized with regexes by the caller,
224 * we assume that g_ascii_strtoull cannot fail.
227 result
= g_ascii_strtoull(text
, &endptr
, 10);
228 BT_ASSERT(endptr
> text
);
229 BT_ASSERT(errno
== 0);
237 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
238 * This function is like match_to_uint, but multiplies the parsed number to get
239 * the expected result.
242 guint64
match_to_uint_ns(const GMatchInfo
*match_info
, gint match_num
) {
245 gint start_pos
, end_pos
, power
;
246 static int pow10
[] = {
247 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
250 nanoseconds
= match_to_uint(match_info
, match_num
);
252 /* Multiply by 10 as many times as there are omitted digits. */
253 ret
= g_match_info_fetch_pos(match_info
, match_num
, &start_pos
, &end_pos
);
256 power
= 9 - (end_pos
- start_pos
);
257 BT_ASSERT(power
>= 0 && power
<= 8);
259 nanoseconds
*= pow10
[power
];
265 * Sets the time (in ns from origin) of a trimmer bound from date and
268 * Returns a negative value if anything goes wrong.
271 int set_bound_ns_from_origin(struct trimmer_bound
*bound
,
272 unsigned int year
, unsigned int month
, unsigned int day
,
273 unsigned int hour
, unsigned int minute
, unsigned int second
,
274 unsigned int ns
, bool is_gmt
)
284 .tm_year
= year
- 1900,
289 result
= bt_timegm(&tm
);
291 result
= mktime(&tm
);
300 bound
->ns_from_origin
= (int64_t) result
;
301 bound
->ns_from_origin
*= NS_PER_S
;
302 bound
->ns_from_origin
+= ns
;
303 bound
->is_set
= true;
310 * Parses a timestamp, figuring out its format.
312 * Returns a negative value if anything goes wrong.
316 * YYYY-MM-DD hh:mm[:ss[.ns]]
320 * TODO: Check overflows.
323 int set_bound_from_str(struct trimmer_comp
*trimmer_comp
,
324 const char *str
, struct trimmer_bound
*bound
, bool is_gmt
)
326 /* Matches YYYY-MM-DD */
327 #define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
329 /* Matches HH:MM[:SS[.NS]] */
330 #define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
332 /* Matches [-]SS[.NS] */
333 #define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
335 GMatchInfo
*match_info
;
338 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
339 if (compile_and_match("^" DATE_RE
" " TIME_RE
"$", str
, &match_info
)) {
340 unsigned int year
= 0, month
= 0, day
= 0, hours
= 0, minutes
= 0, seconds
= 0, nanoseconds
= 0;
341 gint match_count
= g_match_info_get_match_count(match_info
);
343 BT_ASSERT(match_count
>= 6 && match_count
<= 8);
345 year
= match_to_uint(match_info
, 1);
346 month
= match_to_uint(match_info
, 2);
347 day
= match_to_uint(match_info
, 3);
348 hours
= match_to_uint(match_info
, 4);
349 minutes
= match_to_uint(match_info
, 5);
351 if (match_count
>= 7) {
352 seconds
= match_to_uint(match_info
, 6);
355 if (match_count
>= 8) {
356 nanoseconds
= match_to_uint_ns(match_info
, 7);
359 set_bound_ns_from_origin(bound
, year
, month
, day
, hours
, minutes
, seconds
, nanoseconds
, is_gmt
);
364 if (compile_and_match("^" DATE_RE
"$", str
, &match_info
)) {
365 unsigned int year
= 0, month
= 0, day
= 0;
367 BT_ASSERT(g_match_info_get_match_count(match_info
) == 4);
369 year
= match_to_uint(match_info
, 1);
370 month
= match_to_uint(match_info
, 2);
371 day
= match_to_uint(match_info
, 3);
373 set_bound_ns_from_origin(bound
, year
, month
, day
, 0, 0, 0, 0, is_gmt
);
378 /* Try `hh:mm[:ss[.ns]]` format */
379 if (compile_and_match("^" TIME_RE
"$", str
, &match_info
)) {
380 gint match_count
= g_match_info_get_match_count(match_info
);
381 BT_ASSERT(match_count
>= 3 && match_count
<= 5);
382 bound
->time
.hour
= match_to_uint(match_info
, 1);
383 bound
->time
.minute
= match_to_uint(match_info
, 2);
385 if (match_count
>= 4) {
386 bound
->time
.second
= match_to_uint(match_info
, 3);
389 if (match_count
>= 5) {
390 bound
->time
.ns
= match_to_uint_ns(match_info
, 4);
396 /* Try `[-]s[.ns]` format */
397 if (compile_and_match("^" S_NS_RE
"$", str
, &match_info
)) {
398 gboolean is_neg
, fetch_pos_ret
;
399 gint start_pos
, end_pos
, match_count
;
400 guint64 seconds
, nanoseconds
= 0;
402 match_count
= g_match_info_get_match_count(match_info
);
403 BT_ASSERT(match_count
>= 3 && match_count
<= 4);
405 /* Check for presence of negation sign. */
406 fetch_pos_ret
= g_match_info_fetch_pos(match_info
, 1, &start_pos
, &end_pos
);
407 BT_ASSERT(fetch_pos_ret
);
408 is_neg
= (end_pos
- start_pos
) > 0;
410 seconds
= match_to_uint(match_info
, 2);
412 if (match_count
>= 4) {
413 nanoseconds
= match_to_uint_ns(match_info
, 3);
416 bound
->ns_from_origin
= seconds
* NS_PER_S
+ nanoseconds
;
419 bound
->ns_from_origin
= -bound
->ns_from_origin
;
422 bound
->is_set
= true;
427 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
428 "Invalid date/time format: param=\"%s\"", str
);
432 g_match_info_free(match_info
);
438 * Sets a trimmer bound's properties from a parameter string/integer
441 * Returns a negative value if anything goes wrong.
444 int set_bound_from_param(struct trimmer_comp
*trimmer_comp
,
445 const char *param_name
, const bt_value
*param
,
446 struct trimmer_bound
*bound
, bool is_gmt
)
452 if (bt_value_is_signed_integer(param
)) {
453 int64_t value
= bt_value_integer_signed_get(param
);
456 * Just convert it to a temporary string to handle
457 * everything the same way.
459 sprintf(tmp_arg
, "%" PRId64
, value
);
462 BT_ASSERT(bt_value_is_string(param
));
463 arg
= bt_value_string_get(param
);
466 ret
= set_bound_from_str(trimmer_comp
, arg
, bound
, is_gmt
);
472 int validate_trimmer_bounds(struct trimmer_comp
*trimmer_comp
,
473 struct trimmer_bound
*begin
, struct trimmer_bound
*end
)
477 BT_ASSERT(begin
->is_set
);
478 BT_ASSERT(end
->is_set
);
480 if (!begin
->is_infinite
&& !end
->is_infinite
&&
481 begin
->ns_from_origin
> end
->ns_from_origin
) {
482 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
483 "Trimming time range's beginning time is greater than end time: "
484 "begin-ns-from-origin=%" PRId64
", "
485 "end-ns-from-origin=%" PRId64
,
486 begin
->ns_from_origin
,
487 end
->ns_from_origin
);
492 if (!begin
->is_infinite
&& begin
->ns_from_origin
== INT64_MIN
) {
493 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
494 "Invalid trimming time range's beginning time: "
495 "ns-from-origin=%" PRId64
,
496 begin
->ns_from_origin
);
501 if (!end
->is_infinite
&& end
->ns_from_origin
== INT64_MIN
) {
502 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
503 "Invalid trimming time range's end time: "
504 "ns-from-origin=%" PRId64
,
505 end
->ns_from_origin
);
515 enum bt_param_validation_status
validate_bound_type(
516 const bt_value
*value
,
517 struct bt_param_validation_context
*context
)
519 enum bt_param_validation_status status
= BT_PARAM_VALIDATION_STATUS_OK
;
521 if (!bt_value_is_signed_integer(value
) &&
522 !bt_value_is_string(value
)) {
523 status
= bt_param_validation_error(context
,
524 "unexpected type: expected-types=[%s, %s], actual-type=%s",
525 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
526 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
527 bt_common_value_type_string(bt_value_get_type(value
)));
534 struct bt_param_validation_map_value_entry_descr trimmer_params
[] = {
535 { "gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
536 { "begin", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
537 { "end", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .validation_func
= validate_bound_type
} },
538 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
542 bt_component_class_initialize_method_status
init_trimmer_comp_from_params(
543 struct trimmer_comp
*trimmer_comp
,
544 const bt_value
*params
)
546 const bt_value
*value
;
547 bt_component_class_initialize_method_status status
;
548 enum bt_param_validation_status validation_status
;
549 gchar
*validate_error
= NULL
;
551 validation_status
= bt_param_validation_validate(params
,
552 trimmer_params
, &validate_error
);
553 if (validation_status
== BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
) {
554 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
556 } else if (validation_status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
557 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
558 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
, "%s",
564 value
= bt_value_map_borrow_entry_value_const(params
, "gmt");
566 trimmer_comp
->is_gmt
= (bool) bt_value_bool_get(value
);
569 value
= bt_value_map_borrow_entry_value_const(params
, "begin");
571 if (set_bound_from_param(trimmer_comp
, "begin", value
,
572 &trimmer_comp
->begin
, trimmer_comp
->is_gmt
)) {
573 /* set_bound_from_param() logs errors */
574 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
578 trimmer_comp
->begin
.is_infinite
= true;
579 trimmer_comp
->begin
.is_set
= true;
582 value
= bt_value_map_borrow_entry_value_const(params
, "end");
584 if (set_bound_from_param(trimmer_comp
, "end", value
,
585 &trimmer_comp
->end
, trimmer_comp
->is_gmt
)) {
586 /* set_bound_from_param() logs errors */
587 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
591 trimmer_comp
->end
.is_infinite
= true;
592 trimmer_comp
->end
.is_set
= true;
595 if (trimmer_comp
->begin
.is_set
&& trimmer_comp
->end
.is_set
) {
596 /* validate_trimmer_bounds() logs errors */
597 if (validate_trimmer_bounds(trimmer_comp
,
598 &trimmer_comp
->begin
, &trimmer_comp
->end
)) {
599 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR
;
604 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
607 g_free(validate_error
);
612 bt_component_class_initialize_method_status
trimmer_init(
613 bt_self_component_filter
*self_comp_flt
,
614 bt_self_component_filter_configuration
*config
,
615 const bt_value
*params
, void *init_data
)
617 bt_component_class_initialize_method_status status
;
618 bt_self_component_add_port_status add_port_status
;
619 struct trimmer_comp
*trimmer_comp
= create_trimmer_comp();
620 bt_self_component
*self_comp
=
621 bt_self_component_filter_as_self_component(self_comp_flt
);
624 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
628 trimmer_comp
->log_level
= bt_component_get_logging_level(
629 bt_self_component_as_component(self_comp
));
630 trimmer_comp
->self_comp
= self_comp
;
631 trimmer_comp
->self_comp_filter
= self_comp_flt
;
633 add_port_status
= bt_self_component_filter_add_input_port(
634 self_comp_flt
, in_port_name
, NULL
, NULL
);
635 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
636 status
= (int) add_port_status
;
640 add_port_status
= bt_self_component_filter_add_output_port(
641 self_comp_flt
, "out", NULL
, NULL
);
642 if (add_port_status
!= BT_SELF_COMPONENT_ADD_PORT_STATUS_OK
) {
643 status
= (int) add_port_status
;
647 status
= init_trimmer_comp_from_params(trimmer_comp
, params
);
648 if (status
!= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
) {
652 bt_self_component_set_data(self_comp
, trimmer_comp
);
654 status
= BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK
;
659 destroy_trimmer_comp(trimmer_comp
);
667 void destroy_trimmer_iterator(struct trimmer_iterator
*trimmer_it
)
673 bt_message_iterator_put_ref(
674 trimmer_it
->upstream_iter
);
676 if (trimmer_it
->output_messages
) {
677 g_queue_free(trimmer_it
->output_messages
);
680 if (trimmer_it
->stream_states
) {
681 g_hash_table_destroy(trimmer_it
->stream_states
);
690 void destroy_trimmer_iterator_stream_state(
691 struct trimmer_iterator_stream_state
*sstate
)
694 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
699 bt_message_iterator_class_initialize_method_status
trimmer_msg_iter_init(
700 bt_self_message_iterator
*self_msg_iter
,
701 bt_self_message_iterator_configuration
*config
,
702 bt_self_component_port_output
*port
)
704 bt_message_iterator_class_initialize_method_status status
;
705 bt_message_iterator_create_from_message_iterator_status
707 struct trimmer_iterator
*trimmer_it
;
708 bt_self_component
*self_comp
=
709 bt_self_message_iterator_borrow_component(self_msg_iter
);
711 trimmer_it
= g_new0(struct trimmer_iterator
, 1);
713 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
717 trimmer_it
->trimmer_comp
= bt_self_component_get_data(self_comp
);
718 BT_ASSERT(trimmer_it
->trimmer_comp
);
720 if (trimmer_it
->trimmer_comp
->begin
.is_set
&&
721 trimmer_it
->trimmer_comp
->end
.is_set
) {
723 * Both trimming time range's bounds are set, so skip
725 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
728 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
;
731 trimmer_it
->begin
= trimmer_it
->trimmer_comp
->begin
;
732 trimmer_it
->end
= trimmer_it
->trimmer_comp
->end
;
734 bt_message_iterator_create_from_message_iterator(
736 bt_self_component_filter_borrow_input_port_by_name(
737 trimmer_it
->trimmer_comp
->self_comp_filter
, in_port_name
),
738 &trimmer_it
->upstream_iter
);
739 if (msg_iter_status
!= BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK
) {
740 status
= (int) msg_iter_status
;
744 trimmer_it
->output_messages
= g_queue_new();
745 if (!trimmer_it
->output_messages
) {
746 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
750 trimmer_it
->stream_states
= g_hash_table_new_full(g_direct_hash
,
751 g_direct_equal
, NULL
,
752 (GDestroyNotify
) destroy_trimmer_iterator_stream_state
);
753 if (!trimmer_it
->stream_states
) {
754 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR
;
759 * The trimmer requires upstream messages to have times, so it can
760 * always seek forward.
762 bt_self_message_iterator_configuration_set_can_seek_forward(
765 trimmer_it
->self_msg_iter
= self_msg_iter
;
766 bt_self_message_iterator_set_data(self_msg_iter
, trimmer_it
);
768 status
= BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK
;
772 destroy_trimmer_iterator(trimmer_it
);
779 int get_msg_ns_from_origin(const bt_message
*msg
, int64_t *ns_from_origin
,
780 bool *has_clock_snapshot
)
782 const bt_clock_class
*clock_class
= NULL
;
783 const bt_clock_snapshot
*clock_snapshot
= NULL
;
787 BT_ASSERT_DBG(ns_from_origin
);
788 BT_ASSERT_DBG(has_clock_snapshot
);
790 switch (bt_message_get_type(msg
)) {
791 case BT_MESSAGE_TYPE_EVENT
:
793 bt_message_event_borrow_stream_class_default_clock_class_const(
795 if (G_UNLIKELY(!clock_class
)) {
799 clock_snapshot
= bt_message_event_borrow_default_clock_snapshot_const(
802 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
804 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
806 if (G_UNLIKELY(!clock_class
)) {
810 clock_snapshot
= bt_message_packet_beginning_borrow_default_clock_snapshot_const(
813 case BT_MESSAGE_TYPE_PACKET_END
:
815 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
817 if (G_UNLIKELY(!clock_class
)) {
821 clock_snapshot
= bt_message_packet_end_borrow_default_clock_snapshot_const(
824 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
826 enum bt_message_stream_clock_snapshot_state cs_state
;
829 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg
);
830 if (G_UNLIKELY(!clock_class
)) {
834 cs_state
= bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
835 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
836 goto no_clock_snapshot
;
841 case BT_MESSAGE_TYPE_STREAM_END
:
843 enum bt_message_stream_clock_snapshot_state cs_state
;
846 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg
);
847 if (G_UNLIKELY(!clock_class
)) {
851 cs_state
= bt_message_stream_end_borrow_default_clock_snapshot_const(msg
, &clock_snapshot
);
852 if (cs_state
!= BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN
) {
853 goto no_clock_snapshot
;
858 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
860 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
862 if (G_UNLIKELY(!clock_class
)) {
866 clock_snapshot
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
869 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
871 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
873 if (G_UNLIKELY(!clock_class
)) {
877 clock_snapshot
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
880 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
:
882 bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
886 goto no_clock_snapshot
;
889 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
,
891 if (G_UNLIKELY(ret
)) {
895 *has_clock_snapshot
= true;
899 *has_clock_snapshot
= false;
910 void put_messages(bt_message_array_const msgs
, uint64_t count
)
914 for (i
= 0; i
< count
; i
++) {
915 BT_MESSAGE_PUT_REF_AND_RESET(msgs
[i
]);
920 int set_trimmer_iterator_bound(struct trimmer_iterator
*trimmer_it
,
921 struct trimmer_bound
*bound
, int64_t ns_from_origin
,
924 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
927 time_t time_seconds
= (time_t) (ns_from_origin
/ NS_PER_S
);
930 BT_ASSERT(!bound
->is_set
);
933 /* We only need to extract the date from this time */
935 res
= bt_gmtime_r(&time_seconds
, &tm
);
937 res
= bt_localtime_r(&time_seconds
, &tm
);
941 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(trimmer_comp
->self_comp
,
942 "Cannot convert timestamp to date and time",
943 ": ts=%" PRId64
, (int64_t) time_seconds
);
948 ret
= set_bound_ns_from_origin(bound
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1,
949 tm
.tm_mday
, bound
->time
.hour
, bound
->time
.minute
,
950 bound
->time
.second
, bound
->time
.ns
, is_gmt
);
957 bt_message_iterator_class_next_method_status
958 state_set_trimmer_iterator_bounds(
959 struct trimmer_iterator
*trimmer_it
)
961 bt_message_iterator_next_status upstream_iter_status
=
962 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
;
963 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
964 bt_message_array_const msgs
;
966 int64_t ns_from_origin
= INT64_MIN
;
970 BT_ASSERT(!trimmer_it
->begin
.is_set
||
971 !trimmer_it
->end
.is_set
);
974 upstream_iter_status
=
975 bt_message_iterator_next(
976 trimmer_it
->upstream_iter
, &msgs
, &count
);
977 if (upstream_iter_status
!= BT_MESSAGE_ITERATOR_NEXT_STATUS_OK
) {
981 for (i
= 0; i
< count
; i
++) {
982 const bt_message
*msg
= msgs
[i
];
983 bool has_ns_from_origin
;
984 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
,
985 &has_ns_from_origin
);
990 if (!has_ns_from_origin
) {
994 BT_ASSERT_DBG(ns_from_origin
!= INT64_MIN
&&
995 ns_from_origin
!= INT64_MAX
);
996 put_messages(msgs
, count
);
1000 put_messages(msgs
, count
);
1004 if (!trimmer_it
->begin
.is_set
) {
1005 BT_ASSERT(!trimmer_it
->begin
.is_infinite
);
1006 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->begin
,
1007 ns_from_origin
, trimmer_comp
->is_gmt
);
1013 if (!trimmer_it
->end
.is_set
) {
1014 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
1015 ret
= set_trimmer_iterator_bound(trimmer_it
, &trimmer_it
->end
,
1016 ns_from_origin
, trimmer_comp
->is_gmt
);
1022 ret
= validate_trimmer_bounds(trimmer_it
->trimmer_comp
,
1023 &trimmer_it
->begin
, &trimmer_it
->end
);
1031 put_messages(msgs
, count
);
1032 upstream_iter_status
= BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR
;
1035 return (int) upstream_iter_status
;
1039 bt_message_iterator_class_next_method_status
state_seek_initially(
1040 struct trimmer_iterator
*trimmer_it
)
1042 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1043 bt_message_iterator_class_next_method_status status
;
1045 BT_ASSERT(trimmer_it
->begin
.is_set
);
1047 if (trimmer_it
->begin
.is_infinite
) {
1050 status
= (int) bt_message_iterator_can_seek_beginning(
1051 trimmer_it
->upstream_iter
, &can_seek
);
1052 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1054 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1055 "Cannot make upstream message iterator initially seek its beginning.");
1062 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1063 "Cannot make upstream message iterator initially seek its beginning.");
1064 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1068 status
= (int) bt_message_iterator_seek_beginning(
1069 trimmer_it
->upstream_iter
);
1073 status
= (int) bt_message_iterator_can_seek_ns_from_origin(
1074 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
,
1077 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1079 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1080 "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64
,
1081 trimmer_it
->begin
.ns_from_origin
);
1088 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1089 "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64
,
1090 trimmer_it
->begin
.ns_from_origin
);
1091 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1095 status
= (int) bt_message_iterator_seek_ns_from_origin(
1096 trimmer_it
->upstream_iter
, trimmer_it
->begin
.ns_from_origin
);
1099 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1100 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_TRIM
;
1108 void push_message(struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
)
1110 g_queue_push_head(trimmer_it
->output_messages
, (void *) msg
);
1114 const bt_message
*pop_message(struct trimmer_iterator
*trimmer_it
)
1116 return g_queue_pop_tail(trimmer_it
->output_messages
);
1120 int clock_raw_value_from_ns_from_origin(const bt_clock_class
*clock_class
,
1121 int64_t ns_from_origin
, uint64_t *raw_value
)
1124 int64_t cc_offset_s
;
1125 uint64_t cc_offset_cycles
;
1128 bt_clock_class_get_offset(clock_class
, &cc_offset_s
, &cc_offset_cycles
);
1129 cc_freq
= bt_clock_class_get_frequency(clock_class
);
1130 return bt_common_clock_value_from_ns_from_origin(cc_offset_s
,
1131 cc_offset_cycles
, cc_freq
, ns_from_origin
, raw_value
);
1135 bt_message_iterator_class_next_method_status
1136 end_stream(struct trimmer_iterator
*trimmer_it
,
1137 struct trimmer_iterator_stream_state
*sstate
)
1139 bt_message_iterator_class_next_method_status status
=
1140 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1141 /* Initialize to silence maybe-uninitialized warning. */
1142 uint64_t raw_value
= 0;
1143 bt_message
*msg
= NULL
;
1145 BT_ASSERT(!trimmer_it
->end
.is_infinite
);
1146 BT_ASSERT(sstate
->stream
);
1149 * If we haven't seen a message with a clock snapshot, we don't know if the trimmer's end bound is within
1150 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1152 * Also, it would be a bit of a lie to generate a stream end message with the end bound as its
1153 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1154 * seen a message with a clock snapshot and the stream is cut short by another message with a
1155 * clock snapshot, then we are sure that the the end bound time is not below the clock range,
1156 * and we know the stream was active at that time (and that we cut it short).
1158 if (sstate
->seen_clock_snapshot
) {
1159 const bt_clock_class
*clock_class
;
1162 clock_class
= bt_stream_class_borrow_default_clock_class_const(
1163 bt_stream_borrow_class_const(sstate
->stream
));
1164 BT_ASSERT(clock_class
);
1165 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1166 trimmer_it
->end
.ns_from_origin
, &raw_value
);
1168 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1173 if (sstate
->cur_packet
) {
1175 * Create and push a packet end message, making its time
1176 * the trimming range's end time.
1178 * We know that we must have seen a clock snapshot, the one in
1179 * the packet beginning message, since trimmer currently
1180 * requires packet messages to have clock snapshots (see comment
1181 * in create_stream_state_entry).
1183 BT_ASSERT(sstate
->seen_clock_snapshot
);
1185 msg
= bt_message_packet_end_create_with_default_clock_snapshot(
1186 trimmer_it
->self_msg_iter
, sstate
->cur_packet
,
1189 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1193 push_message(trimmer_it
, msg
);
1195 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1198 /* Create and push a stream end message. */
1199 msg
= bt_message_stream_end_create(trimmer_it
->self_msg_iter
,
1202 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1206 if (sstate
->seen_clock_snapshot
) {
1207 bt_message_stream_end_set_default_clock_snapshot(msg
, raw_value
);
1210 push_message(trimmer_it
, msg
);
1214 * Just to make sure that we don't use this stream state again
1215 * in the future without an obvious error.
1217 sstate
->stream
= NULL
;
1220 bt_message_put_ref(msg
);
1225 bt_message_iterator_class_next_method_status
end_iterator_streams(
1226 struct trimmer_iterator
*trimmer_it
)
1228 bt_message_iterator_class_next_method_status status
=
1229 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1230 GHashTableIter iter
;
1231 gpointer key
, sstate
;
1233 if (trimmer_it
->end
.is_infinite
) {
1235 * An infinite trimming range's end time guarantees that
1236 * we received (and pushed) all the appropriate end
1243 * End each stream and then remove them from the hash table of
1244 * stream states to release unneeded references.
1246 g_hash_table_iter_init(&iter
, trimmer_it
->stream_states
);
1248 while (g_hash_table_iter_next(&iter
, &key
, &sstate
)) {
1249 status
= end_stream(trimmer_it
, sstate
);
1256 g_hash_table_remove_all(trimmer_it
->stream_states
);
1263 bt_message_iterator_class_next_method_status
1264 create_stream_state_entry(
1265 struct trimmer_iterator
*trimmer_it
,
1266 const struct bt_stream
*stream
,
1267 struct trimmer_iterator_stream_state
**stream_state
)
1269 struct trimmer_comp
*trimmer_comp
= trimmer_it
->trimmer_comp
;
1270 bt_message_iterator_class_next_method_status status
;
1271 struct trimmer_iterator_stream_state
*sstate
;
1272 const bt_stream_class
*sc
;
1274 BT_ASSERT(!bt_g_hash_table_contains(trimmer_it
->stream_states
, stream
));
1277 * Validate right now that the stream's class
1278 * has a registered default clock class so that
1279 * an existing stream state guarantees existing
1280 * default clock snapshots for its associated
1283 * Also check that clock snapshots are always
1286 sc
= bt_stream_borrow_class_const(stream
);
1287 if (!bt_stream_class_borrow_default_clock_class_const(sc
)) {
1288 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1289 "Unsupported stream: stream class does "
1290 "not have a default clock class: "
1292 "stream-id=%" PRIu64
", "
1293 "stream-name=\"%s\"",
1294 stream
, bt_stream_get_id(stream
),
1295 bt_stream_get_name(stream
));
1296 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1301 * Temporary: make sure packet beginning, packet
1302 * end, discarded events, and discarded packets
1303 * messages have default clock snapshots until
1304 * the support for not having them is
1307 if (bt_stream_class_supports_packets(sc
)) {
1308 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1310 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1311 "Unsupported stream: packets have no beginning clock snapshot: "
1313 "stream-id=%" PRIu64
", "
1314 "stream-name=\"%s\"",
1315 stream
, bt_stream_get_id(stream
),
1316 bt_stream_get_name(stream
));
1317 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1321 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1323 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1324 "Unsupported stream: packets have no end clock snapshot: "
1326 "stream-id=%" PRIu64
", "
1327 "stream-name=\"%s\"",
1328 stream
, bt_stream_get_id(stream
),
1329 bt_stream_get_name(stream
));
1330 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1334 if (bt_stream_class_supports_discarded_packets(sc
) &&
1335 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc
)) {
1336 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1337 "Unsupported stream: discarded packets "
1338 "have no clock snapshots: "
1340 "stream-id=%" PRIu64
", "
1341 "stream-name=\"%s\"",
1342 stream
, bt_stream_get_id(stream
),
1343 bt_stream_get_name(stream
));
1344 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1349 if (bt_stream_class_supports_discarded_events(sc
) &&
1350 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc
)) {
1351 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp
->self_comp
,
1352 "Unsupported stream: discarded events have no clock snapshots: "
1354 "stream-id=%" PRIu64
", "
1355 "stream-name=\"%s\"",
1356 stream
, bt_stream_get_id(stream
),
1357 bt_stream_get_name(stream
));
1358 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1362 sstate
= g_new0(struct trimmer_iterator_stream_state
, 1);
1364 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1368 sstate
->stream
= stream
;
1370 g_hash_table_insert(trimmer_it
->stream_states
, (void *) stream
, sstate
);
1372 *stream_state
= sstate
;
1374 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1381 struct trimmer_iterator_stream_state
*get_stream_state_entry(
1382 struct trimmer_iterator
*trimmer_it
,
1383 const struct bt_stream
*stream
)
1385 struct trimmer_iterator_stream_state
*sstate
;
1387 BT_ASSERT_DBG(stream
);
1388 sstate
= g_hash_table_lookup(trimmer_it
->stream_states
, stream
);
1389 BT_ASSERT_DBG(sstate
);
1395 * Handles a message which is associated to a given stream state. This
1396 * _could_ make the iterator's output message queue grow; this could
1397 * also consume the message without pushing anything to this queue, only
1398 * modifying the stream state.
1400 * This function consumes the `msg` reference, _whatever the outcome_.
1402 * If non-NULL, `ns_from_origin` is the message's time, as given by
1403 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
1405 * This function sets `reached_end` if handling this message made the
1406 * iterator reach the end of the trimming range. Note that the output
1407 * message queue could contain messages even if this function sets
1411 bt_message_iterator_class_next_method_status
1412 handle_message_with_stream(
1413 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1414 const struct bt_stream
*stream
, const int64_t *ns_from_origin
,
1417 bt_message_iterator_class_next_method_status status
=
1418 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1419 bt_message_type msg_type
= bt_message_get_type(msg
);
1421 struct trimmer_iterator_stream_state
*sstate
= NULL
;
1424 * Retrieve the stream's state - except if the message is stream
1425 * beginning, in which case we don't know about about this stream yet.
1427 if (msg_type
!= BT_MESSAGE_TYPE_STREAM_BEGINNING
) {
1428 sstate
= get_stream_state_entry(trimmer_it
, stream
);
1432 case BT_MESSAGE_TYPE_EVENT
:
1434 * Event messages always have a clock snapshot if the stream
1435 * class has a clock class. And we know it has, otherwise we
1436 * couldn't be using the trimmer component.
1438 BT_ASSERT_DBG(ns_from_origin
);
1440 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1441 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1442 status
= end_iterator_streams(trimmer_it
);
1443 *reached_end
= true;
1447 sstate
->seen_clock_snapshot
= true;
1449 push_message(trimmer_it
, msg
);
1453 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1455 * Packet beginning messages won't have a clock snapshot if
1456 * stream_class->packets_have_beginning_default_clock_snapshot
1457 * is false. But for now, assume they always do.
1459 BT_ASSERT(ns_from_origin
);
1460 BT_ASSERT(!sstate
->cur_packet
);
1462 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1463 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1464 status
= end_iterator_streams(trimmer_it
);
1465 *reached_end
= true;
1469 sstate
->cur_packet
=
1470 bt_message_packet_beginning_borrow_packet_const(msg
);
1471 bt_packet_get_ref(sstate
->cur_packet
);
1473 sstate
->seen_clock_snapshot
= true;
1475 push_message(trimmer_it
, msg
);
1479 case BT_MESSAGE_TYPE_PACKET_END
:
1481 * Packet end messages won't have a clock snapshot if
1482 * stream_class->packets_have_end_default_clock_snapshot
1483 * is false. But for now, assume they always do.
1485 BT_ASSERT(ns_from_origin
);
1486 BT_ASSERT(sstate
->cur_packet
);
1488 if (G_UNLIKELY(!trimmer_it
->end
.is_infinite
&&
1489 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1490 status
= end_iterator_streams(trimmer_it
);
1491 *reached_end
= true;
1495 BT_PACKET_PUT_REF_AND_RESET(sstate
->cur_packet
);
1497 sstate
->seen_clock_snapshot
= true;
1499 push_message(trimmer_it
, msg
);
1503 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1504 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1507 * `ns_from_origin` is the message's time range's
1508 * beginning time here.
1510 int64_t end_ns_from_origin
;
1511 const bt_clock_snapshot
*end_cs
;
1513 BT_ASSERT(ns_from_origin
);
1515 sstate
->seen_clock_snapshot
= true;
1517 if (bt_message_get_type(msg
) ==
1518 BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
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_events_borrow_end_default_clock_snapshot_const(
1528 * Safe to ignore the return value because we
1529 * know there's a default clock and it's always
1532 end_cs
= bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1536 if (bt_clock_snapshot_get_ns_from_origin(end_cs
,
1537 &end_ns_from_origin
)) {
1538 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1542 if (!trimmer_it
->end
.is_infinite
&&
1543 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1544 status
= end_iterator_streams(trimmer_it
);
1545 *reached_end
= true;
1549 if (!trimmer_it
->end
.is_infinite
&&
1550 end_ns_from_origin
> trimmer_it
->end
.ns_from_origin
) {
1552 * This message's end time is outside the
1553 * trimming time range: replace it with a new
1554 * message having an end time equal to the
1555 * trimming time range's end and without a
1558 const bt_clock_class
*clock_class
=
1559 bt_clock_snapshot_borrow_clock_class_const(
1561 const bt_clock_snapshot
*begin_cs
;
1562 bt_message
*new_msg
;
1563 uint64_t end_raw_value
;
1565 ret
= clock_raw_value_from_ns_from_origin(clock_class
,
1566 trimmer_it
->end
.ns_from_origin
, &end_raw_value
);
1568 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1572 if (msg_type
== BT_MESSAGE_TYPE_DISCARDED_EVENTS
) {
1573 begin_cs
= bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1575 new_msg
= bt_message_discarded_events_create_with_default_clock_snapshots(
1576 trimmer_it
->self_msg_iter
,
1578 bt_clock_snapshot_get_value(begin_cs
),
1581 begin_cs
= bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1583 new_msg
= bt_message_discarded_packets_create_with_default_clock_snapshots(
1584 trimmer_it
->self_msg_iter
,
1586 bt_clock_snapshot_get_value(begin_cs
),
1591 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR
;
1595 /* Replace the original message */
1596 BT_MESSAGE_MOVE_REF(msg
, new_msg
);
1599 push_message(trimmer_it
, msg
);
1604 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1606 * If this message has a time and this time is greater than the
1607 * trimmer's end bound, it triggers the end of the trim window.
1609 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1610 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1611 status
= end_iterator_streams(trimmer_it
);
1612 *reached_end
= true;
1616 /* Learn about this stream. */
1617 status
= create_stream_state_entry(trimmer_it
, stream
, &sstate
);
1618 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1622 if (ns_from_origin
) {
1623 sstate
->seen_clock_snapshot
= true;
1626 push_message(trimmer_it
, msg
);
1629 case BT_MESSAGE_TYPE_STREAM_END
:
1634 * If this message has a time and this time is greater than the
1635 * trimmer's end bound, it triggers the end of the trim window.
1637 if (G_UNLIKELY(ns_from_origin
&& !trimmer_it
->end
.is_infinite
&&
1638 *ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1639 status
= end_iterator_streams(trimmer_it
);
1640 *reached_end
= true;
1645 * Either the stream end message's time is within the trimmer's
1646 * bounds, or it doesn't have a time. In both cases, pass
1647 * the message unmodified.
1649 push_message(trimmer_it
, msg
);
1652 /* Forget about this stream. */
1653 removed
= g_hash_table_remove(trimmer_it
->stream_states
, sstate
->stream
);
1662 /* We release the message's reference whatever the outcome */
1663 bt_message_put_ref(msg
);
1668 * Handles an input message. This _could_ make the iterator's output
1669 * message queue grow; this could also consume the message without
1670 * pushing anything to this queue, only modifying the stream state.
1672 * This function consumes the `msg` reference, _whatever the outcome_.
1674 * This function sets `reached_end` if handling this message made the
1675 * iterator reach the end of the trimming range. Note that the output
1676 * message queue could contain messages even if this function sets
1680 bt_message_iterator_class_next_method_status
handle_message(
1681 struct trimmer_iterator
*trimmer_it
, const bt_message
*msg
,
1684 bt_message_iterator_class_next_method_status status
;
1685 const bt_stream
*stream
= NULL
;
1686 int64_t ns_from_origin
= INT64_MIN
;
1687 bool has_ns_from_origin
= false;
1690 /* Find message's associated stream */
1691 switch (bt_message_get_type(msg
)) {
1692 case BT_MESSAGE_TYPE_EVENT
:
1693 stream
= bt_event_borrow_stream_const(
1694 bt_message_event_borrow_event_const(msg
));
1696 case BT_MESSAGE_TYPE_PACKET_BEGINNING
:
1697 stream
= bt_packet_borrow_stream_const(
1698 bt_message_packet_beginning_borrow_packet_const(msg
));
1700 case BT_MESSAGE_TYPE_PACKET_END
:
1701 stream
= bt_packet_borrow_stream_const(
1702 bt_message_packet_end_borrow_packet_const(msg
));
1704 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1705 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1707 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1708 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1710 case BT_MESSAGE_TYPE_STREAM_BEGINNING
:
1711 stream
= bt_message_stream_beginning_borrow_stream_const(msg
);
1713 case BT_MESSAGE_TYPE_STREAM_END
:
1714 stream
= bt_message_stream_end_borrow_stream_const(msg
);
1720 /* Retrieve the message's time */
1721 ret
= get_msg_ns_from_origin(msg
, &ns_from_origin
, &has_ns_from_origin
);
1722 if (G_UNLIKELY(ret
)) {
1723 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR
;
1727 if (G_LIKELY(stream
)) {
1728 /* Message associated to a stream */
1729 status
= handle_message_with_stream(trimmer_it
, msg
,
1730 stream
, has_ns_from_origin
? &ns_from_origin
: NULL
, reached_end
);
1733 * handle_message_with_stream_state() unconditionally
1739 * Message not associated to a stream (message iterator
1742 if (G_UNLIKELY(ns_from_origin
> trimmer_it
->end
.ns_from_origin
)) {
1743 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
1744 status
= end_iterator_streams(trimmer_it
);
1745 *reached_end
= true;
1747 push_message(trimmer_it
, msg
);
1748 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1754 /* We release the message's reference whatever the outcome */
1755 bt_message_put_ref(msg
);
1760 void fill_message_array_from_output_messages(
1761 struct trimmer_iterator
*trimmer_it
,
1762 bt_message_array_const msgs
, uint64_t capacity
, uint64_t *count
)
1767 * Move auto-seek messages to the output array (which is this
1768 * iterator's base message array).
1770 while (capacity
> 0 && !g_queue_is_empty(trimmer_it
->output_messages
)) {
1771 msgs
[*count
] = pop_message(trimmer_it
);
1776 BT_ASSERT_DBG(*count
> 0);
1780 bt_message_iterator_class_next_method_status
state_ending(
1781 struct trimmer_iterator
*trimmer_it
,
1782 bt_message_array_const msgs
, uint64_t capacity
,
1785 bt_message_iterator_class_next_method_status status
=
1786 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1788 if (g_queue_is_empty(trimmer_it
->output_messages
)) {
1789 trimmer_it
->state
= TRIMMER_ITERATOR_STATE_ENDED
;
1790 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1794 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1802 bt_message_iterator_class_next_method_status
1803 state_trim(struct trimmer_iterator
*trimmer_it
,
1804 bt_message_array_const msgs
, uint64_t capacity
,
1807 bt_message_iterator_class_next_method_status status
=
1808 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1809 bt_message_array_const my_msgs
;
1812 bool reached_end
= false;
1814 while (g_queue_is_empty(trimmer_it
->output_messages
)) {
1815 status
= (int) bt_message_iterator_next(
1816 trimmer_it
->upstream_iter
, &my_msgs
, &my_count
);
1817 if (G_UNLIKELY(status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
)) {
1818 if (status
== BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
) {
1819 status
= end_iterator_streams(trimmer_it
);
1820 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1825 TRIMMER_ITERATOR_STATE_ENDING
;
1826 status
= state_ending(trimmer_it
, msgs
,
1833 BT_ASSERT_DBG(my_count
> 0);
1835 for (i
= 0; i
< my_count
; i
++) {
1836 status
= handle_message(trimmer_it
, my_msgs
[i
],
1840 * handle_message() unconditionally consumes the
1841 * message reference.
1845 if (G_UNLIKELY(status
!=
1846 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
)) {
1847 put_messages(my_msgs
, my_count
);
1851 if (G_UNLIKELY(reached_end
)) {
1853 * This message's time was passed the
1854 * trimming time range's end time: we
1855 * are done. Their might still be
1856 * messages in the output message queue,
1857 * so move to the "ending" state and
1858 * apply it immediately since
1859 * state_trim() is called within the
1862 put_messages(my_msgs
, my_count
);
1864 TRIMMER_ITERATOR_STATE_ENDING
;
1865 status
= state_ending(trimmer_it
, msgs
,
1873 * There's at least one message in the output message queue:
1874 * move the messages to the output message array.
1876 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it
->output_messages
));
1877 fill_message_array_from_output_messages(trimmer_it
, msgs
,
1885 bt_message_iterator_class_next_method_status
trimmer_msg_iter_next(
1886 bt_self_message_iterator
*self_msg_iter
,
1887 bt_message_array_const msgs
, uint64_t capacity
,
1890 struct trimmer_iterator
*trimmer_it
=
1891 bt_self_message_iterator_get_data(self_msg_iter
);
1892 bt_message_iterator_class_next_method_status status
=
1893 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
;
1895 BT_ASSERT_DBG(trimmer_it
);
1897 if (G_LIKELY(trimmer_it
->state
== TRIMMER_ITERATOR_STATE_TRIM
)) {
1898 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1899 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1903 switch (trimmer_it
->state
) {
1904 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN
:
1905 status
= state_set_trimmer_iterator_bounds(trimmer_it
);
1906 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1910 status
= state_seek_initially(trimmer_it
);
1911 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1915 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1916 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1921 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY
:
1922 status
= state_seek_initially(trimmer_it
);
1923 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1927 status
= state_trim(trimmer_it
, msgs
, capacity
, count
);
1928 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1933 case TRIMMER_ITERATOR_STATE_ENDING
:
1934 status
= state_ending(trimmer_it
, msgs
, capacity
,
1936 if (status
!= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK
) {
1941 case TRIMMER_ITERATOR_STATE_ENDED
:
1942 status
= BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END
;
1954 void trimmer_msg_iter_finalize(bt_self_message_iterator
*self_msg_iter
)
1956 struct trimmer_iterator
*trimmer_it
=
1957 bt_self_message_iterator_get_data(self_msg_iter
);
1959 BT_ASSERT(trimmer_it
);
1960 destroy_trimmer_iterator(trimmer_it
);