a65b2ecc881fcd5928e969a353a35bae9a3f58bb
[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 {
788 const bt_packet *packet = bt_message_packet_beginning_borrow_packet_const(msg);
789 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
790 const bt_stream_class *stream_class = bt_stream_borrow_class_const(stream);
791
792 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(stream_class)) {
793 goto no_clock_snapshot;
794 }
795
796 clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
797 msg);
798 break;
799 }
800 case BT_MESSAGE_TYPE_PACKET_END:
801 {
802 const bt_packet *packet = bt_message_packet_end_borrow_packet_const(msg);
803 const bt_stream *stream = bt_packet_borrow_stream_const(packet);
804 const bt_stream_class *stream_class = bt_stream_borrow_class_const(stream);
805
806 if (!bt_stream_class_packets_have_end_default_clock_snapshot(stream_class)) {
807 goto no_clock_snapshot;
808 }
809
810 clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const(
811 msg);
812 break;
813 }
814 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
815 {
816 enum bt_message_stream_clock_snapshot_state cs_state;
817
818 clock_class =
819 bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg);
820 if (G_UNLIKELY(!clock_class)) {
821 goto error;
822 }
823
824 cs_state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &clock_snapshot);
825 if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
826 goto no_clock_snapshot;
827 }
828
829 break;
830 }
831 case BT_MESSAGE_TYPE_STREAM_END:
832 {
833 enum bt_message_stream_clock_snapshot_state cs_state;
834
835 clock_class =
836 bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg);
837 if (G_UNLIKELY(!clock_class)) {
838 goto error;
839 }
840
841 cs_state = bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &clock_snapshot);
842 if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
843 goto no_clock_snapshot;
844 }
845
846 break;
847 }
848 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
849 {
850 const bt_stream *stream = bt_message_discarded_events_borrow_stream_const(msg);
851 const bt_stream_class *stream_class = bt_stream_borrow_class_const(stream);
852
853 if (!bt_stream_class_discarded_events_have_default_clock_snapshots(stream_class)) {
854 goto no_clock_snapshot;
855 }
856
857 clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
858 msg);
859 break;
860 }
861 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
862 {
863 const bt_stream *stream = bt_message_discarded_packets_borrow_stream_const(msg);
864 const bt_stream_class *stream_class = bt_stream_borrow_class_const(stream);
865
866 if (!bt_stream_class_discarded_packets_have_default_clock_snapshots(stream_class)) {
867 goto no_clock_snapshot;
868 }
869
870 clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
871 msg);
872 break;
873 }
874 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
875 clock_snapshot =
876 bt_message_message_iterator_inactivity_borrow_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_message_iterator_class_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_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_message_iterator_class_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_message_iterator_class_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_message_iterator_can_seek_beginning(
1045 trimmer_it->upstream_iter, &can_seek);
1046 if (status != BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1059 goto end;
1060 }
1061
1062 status = (int) bt_message_iterator_seek_beginning(
1063 trimmer_it->upstream_iter);
1064 } else {
1065 bt_bool can_seek;
1066
1067 status = (int) bt_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1086 goto end;
1087 }
1088
1089 status = (int) bt_message_iterator_seek_ns_from_origin(
1090 trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin);
1091 }
1092
1093 if (status == BT_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_next_method_status
1130 end_stream(struct trimmer_iterator *trimmer_it,
1131 struct trimmer_iterator_stream_state *sstate)
1132 {
1133 bt_message_iterator_class_next_method_status status =
1134 BT_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_next_method_status end_iterator_streams(
1220 struct trimmer_iterator *trimmer_it)
1221 {
1222 bt_message_iterator_class_next_method_status status =
1223 BT_MESSAGE_ITERATOR_CLASS_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_message_iterator_class_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_message_iterator_class_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_MESSAGE_ITERATOR_CLASS_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_supports_packets(sc)) {
1302 if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(
1303 sc)) {
1304 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1305 "Unsupported stream: packets have no beginning clock snapshot: "
1306 "stream-addr=%p, "
1307 "stream-id=%" PRIu64 ", "
1308 "stream-name=\"%s\"",
1309 stream, bt_stream_get_id(stream),
1310 bt_stream_get_name(stream));
1311 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1312 goto end;
1313 }
1314
1315 if (!bt_stream_class_packets_have_end_default_clock_snapshot(
1316 sc)) {
1317 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1318 "Unsupported stream: packets have no end clock snapshot: "
1319 "stream-addr=%p, "
1320 "stream-id=%" PRIu64 ", "
1321 "stream-name=\"%s\"",
1322 stream, bt_stream_get_id(stream),
1323 bt_stream_get_name(stream));
1324 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1325 goto end;
1326 }
1327
1328 if (bt_stream_class_supports_discarded_packets(sc) &&
1329 !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) {
1330 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1331 "Unsupported stream: discarded packets "
1332 "have no clock snapshots: "
1333 "stream-addr=%p, "
1334 "stream-id=%" PRIu64 ", "
1335 "stream-name=\"%s\"",
1336 stream, bt_stream_get_id(stream),
1337 bt_stream_get_name(stream));
1338 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1339 goto end;
1340 }
1341 }
1342
1343 if (bt_stream_class_supports_discarded_events(sc) &&
1344 !bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) {
1345 BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp,
1346 "Unsupported stream: discarded events have no clock snapshots: "
1347 "stream-addr=%p, "
1348 "stream-id=%" PRIu64 ", "
1349 "stream-name=\"%s\"",
1350 stream, bt_stream_get_id(stream),
1351 bt_stream_get_name(stream));
1352 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1353 goto end;
1354 }
1355
1356 sstate = g_new0(struct trimmer_iterator_stream_state, 1);
1357 if (!sstate) {
1358 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
1359 goto end;
1360 }
1361
1362 sstate->stream = stream;
1363
1364 g_hash_table_insert(trimmer_it->stream_states, (void *) stream, sstate);
1365
1366 *stream_state = sstate;
1367
1368 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1369
1370 end:
1371 return status;
1372 }
1373
1374 static
1375 struct trimmer_iterator_stream_state *get_stream_state_entry(
1376 struct trimmer_iterator *trimmer_it,
1377 const struct bt_stream *stream)
1378 {
1379 struct trimmer_iterator_stream_state *sstate;
1380
1381 BT_ASSERT_DBG(stream);
1382 sstate = g_hash_table_lookup(trimmer_it->stream_states, stream);
1383 BT_ASSERT_DBG(sstate);
1384
1385 return sstate;
1386 }
1387
1388 /*
1389 * Handles a message which is associated to a given stream state. This
1390 * _could_ make the iterator's output message queue grow; this could
1391 * also consume the message without pushing anything to this queue, only
1392 * modifying the stream state.
1393 *
1394 * This function consumes the `msg` reference, _whatever the outcome_.
1395 *
1396 * If non-NULL, `ns_from_origin` is the message's time, as given by
1397 * get_msg_ns_from_origin(). If NULL, the message doesn't have a time.
1398 *
1399 * This function sets `reached_end` if handling this message made the
1400 * iterator reach the end of the trimming range. Note that the output
1401 * message queue could contain messages even if this function sets
1402 * `reached_end`.
1403 */
1404 static
1405 bt_message_iterator_class_next_method_status
1406 handle_message_with_stream(
1407 struct trimmer_iterator *trimmer_it, const bt_message *msg,
1408 const struct bt_stream *stream, const int64_t *ns_from_origin,
1409 bool *reached_end)
1410 {
1411 bt_message_iterator_class_next_method_status status =
1412 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1413 bt_message_type msg_type = bt_message_get_type(msg);
1414 int ret;
1415 struct trimmer_iterator_stream_state *sstate = NULL;
1416
1417 /*
1418 * Retrieve the stream's state - except if the message is stream
1419 * beginning, in which case we don't know about about this stream yet.
1420 */
1421 if (msg_type != BT_MESSAGE_TYPE_STREAM_BEGINNING) {
1422 sstate = get_stream_state_entry(trimmer_it, stream);
1423 }
1424
1425 switch (msg_type) {
1426 case BT_MESSAGE_TYPE_EVENT:
1427 /*
1428 * Event messages always have a clock snapshot if the stream
1429 * class has a clock class. And we know it has, otherwise we
1430 * couldn't be using the trimmer component.
1431 */
1432 BT_ASSERT_DBG(ns_from_origin);
1433
1434 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
1435 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1436 status = end_iterator_streams(trimmer_it);
1437 *reached_end = true;
1438 break;
1439 }
1440
1441 sstate->seen_clock_snapshot = true;
1442
1443 push_message(trimmer_it, msg);
1444 msg = NULL;
1445 break;
1446
1447 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1448 /*
1449 * Packet beginning messages won't have a clock snapshot if
1450 * stream_class->packets_have_beginning_default_clock_snapshot
1451 * is false. But for now, assume they always do.
1452 */
1453 BT_ASSERT(ns_from_origin);
1454 BT_ASSERT(!sstate->cur_packet);
1455
1456 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
1457 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1458 status = end_iterator_streams(trimmer_it);
1459 *reached_end = true;
1460 break;
1461 }
1462
1463 sstate->cur_packet =
1464 bt_message_packet_beginning_borrow_packet_const(msg);
1465 bt_packet_get_ref(sstate->cur_packet);
1466
1467 sstate->seen_clock_snapshot = true;
1468
1469 push_message(trimmer_it, msg);
1470 msg = NULL;
1471 break;
1472
1473 case BT_MESSAGE_TYPE_PACKET_END:
1474 /*
1475 * Packet end messages won't have a clock snapshot if
1476 * stream_class->packets_have_end_default_clock_snapshot
1477 * is false. But for now, assume they always do.
1478 */
1479 BT_ASSERT(ns_from_origin);
1480 BT_ASSERT(sstate->cur_packet);
1481
1482 if (G_UNLIKELY(!trimmer_it->end.is_infinite &&
1483 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1484 status = end_iterator_streams(trimmer_it);
1485 *reached_end = true;
1486 break;
1487 }
1488
1489 BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet);
1490
1491 sstate->seen_clock_snapshot = true;
1492
1493 push_message(trimmer_it, msg);
1494 msg = NULL;
1495 break;
1496
1497 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1498 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1499 {
1500 /*
1501 * `ns_from_origin` is the message's time range's
1502 * beginning time here.
1503 */
1504 int64_t end_ns_from_origin;
1505 const bt_clock_snapshot *end_cs;
1506
1507 BT_ASSERT(ns_from_origin);
1508
1509 sstate->seen_clock_snapshot = true;
1510
1511 if (bt_message_get_type(msg) ==
1512 BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
1513 /*
1514 * Safe to ignore the return value because we
1515 * know there's a default clock and it's always
1516 * known.
1517 */
1518 end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1519 msg);
1520 } else {
1521 /*
1522 * Safe to ignore the return value because we
1523 * know there's a default clock and it's always
1524 * known.
1525 */
1526 end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1527 msg);
1528 }
1529
1530 if (bt_clock_snapshot_get_ns_from_origin(end_cs,
1531 &end_ns_from_origin)) {
1532 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1533 goto end;
1534 }
1535
1536 if (!trimmer_it->end.is_infinite &&
1537 *ns_from_origin > trimmer_it->end.ns_from_origin) {
1538 status = end_iterator_streams(trimmer_it);
1539 *reached_end = true;
1540 break;
1541 }
1542
1543 if (!trimmer_it->end.is_infinite &&
1544 end_ns_from_origin > trimmer_it->end.ns_from_origin) {
1545 /*
1546 * This message's end time is outside the
1547 * trimming time range: replace it with a new
1548 * message having an end time equal to the
1549 * trimming time range's end and without a
1550 * count.
1551 */
1552 const bt_clock_class *clock_class =
1553 bt_clock_snapshot_borrow_clock_class_const(
1554 end_cs);
1555 const bt_clock_snapshot *begin_cs;
1556 bt_message *new_msg;
1557 uint64_t end_raw_value;
1558
1559 ret = clock_raw_value_from_ns_from_origin(clock_class,
1560 trimmer_it->end.ns_from_origin, &end_raw_value);
1561 if (ret) {
1562 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1563 goto end;
1564 }
1565
1566 if (msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) {
1567 begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1568 msg);
1569 new_msg = bt_message_discarded_events_create_with_default_clock_snapshots(
1570 trimmer_it->self_msg_iter,
1571 sstate->stream,
1572 bt_clock_snapshot_get_value(begin_cs),
1573 end_raw_value);
1574 } else {
1575 begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1576 msg);
1577 new_msg = bt_message_discarded_packets_create_with_default_clock_snapshots(
1578 trimmer_it->self_msg_iter,
1579 sstate->stream,
1580 bt_clock_snapshot_get_value(begin_cs),
1581 end_raw_value);
1582 }
1583
1584 if (!new_msg) {
1585 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
1586 goto end;
1587 }
1588
1589 /* Replace the original message */
1590 BT_MESSAGE_MOVE_REF(msg, new_msg);
1591 }
1592
1593 push_message(trimmer_it, msg);
1594 msg = NULL;
1595 break;
1596 }
1597
1598 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1599 /*
1600 * If this message has a time and this time is greater than the
1601 * trimmer's end bound, it triggers the end of the trim window.
1602 */
1603 if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite &&
1604 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1605 status = end_iterator_streams(trimmer_it);
1606 *reached_end = true;
1607 break;
1608 }
1609
1610 /* Learn about this stream. */
1611 status = create_stream_state_entry(trimmer_it, stream, &sstate);
1612 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1613 goto end;
1614 }
1615
1616 if (ns_from_origin) {
1617 sstate->seen_clock_snapshot = true;
1618 }
1619
1620 push_message(trimmer_it, msg);
1621 msg = NULL;
1622 break;
1623 case BT_MESSAGE_TYPE_STREAM_END:
1624 {
1625 gboolean removed;
1626
1627 /*
1628 * If this message has a time and this time is greater than the
1629 * trimmer's end bound, it triggers the end of the trim window.
1630 */
1631 if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite &&
1632 *ns_from_origin > trimmer_it->end.ns_from_origin)) {
1633 status = end_iterator_streams(trimmer_it);
1634 *reached_end = true;
1635 break;
1636 }
1637
1638 /*
1639 * Either the stream end message's time is within the trimmer's
1640 * bounds, or it doesn't have a time. In both cases, pass
1641 * the message unmodified.
1642 */
1643 push_message(trimmer_it, msg);
1644 msg = NULL;
1645
1646 /* Forget about this stream. */
1647 removed = g_hash_table_remove(trimmer_it->stream_states, sstate->stream);
1648 BT_ASSERT(removed);
1649 break;
1650 }
1651 default:
1652 break;
1653 }
1654
1655 end:
1656 /* We release the message's reference whatever the outcome */
1657 bt_message_put_ref(msg);
1658 return status;
1659 }
1660
1661 /*
1662 * Handles an input message. This _could_ make the iterator's output
1663 * message queue grow; this could also consume the message without
1664 * pushing anything to this queue, only modifying the stream state.
1665 *
1666 * This function consumes the `msg` reference, _whatever the outcome_.
1667 *
1668 * This function sets `reached_end` if handling this message made the
1669 * iterator reach the end of the trimming range. Note that the output
1670 * message queue could contain messages even if this function sets
1671 * `reached_end`.
1672 */
1673 static inline
1674 bt_message_iterator_class_next_method_status handle_message(
1675 struct trimmer_iterator *trimmer_it, const bt_message *msg,
1676 bool *reached_end)
1677 {
1678 bt_message_iterator_class_next_method_status status;
1679 const bt_stream *stream = NULL;
1680 int64_t ns_from_origin = INT64_MIN;
1681 bool has_ns_from_origin = false;
1682 int ret;
1683
1684 /* Find message's associated stream */
1685 switch (bt_message_get_type(msg)) {
1686 case BT_MESSAGE_TYPE_EVENT:
1687 stream = bt_event_borrow_stream_const(
1688 bt_message_event_borrow_event_const(msg));
1689 break;
1690 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1691 stream = bt_packet_borrow_stream_const(
1692 bt_message_packet_beginning_borrow_packet_const(msg));
1693 break;
1694 case BT_MESSAGE_TYPE_PACKET_END:
1695 stream = bt_packet_borrow_stream_const(
1696 bt_message_packet_end_borrow_packet_const(msg));
1697 break;
1698 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1699 stream = bt_message_discarded_events_borrow_stream_const(msg);
1700 break;
1701 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1702 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1703 break;
1704 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1705 stream = bt_message_stream_beginning_borrow_stream_const(msg);
1706 break;
1707 case BT_MESSAGE_TYPE_STREAM_END:
1708 stream = bt_message_stream_end_borrow_stream_const(msg);
1709 break;
1710 default:
1711 break;
1712 }
1713
1714 /* Retrieve the message's time */
1715 ret = get_msg_ns_from_origin(msg, &ns_from_origin, &has_ns_from_origin);
1716 if (G_UNLIKELY(ret)) {
1717 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
1718 goto end;
1719 }
1720
1721 if (G_LIKELY(stream)) {
1722 /* Message associated to a stream */
1723 status = handle_message_with_stream(trimmer_it, msg,
1724 stream, has_ns_from_origin ? &ns_from_origin : NULL, reached_end);
1725
1726 /*
1727 * handle_message_with_stream_state() unconditionally
1728 * consumes `msg`.
1729 */
1730 msg = NULL;
1731 } else {
1732 /*
1733 * Message not associated to a stream (message iterator
1734 * inactivity).
1735 */
1736 if (G_UNLIKELY(ns_from_origin > trimmer_it->end.ns_from_origin)) {
1737 BT_MESSAGE_PUT_REF_AND_RESET(msg);
1738 status = end_iterator_streams(trimmer_it);
1739 *reached_end = true;
1740 } else {
1741 push_message(trimmer_it, msg);
1742 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1743 msg = NULL;
1744 }
1745 }
1746
1747 end:
1748 /* We release the message's reference whatever the outcome */
1749 bt_message_put_ref(msg);
1750 return status;
1751 }
1752
1753 static inline
1754 void fill_message_array_from_output_messages(
1755 struct trimmer_iterator *trimmer_it,
1756 bt_message_array_const msgs, uint64_t capacity, uint64_t *count)
1757 {
1758 *count = 0;
1759
1760 /*
1761 * Move auto-seek messages to the output array (which is this
1762 * iterator's base message array).
1763 */
1764 while (capacity > 0 && !g_queue_is_empty(trimmer_it->output_messages)) {
1765 msgs[*count] = pop_message(trimmer_it);
1766 capacity--;
1767 (*count)++;
1768 }
1769
1770 BT_ASSERT_DBG(*count > 0);
1771 }
1772
1773 static inline
1774 bt_message_iterator_class_next_method_status state_ending(
1775 struct trimmer_iterator *trimmer_it,
1776 bt_message_array_const msgs, uint64_t capacity,
1777 uint64_t *count)
1778 {
1779 bt_message_iterator_class_next_method_status status =
1780 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1781
1782 if (g_queue_is_empty(trimmer_it->output_messages)) {
1783 trimmer_it->state = TRIMMER_ITERATOR_STATE_ENDED;
1784 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
1785 goto end;
1786 }
1787
1788 fill_message_array_from_output_messages(trimmer_it, msgs,
1789 capacity, count);
1790
1791 end:
1792 return status;
1793 }
1794
1795 static inline
1796 bt_message_iterator_class_next_method_status
1797 state_trim(struct trimmer_iterator *trimmer_it,
1798 bt_message_array_const msgs, uint64_t capacity,
1799 uint64_t *count)
1800 {
1801 bt_message_iterator_class_next_method_status status =
1802 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1803 bt_message_array_const my_msgs;
1804 uint64_t my_count;
1805 uint64_t i;
1806 bool reached_end = false;
1807
1808 while (g_queue_is_empty(trimmer_it->output_messages)) {
1809 status = (int) bt_message_iterator_next(
1810 trimmer_it->upstream_iter, &my_msgs, &my_count);
1811 if (G_UNLIKELY(status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) {
1812 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
1813 status = end_iterator_streams(trimmer_it);
1814 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1815 goto end;
1816 }
1817
1818 trimmer_it->state =
1819 TRIMMER_ITERATOR_STATE_ENDING;
1820 status = state_ending(trimmer_it, msgs,
1821 capacity, count);
1822 }
1823
1824 goto end;
1825 }
1826
1827 BT_ASSERT_DBG(my_count > 0);
1828
1829 for (i = 0; i < my_count; i++) {
1830 status = handle_message(trimmer_it, my_msgs[i],
1831 &reached_end);
1832
1833 /*
1834 * handle_message() unconditionally consumes the
1835 * message reference.
1836 */
1837 my_msgs[i] = NULL;
1838
1839 if (G_UNLIKELY(status !=
1840 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK)) {
1841 put_messages(my_msgs, my_count);
1842 goto end;
1843 }
1844
1845 if (G_UNLIKELY(reached_end)) {
1846 /*
1847 * This message's time was passed the
1848 * trimming time range's end time: we
1849 * are done. Their might still be
1850 * messages in the output message queue,
1851 * so move to the "ending" state and
1852 * apply it immediately since
1853 * state_trim() is called within the
1854 * "next" method.
1855 */
1856 put_messages(my_msgs, my_count);
1857 trimmer_it->state =
1858 TRIMMER_ITERATOR_STATE_ENDING;
1859 status = state_ending(trimmer_it, msgs,
1860 capacity, count);
1861 goto end;
1862 }
1863 }
1864 }
1865
1866 /*
1867 * There's at least one message in the output message queue:
1868 * move the messages to the output message array.
1869 */
1870 BT_ASSERT_DBG(!g_queue_is_empty(trimmer_it->output_messages));
1871 fill_message_array_from_output_messages(trimmer_it, msgs,
1872 capacity, count);
1873
1874 end:
1875 return status;
1876 }
1877
1878 BT_HIDDEN
1879 bt_message_iterator_class_next_method_status trimmer_msg_iter_next(
1880 bt_self_message_iterator *self_msg_iter,
1881 bt_message_array_const msgs, uint64_t capacity,
1882 uint64_t *count)
1883 {
1884 struct trimmer_iterator *trimmer_it =
1885 bt_self_message_iterator_get_data(self_msg_iter);
1886 bt_message_iterator_class_next_method_status status =
1887 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
1888
1889 BT_ASSERT_DBG(trimmer_it);
1890
1891 if (G_LIKELY(trimmer_it->state == TRIMMER_ITERATOR_STATE_TRIM)) {
1892 status = state_trim(trimmer_it, msgs, capacity, count);
1893 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1894 goto end;
1895 }
1896 } else {
1897 switch (trimmer_it->state) {
1898 case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN:
1899 status = state_set_trimmer_iterator_bounds(trimmer_it);
1900 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1901 goto end;
1902 }
1903
1904 status = state_seek_initially(trimmer_it);
1905 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1906 goto end;
1907 }
1908
1909 status = state_trim(trimmer_it, msgs, capacity, count);
1910 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1911 goto end;
1912 }
1913
1914 break;
1915 case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY:
1916 status = state_seek_initially(trimmer_it);
1917 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1918 goto end;
1919 }
1920
1921 status = state_trim(trimmer_it, msgs, capacity, count);
1922 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1923 goto end;
1924 }
1925
1926 break;
1927 case TRIMMER_ITERATOR_STATE_ENDING:
1928 status = state_ending(trimmer_it, msgs, capacity,
1929 count);
1930 if (status != BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1931 goto end;
1932 }
1933
1934 break;
1935 case TRIMMER_ITERATOR_STATE_ENDED:
1936 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
1937 break;
1938 default:
1939 bt_common_abort();
1940 }
1941 }
1942
1943 end:
1944 return status;
1945 }
1946
1947 BT_HIDDEN
1948 void trimmer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter)
1949 {
1950 struct trimmer_iterator *trimmer_it =
1951 bt_self_message_iterator_get_data(self_msg_iter);
1952
1953 BT_ASSERT(trimmer_it);
1954 destroy_trimmer_iterator(trimmer_it);
1955 }
This page took 0.100552 seconds and 3 git commands to generate.