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