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