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