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