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