Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[babeltrace.git] / src / plugins / utils / trimmer / trimmer.c
CommitLineData
cab3f160 1/*
cab3f160 2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7de0e49a 3 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
cab3f160
JG
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
0d9a3d3e 24#define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level)
350ad6c1 25#define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER"
d9c39b0a 26#include "logging/comp-logging.h"
b4565e8b 27
578e048b
MJ
28#include "compat/utc.h"
29#include "compat/time.h"
3fadfbc0 30#include <babeltrace2/babeltrace.h>
578e048b 31#include "common/common.h"
578e048b 32#include "common/assert.h"
c4f23e30 33#include <stdbool.h>
7de0e49a
PP
34#include <stdint.h>
35#include <inttypes.h>
36#include <glib.h>
188edac1 37#include "compat/glib.h"
90a55a4e 38#include "plugins/common/param-validation/param-validation.h"
7de0e49a
PP
39
40#include "trimmer.h"
41
42#define NS_PER_S INT64_C(1000000000)
43
44static const char * const in_port_name = "in";
45
46struct trimmer_time {
47 unsigned int hour, minute, second, ns;
48};
49
50struct trimmer_bound {
51 /*
52 * Nanoseconds from origin, valid if `is_set` is set and
53 * `is_infinite` is false.
54 */
55 int64_t ns_from_origin;
56
57 /* True if this bound's full time (`ns_from_origin`) is set */
58 bool is_set;
59
60 /*
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.
64 */
65 bool is_infinite;
66
67 /*
68 * This bound's time without the date; this time is used to set
69 * `ns_from_origin` once we know the date.
70 */
71 struct trimmer_time time;
72};
73
74struct trimmer_comp {
75 struct trimmer_bound begin, end;
76 bool is_gmt;
0d9a3d3e 77 bt_logging_level log_level;
ec4ae660 78 bt_self_component *self_comp;
7de0e49a
PP
79};
80
81enum trimmer_iterator_state {
82 /*
83 * Find the first message's date and set the bounds's times
84 * accordingly.
85 */
86 TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN,
87
88 /*
89 * Initially seek to the trimming range's beginning time.
90 */
91 TRIMMER_ITERATOR_STATE_SEEK_INITIALLY,
92
93 /*
94 * Fill the output message queue for as long as received input
95 * messages are within the trimming time range.
96 */
97 TRIMMER_ITERATOR_STATE_TRIM,
98
99 /* Flush the remaining messages in the output message queue */
100 TRIMMER_ITERATOR_STATE_ENDING,
101
102 /* Trimming operation and message iterator is ended */
103 TRIMMER_ITERATOR_STATE_ENDED,
104};
105
106struct trimmer_iterator {
107 /* Weak */
108 struct trimmer_comp *trimmer_comp;
109
110 /* Weak */
111 bt_self_message_iterator *self_msg_iter;
112
113 enum trimmer_iterator_state state;
114
115 /* Owned by this */
116 bt_self_component_port_input_message_iterator *upstream_iter;
117 struct trimmer_bound begin, end;
118
119 /*
120 * Queue of `const bt_message *` (owned by the queue).
121 *
122 * This is where the trimming operation pushes the messages to
123 * output by this message iterator.
124 */
125 GQueue *output_messages;
126
127 /*
128 * Hash table of `bt_stream *` (weak) to
129 * `struct trimmer_iterator_stream_state *` (owned by the HT).
130 */
131 GHashTable *stream_states;
132};
133
134struct trimmer_iterator_stream_state {
7de0e49a
PP
135 /* Weak */
136 const bt_stream *stream;
137
188edac1
SM
138 /* Have we seen a message with clock_snapshot going through this stream? */
139 bool seen_clock_snapshot;
140
7de0e49a
PP
141 /* Owned by this (`NULL` initially and between packets) */
142 const bt_packet *cur_packet;
7de0e49a 143};
cab3f160
JG
144
145static
7de0e49a 146void destroy_trimmer_comp(struct trimmer_comp *trimmer_comp)
cab3f160 147{
7de0e49a
PP
148 BT_ASSERT(trimmer_comp);
149 g_free(trimmer_comp);
cab3f160
JG
150}
151
152static
7de0e49a
PP
153struct trimmer_comp *create_trimmer_comp(void)
154{
155 return g_new0(struct trimmer_comp, 1);
156}
157
158BT_HIDDEN
159void trimmer_finalize(bt_self_component_filter *self_comp)
cab3f160 160{
7de0e49a
PP
161 struct trimmer_comp *trimmer_comp =
162 bt_self_component_get_data(
163 bt_self_component_filter_as_self_component(self_comp));
164
165 if (trimmer_comp) {
166 destroy_trimmer_comp(trimmer_comp);
167 }
cab3f160
JG
168}
169
d4d7ffc9
SM
170/*
171 * Compile regex in `pattern`, and try to match `string`. If there's a match,
172 * return true and set `*match_info` to the list of matches. The list of
173 * matches must be freed by the caller. If there's no match, return false and
174 * set `*match_info` to NULL;
175 */
176static
177bool compile_and_match(const char *pattern, const char *string, GMatchInfo **match_info) {
178 bool matches = false;
179 GError *regex_error = NULL;
180 GRegex *regex;
181
182 regex = g_regex_new(pattern, 0, 0, &regex_error);
183 if (!regex) {
184 goto end;
185 }
186
187 matches = g_regex_match(regex, string, 0, match_info);
188 if (!matches) {
189 /*
190 * g_regex_match allocates `*match_info` even if it returns
191 * FALSE. If there's no match, we have no use for it, so free
192 * it immediatly and don't return it to the caller.
193 */
a42eab88 194 g_match_info_free(*match_info);
d4d7ffc9
SM
195 *match_info = NULL;
196 }
197
198 g_regex_unref(regex);
199
200end:
201
202 if (regex_error) {
203 g_error_free(regex_error);
204 }
205
206 return matches;
207}
208
209/*
210 * Convert the captured text in match number `match_num` in `match_info`
211 * to an unsigned integer.
212 */
213static
214guint64 match_to_uint(const GMatchInfo *match_info, gint match_num) {
215 gchar *text, *endptr;
216 guint64 result;
217
218 text = g_match_info_fetch(match_info, match_num);
219 BT_ASSERT(text);
220
221 /*
222 * Because the input is carefully sanitized with regexes by the caller,
223 * we assume that g_ascii_strtoull cannot fail.
224 */
225 errno = 0;
226 result = g_ascii_strtoull(text, &endptr, 10);
227 BT_ASSERT(endptr > text);
228 BT_ASSERT(errno == 0);
229
230 g_free(text);
231
232 return result;
233}
234
235/*
236 * When parsing the nanoseconds part, .512 means .512000000, not .000000512.
237 * This function is like match_to_uint, but multiplies the parsed number to get
238 * the expected result.
239 */
240static
241guint64 match_to_uint_ns(const GMatchInfo *match_info, gint match_num) {
242 guint64 nanoseconds;
243 gboolean ret;
244 gint start_pos, end_pos, power;
245 static int pow10[] = {
246 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000,
247 };
248
249 nanoseconds = match_to_uint(match_info, match_num);
250
251 /* Multiply by 10 as many times as there are omitted digits. */
252 ret = g_match_info_fetch_pos(match_info, match_num, &start_pos, &end_pos);
253 BT_ASSERT(ret);
254
255 power = 9 - (end_pos - start_pos);
256 BT_ASSERT(power >= 0 && power <= 8);
257
258 nanoseconds *= pow10[power];
259
260 return nanoseconds;
261}
262
7de0e49a
PP
263/*
264 * Sets the time (in ns from origin) of a trimmer bound from date and
265 * time components.
266 *
267 * Returns a negative value if anything goes wrong.
268 */
269static
270int set_bound_ns_from_origin(struct trimmer_bound *bound,
271 unsigned int year, unsigned int month, unsigned int day,
272 unsigned int hour, unsigned int minute, unsigned int second,
273 unsigned int ns, bool is_gmt)
cab3f160 274{
7de0e49a
PP
275 int ret = 0;
276 time_t result;
277 struct tm tm = {
278 .tm_sec = second,
279 .tm_min = minute,
280 .tm_hour = hour,
281 .tm_mday = day,
282 .tm_mon = month - 1,
283 .tm_year = year - 1900,
284 .tm_isdst = -1,
285 };
286
287 if (is_gmt) {
288 result = bt_timegm(&tm);
289 } else {
290 result = mktime(&tm);
291 }
292
293 if (result < 0) {
294 ret = -1;
295 goto end;
296 }
cab3f160 297
7de0e49a
PP
298 BT_ASSERT(bound);
299 bound->ns_from_origin = (int64_t) result;
300 bound->ns_from_origin *= NS_PER_S;
301 bound->ns_from_origin += ns;
302 bound->is_set = true;
303
304end:
305 return ret;
cab3f160
JG
306}
307
528debdf
MD
308/*
309 * Parses a timestamp, figuring out its format.
310 *
311 * Returns a negative value if anything goes wrong.
312 *
313 * Expected formats:
314 *
7de0e49a
PP
315 * YYYY-MM-DD hh:mm[:ss[.ns]]
316 * [hh:mm:]ss[.ns]
317 * [-]s[.ns]
318 *
319 * TODO: Check overflows.
320 */
321static
0d9a3d3e
PP
322int set_bound_from_str(struct trimmer_comp *trimmer_comp,
323 const char *str, struct trimmer_bound *bound, bool is_gmt)
7de0e49a 324{
d4d7ffc9
SM
325/* Matches YYYY-MM-DD */
326#define DATE_RE "([0-9]{4})-([0-9]{2})-([0-9]{2})"
327
328/* Matches HH:MM[:SS[.NS]] */
329#define TIME_RE "([0-9]{2}):([0-9]{2})(?::([0-9]{2})(?:\\.([0-9]{1,9}))?)?"
330
331/* Matches [-]SS[.NS] */
332#define S_NS_RE "^(-?)([0-9]+)(?:\\.([0-9]{1,9}))?$"
333
334 GMatchInfo *match_info;
7de0e49a 335 int ret = 0;
7de0e49a 336
d4d7ffc9
SM
337 /* Try `YYYY-MM-DD hh:mm[:ss[.ns]]` format */
338 if (compile_and_match("^" DATE_RE " " TIME_RE "$", str, &match_info)) {
339 unsigned int year = 0, month = 0, day = 0, hours = 0, minutes = 0, seconds = 0, nanoseconds = 0;
340 gint match_count = g_match_info_get_match_count(match_info);
7de0e49a 341
d4d7ffc9 342 BT_ASSERT(match_count >= 6 && match_count <= 8);
7de0e49a 343
d4d7ffc9
SM
344 year = match_to_uint(match_info, 1);
345 month = match_to_uint(match_info, 2);
346 day = match_to_uint(match_info, 3);
347 hours = match_to_uint(match_info, 4);
348 minutes = match_to_uint(match_info, 5);
7de0e49a 349
d4d7ffc9
SM
350 if (match_count >= 7) {
351 seconds = match_to_uint(match_info, 6);
352 }
7de0e49a 353
d4d7ffc9
SM
354 if (match_count >= 8) {
355 nanoseconds = match_to_uint_ns(match_info, 7);
356 }
357
358 set_bound_ns_from_origin(bound, year, month, day, hours, minutes, seconds, nanoseconds, is_gmt);
7de0e49a 359
7de0e49a
PP
360 goto end;
361 }
362
d4d7ffc9
SM
363 if (compile_and_match("^" DATE_RE "$", str, &match_info)) {
364 unsigned int year = 0, month = 0, day = 0;
365
366 BT_ASSERT(g_match_info_get_match_count(match_info) == 4);
367
368 year = match_to_uint(match_info, 1);
369 month = match_to_uint(match_info, 2);
370 day = match_to_uint(match_info, 3);
371
372 set_bound_ns_from_origin(bound, year, month, day, 0, 0, 0, 0, is_gmt);
373
7de0e49a
PP
374 goto end;
375 }
376
d4d7ffc9
SM
377 /* Try `hh:mm[:ss[.ns]]` format */
378 if (compile_and_match("^" TIME_RE "$", str, &match_info)) {
379 gint match_count = g_match_info_get_match_count(match_info);
380 BT_ASSERT(match_count >= 3 && match_count <= 5);
381 bound->time.hour = match_to_uint(match_info, 1);
382 bound->time.minute = match_to_uint(match_info, 2);
383
384 if (match_count >= 4) {
385 bound->time.second = match_to_uint(match_info, 3);
386 }
387
388 if (match_count >= 5) {
389 bound->time.ns = match_to_uint_ns(match_info, 4);
390 }
391
7de0e49a
PP
392 goto end;
393 }
394
d4d7ffc9
SM
395 /* Try `[-]s[.ns]` format */
396 if (compile_and_match("^" S_NS_RE "$", str, &match_info)) {
397 gboolean is_neg, fetch_pos_ret;
398 gint start_pos, end_pos, match_count;
399 guint64 seconds, nanoseconds = 0;
400
401 match_count = g_match_info_get_match_count(match_info);
402 BT_ASSERT(match_count >= 3 && match_count <= 4);
403
404 /* Check for presence of negation sign. */
405 fetch_pos_ret = g_match_info_fetch_pos(match_info, 1, &start_pos, &end_pos);
406 BT_ASSERT(fetch_pos_ret);
407 is_neg = (end_pos - start_pos) > 0;
408
409 seconds = match_to_uint(match_info, 2);
410
411 if (match_count >= 4) {
412 nanoseconds = match_to_uint_ns(match_info, 3);
413 }
414
415 bound->ns_from_origin = seconds * NS_PER_S + nanoseconds;
416
417 if (is_neg) {
418 bound->ns_from_origin = -bound->ns_from_origin;
419 }
420
7de0e49a 421 bound->is_set = true;
d4d7ffc9 422
7de0e49a
PP
423 goto end;
424 }
425
ab90ee94
SM
426 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
427 "Invalid date/time format: param=\"%s\"", str);
7de0e49a
PP
428 ret = -1;
429
430end:
431 return ret;
432}
433
434/*
435 * Sets a trimmer bound's properties from a parameter string/integer
436 * value.
437 *
438 * Returns a negative value if anything goes wrong.
528debdf 439 */
44d3cbf0 440static
0d9a3d3e
PP
441int set_bound_from_param(struct trimmer_comp *trimmer_comp,
442 const char *param_name, const bt_value *param,
7de0e49a 443 struct trimmer_bound *bound, bool is_gmt)
528debdf
MD
444{
445 int ret;
268fae9a 446 const char *arg;
7de0e49a 447 char tmp_arg[64];
268fae9a 448
fdd3a2da 449 if (bt_value_is_signed_integer(param)) {
9c08c816 450 int64_t value = bt_value_integer_signed_get(param);
7de0e49a
PP
451
452 /*
453 * Just convert it to a temporary string to handle
454 * everything the same way.
455 */
456 sprintf(tmp_arg, "%" PRId64, value);
457 arg = tmp_arg;
7de0e49a 458 } else {
90a55a4e
SM
459 BT_ASSERT(bt_value_is_string(param));
460 arg = bt_value_string_get(param);
268fae9a
PP
461 }
462
0d9a3d3e 463 ret = set_bound_from_str(trimmer_comp, arg, bound, is_gmt);
7de0e49a 464
7de0e49a
PP
465 return ret;
466}
467
468static
0d9a3d3e
PP
469int validate_trimmer_bounds(struct trimmer_comp *trimmer_comp,
470 struct trimmer_bound *begin, struct trimmer_bound *end)
7de0e49a
PP
471{
472 int ret = 0;
473
474 BT_ASSERT(begin->is_set);
475 BT_ASSERT(end->is_set);
476
477 if (!begin->is_infinite && !end->is_infinite &&
478 begin->ns_from_origin > end->ns_from_origin) {
ab90ee94
SM
479 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
480 "Trimming time range's beginning time is greater than end time: "
7de0e49a
PP
481 "begin-ns-from-origin=%" PRId64 ", "
482 "end-ns-from-origin=%" PRId64,
483 begin->ns_from_origin,
484 end->ns_from_origin);
485 ret = -1;
486 goto end;
528debdf
MD
487 }
488
7de0e49a 489 if (!begin->is_infinite && begin->ns_from_origin == INT64_MIN) {
ab90ee94
SM
490 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
491 "Invalid trimming time range's beginning time: "
7de0e49a
PP
492 "ns-from-origin=%" PRId64,
493 begin->ns_from_origin);
494 ret = -1;
495 goto end;
496 }
528debdf 497
7de0e49a 498 if (!end->is_infinite && end->ns_from_origin == INT64_MIN) {
ab90ee94
SM
499 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
500 "Invalid trimming time range's end time: "
7de0e49a
PP
501 "ns-from-origin=%" PRId64,
502 end->ns_from_origin);
503 ret = -1;
504 goto end;
505 }
528debdf 506
7de0e49a
PP
507end:
508 return ret;
528debdf
MD
509}
510
511static
90a55a4e
SM
512enum bt_param_validation_status validate_bound_type(
513 const bt_value *value,
514 struct bt_param_validation_context *context)
515{
516 enum bt_param_validation_status status = BT_PARAM_VALIDATION_STATUS_OK;
517
518 if (!bt_value_is_signed_integer(value) &&
519 !bt_value_is_string(value)) {
520 status = bt_param_validation_error(context,
521 "unexpected type: expected-types=[%s, %s], actual-type=%s",
522 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER),
523 bt_common_value_type_string(BT_VALUE_TYPE_STRING),
524 bt_common_value_type_string(bt_value_get_type(value)));
525 }
526
527 return status;
528}
529
530struct bt_param_validation_map_value_entry_descr trimmer_params[] = {
531 { "gmt", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
532 { "begin", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .validation_func = validate_bound_type } },
533 { "end", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .validation_func = validate_bound_type } },
534 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
535};
536
537static
538bt_component_class_initialize_method_status init_trimmer_comp_from_params(
539 struct trimmer_comp *trimmer_comp,
7de0e49a 540 const bt_value *params)
44d3cbf0 541{
7de0e49a 542 const bt_value *value;
90a55a4e
SM
543 bt_component_class_initialize_method_status status;
544 enum bt_param_validation_status validation_status;
545 gchar *validate_error = NULL;
546
547 validation_status = bt_param_validation_validate(params,
548 trimmer_params, &validate_error);
549 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
550 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
551 goto end;
552 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
553 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
554 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, "%s",
555 validate_error);
556 goto end;
557 }
44d3cbf0 558
f6ccaed9 559 BT_ASSERT(params);
7de0e49a 560 value = bt_value_map_borrow_entry_value_const(params, "gmt");
528debdf 561 if (value) {
7de0e49a 562 trimmer_comp->is_gmt = (bool) bt_value_bool_get(value);
528debdf
MD
563 }
564
7de0e49a 565 value = bt_value_map_borrow_entry_value_const(params, "begin");
528debdf 566 if (value) {
0d9a3d3e 567 if (set_bound_from_param(trimmer_comp, "begin", value,
7de0e49a
PP
568 &trimmer_comp->begin, trimmer_comp->is_gmt)) {
569 /* set_bound_from_param() logs errors */
90a55a4e 570 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
268fae9a 571 goto end;
44d3cbf0 572 }
7de0e49a
PP
573 } else {
574 trimmer_comp->begin.is_infinite = true;
575 trimmer_comp->begin.is_set = true;
44d3cbf0 576 }
528debdf 577
7de0e49a 578 value = bt_value_map_borrow_entry_value_const(params, "end");
528debdf 579 if (value) {
0d9a3d3e 580 if (set_bound_from_param(trimmer_comp, "end", value,
7de0e49a
PP
581 &trimmer_comp->end, trimmer_comp->is_gmt)) {
582 /* set_bound_from_param() logs errors */
90a55a4e 583 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
268fae9a 584 goto end;
528debdf 585 }
7de0e49a
PP
586 } else {
587 trimmer_comp->end.is_infinite = true;
588 trimmer_comp->end.is_set = true;
528debdf 589 }
268fae9a 590
7de0e49a
PP
591 if (trimmer_comp->begin.is_set && trimmer_comp->end.is_set) {
592 /* validate_trimmer_bounds() logs errors */
90a55a4e
SM
593 if (validate_trimmer_bounds(trimmer_comp,
594 &trimmer_comp->begin, &trimmer_comp->end)) {
595 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
596 goto end;
597 }
55595636 598 }
7de0e49a 599
90a55a4e
SM
600 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
601
602end:
603 g_free(validate_error);
604
605 return status;
44d3cbf0
JG
606}
607
21a9f056 608bt_component_class_initialize_method_status trimmer_init(
d24d5663 609 bt_self_component_filter *self_comp_flt,
59225a3e 610 bt_self_component_filter_configuration *config,
7de0e49a 611 const bt_value *params, void *init_data)
cab3f160 612{
90a55a4e 613 bt_component_class_initialize_method_status status;
d24d5663 614 bt_self_component_add_port_status add_port_status;
7de0e49a 615 struct trimmer_comp *trimmer_comp = create_trimmer_comp();
ec4ae660
PP
616 bt_self_component *self_comp =
617 bt_self_component_filter_as_self_component(self_comp_flt);
90a55a4e 618
7de0e49a 619 if (!trimmer_comp) {
21a9f056 620 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
7de0e49a 621 goto error;
cab3f160
JG
622 }
623
0d9a3d3e 624 trimmer_comp->log_level = bt_component_get_logging_level(
ec4ae660
PP
625 bt_self_component_as_component(self_comp));
626 trimmer_comp->self_comp = self_comp;
90a55a4e 627
d24d5663 628 add_port_status = bt_self_component_filter_add_input_port(
ec4ae660 629 self_comp_flt, in_port_name, NULL, NULL);
90a55a4e
SM
630 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
631 status = (int) add_port_status;
d24d5663 632 goto error;
b9d103be
PP
633 }
634
d24d5663 635 add_port_status = bt_self_component_filter_add_output_port(
ec4ae660 636 self_comp_flt, "out", NULL, NULL);
90a55a4e
SM
637 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
638 status = (int) add_port_status;
d24d5663 639 goto error;
b9d103be
PP
640 }
641
90a55a4e
SM
642 status = init_trimmer_comp_from_params(trimmer_comp, params);
643 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
cab3f160
JG
644 goto error;
645 }
646
ec4ae660 647 bt_self_component_set_data(self_comp, trimmer_comp);
90a55a4e
SM
648
649 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
7de0e49a
PP
650 goto end;
651
652error:
7de0e49a
PP
653 if (trimmer_comp) {
654 destroy_trimmer_comp(trimmer_comp);
655 }
656
cab3f160 657end:
8dad9b32 658 return status;
7de0e49a
PP
659}
660
661static
662void destroy_trimmer_iterator(struct trimmer_iterator *trimmer_it)
663{
e803df70
SM
664 if (!trimmer_it) {
665 goto end;
666 }
667
7de0e49a
PP
668 bt_self_component_port_input_message_iterator_put_ref(
669 trimmer_it->upstream_iter);
670
671 if (trimmer_it->output_messages) {
672 g_queue_free(trimmer_it->output_messages);
673 }
674
675 if (trimmer_it->stream_states) {
676 g_hash_table_destroy(trimmer_it->stream_states);
677 }
678
679 g_free(trimmer_it);
e803df70
SM
680end:
681 return;
7de0e49a
PP
682}
683
684static
685void destroy_trimmer_iterator_stream_state(
686 struct trimmer_iterator_stream_state *sstate)
687{
688 BT_ASSERT(sstate);
689 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
7de0e49a
PP
690 g_free(sstate);
691}
692
693BT_HIDDEN
21a9f056 694bt_component_class_message_iterator_initialize_method_status trimmer_msg_iter_init(
7de0e49a 695 bt_self_message_iterator *self_msg_iter,
8d8b141d 696 bt_self_message_iterator_configuration *config,
7de0e49a
PP
697 bt_self_component_filter *self_comp,
698 bt_self_component_port_output *port)
699{
21a9f056 700 bt_component_class_message_iterator_initialize_method_status status;
e803df70
SM
701 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
702 msg_iter_status;
7de0e49a
PP
703 struct trimmer_iterator *trimmer_it;
704
705 trimmer_it = g_new0(struct trimmer_iterator, 1);
706 if (!trimmer_it) {
21a9f056 707 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 708 goto error;
7de0e49a
PP
709 }
710
711 trimmer_it->trimmer_comp = bt_self_component_get_data(
712 bt_self_component_filter_as_self_component(self_comp));
713 BT_ASSERT(trimmer_it->trimmer_comp);
714
715 if (trimmer_it->trimmer_comp->begin.is_set &&
716 trimmer_it->trimmer_comp->end.is_set) {
717 /*
718 * Both trimming time range's bounds are set, so skip
719 * the
720 * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN`
721 * phase.
722 */
723 trimmer_it->state = TRIMMER_ITERATOR_STATE_SEEK_INITIALLY;
724 }
725
726 trimmer_it->begin = trimmer_it->trimmer_comp->begin;
727 trimmer_it->end = trimmer_it->trimmer_comp->end;
e803df70 728 msg_iter_status =
ca02df0a
PP
729 bt_self_component_port_input_message_iterator_create_from_message_iterator(
730 self_msg_iter,
7de0e49a 731 bt_self_component_filter_borrow_input_port_by_name(
e803df70
SM
732 self_comp, in_port_name), &trimmer_it->upstream_iter);
733 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
734 status = (int) msg_iter_status;
735 goto error;
7de0e49a
PP
736 }
737
738 trimmer_it->output_messages = g_queue_new();
739 if (!trimmer_it->output_messages) {
21a9f056 740 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 741 goto error;
7de0e49a
PP
742 }
743
744 trimmer_it->stream_states = g_hash_table_new_full(g_direct_hash,
745 g_direct_equal, NULL,
746 (GDestroyNotify) destroy_trimmer_iterator_stream_state);
747 if (!trimmer_it->stream_states) {
21a9f056 748 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
e803df70 749 goto error;
7de0e49a
PP
750 }
751
c0e46a7c
SM
752 /*
753 * The trimmer requires upstream messages to have times, so it can
754 * always seek forward.
755 */
756 bt_self_message_iterator_configuration_set_can_seek_forward(
757 config, BT_TRUE);
758
7de0e49a
PP
759 trimmer_it->self_msg_iter = self_msg_iter;
760 bt_self_message_iterator_set_data(self_msg_iter, trimmer_it);
761
21a9f056 762 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK;
e803df70
SM
763 goto end;
764
765error:
766 destroy_trimmer_iterator(trimmer_it);
7de0e49a 767
e803df70 768end:
7de0e49a
PP
769 return status;
770}
771
772static inline
773int get_msg_ns_from_origin(const bt_message *msg, int64_t *ns_from_origin,
188edac1 774 bool *has_clock_snapshot)
7de0e49a
PP
775{
776 const bt_clock_class *clock_class = NULL;
777 const bt_clock_snapshot *clock_snapshot = NULL;
7de0e49a
PP
778 int ret = 0;
779
98b15851
PP
780 BT_ASSERT_DBG(msg);
781 BT_ASSERT_DBG(ns_from_origin);
782 BT_ASSERT_DBG(has_clock_snapshot);
7de0e49a
PP
783
784 switch (bt_message_get_type(msg)) {
785 case BT_MESSAGE_TYPE_EVENT:
786 clock_class =
787 bt_message_event_borrow_stream_class_default_clock_class_const(
788 msg);
91d81473 789 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
790 goto error;
791 }
792
0cbc2c33
PP
793 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(
794 msg);
7de0e49a
PP
795 break;
796 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
797 clock_class =
798 bt_message_packet_beginning_borrow_stream_class_default_clock_class_const(
799 msg);
91d81473 800 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
801 goto error;
802 }
803
0cbc2c33
PP
804 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
805 msg);
7de0e49a
PP
806 break;
807 case BT_MESSAGE_TYPE_PACKET_END:
808 clock_class =
809 bt_message_packet_end_borrow_stream_class_default_clock_class_const(
810 msg);
91d81473 811 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
812 goto error;
813 }
814
0cbc2c33
PP
815 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
816 msg);
7de0e49a 817 break;
188edac1
SM
818 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
819 {
820 enum bt_message_stream_clock_snapshot_state cs_state;
821
7de0e49a 822 clock_class =
188edac1 823 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg);
91d81473 824 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
825 goto error;
826 }
827
188edac1
SM
828 cs_state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &clock_snapshot);
829 if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
830 goto no_clock_snapshot;
7de0e49a
PP
831 }
832
7de0e49a 833 break;
188edac1
SM
834 }
835 case BT_MESSAGE_TYPE_STREAM_END:
836 {
837 enum bt_message_stream_clock_snapshot_state cs_state;
838
7de0e49a 839 clock_class =
188edac1 840 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg);
91d81473 841 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
842 goto error;
843 }
844
188edac1
SM
845 cs_state = bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &clock_snapshot);
846 if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
7de0e49a
PP
847 goto no_clock_snapshot;
848 }
849
850 break;
188edac1
SM
851 }
852 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
7de0e49a 853 clock_class =
188edac1 854 bt_message_discarded_events_borrow_stream_class_default_clock_class_const(
7de0e49a 855 msg);
91d81473 856 if (G_UNLIKELY(!clock_class)) {
7de0e49a
PP
857 goto error;
858 }
859
188edac1
SM
860 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
861 msg);
862 break;
863 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
864 clock_class =
865 bt_message_discarded_packets_borrow_stream_class_default_clock_class_const(
866 msg);
867 if (G_UNLIKELY(!clock_class)) {
868 goto error;
7de0e49a
PP
869 }
870
188edac1
SM
871 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
872 msg);
7de0e49a
PP
873 break;
874 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
0cbc2c33 875 clock_snapshot =
7de0e49a 876 bt_message_message_iterator_inactivity_borrow_default_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
d24d5663
PP
951bt_component_class_message_iterator_next_method_status
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 =
969 bt_self_component_port_input_message_iterator_next(
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
d24d5663 1033bt_component_class_message_iterator_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;
f2fb1b32 1037 bt_component_class_message_iterator_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
1044 status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1045 trimmer_it->upstream_iter, &can_seek);
1046 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
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.");
d24d5663 1058 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1059 goto end;
1060 }
1061
1062 status = (int) bt_self_component_port_input_message_iterator_seek_beginning(
1063 trimmer_it->upstream_iter);
1064 } else {
f2fb1b32
SM
1065 bt_bool can_seek;
1066
1067 status = (int) bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1068 trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin,
1069 &can_seek);
1070
1071 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
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);
d24d5663 1085 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1086 goto end;
1087 }
1088
1089 status = (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1090 trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin);
1091 }
1092
d24d5663 1093 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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
d24d5663
PP
1129bt_component_class_message_iterator_next_method_status
1130end_stream(struct trimmer_iterator *trimmer_it,
7de0e49a
PP
1131 struct trimmer_iterator_stream_state *sstate)
1132{
d24d5663
PP
1133 bt_component_class_message_iterator_next_method_status status =
1134 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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) {
d24d5663 1162 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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) {
d24d5663 1183 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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) {
d24d5663 1196 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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
d24d5663 1219bt_component_class_message_iterator_next_method_status end_iterator_streams(
7de0e49a
PP
1220 struct trimmer_iterator *trimmer_it)
1221{
d24d5663
PP
1222 bt_component_class_message_iterator_next_method_status status =
1223 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_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
SM
1256static
1257bt_component_class_message_iterator_next_method_status
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;
1264 bt_component_class_message_iterator_next_method_status status;
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));
1290 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
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 */
1301 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1302 sc)) {
ab90ee94
SM
1303 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1304 "Unsupported stream: packets have no beginning clock snapshot: "
188edac1
SM
1305 "stream-addr=%p, "
1306 "stream-id=%" PRIu64 ", "
1307 "stream-name=\"%s\"",
1308 stream, bt_stream_get_id(stream),
1309 bt_stream_get_name(stream));
1310 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1311 goto end;
1312 }
1313
1314 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1315 sc)) {
ab90ee94
SM
1316 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1317 "Unsupported stream: packets have no end clock snapshot: "
188edac1
SM
1318 "stream-addr=%p, "
1319 "stream-id=%" PRIu64 ", "
1320 "stream-name=\"%s\"",
1321 stream, bt_stream_get_id(stream),
1322 bt_stream_get_name(stream));
1323 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1324 goto end;
1325 }
1326
1327 if (bt_stream_class_supports_discarded_events(sc) &&
1328 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) {
ab90ee94
SM
1329 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1330 "Unsupported stream: discarded events have no clock snapshots: "
188edac1
SM
1331 "stream-addr=%p, "
1332 "stream-id=%" PRIu64 ", "
1333 "stream-name=\"%s\"",
1334 stream, bt_stream_get_id(stream),
1335 bt_stream_get_name(stream));
1336 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1337 goto end;
1338 }
1339
1340 if (bt_stream_class_supports_discarded_packets(sc) &&
1341 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) {
ab90ee94
SM
1342 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1343 "Unsupported stream: discarded packets "
188edac1
SM
1344 "have no clock snapshots: "
1345 "stream-addr=%p, "
1346 "stream-id=%" PRIu64 ", "
1347 "stream-name=\"%s\"",
1348 stream, bt_stream_get_id(stream),
1349 bt_stream_get_name(stream));
1350 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
1351 goto end;
1352 }
1353
1354 sstate = g_new0(struct trimmer_iterator_stream_state, 1);
1355 if (!sstate) {
1356 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
1357 goto end;
1358 }
1359
1360 sstate->stream = stream;
1361
1362 g_hash_table_insert(trimmer_it->stream_states, (void *) stream, sstate);
1363
1364 *stream_state = sstate;
1365
1366 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
1367
1368end:
1369 return status;
1370}
1371
1372static
1373struct trimmer_iterator_stream_state *get_stream_state_entry(
1374 struct trimmer_iterator *trimmer_it,
1375 const struct bt_stream *stream)
1376{
1377 struct trimmer_iterator_stream_state *sstate;
1378
98b15851 1379 BT_ASSERT_DBG(stream);
188edac1 1380 sstate = g_hash_table_lookup(trimmer_it->stream_states, stream);
98b15851 1381 BT_ASSERT_DBG(sstate);
188edac1
SM
1382
1383 return sstate;
1384}
1385
7de0e49a
PP
1386/*
1387 * Handles a message which is associated to a given stream state. This
1388 * _could_ make the iterator's output message queue grow; this could
1389 * also consume the message without pushing anything to this queue, only
1390 * modifying the stream state.
1391 *
1392 * This function consumes the `msg` reference, _whatever the outcome_.
1393 *
188edac1
SM
1394 * If non-NULL, `ns_from_origin` is the message's time, as given by
1395 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
7de0e49a
PP
1396 *
1397 * This function sets `reached_end` if handling this message made the
1398 * iterator reach the end of the trimming range. Note that the output
1399 * message queue could contain messages even if this function sets
1400 * `reached_end`.
1401 */
188edac1 1402static
d24d5663 1403bt_component_class_message_iterator_next_method_status
188edac1 1404handle_message_with_stream(
7de0e49a 1405 struct trimmer_iterator *trimmer_it, const bt_message *msg,
188edac1
SM
1406 const struct bt_stream *stream, const int64_t *ns_from_origin,
1407 bool *reached_end)
7de0e49a 1408{
d24d5663
PP
1409 bt_component_class_message_iterator_next_method_status status =
1410 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1411 bt_message_type msg_type = bt_message_get_type(msg);
1412 int ret;
188edac1
SM
1413 struct trimmer_iterator_stream_state *sstate = NULL;
1414
1415 /*
1416 * Retrieve the stream's state - except if the message is stream
1417 * beginning, in which case we don't know about about this stream yet.
1418 */
1419 if (msg_type != BT_MESSAGE_TYPE_STREAM_BEGINNING) {
1420 sstate = get_stream_state_entry(trimmer_it, stream);
1421 }
7de0e49a
PP
1422
1423 switch (msg_type) {
1424 case BT_MESSAGE_TYPE_EVENT:
188edac1
SM
1425 /*
1426 * Event messages always have a clock snapshot if the stream
1427 * class has a clock class. And we know it has, otherwise we
1428 * couldn't be using the trimmer component.
1429 */
98b15851 1430 BT_ASSERT_DBG(ns_from_origin);
188edac1 1431
91d81473 1432 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1433 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1434 status = end_iterator_streams(trimmer_it);
1435 *reached_end = true;
1436 break;
1437 }
1438
188edac1
SM
1439 sstate->seen_clock_snapshot = true;
1440
7de0e49a
PP
1441 push_message(trimmer_it, msg);
1442 msg = NULL;
1443 break;
188edac1 1444
7de0e49a 1445 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
188edac1
SM
1446 /*
1447 * Packet beginning messages won't have a clock snapshot if
1448 * stream_class->packets_have_beginning_default_clock_snapshot
1449 * is false. But for now, assume they always do.
1450 */
1451 BT_ASSERT(ns_from_origin);
1452 BT_ASSERT(!sstate->cur_packet);
1453
91d81473 1454 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1455 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1456 status = end_iterator_streams(trimmer_it);
1457 *reached_end = true;
1458 break;
1459 }
1460
7de0e49a
PP
1461 sstate->cur_packet =
1462 bt_message_packet_beginning_borrow_packet_const(msg);
1463 bt_packet_get_ref(sstate->cur_packet);
188edac1
SM
1464
1465 sstate->seen_clock_snapshot = true;
1466
7de0e49a
PP
1467 push_message(trimmer_it, msg);
1468 msg = NULL;
1469 break;
188edac1 1470
7de0e49a 1471 case BT_MESSAGE_TYPE_PACKET_END:
188edac1
SM
1472 /*
1473 * Packet end messages won't have a clock snapshot if
1474 * stream_class->packets_have_end_default_clock_snapshot
1475 * is false. But for now, assume they always do.
1476 */
1477 BT_ASSERT(ns_from_origin);
1478 BT_ASSERT(sstate->cur_packet);
7de0e49a 1479
91d81473 1480 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
188edac1 1481 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1482 status = end_iterator_streams(trimmer_it);
1483 *reached_end = true;
1484 break;
1485 }
1486
7de0e49a 1487 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
188edac1
SM
1488
1489 sstate->seen_clock_snapshot = true;
1490
7de0e49a
PP
1491 push_message(trimmer_it, msg);
1492 msg = NULL;
1493 break;
188edac1 1494
7de0e49a
PP
1495 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1496 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1497 {
1498 /*
1499 * `ns_from_origin` is the message's time range's
1500 * beginning time here.
1501 */
1502 int64_t end_ns_from_origin;
1503 const bt_clock_snapshot *end_cs;
1504
188edac1
SM
1505 BT_ASSERT(ns_from_origin);
1506
1507 sstate->seen_clock_snapshot = true;
1508
7de0e49a
PP
1509 if (bt_message_get_type(msg) ==
1510 BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
1511 /*
1512 * Safe to ignore the return value because we
1513 * know there's a default clock and it's always
1514 * known.
1515 */
9b24b6aa 1516 end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 1517 msg);
7de0e49a
PP
1518 } else {
1519 /*
1520 * Safe to ignore the return value because we
1521 * know there's a default clock and it's always
1522 * known.
1523 */
9b24b6aa 1524 end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 1525 msg);
7de0e49a
PP
1526 }
1527
1528 if (bt_clock_snapshot_get_ns_from_origin(end_cs,
1529 &end_ns_from_origin)) {
d24d5663 1530 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1531 goto end;
1532 }
1533
7de0e49a 1534 if (!trimmer_it->end.is_infinite &&
188edac1 1535 *ns_from_origin > trimmer_it->end.ns_from_origin) {
7de0e49a
PP
1536 status = end_iterator_streams(trimmer_it);
1537 *reached_end = true;
1538 break;
1539 }
1540
1541 if (!trimmer_it->end.is_infinite &&
1542 end_ns_from_origin > trimmer_it->end.ns_from_origin) {
1543 /*
1544 * This message's end time is outside the
1545 * trimming time range: replace it with a new
1546 * message having an end time equal to the
1547 * trimming time range's end and without a
1548 * count.
1549 */
1550 const bt_clock_class *clock_class =
1551 bt_clock_snapshot_borrow_clock_class_const(
1552 end_cs);
1553 const bt_clock_snapshot *begin_cs;
1554 bt_message *new_msg;
1555 uint64_t end_raw_value;
1556
1557 ret = clock_raw_value_from_ns_from_origin(clock_class,
1558 trimmer_it->end.ns_from_origin, &end_raw_value);
1559 if (ret) {
d24d5663 1560 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1561 goto end;
1562 }
1563
1564 if (msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
9b24b6aa 1565 begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1566 msg);
7de0e49a
PP
1567 new_msg = bt_message_discarded_events_create_with_default_clock_snapshots(
1568 trimmer_it->self_msg_iter,
1569 sstate->stream,
1570 bt_clock_snapshot_get_value(begin_cs),
1571 end_raw_value);
1572 } else {
9b24b6aa 1573 begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1574 msg);
7de0e49a
PP
1575 new_msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
1576 trimmer_it->self_msg_iter,
1577 sstate->stream,
1578 bt_clock_snapshot_get_value(begin_cs),
1579 end_raw_value);
1580 }
1581
1582 if (!new_msg) {
d24d5663 1583 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
7de0e49a
PP
1584 goto end;
1585 }
1586
1587 /* Replace the original message */
1588 BT_MESSAGE_MOVE_REF(msg, new_msg);
1589 }
1590
7de0e49a
PP
1591 push_message(trimmer_it, msg);
1592 msg = NULL;
1593 break;
1594 }
188edac1
SM
1595
1596 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1597 /*
1598 * If this message has a time and this time is greater than the
1599 * trimmer's end bound, it triggers the end of the trim window.
1600 */
1601 if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite &&
1602 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1603 status = end_iterator_streams(trimmer_it);
1604 *reached_end = true;
1605 break;
1606 }
1607
188edac1
SM
1608 /* Learn about this stream. */
1609 status = create_stream_state_entry(trimmer_it, stream, &sstate);
1610 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
1611 goto end;
7de0e49a
PP
1612 }
1613
188edac1
SM
1614 if (ns_from_origin) {
1615 sstate->seen_clock_snapshot = true;
7de0e49a
PP
1616 }
1617
5b7b55be
SM
1618 push_message(trimmer_it, msg);
1619 msg = NULL;
7de0e49a
PP
1620 break;
1621 case BT_MESSAGE_TYPE_STREAM_END:
188edac1
SM
1622 {
1623 gboolean removed;
1624
5b7b55be 1625 /*
188edac1
SM
1626 * If this message has a time and this time is greater than the
1627 * trimmer's end bound, it triggers the end of the trim window.
5b7b55be 1628 */
188edac1
SM
1629 if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite &&
1630 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1631 status = end_iterator_streams(trimmer_it);
1632 *reached_end = true;
1633 break;
7de0e49a 1634 }
188edac1
SM
1635
1636 /*
1637 * Either the stream end message's time is within the trimmer's
1638 * bounds, or it doesn't have a time. In both cases, pass
1639 * the message unmodified.
1640 */
1641 push_message(trimmer_it, msg);
1642 msg = NULL;
1643
1644 /* Forget about this stream. */
1645 removed = g_hash_table_remove(trimmer_it->stream_states, sstate->stream);
1646 BT_ASSERT(removed);
7de0e49a 1647 break;
188edac1 1648 }
7de0e49a
PP
1649 default:
1650 break;
1651 }
1652
1653end:
1654 /* We release the message's reference whatever the outcome */
1655 bt_message_put_ref(msg);
188edac1 1656 return status;
7de0e49a
PP
1657}
1658
1659/*
1660 * Handles an input message. This _could_ make the iterator's output
1661 * message queue grow; this could also consume the message without
1662 * pushing anything to this queue, only modifying the stream state.
1663 *
1664 * This function consumes the `msg` reference, _whatever the outcome_.
1665 *
1666 * This function sets `reached_end` if handling this message made the
1667 * iterator reach the end of the trimming range. Note that the output
1668 * message queue could contain messages even if this function sets
1669 * `reached_end`.
1670 */
1671static inline
d24d5663 1672bt_component_class_message_iterator_next_method_status handle_message(
7de0e49a
PP
1673 struct trimmer_iterator *trimmer_it, const bt_message *msg,
1674 bool *reached_end)
1675{
d24d5663 1676 bt_component_class_message_iterator_next_method_status status;
7de0e49a
PP
1677 const bt_stream *stream = NULL;
1678 int64_t ns_from_origin = INT64_MIN;
4af85094 1679 bool has_ns_from_origin = false;
7de0e49a 1680 int ret;
7de0e49a
PP
1681
1682 /* Find message's associated stream */
1683 switch (bt_message_get_type(msg)) {
1684 case BT_MESSAGE_TYPE_EVENT:
1685 stream = bt_event_borrow_stream_const(
1686 bt_message_event_borrow_event_const(msg));
1687 break;
1688 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1689 stream = bt_packet_borrow_stream_const(
1690 bt_message_packet_beginning_borrow_packet_const(msg));
1691 break;
1692 case BT_MESSAGE_TYPE_PACKET_END:
1693 stream = bt_packet_borrow_stream_const(
1694 bt_message_packet_end_borrow_packet_const(msg));
1695 break;
1696 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1697 stream = bt_message_discarded_events_borrow_stream_const(msg);
1698 break;
1699 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1700 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1701 break;
7de0e49a
PP
1702 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1703 stream = bt_message_stream_beginning_borrow_stream_const(msg);
1704 break;
1705 case BT_MESSAGE_TYPE_STREAM_END:
1706 stream = bt_message_stream_end_borrow_stream_const(msg);
1707 break;
1708 default:
1709 break;
1710 }
1711
7de0e49a 1712 /* Retrieve the message's time */
188edac1 1713 ret = get_msg_ns_from_origin(msg, &ns_from_origin, &has_ns_from_origin);
91d81473 1714 if (G_UNLIKELY(ret)) {
d24d5663 1715 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
7de0e49a
PP
1716 goto end;
1717 }
1718
188edac1 1719 if (G_LIKELY(stream)) {
7de0e49a 1720 /* Message associated to a stream */
188edac1
SM
1721 status = handle_message_with_stream(trimmer_it, msg,
1722 stream, has_ns_from_origin ? &ns_from_origin : NULL, reached_end);
7de0e49a
PP
1723
1724 /*
1725 * handle_message_with_stream_state() unconditionally
1726 * consumes `msg`.
1727 */
1728 msg = NULL;
1729 } else {
1730 /*
1731 * Message not associated to a stream (message iterator
1732 * inactivity).
1733 */
91d81473 1734 if (G_UNLIKELY(ns_from_origin > trimmer_it->end.ns_from_origin)) {
7de0e49a
PP
1735 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1736 status = end_iterator_streams(trimmer_it);
1737 *reached_end = true;
1738 } else {
1739 push_message(trimmer_it, msg);
d24d5663 1740 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1741 msg = NULL;
1742 }
1743 }
1744
1745end:
1746 /* We release the message's reference whatever the outcome */
1747 bt_message_put_ref(msg);
1748 return status;
1749}
1750
1751static inline
1752void fill_message_array_from_output_messages(
1753 struct trimmer_iterator *trimmer_it,
1754 bt_message_array_const msgs, uint64_t capacity, uint64_t *count)
1755{
1756 *count = 0;
1757
1758 /*
1759 * Move auto-seek messages to the output array (which is this
1760 * iterator's base message array).
1761 */
1762 while (capacity > 0 && !g_queue_is_empty(trimmer_it->output_messages)) {
1763 msgs[*count] = pop_message(trimmer_it);
1764 capacity--;
1765 (*count)++;
1766 }
1767
98b15851 1768 BT_ASSERT_DBG(*count > 0);
7de0e49a
PP
1769}
1770
1771static inline
d24d5663 1772bt_component_class_message_iterator_next_method_status state_ending(
7de0e49a
PP
1773 struct trimmer_iterator *trimmer_it,
1774 bt_message_array_const msgs, uint64_t capacity,
1775 uint64_t *count)
1776{
d24d5663
PP
1777 bt_component_class_message_iterator_next_method_status status =
1778 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1779
1780 if (g_queue_is_empty(trimmer_it->output_messages)) {
1781 trimmer_it->state = TRIMMER_ITERATOR_STATE_ENDED;
d24d5663 1782 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
7de0e49a
PP
1783 goto end;
1784 }
1785
1786 fill_message_array_from_output_messages(trimmer_it, msgs,
1787 capacity, count);
1788
1789end:
1790 return status;
1791}
1792
1793static inline
d24d5663
PP
1794bt_component_class_message_iterator_next_method_status
1795state_trim(struct trimmer_iterator *trimmer_it,
7de0e49a
PP
1796 bt_message_array_const msgs, uint64_t capacity,
1797 uint64_t *count)
1798{
d24d5663
PP
1799 bt_component_class_message_iterator_next_method_status status =
1800 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
7de0e49a
PP
1801 bt_message_array_const my_msgs;
1802 uint64_t my_count;
1803 uint64_t i;
1804 bool reached_end = false;
1805
1806 while (g_queue_is_empty(trimmer_it->output_messages)) {
1807 status = (int) bt_self_component_port_input_message_iterator_next(
1808 trimmer_it->upstream_iter, &my_msgs, &my_count);
d24d5663
PP
1809 if (G_UNLIKELY(status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK)) {
1810 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) {
7de0e49a 1811 status = end_iterator_streams(trimmer_it);
d24d5663 1812 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1813 goto end;
1814 }
1815
1816 trimmer_it->state =
1817 TRIMMER_ITERATOR_STATE_ENDING;
1818 status = state_ending(trimmer_it, msgs,
1819 capacity, count);
1820 }
1821
1822 goto end;
1823 }
1824
98b15851 1825 BT_ASSERT_DBG(my_count > 0);
7de0e49a
PP
1826
1827 for (i = 0; i < my_count; i++) {
1828 status = handle_message(trimmer_it, my_msgs[i],
1829 &reached_end);
1830
1831 /*
1832 * handle_message() unconditionally consumes the
1833 * message reference.
1834 */
1835 my_msgs[i] = NULL;
1836
91d81473 1837 if (G_UNLIKELY(status !=
d24d5663 1838 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK)) {
7de0e49a
PP
1839 put_messages(my_msgs, my_count);
1840 goto end;
1841 }
1842
91d81473 1843 if (G_UNLIKELY(reached_end)) {
7de0e49a
PP
1844 /*
1845 * This message's time was passed the
1846 * trimming time range's end time: we
1847 * are done. Their might still be
1848 * messages in the output message queue,
1849 * so move to the "ending" state and
1850 * apply it immediately since
1851 * state_trim() is called within the
1852 * "next" method.
1853 */
1854 put_messages(my_msgs, my_count);
1855 trimmer_it->state =
1856 TRIMMER_ITERATOR_STATE_ENDING;
1857 status = state_ending(trimmer_it, msgs,
1858 capacity, count);
1859 goto end;
1860 }
1861 }
1862 }
1863
1864 /*
1865 * There's at least one message in the output message queue:
1866 * move the messages to the output message array.
1867 */
98b15851 1868 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it->output_messages));
7de0e49a
PP
1869 fill_message_array_from_output_messages(trimmer_it, msgs,
1870 capacity, count);
1871
1872end:
1873 return status;
1874}
1875
1876BT_HIDDEN
d24d5663 1877bt_component_class_message_iterator_next_method_status trimmer_msg_iter_next(
7de0e49a
PP
1878 bt_self_message_iterator *self_msg_iter,
1879 bt_message_array_const msgs, uint64_t capacity,
1880 uint64_t *count)
1881{
1882 struct trimmer_iterator *trimmer_it =
1883 bt_self_message_iterator_get_data(self_msg_iter);
d24d5663
PP
1884 bt_component_class_message_iterator_next_method_status status =
1885 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
7de0e49a 1886
98b15851 1887 BT_ASSERT_DBG(trimmer_it);
7de0e49a 1888
91d81473 1889 if (G_LIKELY(trimmer_it->state == TRIMMER_ITERATOR_STATE_TRIM)) {
7de0e49a 1890 status = state_trim(trimmer_it, msgs, capacity, count);
d24d5663 1891 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1892 goto end;
1893 }
1894 } else {
1895 switch (trimmer_it->state) {
1896 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN:
1897 status = state_set_trimmer_iterator_bounds(trimmer_it);
d24d5663 1898 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1899 goto end;
1900 }
1901
1902 status = state_seek_initially(trimmer_it);
d24d5663 1903 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1904 goto end;
1905 }
1906
1907 status = state_trim(trimmer_it, msgs, capacity, count);
d24d5663 1908 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1909 goto end;
1910 }
1911
1912 break;
1913 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY:
1914 status = state_seek_initially(trimmer_it);
d24d5663 1915 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1916 goto end;
1917 }
1918
1919 status = state_trim(trimmer_it, msgs, capacity, count);
d24d5663 1920 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1921 goto end;
1922 }
1923
1924 break;
1925 case TRIMMER_ITERATOR_STATE_ENDING:
1926 status = state_ending(trimmer_it, msgs, capacity,
1927 count);
d24d5663 1928 if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
7de0e49a
PP
1929 goto end;
1930 }
1931
1932 break;
1933 case TRIMMER_ITERATOR_STATE_ENDED:
d24d5663 1934 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
7de0e49a
PP
1935 break;
1936 default:
1937 abort();
1938 }
1939 }
1940
1941end:
1942 return status;
1943}
1944
1945BT_HIDDEN
1946void trimmer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
1947{
1948 struct trimmer_iterator *trimmer_it =
1949 bt_self_message_iterator_get_data(self_msg_iter);
1950
1951 BT_ASSERT(trimmer_it);
1952 destroy_trimmer_iterator(trimmer_it);
1953}
This page took 0.148646 seconds and 4 git commands to generate.