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