port: fix -Wdeprecated-declarations warning about sprintf on macOS clang 14
[babeltrace.git] / src / plugins / utils / trimmer / trimmer.c
CommitLineData
cab3f160 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
cab3f160 4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7de0e49a 5 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
cab3f160
JG
6 */
7
0d9a3d3e 8#define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level)
350ad6c1 9#define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER"
d9c39b0a 10#include "logging/comp-logging.h"
b4565e8b 11
578e048b
MJ
12#include "compat/utc.h"
13#include "compat/time.h"
3fadfbc0 14#include <babeltrace2/babeltrace.h>
578e048b 15#include "common/common.h"
578e048b 16#include "common/assert.h"
c4f23e30 17#include <stdbool.h>
7de0e49a
PP
18#include <stdint.h>
19#include <inttypes.h>
20#include <glib.h>
188edac1 21#include "compat/glib.h"
90a55a4e 22#include "plugins/common/param-validation/param-validation.h"
7de0e49a
PP
23
24#include "trimmer.h"
25
26#define NS_PER_S INT64_C(1000000000)
27
28static const char * const in_port_name = "in";
29
30struct trimmer_time {
31 unsigned int hour, minute, second, ns;
32};
33
34struct trimmer_bound {
35 /*
36 * Nanoseconds from origin, valid if `is_set` is set and
37 * `is_infinite` is false.
38 */
39 int64_t ns_from_origin;
40
41 /* True if this bound's full time (`ns_from_origin`) is set */
42 bool is_set;
43
44 /*
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.
48 */
49 bool is_infinite;
50
51 /*
52 * This bound's time without the date; this time is used to set
53 * `ns_from_origin` once we know the date.
54 */
55 struct trimmer_time time;
56};
57
58struct trimmer_comp {
59 struct trimmer_bound begin, end;
60 bool is_gmt;
0d9a3d3e 61 bt_logging_level log_level;
ec4ae660 62 bt_self_component *self_comp;
a3f0c7db 63 bt_self_component_filter *self_comp_filter;
7de0e49a
PP
64};
65
66enum trimmer_iterator_state {
67 /*
68 * Find the first message's date and set the bounds's times
69 * accordingly.
70 */
71 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN,
72
73 /*
74 * Initially seek to the trimming range's beginning time.
75 */
76 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY,
77
78 /*
79 * Fill the output message queue for as long as received input
80 * messages are within the trimming time range.
81 */
82 TRIMMER_ITERATOR_STATE_TRIM,
83
84 /* Flush the remaining messages in the output message queue */
85 TRIMMER_ITERATOR_STATE_ENDING,
86
87 /* Trimming operation and message iterator is ended */
88 TRIMMER_ITERATOR_STATE_ENDED,
89};
90
91struct trimmer_iterator {
92 /* Weak */
93 struct trimmer_comp *trimmer_comp;
94
95 /* Weak */
96 bt_self_message_iterator *self_msg_iter;
97
98 enum trimmer_iterator_state state;
99
100 /* Owned by this */
9a2c8b8e 101 bt_message_iterator *upstream_iter;
7de0e49a
PP
102 struct trimmer_bound begin, end;
103
104 /*
105 * Queue of `const bt_message *` (owned by the queue).
106 *
107 * This is where the trimming operation pushes the messages to
108 * output by this message iterator.
109 */
110 GQueue *output_messages;
111
112 /*
113 * Hash table of `bt_stream *` (weak) to
114 * `struct trimmer_iterator_stream_state *` (owned by the HT).
115 */
116 GHashTable *stream_states;
117};
118
119struct trimmer_iterator_stream_state {
7de0e49a
PP
120 /* Weak */
121 const bt_stream *stream;
122
188edac1
SM
123 /* Have we seen a message with clock_snapshot going through this stream? */
124 bool seen_clock_snapshot;
125
7de0e49a
PP
126 /* Owned by this (`NULL` initially and between packets) */
127 const bt_packet *cur_packet;
7de0e49a 128};
cab3f160
JG
129
130static
7de0e49a 131void destroy_trimmer_comp(struct trimmer_comp *trimmer_comp)
cab3f160 132{
7de0e49a
PP
133 BT_ASSERT(trimmer_comp);
134 g_free(trimmer_comp);
cab3f160
JG
135}
136
137static
7de0e49a
PP
138struct trimmer_comp *create_trimmer_comp(void)
139{
140 return g_new0(struct trimmer_comp, 1);
141}
142
143BT_HIDDEN
144void trimmer_finalize(bt_self_component_filter *self_comp)
cab3f160 145{
7de0e49a
PP
146 struct trimmer_comp *trimmer_comp =
147 bt_self_component_get_data(
148 bt_self_component_filter_as_self_component(self_comp));
149
150 if (trimmer_comp) {
151 destroy_trimmer_comp(trimmer_comp);
152 }
cab3f160
JG
153}
154
d4d7ffc9
SM
155/*
156 * Compile regex in `pattern`, and try to match `string`. If there's a match,
157 * return true and set `*match_info` to the list of matches. The list of
158 * matches must be freed by the caller. If there's no match, return false and
159 * set `*match_info` to NULL;
160 */
161static
162bool compile_and_match(const char *pattern, const char *string, GMatchInfo **match_info) {
163 bool matches = false;
164 GError *regex_error = NULL;
165 GRegex *regex;
166
167 regex = g_regex_new(pattern, 0, 0, &regex_error);
168 if (!regex) {
169 goto end;
170 }
171
172 matches = g_regex_match(regex, string, 0, match_info);
173 if (!matches) {
174 /*
175 * g_regex_match allocates `*match_info` even if it returns
176 * FALSE. If there's no match, we have no use for it, so free
177 * it immediatly and don't return it to the caller.
178 */
a42eab88 179 g_match_info_free(*match_info);
d4d7ffc9
SM
180 *match_info = NULL;
181 }
182
183 g_regex_unref(regex);
184
185end:
186
187 if (regex_error) {
188 g_error_free(regex_error);
189 }
190
191 return matches;
192}
193
194/*
195 * Convert the captured text in match number `match_num` in `match_info`
196 * to an unsigned integer.
197 */
198static
199guint64 match_to_uint(const GMatchInfo *match_info, gint match_num) {
200 gchar *text, *endptr;
201 guint64 result;
202
203 text = g_match_info_fetch(match_info, match_num);
204 BT_ASSERT(text);
205
206 /*
207 * Because the input is carefully sanitized with regexes by the caller,
208 * we assume that g_ascii_strtoull cannot fail.
209 */
210 errno = 0;
211 result = g_ascii_strtoull(text, &endptr, 10);
212 BT_ASSERT(endptr > text);
213 BT_ASSERT(errno == 0);
214
215 g_free(text);
216
217 return result;
218}
219
220/*
221 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
222 * This function is like match_to_uint, but multiplies the parsed number to get
223 * the expected result.
224 */
225static
226guint64 match_to_uint_ns(const GMatchInfo *match_info, gint match_num) {
227 guint64 nanoseconds;
228 gboolean ret;
229 gint start_pos, end_pos, power;
230 static int pow10[] = {
231 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
232 };
233
234 nanoseconds = match_to_uint(match_info, match_num);
235
236 /* Multiply by 10 as many times as there are omitted digits. */
237 ret = g_match_info_fetch_pos(match_info, match_num, &start_pos, &end_pos);
238 BT_ASSERT(ret);
239
240 power = 9 - (end_pos - start_pos);
241 BT_ASSERT(power >= 0 && power <= 8);
242
243 nanoseconds *= pow10[power];
244
245 return nanoseconds;
246}
247
7de0e49a
PP
248/*
249 * Sets the time (in ns from origin) of a trimmer bound from date and
250 * time components.
251 *
252 * Returns a negative value if anything goes wrong.
253 */
254static
255int set_bound_ns_from_origin(struct trimmer_bound *bound,
256 unsigned int year, unsigned int month, unsigned int day,
257 unsigned int hour, unsigned int minute, unsigned int second,
258 unsigned int ns, bool is_gmt)
cab3f160 259{
7de0e49a
PP
260 int ret = 0;
261 time_t result;
262 struct tm tm = {
263 .tm_sec = second,
264 .tm_min = minute,
265 .tm_hour = hour,
266 .tm_mday = day,
267 .tm_mon = month - 1,
268 .tm_year = year - 1900,
269 .tm_isdst = -1,
270 };
271
272 if (is_gmt) {
273 result = bt_timegm(&tm);
274 } else {
275 result = mktime(&tm);
276 }
277
278 if (result < 0) {
279 ret = -1;
280 goto end;
281 }
cab3f160 282
7de0e49a
PP
283 BT_ASSERT(bound);
284 bound->ns_from_origin = (int64_t) result;
285 bound->ns_from_origin *= NS_PER_S;
286 bound->ns_from_origin += ns;
287 bound->is_set = true;
288
289end:
290 return ret;
cab3f160
JG
291}
292
528debdf
MD
293/*
294 * Parses a timestamp, figuring out its format.
295 *
296 * Returns a negative value if anything goes wrong.
297 *
298 * Expected formats:
299 *
7de0e49a
PP
300 * YYYY-MM-DD hh:mm[:ss[.ns]]
301 * [hh:mm:]ss[.ns]
302 * [-]s[.ns]
303 *
304 * TODO: Check overflows.
305 */
306static
0d9a3d3e
PP
307int set_bound_from_str(struct trimmer_comp *trimmer_comp,
308 const char *str, struct trimmer_bound *bound, bool is_gmt)
7de0e49a 309{
d4d7ffc9
SM
310/* Matches YYYY-MM-DD */
311#define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
312
313/* Matches HH:MM[:SS[.NS]] */
314#define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
315
316/* Matches [-]SS[.NS] */
317#define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
318
319 GMatchInfo *match_info;
7de0e49a 320 int ret = 0;
7de0e49a 321
d4d7ffc9
SM
322 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
323 if (compile_and_match("^" DATE_RE " " TIME_RE "$", str, &match_info)) {
324 unsigned int year = 0, month = 0, day = 0, hours = 0, minutes = 0, seconds = 0, nanoseconds = 0;
325 gint match_count = g_match_info_get_match_count(match_info);
7de0e49a 326
d4d7ffc9 327 BT_ASSERT(match_count >= 6 && match_count <= 8);
7de0e49a 328
d4d7ffc9
SM
329 year = match_to_uint(match_info, 1);
330 month = match_to_uint(match_info, 2);
331 day = match_to_uint(match_info, 3);
332 hours = match_to_uint(match_info, 4);
333 minutes = match_to_uint(match_info, 5);
7de0e49a 334
d4d7ffc9
SM
335 if (match_count >= 7) {
336 seconds = match_to_uint(match_info, 6);
337 }
7de0e49a 338
d4d7ffc9
SM
339 if (match_count >= 8) {
340 nanoseconds = match_to_uint_ns(match_info, 7);
341 }
342
343 set_bound_ns_from_origin(bound, year, month, day, hours, minutes, seconds, nanoseconds, is_gmt);
7de0e49a 344
7de0e49a
PP
345 goto end;
346 }
347
d4d7ffc9
SM
348 if (compile_and_match("^" DATE_RE "$", str, &match_info)) {
349 unsigned int year = 0, month = 0, day = 0;
350
351 BT_ASSERT(g_match_info_get_match_count(match_info) == 4);
352
353 year = match_to_uint(match_info, 1);
354 month = match_to_uint(match_info, 2);
355 day = match_to_uint(match_info, 3);
356
357 set_bound_ns_from_origin(bound, year, month, day, 0, 0, 0, 0, is_gmt);
358
7de0e49a
PP
359 goto end;
360 }
361
d4d7ffc9
SM
362 /* Try `hh:mm[:ss[.ns]]` format */
363 if (compile_and_match("^" TIME_RE "$", str, &match_info)) {
364 gint match_count = g_match_info_get_match_count(match_info);
365 BT_ASSERT(match_count >= 3 && match_count <= 5);
366 bound->time.hour = match_to_uint(match_info, 1);
367 bound->time.minute = match_to_uint(match_info, 2);
368
369 if (match_count >= 4) {
370 bound->time.second = match_to_uint(match_info, 3);
371 }
372
373 if (match_count >= 5) {
374 bound->time.ns = match_to_uint_ns(match_info, 4);
375 }
376
7de0e49a
PP
377 goto end;
378 }
379
d4d7ffc9
SM
380 /* Try `[-]s[.ns]` format */
381 if (compile_and_match("^" S_NS_RE "$", str, &match_info)) {
382 gboolean is_neg, fetch_pos_ret;
383 gint start_pos, end_pos, match_count;
384 guint64 seconds, nanoseconds = 0;
385
386 match_count = g_match_info_get_match_count(match_info);
387 BT_ASSERT(match_count >= 3 && match_count <= 4);
388
389 /* Check for presence of negation sign. */
390 fetch_pos_ret = g_match_info_fetch_pos(match_info, 1, &start_pos, &end_pos);
391 BT_ASSERT(fetch_pos_ret);
392 is_neg = (end_pos - start_pos) > 0;
393
394 seconds = match_to_uint(match_info, 2);
395
396 if (match_count >= 4) {
397 nanoseconds = match_to_uint_ns(match_info, 3);
398 }
399
400 bound->ns_from_origin = seconds * NS_PER_S + nanoseconds;
401
402 if (is_neg) {
403 bound->ns_from_origin = -bound->ns_from_origin;
404 }
405
7de0e49a 406 bound->is_set = true;
d4d7ffc9 407
7de0e49a
PP
408 goto end;
409 }
410
ab90ee94
SM
411 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
412 "Invalid date/time format: param=\"%s\"", str);
7de0e49a
PP
413 ret = -1;
414
415end:
f1083338
SM
416 g_match_info_free(match_info);
417
7de0e49a
PP
418 return ret;
419}
420
421/*
422 * Sets a trimmer bound's properties from a parameter string/integer
423 * value.
424 *
425 * Returns a negative value if anything goes wrong.
528debdf 426 */
44d3cbf0 427static
0d9a3d3e
PP
428int set_bound_from_param(struct trimmer_comp *trimmer_comp,
429 const char *param_name, const bt_value *param,
7de0e49a 430 struct trimmer_bound *bound, bool is_gmt)
528debdf
MD
431{
432 int ret;
268fae9a 433 const char *arg;
7de0e49a 434 char tmp_arg[64];
268fae9a 435
fdd3a2da 436 if (bt_value_is_signed_integer(param)) {
9c08c816 437 int64_t value = bt_value_integer_signed_get(param);
7de0e49a
PP
438
439 /*
440 * Just convert it to a temporary string to handle
441 * everything the same way.
442 */
495dd92c 443 snprintf(tmp_arg, sizeof(tmp_arg), "%" PRId64, value);
7de0e49a 444 arg = tmp_arg;
7de0e49a 445 } else {
90a55a4e
SM
446 BT_ASSERT(bt_value_is_string(param));
447 arg = bt_value_string_get(param);
268fae9a
PP
448 }
449
0d9a3d3e 450 ret = set_bound_from_str(trimmer_comp, arg, bound, is_gmt);
7de0e49a 451
7de0e49a
PP
452 return ret;
453}
454
455static
0d9a3d3e
PP
456int validate_trimmer_bounds(struct trimmer_comp *trimmer_comp,
457 struct trimmer_bound *begin, struct trimmer_bound *end)
7de0e49a
PP
458{
459 int ret = 0;
460
461 BT_ASSERT(begin->is_set);
462 BT_ASSERT(end->is_set);
463
464 if (!begin->is_infinite && !end->is_infinite &&
465 begin->ns_from_origin > end->ns_from_origin) {
ab90ee94
SM
466 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
467 "Trimming time range's beginning time is greater than end time: "
7de0e49a
PP
468 "begin-ns-from-origin=%" PRId64 ", "
469 "end-ns-from-origin=%" PRId64,
470 begin->ns_from_origin,
471 end->ns_from_origin);
472 ret = -1;
473 goto end;
528debdf
MD
474 }
475
7de0e49a 476 if (!begin->is_infinite && begin->ns_from_origin == INT64_MIN) {
ab90ee94
SM
477 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
478 "Invalid trimming time range's beginning time: "
7de0e49a
PP
479 "ns-from-origin=%" PRId64,
480 begin->ns_from_origin);
481 ret = -1;
482 goto end;
483 }
528debdf 484
7de0e49a 485 if (!end->is_infinite && end->ns_from_origin == INT64_MIN) {
ab90ee94
SM
486 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
487 "Invalid trimming time range's end time: "
7de0e49a
PP
488 "ns-from-origin=%" PRId64,
489 end->ns_from_origin);
490 ret = -1;
491 goto end;
492 }
528debdf 493
7de0e49a
PP
494end:
495 return ret;
528debdf
MD
496}
497
498static
90a55a4e
SM
499enum bt_param_validation_status validate_bound_type(
500 const bt_value *value,
501 struct bt_param_validation_context *context)
502{
503 enum bt_param_validation_status status = BT_PARAM_VALIDATION_STATUS_OK;
504
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)));
512 }
513
514 return status;
515}
516
d9120ccb 517static
90a55a4e
SM
518struct 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
523};
524
525static
526bt_component_class_initialize_method_status init_trimmer_comp_from_params(
527 struct trimmer_comp *trimmer_comp,
7de0e49a 528 const bt_value *params)
44d3cbf0 529{
7de0e49a 530 const bt_value *value;
90a55a4e
SM
531 bt_component_class_initialize_method_status status;
532 enum bt_param_validation_status validation_status;
533 gchar *validate_error = NULL;
534
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;
539 goto end;
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",
543 validate_error);
544 goto end;
545 }
44d3cbf0 546
f6ccaed9 547 BT_ASSERT(params);
7de0e49a 548 value = bt_value_map_borrow_entry_value_const(params, "gmt");
528debdf 549 if (value) {
7de0e49a 550 trimmer_comp->is_gmt = (bool) bt_value_bool_get(value);
528debdf
MD
551 }
552
7de0e49a 553 value = bt_value_map_borrow_entry_value_const(params, "begin");
528debdf 554 if (value) {
0d9a3d3e 555 if (set_bound_from_param(trimmer_comp, "begin", value,
7de0e49a
PP
556 &trimmer_comp->begin, trimmer_comp->is_gmt)) {
557 /* set_bound_from_param() logs errors */
90a55a4e 558 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
268fae9a 559 goto end;
44d3cbf0 560 }
7de0e49a
PP
561 } else {
562 trimmer_comp->begin.is_infinite = true;
563 trimmer_comp->begin.is_set = true;
44d3cbf0 564 }
528debdf 565
7de0e49a 566 value = bt_value_map_borrow_entry_value_const(params, "end");
528debdf 567 if (value) {
0d9a3d3e 568 if (set_bound_from_param(trimmer_comp, "end", value,
7de0e49a
PP
569 &trimmer_comp->end, trimmer_comp->is_gmt)) {
570 /* set_bound_from_param() logs errors */
90a55a4e 571 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
268fae9a 572 goto end;
528debdf 573 }
7de0e49a
PP
574 } else {
575 trimmer_comp->end.is_infinite = true;
576 trimmer_comp->end.is_set = true;
528debdf 577 }
268fae9a 578
7de0e49a
PP
579 if (trimmer_comp->begin.is_set && trimmer_comp->end.is_set) {
580 /* validate_trimmer_bounds() logs errors */
90a55a4e
SM
581 if (validate_trimmer_bounds(trimmer_comp,
582 &trimmer_comp->begin, &trimmer_comp->end)) {
583 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
584 goto end;
585 }
55595636 586 }
7de0e49a 587
90a55a4e
SM
588 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
589
590end:
591 g_free(validate_error);
592
593 return status;
44d3cbf0
JG
594}
595
21a9f056 596bt_component_class_initialize_method_status trimmer_init(
d24d5663 597 bt_self_component_filter *self_comp_flt,
59225a3e 598 bt_self_component_filter_configuration *config,
7de0e49a 599 const bt_value *params, void *init_data)
cab3f160 600{
90a55a4e 601 bt_component_class_initialize_method_status status;
d24d5663 602 bt_self_component_add_port_status add_port_status;
7de0e49a 603 struct trimmer_comp *trimmer_comp = create_trimmer_comp();
ec4ae660
PP
604 bt_self_component *self_comp =
605 bt_self_component_filter_as_self_component(self_comp_flt);
90a55a4e 606
7de0e49a 607 if (!trimmer_comp) {
21a9f056 608 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
7de0e49a 609 goto error;
cab3f160
JG
610 }
611
0d9a3d3e 612 trimmer_comp->log_level = bt_component_get_logging_level(
ec4ae660
PP
613 bt_self_component_as_component(self_comp));
614 trimmer_comp->self_comp = self_comp;
a3f0c7db 615 trimmer_comp->self_comp_filter = self_comp_flt;
90a55a4e 616
d24d5663 617 add_port_status = bt_self_component_filter_add_input_port(
ec4ae660 618 self_comp_flt, in_port_name, NULL, NULL);
90a55a4e
SM
619 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
620 status = (int) add_port_status;
d24d5663 621 goto error;
b9d103be
PP
622 }
623
d24d5663 624 add_port_status = bt_self_component_filter_add_output_port(
ec4ae660 625 self_comp_flt, "out", NULL, NULL);
90a55a4e
SM
626 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
627 status = (int) add_port_status;
d24d5663 628 goto error;
b9d103be
PP
629 }
630
90a55a4e
SM
631 status = init_trimmer_comp_from_params(trimmer_comp, params);
632 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
cab3f160
JG
633 goto error;
634 }
635
ec4ae660 636 bt_self_component_set_data(self_comp, trimmer_comp);
90a55a4e
SM
637
638 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
7de0e49a
PP
639 goto end;
640
641error:
7de0e49a
PP
642 if (trimmer_comp) {
643 destroy_trimmer_comp(trimmer_comp);
644 }
645
cab3f160 646end:
8dad9b32 647 return status;
7de0e49a
PP
648}
649
650static
651void destroy_trimmer_iterator(struct trimmer_iterator *trimmer_it)
652{
e803df70
SM
653 if (!trimmer_it) {
654 goto end;
655 }
656
9a2c8b8e 657 bt_message_iterator_put_ref(
7de0e49a
PP
658 trimmer_it->upstream_iter);
659
660 if (trimmer_it->output_messages) {
661 g_queue_free(trimmer_it->output_messages);
662 }
663
664 if (trimmer_it->stream_states) {
665 g_hash_table_destroy(trimmer_it->stream_states);
666 }
667
668 g_free(trimmer_it);
e803df70
SM
669end:
670 return;
7de0e49a
PP
671}
672
673static
674void destroy_trimmer_iterator_stream_state(
675 struct trimmer_iterator_stream_state *sstate)
676{
677 BT_ASSERT(sstate);
678 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
7de0e49a
PP
679 g_free(sstate);
680}
681
682BT_HIDDEN
a3f0c7db 683bt_message_iterator_class_initialize_method_status trimmer_msg_iter_init(
7de0e49a 684 bt_self_message_iterator *self_msg_iter,
8d8b141d 685 bt_self_message_iterator_configuration *config,
7de0e49a
PP
686 bt_self_component_port_output *port)
687{
a3f0c7db 688 bt_message_iterator_class_initialize_method_status status;
9a2c8b8e 689 bt_message_iterator_create_from_message_iterator_status
e803df70 690 msg_iter_status;
7de0e49a 691 struct trimmer_iterator *trimmer_it;
f615b250
PP
692 bt_self_component *self_comp =
693 bt_self_message_iterator_borrow_component(self_msg_iter);
7de0e49a
PP
694
695 trimmer_it = g_new0(struct trimmer_iterator, 1);
696 if (!trimmer_it) {
a3f0c7db 697 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 698 goto error;
7de0e49a
PP
699 }
700
a3f0c7db 701 trimmer_it->trimmer_comp = bt_self_component_get_data(self_comp);
7de0e49a
PP
702 BT_ASSERT(trimmer_it->trimmer_comp);
703
704 if (trimmer_it->trimmer_comp->begin.is_set &&
705 trimmer_it->trimmer_comp->end.is_set) {
706 /*
707 * Both trimming time range's bounds are set, so skip
708 * the
709 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
710 * phase.
711 */
712 trimmer_it->state = TRIMMER_ITERATOR_STATE_SEEK_INITIALLY;
713 }
714
715 trimmer_it->begin = trimmer_it->trimmer_comp->begin;
716 trimmer_it->end = trimmer_it->trimmer_comp->end;
e803df70 717 msg_iter_status =
9a2c8b8e 718 bt_message_iterator_create_from_message_iterator(
ca02df0a 719 self_msg_iter,
7de0e49a 720 bt_self_component_filter_borrow_input_port_by_name(
a3f0c7db
SM
721 trimmer_it->trimmer_comp->self_comp_filter, in_port_name),
722 &trimmer_it->upstream_iter);
9a2c8b8e 723 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
e803df70
SM
724 status = (int) msg_iter_status;
725 goto error;
7de0e49a
PP
726 }
727
728 trimmer_it->output_messages = g_queue_new();
729 if (!trimmer_it->output_messages) {
a3f0c7db 730 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 731 goto error;
7de0e49a
PP
732 }
733
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) {
a3f0c7db 738 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 739 goto error;
7de0e49a
PP
740 }
741
c0e46a7c
SM
742 /*
743 * The trimmer requires upstream messages to have times, so it can
744 * always seek forward.
745 */
746 bt_self_message_iterator_configuration_set_can_seek_forward(
747 config, BT_TRUE);
748
7de0e49a
PP
749 trimmer_it->self_msg_iter = self_msg_iter;
750 bt_self_message_iterator_set_data(self_msg_iter, trimmer_it);
751
a3f0c7db 752 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
e803df70
SM
753 goto end;
754
755error:
756 destroy_trimmer_iterator(trimmer_it);
7de0e49a 757
e803df70 758end:
7de0e49a
PP
759 return status;
760}
761
762static inline
763int get_msg_ns_from_origin(const bt_message *msg, int64_t *ns_from_origin,
188edac1 764 bool *has_clock_snapshot)
7de0e49a
PP
765{
766 const bt_clock_class *clock_class = NULL;
767 const bt_clock_snapshot *clock_snapshot = NULL;
7de0e49a
PP
768 int ret = 0;
769
98b15851
PP
770 BT_ASSERT_DBG(msg);
771 BT_ASSERT_DBG(ns_from_origin);
772 BT_ASSERT_DBG(has_clock_snapshot);
7de0e49a
PP
773
774 switch (bt_message_get_type(msg)) {
775 case BT_MESSAGE_TYPE_EVENT:
776 clock_class =
777 bt_message_event_borrow_stream_class_default_clock_class_const(
778 msg);
91d81473 779 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
780 goto error;
781 }
782
0cbc2c33
PP
783 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
784 msg);
7de0e49a
PP
785 break;
786 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
8a30271a
SM
787 {
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);
791
792 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(stream_class)) {
793 goto no_clock_snapshot;
7de0e49a
PP
794 }
795
0cbc2c33
PP
796 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
797 msg);
7de0e49a 798 break;
8a30271a 799 }
7de0e49a 800 case BT_MESSAGE_TYPE_PACKET_END:
8a30271a
SM
801 {
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);
805
806 if (!bt_stream_class_packets_have_end_default_clock_snapshot(stream_class)) {
807 goto no_clock_snapshot;
7de0e49a
PP
808 }
809
0cbc2c33
PP
810 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
811 msg);
7de0e49a 812 break;
8a30271a 813 }
188edac1
SM
814 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
815 {
816 enum bt_message_stream_clock_snapshot_state cs_state;
817
7de0e49a 818 clock_class =
188edac1 819 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg);
91d81473 820 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
821 goto error;
822 }
823
188edac1
SM
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;
7de0e49a
PP
827 }
828
7de0e49a 829 break;
188edac1
SM
830 }
831 case BT_MESSAGE_TYPE_STREAM_END:
832 {
833 enum bt_message_stream_clock_snapshot_state cs_state;
834
7de0e49a 835 clock_class =
188edac1 836 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg);
91d81473 837 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
838 goto error;
839 }
840
188edac1
SM
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) {
7de0e49a
PP
843 goto no_clock_snapshot;
844 }
845
846 break;
188edac1
SM
847 }
848 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
8a30271a
SM
849 {
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);
852
853 if (!bt_stream_class_discarded_events_have_default_clock_snapshots(stream_class)) {
854 goto no_clock_snapshot;
7de0e49a
PP
855 }
856
188edac1
SM
857 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
858 msg);
859 break;
8a30271a 860 }
188edac1 861 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
8a30271a
SM
862 {
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);
865
866 if (!bt_stream_class_discarded_packets_have_default_clock_snapshots(stream_class)) {
867 goto no_clock_snapshot;
7de0e49a
PP
868 }
869
188edac1
SM
870 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
871 msg);
7de0e49a 872 break;
8a30271a 873 }
7de0e49a 874 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
0cbc2c33 875 clock_snapshot =
60d02328 876 bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(
0cbc2c33 877 msg);
7de0e49a
PP
878 break;
879 default:
880 goto no_clock_snapshot;
881 }
882
7de0e49a
PP
883 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot,
884 ns_from_origin);
91d81473 885 if (G_UNLIKELY(ret)) {
7de0e49a
PP
886 goto error;
887 }
888
188edac1 889 *has_clock_snapshot = true;
7de0e49a
PP
890 goto end;
891
892no_clock_snapshot:
188edac1 893 *has_clock_snapshot = false;
7de0e49a
PP
894 goto end;
895
cab3f160 896error:
7de0e49a
PP
897 ret = -1;
898
899end:
900 return ret;
901}
902
903static inline
904void put_messages(bt_message_array_const msgs, uint64_t count)
905{
906 uint64_t i;
907
908 for (i = 0; i < count; i++) {
909 BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]);
910 }
911}
912
913static inline
0d9a3d3e
PP
914int set_trimmer_iterator_bound(struct trimmer_iterator *trimmer_it,
915 struct trimmer_bound *bound, int64_t ns_from_origin,
916 bool is_gmt)
7de0e49a 917{
0d9a3d3e 918 struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp;
7de0e49a 919 struct tm tm;
53a42a43 920 struct tm *res;
7de0e49a
PP
921 time_t time_seconds = (time_t) (ns_from_origin / NS_PER_S);
922 int ret = 0;
923
924 BT_ASSERT(!bound->is_set);
925 errno = 0;
926
927 /* We only need to extract the date from this time */
928 if (is_gmt) {
53a42a43 929 res = bt_gmtime_r(&time_seconds, &tm);
7de0e49a 930 } else {
53a42a43 931 res = bt_localtime_r(&time_seconds, &tm);
7de0e49a
PP
932 }
933
53a42a43 934 if (!res) {
ab90ee94
SM
935 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(trimmer_comp->self_comp,
936 "Cannot convert timestamp to date and time",
f42489db 937 ": ts=%" PRId64, (int64_t) time_seconds);
7de0e49a
PP
938 ret = -1;
939 goto end;
940 }
941
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);
945
946end:
cab3f160
JG
947 return ret;
948}
7de0e49a
PP
949
950static
a3f0c7db 951bt_message_iterator_class_next_method_status
d24d5663 952state_set_trimmer_iterator_bounds(
7de0e49a
PP
953 struct trimmer_iterator *trimmer_it)
954{
d24d5663 955 bt_message_iterator_next_status upstream_iter_status =
9275bef4 956 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK;
7de0e49a
PP
957 struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp;
958 bt_message_array_const msgs;
959 uint64_t count = 0;
960 int64_t ns_from_origin = INT64_MIN;
961 uint64_t i;
962 int ret;
963
964 BT_ASSERT(!trimmer_it->begin.is_set ||
965 !trimmer_it->end.is_set);
966
967 while (true) {
968 upstream_iter_status =
9a2c8b8e 969 bt_message_iterator_next(
7de0e49a 970 trimmer_it->upstream_iter, &msgs, &count);
d24d5663 971 if (upstream_iter_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
7de0e49a
PP
972 goto end;
973 }
974
975 for (i = 0; i < count; i++) {
976 const bt_message *msg = msgs[i];
188edac1 977 bool has_ns_from_origin;
7de0e49a 978 ret = get_msg_ns_from_origin(msg, &ns_from_origin,
188edac1 979 &has_ns_from_origin);
7de0e49a
PP
980 if (ret) {
981 goto error;
982 }
983
188edac1 984 if (!has_ns_from_origin) {
7de0e49a
PP
985 continue;
986 }
987
98b15851 988 BT_ASSERT_DBG(ns_from_origin != INT64_MIN &&
7de0e49a
PP
989 ns_from_origin != INT64_MAX);
990 put_messages(msgs, count);
991 goto found;
992 }
993
994 put_messages(msgs, count);
995 }
996
997found:
998 if (!trimmer_it->begin.is_set) {
999 BT_ASSERT(!trimmer_it->begin.is_infinite);
0d9a3d3e 1000 ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->begin,
7de0e49a
PP
1001 ns_from_origin, trimmer_comp->is_gmt);
1002 if (ret) {
1003 goto error;
1004 }
1005 }
1006
1007 if (!trimmer_it->end.is_set) {
1008 BT_ASSERT(!trimmer_it->end.is_infinite);
0d9a3d3e 1009 ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->end,
7de0e49a
PP
1010 ns_from_origin, trimmer_comp->is_gmt);
1011 if (ret) {
1012 goto error;
1013 }
1014 }
1015
0d9a3d3e
PP
1016 ret = validate_trimmer_bounds(trimmer_it->trimmer_comp,
1017 &trimmer_it->begin, &trimmer_it->end);
7de0e49a
PP
1018 if (ret) {
1019 goto error;
1020 }
1021
1022 goto end;
1023
1024error:
1025 put_messages(msgs, count);
9275bef4 1026 upstream_iter_status = BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR;
7de0e49a
PP
1027
1028end:
1029 return (int) upstream_iter_status;
1030}
1031
1032static
a3f0c7db 1033bt_message_iterator_class_next_method_status state_seek_initially(
7de0e49a
PP
1034 struct trimmer_iterator *trimmer_it)
1035{
0d9a3d3e 1036 struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp;
a3f0c7db 1037 bt_message_iterator_class_next_method_status status;
7de0e49a
PP
1038
1039 BT_ASSERT(trimmer_it->begin.is_set);
1040
1041 if (trimmer_it->begin.is_infinite) {
f2fb1b32
SM
1042 bt_bool can_seek;
1043
9a2c8b8e 1044 status = (int) bt_message_iterator_can_seek_beginning(
f2fb1b32 1045 trimmer_it->upstream_iter, &can_seek);
a3f0c7db 1046 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
f2fb1b32
SM
1047 if (status < 0) {
1048 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1049 "Cannot make upstream message iterator initially seek its beginning.");
1050 }
1051
1052 goto end;
1053 }
1054
1055 if (!can_seek) {
ab90ee94
SM
1056 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1057 "Cannot make upstream message iterator initially seek its beginning.");
a3f0c7db 1058 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1059 goto end;
1060 }
1061
9a2c8b8e 1062 status = (int) bt_message_iterator_seek_beginning(
7de0e49a
PP
1063 trimmer_it->upstream_iter);
1064 } else {
f2fb1b32
SM
1065 bt_bool can_seek;
1066
9a2c8b8e 1067 status = (int) bt_message_iterator_can_seek_ns_from_origin(
f2fb1b32
SM
1068 trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin,
1069 &can_seek);
1070
a3f0c7db 1071 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
f2fb1b32
SM
1072 if (status < 0) {
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);
1076 }
1077
1078 goto end;
1079 }
1080
1081 if (!can_seek) {
ab90ee94
SM
1082 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1083 "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64,
7de0e49a 1084 trimmer_it->begin.ns_from_origin);
a3f0c7db 1085 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1086 goto end;
1087 }
1088
9a2c8b8e 1089 status = (int) bt_message_iterator_seek_ns_from_origin(
7de0e49a
PP
1090 trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin);
1091 }
1092
a3f0c7db 1093 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1094 trimmer_it->state = TRIMMER_ITERATOR_STATE_TRIM;
1095 }
1096
1097end:
1098 return status;
1099}
1100
1101static inline
1102void push_message(struct trimmer_iterator *trimmer_it, const bt_message *msg)
1103{
1104 g_queue_push_head(trimmer_it->output_messages, (void *) msg);
1105}
1106
1107static inline
1108const bt_message *pop_message(struct trimmer_iterator *trimmer_it)
1109{
1110 return g_queue_pop_tail(trimmer_it->output_messages);
1111}
1112
1113static inline
1114int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1115 int64_t ns_from_origin, uint64_t *raw_value)
1116{
1117
1118 int64_t cc_offset_s;
1119 uint64_t cc_offset_cycles;
1120 uint64_t cc_freq;
1121
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);
1126}
1127
1128static inline
a3f0c7db 1129bt_message_iterator_class_next_method_status
d24d5663 1130end_stream(struct trimmer_iterator *trimmer_it,
7de0e49a
PP
1131 struct trimmer_iterator_stream_state *sstate)
1132{
a3f0c7db
SM
1133 bt_message_iterator_class_next_method_status status =
1134 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
188edac1
SM
1135 /* Initialize to silence maybe-uninitialized warning. */
1136 uint64_t raw_value = 0;
7de0e49a
PP
1137 bt_message *msg = NULL;
1138
1139 BT_ASSERT(!trimmer_it->end.is_infinite);
188edac1 1140 BT_ASSERT(sstate->stream);
7de0e49a 1141
188edac1
SM
1142 /*
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.
1145 *
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).
1151 */
1152 if (sstate->seen_clock_snapshot) {
1153 const bt_clock_class *clock_class;
1154 int ret;
7de0e49a 1155
7de0e49a
PP
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);
1161 if (ret) {
a3f0c7db 1162 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1163 goto end;
1164 }
188edac1
SM
1165 }
1166
1167 if (sstate->cur_packet) {
1168 /*
1169 * Create and push a packet end message, making its time
1170 * the trimming range's end time.
1171 *
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).
1176 */
1177 BT_ASSERT(sstate->seen_clock_snapshot);
7de0e49a
PP
1178
1179 msg = bt_message_packet_end_create_with_default_clock_snapshot(
1180 trimmer_it->self_msg_iter, sstate->cur_packet,
1181 raw_value);
1182 if (!msg) {
a3f0c7db 1183 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
7de0e49a
PP
1184 goto end;
1185 }
1186
1187 push_message(trimmer_it, msg);
1188 msg = NULL;
1189 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
7de0e49a
PP
1190 }
1191
188edac1 1192 /* Create and push a stream end message. */
7de0e49a
PP
1193 msg = bt_message_stream_end_create(trimmer_it->self_msg_iter,
1194 sstate->stream);
1195 if (!msg) {
a3f0c7db 1196 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
7de0e49a
PP
1197 goto end;
1198 }
1199
188edac1
SM
1200 if (sstate->seen_clock_snapshot) {
1201 bt_message_stream_end_set_default_clock_snapshot(msg, raw_value);
1202 }
1203
7de0e49a
PP
1204 push_message(trimmer_it, msg);
1205 msg = NULL;
1206
1207 /*
1208 * Just to make sure that we don't use this stream state again
1209 * in the future without an obvious error.
1210 */
1211 sstate->stream = NULL;
1212
1213end:
1214 bt_message_put_ref(msg);
1215 return status;
1216}
1217
1218static inline
a3f0c7db 1219bt_message_iterator_class_next_method_status end_iterator_streams(
7de0e49a
PP
1220 struct trimmer_iterator *trimmer_it)
1221{
a3f0c7db
SM
1222 bt_message_iterator_class_next_method_status status =
1223 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1224 GHashTableIter iter;
1225 gpointer key, sstate;
1226
1227 if (trimmer_it->end.is_infinite) {
1228 /*
1229 * An infinite trimming range's end time guarantees that
1230 * we received (and pushed) all the appropriate end
1231 * messages.
1232 */
1233 goto remove_all;
1234 }
1235
1236 /*
1237 * End each stream and then remove them from the hash table of
1238 * stream states to release unneeded references.
1239 */
1240 g_hash_table_iter_init(&iter, trimmer_it->stream_states);
1241
1242 while (g_hash_table_iter_next(&iter, &key, &sstate)) {
1243 status = end_stream(trimmer_it, sstate);
1244 if (status) {
1245 goto end;
1246 }
1247 }
1248
1249remove_all:
1250 g_hash_table_remove_all(trimmer_it->stream_states);
1251
1252end:
1253 return status;
1254}
1255
188edac1 1256static
a3f0c7db 1257bt_message_iterator_class_next_method_status
188edac1
SM
1258create_stream_state_entry(
1259 struct trimmer_iterator *trimmer_it,
1260 const struct bt_stream *stream,
1261 struct trimmer_iterator_stream_state **stream_state)
1262{
1263 struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp;
a3f0c7db 1264 bt_message_iterator_class_next_method_status status;
188edac1
SM
1265 struct trimmer_iterator_stream_state *sstate;
1266 const bt_stream_class *sc;
1267
1268 BT_ASSERT(!bt_g_hash_table_contains(trimmer_it->stream_states, stream));
1269
1270 /*
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
1275 * messages.
1276 *
1277 * Also check that clock snapshots are always
1278 * known.
1279 */
1280 sc = bt_stream_borrow_class_const(stream);
1281 if (!bt_stream_class_borrow_default_clock_class_const(sc)) {
ab90ee94
SM
1282 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1283 "Unsupported stream: stream class does "
188edac1
SM
1284 "not have a default clock class: "
1285 "stream-addr=%p, "
1286 "stream-id=%" PRIu64 ", "
1287 "stream-name=\"%s\"",
1288 stream, bt_stream_get_id(stream),
1289 bt_stream_get_name(stream));
a3f0c7db 1290 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
188edac1
SM
1291 goto end;
1292 }
1293
1294 /*
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
1299 * implemented.
1300 */
21f6a44a
SM
1301 if (bt_stream_class_supports_packets(sc)) {
1302 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1303 sc)) {
1304 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1305 "Unsupported stream: packets have no beginning clock snapshot: "
1306 "stream-addr=%p, "
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;
1312 goto end;
1313 }
188edac1 1314
21f6a44a
SM
1315 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1316 sc)) {
1317 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1318 "Unsupported stream: packets have no end clock snapshot: "
1319 "stream-addr=%p, "
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;
1325 goto end;
1326 }
1327
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: "
1333 "stream-addr=%p, "
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;
1339 goto end;
1340 }
188edac1
SM
1341 }
1342
1343 if (bt_stream_class_supports_discarded_events(sc) &&
1344 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) {
ab90ee94
SM
1345 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1346 "Unsupported stream: discarded events have no clock snapshots: "
188edac1
SM
1347 "stream-addr=%p, "
1348 "stream-id=%" PRIu64 ", "
1349 "stream-name=\"%s\"",
1350 stream, bt_stream_get_id(stream),
1351 bt_stream_get_name(stream));
a3f0c7db 1352 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
188edac1
SM
1353 goto end;
1354 }
1355
188edac1
SM
1356 sstate = g_new0(struct trimmer_iterator_stream_state, 1);
1357 if (!sstate) {
a3f0c7db 1358 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
188edac1
SM
1359 goto end;
1360 }
1361
1362 sstate->stream = stream;
1363
1364 g_hash_table_insert(trimmer_it->stream_states, (void *) stream, sstate);
1365
1366 *stream_state = sstate;
1367
a3f0c7db 1368 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
188edac1
SM
1369
1370end:
1371 return status;
1372}
1373
1374static
1375struct trimmer_iterator_stream_state *get_stream_state_entry(
1376 struct trimmer_iterator *trimmer_it,
1377 const struct bt_stream *stream)
1378{
1379 struct trimmer_iterator_stream_state *sstate;
1380
98b15851 1381 BT_ASSERT_DBG(stream);
188edac1 1382 sstate = g_hash_table_lookup(trimmer_it->stream_states, stream);
98b15851 1383 BT_ASSERT_DBG(sstate);
188edac1
SM
1384
1385 return sstate;
1386}
1387
7de0e49a
PP
1388/*
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.
1393 *
1394 * This function consumes the `msg` reference, _whatever the outcome_.
1395 *
188edac1
SM
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.
7de0e49a
PP
1398 *
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
1402 * `reached_end`.
1403 */
188edac1 1404static
a3f0c7db 1405bt_message_iterator_class_next_method_status
188edac1 1406handle_message_with_stream(
7de0e49a 1407 struct trimmer_iterator *trimmer_it, const bt_message *msg,
188edac1
SM
1408 const struct bt_stream *stream, const int64_t *ns_from_origin,
1409 bool *reached_end)
7de0e49a 1410{
a3f0c7db
SM
1411 bt_message_iterator_class_next_method_status status =
1412 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1413 bt_message_type msg_type = bt_message_get_type(msg);
1414 int ret;
188edac1
SM
1415 struct trimmer_iterator_stream_state *sstate = NULL;
1416
1417 /*
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.
1420 */
1421 if (msg_type != BT_MESSAGE_TYPE_STREAM_BEGINNING) {
1422 sstate = get_stream_state_entry(trimmer_it, stream);
1423 }
7de0e49a
PP
1424
1425 switch (msg_type) {
1426 case BT_MESSAGE_TYPE_EVENT:
188edac1
SM
1427 /*
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.
1431 */
98b15851 1432 BT_ASSERT_DBG(ns_from_origin);
188edac1 1433
91d81473 1434 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1435 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1436 status = end_iterator_streams(trimmer_it);
1437 *reached_end = true;
1438 break;
1439 }
1440
188edac1
SM
1441 sstate->seen_clock_snapshot = true;
1442
7de0e49a
PP
1443 push_message(trimmer_it, msg);
1444 msg = NULL;
1445 break;
188edac1 1446
7de0e49a 1447 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
188edac1
SM
1448 /*
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.
1452 */
1453 BT_ASSERT(ns_from_origin);
1454 BT_ASSERT(!sstate->cur_packet);
1455
91d81473 1456 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1457 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1458 status = end_iterator_streams(trimmer_it);
1459 *reached_end = true;
1460 break;
1461 }
1462
7de0e49a
PP
1463 sstate->cur_packet =
1464 bt_message_packet_beginning_borrow_packet_const(msg);
1465 bt_packet_get_ref(sstate->cur_packet);
188edac1
SM
1466
1467 sstate->seen_clock_snapshot = true;
1468
7de0e49a
PP
1469 push_message(trimmer_it, msg);
1470 msg = NULL;
1471 break;
188edac1 1472
7de0e49a 1473 case BT_MESSAGE_TYPE_PACKET_END:
188edac1
SM
1474 /*
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.
1478 */
1479 BT_ASSERT(ns_from_origin);
1480 BT_ASSERT(sstate->cur_packet);
7de0e49a 1481
91d81473 1482 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1483 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1484 status = end_iterator_streams(trimmer_it);
1485 *reached_end = true;
1486 break;
1487 }
1488
7de0e49a 1489 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
188edac1
SM
1490
1491 sstate->seen_clock_snapshot = true;
1492
7de0e49a
PP
1493 push_message(trimmer_it, msg);
1494 msg = NULL;
1495 break;
188edac1 1496
7de0e49a
PP
1497 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1498 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1499 {
1500 /*
1501 * `ns_from_origin` is the message's time range's
1502 * beginning time here.
1503 */
1504 int64_t end_ns_from_origin;
1505 const bt_clock_snapshot *end_cs;
1506
188edac1
SM
1507 BT_ASSERT(ns_from_origin);
1508
1509 sstate->seen_clock_snapshot = true;
1510
7de0e49a
PP
1511 if (bt_message_get_type(msg) ==
1512 BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
1513 /*
1514 * Safe to ignore the return value because we
1515 * know there's a default clock and it's always
1516 * known.
1517 */
9b24b6aa 1518 end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 1519 msg);
7de0e49a
PP
1520 } else {
1521 /*
1522 * Safe to ignore the return value because we
1523 * know there's a default clock and it's always
1524 * known.
1525 */
9b24b6aa 1526 end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 1527 msg);
7de0e49a
PP
1528 }
1529
1530 if (bt_clock_snapshot_get_ns_from_origin(end_cs,
1531 &end_ns_from_origin)) {
a3f0c7db 1532 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1533 goto end;
1534 }
1535
7de0e49a 1536 if (!trimmer_it->end.is_infinite &&
188edac1 1537 *ns_from_origin > trimmer_it->end.ns_from_origin) {
7de0e49a
PP
1538 status = end_iterator_streams(trimmer_it);
1539 *reached_end = true;
1540 break;
1541 }
1542
1543 if (!trimmer_it->end.is_infinite &&
1544 end_ns_from_origin > trimmer_it->end.ns_from_origin) {
1545 /*
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
1550 * count.
1551 */
1552 const bt_clock_class *clock_class =
1553 bt_clock_snapshot_borrow_clock_class_const(
1554 end_cs);
1555 const bt_clock_snapshot *begin_cs;
1556 bt_message *new_msg;
1557 uint64_t end_raw_value;
1558
1559 ret = clock_raw_value_from_ns_from_origin(clock_class,
1560 trimmer_it->end.ns_from_origin, &end_raw_value);
1561 if (ret) {
a3f0c7db 1562 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1563 goto end;
1564 }
1565
1566 if (msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
9b24b6aa 1567 begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1568 msg);
7de0e49a
PP
1569 new_msg = bt_message_discarded_events_create_with_default_clock_snapshots(
1570 trimmer_it->self_msg_iter,
1571 sstate->stream,
1572 bt_clock_snapshot_get_value(begin_cs),
1573 end_raw_value);
1574 } else {
9b24b6aa 1575 begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1576 msg);
7de0e49a
PP
1577 new_msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
1578 trimmer_it->self_msg_iter,
1579 sstate->stream,
1580 bt_clock_snapshot_get_value(begin_cs),
1581 end_raw_value);
1582 }
1583
1584 if (!new_msg) {
a3f0c7db 1585 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
7de0e49a
PP
1586 goto end;
1587 }
1588
1589 /* Replace the original message */
1590 BT_MESSAGE_MOVE_REF(msg, new_msg);
1591 }
1592
7de0e49a
PP
1593 push_message(trimmer_it, msg);
1594 msg = NULL;
1595 break;
1596 }
188edac1
SM
1597
1598 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1599 /*
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.
1602 */
1603 if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite &&
1604 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1605 status = end_iterator_streams(trimmer_it);
1606 *reached_end = true;
1607 break;
1608 }
1609
188edac1
SM
1610 /* Learn about this stream. */
1611 status = create_stream_state_entry(trimmer_it, stream, &sstate);
a3f0c7db 1612 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
188edac1 1613 goto end;
7de0e49a
PP
1614 }
1615
188edac1
SM
1616 if (ns_from_origin) {
1617 sstate->seen_clock_snapshot = true;
7de0e49a
PP
1618 }
1619
5b7b55be
SM
1620 push_message(trimmer_it, msg);
1621 msg = NULL;
7de0e49a
PP
1622 break;
1623 case BT_MESSAGE_TYPE_STREAM_END:
188edac1
SM
1624 {
1625 gboolean removed;
1626
5b7b55be 1627 /*
188edac1
SM
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.
5b7b55be 1630 */
188edac1
SM
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;
1635 break;
7de0e49a 1636 }
188edac1
SM
1637
1638 /*
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.
1642 */
1643 push_message(trimmer_it, msg);
1644 msg = NULL;
1645
1646 /* Forget about this stream. */
1647 removed = g_hash_table_remove(trimmer_it->stream_states, sstate->stream);
1648 BT_ASSERT(removed);
7de0e49a 1649 break;
188edac1 1650 }
7de0e49a
PP
1651 default:
1652 break;
1653 }
1654
1655end:
1656 /* We release the message's reference whatever the outcome */
1657 bt_message_put_ref(msg);
188edac1 1658 return status;
7de0e49a
PP
1659}
1660
1661/*
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.
1665 *
1666 * This function consumes the `msg` reference, _whatever the outcome_.
1667 *
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
1671 * `reached_end`.
1672 */
1673static inline
a3f0c7db 1674bt_message_iterator_class_next_method_status handle_message(
7de0e49a
PP
1675 struct trimmer_iterator *trimmer_it, const bt_message *msg,
1676 bool *reached_end)
1677{
a3f0c7db 1678 bt_message_iterator_class_next_method_status status;
7de0e49a
PP
1679 const bt_stream *stream = NULL;
1680 int64_t ns_from_origin = INT64_MIN;
4af85094 1681 bool has_ns_from_origin = false;
7de0e49a 1682 int ret;
7de0e49a
PP
1683
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));
1689 break;
1690 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1691 stream = bt_packet_borrow_stream_const(
1692 bt_message_packet_beginning_borrow_packet_const(msg));
1693 break;
1694 case BT_MESSAGE_TYPE_PACKET_END:
1695 stream = bt_packet_borrow_stream_const(
1696 bt_message_packet_end_borrow_packet_const(msg));
1697 break;
1698 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1699 stream = bt_message_discarded_events_borrow_stream_const(msg);
1700 break;
1701 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1702 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1703 break;
7de0e49a
PP
1704 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1705 stream = bt_message_stream_beginning_borrow_stream_const(msg);
1706 break;
1707 case BT_MESSAGE_TYPE_STREAM_END:
1708 stream = bt_message_stream_end_borrow_stream_const(msg);
1709 break;
1710 default:
1711 break;
1712 }
1713
7de0e49a 1714 /* Retrieve the message's time */
188edac1 1715 ret = get_msg_ns_from_origin(msg, &ns_from_origin, &has_ns_from_origin);
91d81473 1716 if (G_UNLIKELY(ret)) {
a3f0c7db 1717 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1718 goto end;
1719 }
1720
188edac1 1721 if (G_LIKELY(stream)) {
7de0e49a 1722 /* Message associated to a stream */
188edac1
SM
1723 status = handle_message_with_stream(trimmer_it, msg,
1724 stream, has_ns_from_origin ? &ns_from_origin : NULL, reached_end);
7de0e49a
PP
1725
1726 /*
1727 * handle_message_with_stream_state() unconditionally
1728 * consumes `msg`.
1729 */
1730 msg = NULL;
1731 } else {
1732 /*
1733 * Message not associated to a stream (message iterator
1734 * inactivity).
1735 */
91d81473 1736 if (G_UNLIKELY(ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1737 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1738 status = end_iterator_streams(trimmer_it);
1739 *reached_end = true;
1740 } else {
1741 push_message(trimmer_it, msg);
a3f0c7db 1742 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1743 msg = NULL;
1744 }
1745 }
1746
1747end:
1748 /* We release the message's reference whatever the outcome */
1749 bt_message_put_ref(msg);
1750 return status;
1751}
1752
1753static inline
1754void fill_message_array_from_output_messages(
1755 struct trimmer_iterator *trimmer_it,
1756 bt_message_array_const msgs, uint64_t capacity, uint64_t *count)
1757{
1758 *count = 0;
1759
1760 /*
1761 * Move auto-seek messages to the output array (which is this
1762 * iterator's base message array).
1763 */
1764 while (capacity > 0 && !g_queue_is_empty(trimmer_it->output_messages)) {
1765 msgs[*count] = pop_message(trimmer_it);
1766 capacity--;
1767 (*count)++;
1768 }
1769
98b15851 1770 BT_ASSERT_DBG(*count > 0);
7de0e49a
PP
1771}
1772
1773static inline
a3f0c7db 1774bt_message_iterator_class_next_method_status state_ending(
7de0e49a
PP
1775 struct trimmer_iterator *trimmer_it,
1776 bt_message_array_const msgs, uint64_t capacity,
1777 uint64_t *count)
1778{
a3f0c7db
SM
1779 bt_message_iterator_class_next_method_status status =
1780 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1781
1782 if (g_queue_is_empty(trimmer_it->output_messages)) {
1783 trimmer_it->state = TRIMMER_ITERATOR_STATE_ENDED;
a3f0c7db 1784 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
7de0e49a
PP
1785 goto end;
1786 }
1787
1788 fill_message_array_from_output_messages(trimmer_it, msgs,
1789 capacity, count);
1790
1791end:
1792 return status;
1793}
1794
1795static inline
a3f0c7db 1796bt_message_iterator_class_next_method_status
d24d5663 1797state_trim(struct trimmer_iterator *trimmer_it,
7de0e49a
PP
1798 bt_message_array_const msgs, uint64_t capacity,
1799 uint64_t *count)
1800{
a3f0c7db
SM
1801 bt_message_iterator_class_next_method_status status =
1802 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1803 bt_message_array_const my_msgs;
1804 uint64_t my_count;
1805 uint64_t i;
1806 bool reached_end = false;
1807
1808 while (g_queue_is_empty(trimmer_it->output_messages)) {
9a2c8b8e 1809 status = (int) bt_message_iterator_next(
7de0e49a 1810 trimmer_it->upstream_iter, &my_msgs, &my_count);
a3f0c7db
SM
1811 if (G_UNLIKELY(status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) {
1812 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
7de0e49a 1813 status = end_iterator_streams(trimmer_it);
a3f0c7db 1814 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1815 goto end;
1816 }
1817
1818 trimmer_it->state =
1819 TRIMMER_ITERATOR_STATE_ENDING;
1820 status = state_ending(trimmer_it, msgs,
1821 capacity, count);
1822 }
1823
1824 goto end;
1825 }
1826
98b15851 1827 BT_ASSERT_DBG(my_count > 0);
7de0e49a
PP
1828
1829 for (i = 0; i < my_count; i++) {
1830 status = handle_message(trimmer_it, my_msgs[i],
1831 &reached_end);
1832
1833 /*
1834 * handle_message() unconditionally consumes the
1835 * message reference.
1836 */
1837 my_msgs[i] = NULL;
1838
91d81473 1839 if (G_UNLIKELY(status !=
a3f0c7db 1840 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) {
7de0e49a
PP
1841 put_messages(my_msgs, my_count);
1842 goto end;
1843 }
1844
91d81473 1845 if (G_UNLIKELY(reached_end)) {
7de0e49a
PP
1846 /*
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
1854 * "next" method.
1855 */
1856 put_messages(my_msgs, my_count);
1857 trimmer_it->state =
1858 TRIMMER_ITERATOR_STATE_ENDING;
1859 status = state_ending(trimmer_it, msgs,
1860 capacity, count);
1861 goto end;
1862 }
1863 }
1864 }
1865
1866 /*
1867 * There's at least one message in the output message queue:
1868 * move the messages to the output message array.
1869 */
98b15851 1870 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it->output_messages));
7de0e49a
PP
1871 fill_message_array_from_output_messages(trimmer_it, msgs,
1872 capacity, count);
1873
1874end:
1875 return status;
1876}
1877
1878BT_HIDDEN
a3f0c7db 1879bt_message_iterator_class_next_method_status trimmer_msg_iter_next(
7de0e49a
PP
1880 bt_self_message_iterator *self_msg_iter,
1881 bt_message_array_const msgs, uint64_t capacity,
1882 uint64_t *count)
1883{
1884 struct trimmer_iterator *trimmer_it =
1885 bt_self_message_iterator_get_data(self_msg_iter);
a3f0c7db
SM
1886 bt_message_iterator_class_next_method_status status =
1887 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
7de0e49a 1888
98b15851 1889 BT_ASSERT_DBG(trimmer_it);
7de0e49a 1890
91d81473 1891 if (G_LIKELY(trimmer_it->state == TRIMMER_ITERATOR_STATE_TRIM)) {
7de0e49a 1892 status = state_trim(trimmer_it, msgs, capacity, count);
a3f0c7db 1893 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1894 goto end;
1895 }
1896 } else {
1897 switch (trimmer_it->state) {
1898 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN:
1899 status = state_set_trimmer_iterator_bounds(trimmer_it);
a3f0c7db 1900 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1901 goto end;
1902 }
1903
1904 status = state_seek_initially(trimmer_it);
a3f0c7db 1905 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1906 goto end;
1907 }
1908
1909 status = state_trim(trimmer_it, msgs, capacity, count);
a3f0c7db 1910 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1911 goto end;
1912 }
1913
1914 break;
1915 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY:
1916 status = state_seek_initially(trimmer_it);
a3f0c7db 1917 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1918 goto end;
1919 }
1920
1921 status = state_trim(trimmer_it, msgs, capacity, count);
a3f0c7db 1922 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1923 goto end;
1924 }
1925
1926 break;
1927 case TRIMMER_ITERATOR_STATE_ENDING:
1928 status = state_ending(trimmer_it, msgs, capacity,
1929 count);
a3f0c7db 1930 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1931 goto end;
1932 }
1933
1934 break;
1935 case TRIMMER_ITERATOR_STATE_ENDED:
a3f0c7db 1936 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
7de0e49a
PP
1937 break;
1938 default:
498e7994 1939 bt_common_abort();
7de0e49a
PP
1940 }
1941 }
1942
1943end:
1944 return status;
1945}
1946
1947BT_HIDDEN
1948void trimmer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
1949{
1950 struct trimmer_iterator *trimmer_it =
1951 bt_self_message_iterator_get_data(self_msg_iter);
1952
1953 BT_ASSERT(trimmer_it);
1954 destroy_trimmer_iterator(trimmer_it);
1955}
This page took 0.173827 seconds and 4 git commands to generate.