Commit | Line | Data |
---|---|---|
cab3f160 | 1 | /* |
cab3f160 | 2 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
781751d8 | 3 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
cab3f160 JG |
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 | ||
19f15b11 | 24 | #define BT_LOG_OUTPUT_LEVEL (trimmer_comp->log_level) |
b03487ab | 25 | #define BT_LOG_TAG "PLUGIN/FLT.UTILS.TRIMMER" |
3fa1b6a3 | 26 | #include "logging/comp-logging.h" |
f4f9e43b | 27 | |
57952005 MJ |
28 | #include "compat/utc.h" |
29 | #include "compat/time.h" | |
71c5da58 | 30 | #include <babeltrace2/babeltrace.h> |
57952005 | 31 | #include "common/common.h" |
57952005 | 32 | #include "common/assert.h" |
781751d8 PP |
33 | #include <stdint.h> |
34 | #include <inttypes.h> | |
35 | #include <glib.h> | |
b7cbc799 | 36 | #include "compat/glib.h" |
781751d8 PP |
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; | |
19f15b11 | 75 | bt_logging_level log_level; |
b70dca78 | 76 | bt_self_component *self_comp; |
781751d8 PP |
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 { | |
781751d8 PP |
133 | /* Weak */ |
134 | const bt_stream *stream; | |
135 | ||
b7cbc799 SM |
136 | /* Have we seen a message with clock_snapshot going through this stream? */ |
137 | bool seen_clock_snapshot; | |
138 | ||
781751d8 PP |
139 | /* Owned by this (`NULL` initially and between packets) */ |
140 | const bt_packet *cur_packet; | |
781751d8 | 141 | }; |
cab3f160 JG |
142 | |
143 | static | |
781751d8 | 144 | void destroy_trimmer_comp(struct trimmer_comp *trimmer_comp) |
cab3f160 | 145 | { |
781751d8 PP |
146 | BT_ASSERT(trimmer_comp); |
147 | g_free(trimmer_comp); | |
cab3f160 JG |
148 | } |
149 | ||
150 | static | |
781751d8 PP |
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) | |
cab3f160 | 158 | { |
781751d8 PP |
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 | } | |
cab3f160 JG |
166 | } |
167 | ||
8f9023e8 SM |
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, ®ex_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 | */ | |
fc487522 | 192 | g_match_info_free(*match_info); |
8f9023e8 SM |
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 | ||
781751d8 PP |
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) | |
cab3f160 | 272 | { |
781751d8 PP |
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 | } | |
cab3f160 | 295 | |
781751d8 PP |
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; | |
cab3f160 JG |
304 | } |
305 | ||
528debdf MD |
306 | /* |
307 | * Parses a timestamp, figuring out its format. | |
308 | * | |
309 | * Returns a negative value if anything goes wrong. | |
310 | * | |
311 | * Expected formats: | |
312 | * | |
781751d8 PP |
313 | * YYYY-MM-DD hh:mm[:ss[.ns]] |
314 | * [hh:mm:]ss[.ns] | |
315 | * [-]s[.ns] | |
316 | * | |
317 | * TODO: Check overflows. | |
318 | */ | |
319 | static | |
19f15b11 PP |
320 | int set_bound_from_str(struct trimmer_comp *trimmer_comp, |
321 | const char *str, struct trimmer_bound *bound, bool is_gmt) | |
781751d8 | 322 | { |
8f9023e8 SM |
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; | |
781751d8 | 333 | int ret = 0; |
781751d8 | 334 | |
8f9023e8 SM |
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); | |
781751d8 | 339 | |
8f9023e8 | 340 | BT_ASSERT(match_count >= 6 && match_count <= 8); |
781751d8 | 341 | |
8f9023e8 SM |
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); | |
781751d8 | 347 | |
8f9023e8 SM |
348 | if (match_count >= 7) { |
349 | seconds = match_to_uint(match_info, 6); | |
350 | } | |
781751d8 | 351 | |
8f9023e8 SM |
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); | |
781751d8 | 357 | |
781751d8 PP |
358 | goto end; |
359 | } | |
360 | ||
8f9023e8 SM |
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 | ||
781751d8 PP |
372 | goto end; |
373 | } | |
374 | ||
8f9023e8 SM |
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 | ||
781751d8 PP |
390 | goto end; |
391 | } | |
392 | ||
8f9023e8 SM |
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 | ||
781751d8 | 419 | bound->is_set = true; |
8f9023e8 | 420 | |
781751d8 PP |
421 | goto end; |
422 | } | |
423 | ||
ccaf1ae5 SM |
424 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
425 | "Invalid date/time format: param=\"%s\"", str); | |
781751d8 PP |
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. | |
528debdf | 437 | */ |
44d3cbf0 | 438 | static |
19f15b11 PP |
439 | int set_bound_from_param(struct trimmer_comp *trimmer_comp, |
440 | const char *param_name, const bt_value *param, | |
781751d8 | 441 | struct trimmer_bound *bound, bool is_gmt) |
528debdf MD |
442 | { |
443 | int ret; | |
922fc2dd | 444 | const char *arg; |
781751d8 | 445 | char tmp_arg[64]; |
922fc2dd | 446 | |
68d9d039 | 447 | if (bt_value_is_signed_integer(param)) { |
60bbfc7c | 448 | int64_t value = bt_value_integer_signed_get(param); |
781751d8 PP |
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 { | |
ccaf1ae5 SM |
459 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
460 | "`%s` parameter must be an integer or a string value.", | |
922fc2dd | 461 | param_name); |
781751d8 PP |
462 | ret = -1; |
463 | goto end; | |
922fc2dd PP |
464 | } |
465 | ||
19f15b11 | 466 | ret = set_bound_from_str(trimmer_comp, arg, bound, is_gmt); |
781751d8 PP |
467 | |
468 | end: | |
469 | return ret; | |
470 | } | |
471 | ||
472 | static | |
19f15b11 PP |
473 | int validate_trimmer_bounds(struct trimmer_comp *trimmer_comp, |
474 | struct trimmer_bound *begin, struct trimmer_bound *end) | |
781751d8 PP |
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) { | |
ccaf1ae5 SM |
483 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
484 | "Trimming time range's beginning time is greater than end time: " | |
781751d8 PP |
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; | |
528debdf MD |
491 | } |
492 | ||
781751d8 | 493 | if (!begin->is_infinite && begin->ns_from_origin == INT64_MIN) { |
ccaf1ae5 SM |
494 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
495 | "Invalid trimming time range's beginning time: " | |
781751d8 PP |
496 | "ns-from-origin=%" PRId64, |
497 | begin->ns_from_origin); | |
498 | ret = -1; | |
499 | goto end; | |
500 | } | |
528debdf | 501 | |
781751d8 | 502 | if (!end->is_infinite && end->ns_from_origin == INT64_MIN) { |
ccaf1ae5 SM |
503 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
504 | "Invalid trimming time range's end time: " | |
781751d8 PP |
505 | "ns-from-origin=%" PRId64, |
506 | end->ns_from_origin); | |
507 | ret = -1; | |
508 | goto end; | |
509 | } | |
528debdf | 510 | |
781751d8 PP |
511 | end: |
512 | return ret; | |
528debdf MD |
513 | } |
514 | ||
515 | static | |
781751d8 PP |
516 | int init_trimmer_comp_from_params(struct trimmer_comp *trimmer_comp, |
517 | const bt_value *params) | |
44d3cbf0 | 518 | { |
781751d8 PP |
519 | const bt_value *value; |
520 | int ret = 0; | |
44d3cbf0 | 521 | |
8b45963b | 522 | BT_ASSERT(params); |
781751d8 | 523 | value = bt_value_map_borrow_entry_value_const(params, "gmt"); |
528debdf | 524 | if (value) { |
781751d8 | 525 | trimmer_comp->is_gmt = (bool) bt_value_bool_get(value); |
528debdf MD |
526 | } |
527 | ||
781751d8 | 528 | value = bt_value_map_borrow_entry_value_const(params, "begin"); |
528debdf | 529 | if (value) { |
19f15b11 | 530 | if (set_bound_from_param(trimmer_comp, "begin", value, |
781751d8 PP |
531 | &trimmer_comp->begin, trimmer_comp->is_gmt)) { |
532 | /* set_bound_from_param() logs errors */ | |
fb25b9e3 | 533 | ret = -1; |
922fc2dd | 534 | goto end; |
44d3cbf0 | 535 | } |
781751d8 PP |
536 | } else { |
537 | trimmer_comp->begin.is_infinite = true; | |
538 | trimmer_comp->begin.is_set = true; | |
44d3cbf0 | 539 | } |
528debdf | 540 | |
781751d8 | 541 | value = bt_value_map_borrow_entry_value_const(params, "end"); |
528debdf | 542 | if (value) { |
19f15b11 | 543 | if (set_bound_from_param(trimmer_comp, "end", value, |
781751d8 PP |
544 | &trimmer_comp->end, trimmer_comp->is_gmt)) { |
545 | /* set_bound_from_param() logs errors */ | |
fb25b9e3 | 546 | ret = -1; |
922fc2dd | 547 | goto end; |
528debdf | 548 | } |
781751d8 PP |
549 | } else { |
550 | trimmer_comp->end.is_infinite = true; | |
551 | trimmer_comp->end.is_set = true; | |
528debdf | 552 | } |
922fc2dd | 553 | |
528debdf | 554 | end: |
781751d8 PP |
555 | if (trimmer_comp->begin.is_set && trimmer_comp->end.is_set) { |
556 | /* validate_trimmer_bounds() logs errors */ | |
19f15b11 PP |
557 | ret = validate_trimmer_bounds(trimmer_comp, |
558 | &trimmer_comp->begin, &trimmer_comp->end); | |
55595636 | 559 | } |
781751d8 | 560 | |
528debdf | 561 | return ret; |
44d3cbf0 JG |
562 | } |
563 | ||
fb25b9e3 PP |
564 | bt_component_class_init_method_status trimmer_init( |
565 | bt_self_component_filter *self_comp_flt, | |
781751d8 | 566 | const bt_value *params, void *init_data) |
cab3f160 | 567 | { |
781751d8 | 568 | int ret; |
fb25b9e3 PP |
569 | bt_component_class_init_method_status status = |
570 | BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK; | |
571 | bt_self_component_add_port_status add_port_status; | |
781751d8 | 572 | struct trimmer_comp *trimmer_comp = create_trimmer_comp(); |
b70dca78 PP |
573 | bt_self_component *self_comp = |
574 | bt_self_component_filter_as_self_component(self_comp_flt); | |
781751d8 | 575 | if (!trimmer_comp) { |
fb25b9e3 | 576 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; |
781751d8 | 577 | goto error; |
cab3f160 JG |
578 | } |
579 | ||
19f15b11 | 580 | trimmer_comp->log_level = bt_component_get_logging_level( |
b70dca78 PP |
581 | bt_self_component_as_component(self_comp)); |
582 | trimmer_comp->self_comp = self_comp; | |
fb25b9e3 | 583 | add_port_status = bt_self_component_filter_add_input_port( |
b70dca78 | 584 | self_comp_flt, in_port_name, NULL, NULL); |
fb25b9e3 PP |
585 | switch (add_port_status) { |
586 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
587 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
b9d103be | 588 | goto error; |
fb25b9e3 PP |
589 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: |
590 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; | |
591 | goto error; | |
592 | default: | |
593 | break; | |
b9d103be PP |
594 | } |
595 | ||
fb25b9e3 | 596 | add_port_status = bt_self_component_filter_add_output_port( |
b70dca78 | 597 | self_comp_flt, "out", NULL, NULL); |
fb25b9e3 PP |
598 | switch (add_port_status) { |
599 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
600 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
b9d103be | 601 | goto error; |
fb25b9e3 PP |
602 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: |
603 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR; | |
604 | goto error; | |
605 | default: | |
606 | break; | |
b9d103be PP |
607 | } |
608 | ||
781751d8 PP |
609 | ret = init_trimmer_comp_from_params(trimmer_comp, params); |
610 | if (ret) { | |
fb25b9e3 | 611 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; |
cab3f160 JG |
612 | goto error; |
613 | } | |
614 | ||
b70dca78 | 615 | bt_self_component_set_data(self_comp, trimmer_comp); |
781751d8 PP |
616 | goto end; |
617 | ||
618 | error: | |
fb25b9e3 PP |
619 | if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) { |
620 | status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR; | |
781751d8 PP |
621 | } |
622 | ||
623 | if (trimmer_comp) { | |
624 | destroy_trimmer_comp(trimmer_comp); | |
625 | } | |
626 | ||
cab3f160 | 627 | end: |
64e6ceea | 628 | return status; |
781751d8 PP |
629 | } |
630 | ||
631 | static | |
632 | void destroy_trimmer_iterator(struct trimmer_iterator *trimmer_it) | |
633 | { | |
ab8b2b1b SM |
634 | if (!trimmer_it) { |
635 | goto end; | |
636 | } | |
637 | ||
781751d8 PP |
638 | bt_self_component_port_input_message_iterator_put_ref( |
639 | trimmer_it->upstream_iter); | |
640 | ||
641 | if (trimmer_it->output_messages) { | |
642 | g_queue_free(trimmer_it->output_messages); | |
643 | } | |
644 | ||
645 | if (trimmer_it->stream_states) { | |
646 | g_hash_table_destroy(trimmer_it->stream_states); | |
647 | } | |
648 | ||
649 | g_free(trimmer_it); | |
ab8b2b1b SM |
650 | end: |
651 | return; | |
781751d8 PP |
652 | } |
653 | ||
654 | static | |
655 | void destroy_trimmer_iterator_stream_state( | |
656 | struct trimmer_iterator_stream_state *sstate) | |
657 | { | |
658 | BT_ASSERT(sstate); | |
659 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); | |
781751d8 PP |
660 | g_free(sstate); |
661 | } | |
662 | ||
663 | BT_HIDDEN | |
fb25b9e3 | 664 | bt_component_class_message_iterator_init_method_status trimmer_msg_iter_init( |
781751d8 PP |
665 | bt_self_message_iterator *self_msg_iter, |
666 | bt_self_component_filter *self_comp, | |
667 | bt_self_component_port_output *port) | |
668 | { | |
ab8b2b1b SM |
669 | bt_component_class_message_iterator_init_method_status status; |
670 | bt_self_component_port_input_message_iterator_create_from_message_iterator_status | |
671 | msg_iter_status; | |
781751d8 PP |
672 | struct trimmer_iterator *trimmer_it; |
673 | ||
674 | trimmer_it = g_new0(struct trimmer_iterator, 1); | |
675 | if (!trimmer_it) { | |
fb25b9e3 | 676 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR; |
ab8b2b1b | 677 | goto error; |
781751d8 PP |
678 | } |
679 | ||
680 | trimmer_it->trimmer_comp = bt_self_component_get_data( | |
681 | bt_self_component_filter_as_self_component(self_comp)); | |
682 | BT_ASSERT(trimmer_it->trimmer_comp); | |
683 | ||
684 | if (trimmer_it->trimmer_comp->begin.is_set && | |
685 | trimmer_it->trimmer_comp->end.is_set) { | |
686 | /* | |
687 | * Both trimming time range's bounds are set, so skip | |
688 | * the | |
689 | * `TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN` | |
690 | * phase. | |
691 | */ | |
692 | trimmer_it->state = TRIMMER_ITERATOR_STATE_SEEK_INITIALLY; | |
693 | } | |
694 | ||
695 | trimmer_it->begin = trimmer_it->trimmer_comp->begin; | |
696 | trimmer_it->end = trimmer_it->trimmer_comp->end; | |
ab8b2b1b | 697 | msg_iter_status = |
692f1a01 PP |
698 | bt_self_component_port_input_message_iterator_create_from_message_iterator( |
699 | self_msg_iter, | |
781751d8 | 700 | bt_self_component_filter_borrow_input_port_by_name( |
ab8b2b1b SM |
701 | self_comp, in_port_name), &trimmer_it->upstream_iter); |
702 | if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) { | |
703 | status = (int) msg_iter_status; | |
704 | goto error; | |
781751d8 PP |
705 | } |
706 | ||
707 | trimmer_it->output_messages = g_queue_new(); | |
708 | if (!trimmer_it->output_messages) { | |
fb25b9e3 | 709 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR; |
ab8b2b1b | 710 | goto error; |
781751d8 PP |
711 | } |
712 | ||
713 | trimmer_it->stream_states = g_hash_table_new_full(g_direct_hash, | |
714 | g_direct_equal, NULL, | |
715 | (GDestroyNotify) destroy_trimmer_iterator_stream_state); | |
716 | if (!trimmer_it->stream_states) { | |
fb25b9e3 | 717 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR; |
ab8b2b1b | 718 | goto error; |
781751d8 PP |
719 | } |
720 | ||
721 | trimmer_it->self_msg_iter = self_msg_iter; | |
722 | bt_self_message_iterator_set_data(self_msg_iter, trimmer_it); | |
723 | ||
ab8b2b1b SM |
724 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK; |
725 | goto end; | |
726 | ||
727 | error: | |
728 | destroy_trimmer_iterator(trimmer_it); | |
781751d8 | 729 | |
ab8b2b1b | 730 | end: |
781751d8 PP |
731 | return status; |
732 | } | |
733 | ||
734 | static inline | |
735 | int get_msg_ns_from_origin(const bt_message *msg, int64_t *ns_from_origin, | |
b7cbc799 | 736 | bool *has_clock_snapshot) |
781751d8 PP |
737 | { |
738 | const bt_clock_class *clock_class = NULL; | |
739 | const bt_clock_snapshot *clock_snapshot = NULL; | |
781751d8 PP |
740 | int ret = 0; |
741 | ||
742 | BT_ASSERT(msg); | |
743 | BT_ASSERT(ns_from_origin); | |
b7cbc799 | 744 | BT_ASSERT(has_clock_snapshot); |
781751d8 PP |
745 | |
746 | switch (bt_message_get_type(msg)) { | |
747 | case BT_MESSAGE_TYPE_EVENT: | |
748 | clock_class = | |
749 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
750 | msg); | |
85e7137b | 751 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
752 | goto error; |
753 | } | |
754 | ||
11ddb3ef PP |
755 | clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const( |
756 | msg); | |
781751d8 PP |
757 | break; |
758 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
759 | clock_class = | |
760 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( | |
761 | msg); | |
85e7137b | 762 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
763 | goto error; |
764 | } | |
765 | ||
11ddb3ef PP |
766 | clock_snapshot = bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
767 | msg); | |
781751d8 PP |
768 | break; |
769 | case BT_MESSAGE_TYPE_PACKET_END: | |
770 | clock_class = | |
771 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( | |
772 | msg); | |
85e7137b | 773 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
774 | goto error; |
775 | } | |
776 | ||
11ddb3ef PP |
777 | clock_snapshot = bt_message_packet_end_borrow_default_clock_snapshot_const( |
778 | msg); | |
781751d8 | 779 | break; |
b7cbc799 SM |
780 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
781 | { | |
782 | enum bt_message_stream_clock_snapshot_state cs_state; | |
783 | ||
781751d8 | 784 | clock_class = |
b7cbc799 | 785 | bt_message_stream_beginning_borrow_stream_class_default_clock_class_const(msg); |
85e7137b | 786 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
787 | goto error; |
788 | } | |
789 | ||
b7cbc799 SM |
790 | cs_state = bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &clock_snapshot); |
791 | if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
792 | goto no_clock_snapshot; | |
781751d8 PP |
793 | } |
794 | ||
781751d8 | 795 | break; |
b7cbc799 SM |
796 | } |
797 | case BT_MESSAGE_TYPE_STREAM_END: | |
798 | { | |
799 | enum bt_message_stream_clock_snapshot_state cs_state; | |
800 | ||
781751d8 | 801 | clock_class = |
b7cbc799 | 802 | bt_message_stream_end_borrow_stream_class_default_clock_class_const(msg); |
85e7137b | 803 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
804 | goto error; |
805 | } | |
806 | ||
b7cbc799 SM |
807 | cs_state = bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &clock_snapshot); |
808 | if (cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
781751d8 PP |
809 | goto no_clock_snapshot; |
810 | } | |
811 | ||
812 | break; | |
b7cbc799 SM |
813 | } |
814 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
781751d8 | 815 | clock_class = |
b7cbc799 | 816 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
781751d8 | 817 | msg); |
85e7137b | 818 | if (G_UNLIKELY(!clock_class)) { |
781751d8 PP |
819 | goto error; |
820 | } | |
821 | ||
b7cbc799 SM |
822 | clock_snapshot = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
823 | msg); | |
824 | break; | |
825 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
826 | clock_class = | |
827 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( | |
828 | msg); | |
829 | if (G_UNLIKELY(!clock_class)) { | |
830 | goto error; | |
781751d8 PP |
831 | } |
832 | ||
b7cbc799 SM |
833 | clock_snapshot = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
834 | msg); | |
781751d8 PP |
835 | break; |
836 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
11ddb3ef | 837 | clock_snapshot = |
781751d8 | 838 | bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
11ddb3ef | 839 | msg); |
781751d8 PP |
840 | break; |
841 | default: | |
842 | goto no_clock_snapshot; | |
843 | } | |
844 | ||
781751d8 PP |
845 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, |
846 | ns_from_origin); | |
85e7137b | 847 | if (G_UNLIKELY(ret)) { |
781751d8 PP |
848 | goto error; |
849 | } | |
850 | ||
b7cbc799 | 851 | *has_clock_snapshot = true; |
781751d8 PP |
852 | goto end; |
853 | ||
854 | no_clock_snapshot: | |
b7cbc799 | 855 | *has_clock_snapshot = false; |
781751d8 PP |
856 | goto end; |
857 | ||
cab3f160 | 858 | error: |
781751d8 PP |
859 | ret = -1; |
860 | ||
861 | end: | |
862 | return ret; | |
863 | } | |
864 | ||
865 | static inline | |
866 | void put_messages(bt_message_array_const msgs, uint64_t count) | |
867 | { | |
868 | uint64_t i; | |
869 | ||
870 | for (i = 0; i < count; i++) { | |
871 | BT_MESSAGE_PUT_REF_AND_RESET(msgs[i]); | |
872 | } | |
873 | } | |
874 | ||
875 | static inline | |
19f15b11 PP |
876 | int set_trimmer_iterator_bound(struct trimmer_iterator *trimmer_it, |
877 | struct trimmer_bound *bound, int64_t ns_from_origin, | |
878 | bool is_gmt) | |
781751d8 | 879 | { |
19f15b11 | 880 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
781751d8 | 881 | struct tm tm; |
ce8955eb | 882 | struct tm *res; |
781751d8 PP |
883 | time_t time_seconds = (time_t) (ns_from_origin / NS_PER_S); |
884 | int ret = 0; | |
885 | ||
886 | BT_ASSERT(!bound->is_set); | |
887 | errno = 0; | |
888 | ||
889 | /* We only need to extract the date from this time */ | |
890 | if (is_gmt) { | |
ce8955eb | 891 | res = bt_gmtime_r(&time_seconds, &tm); |
781751d8 | 892 | } else { |
ce8955eb | 893 | res = bt_localtime_r(&time_seconds, &tm); |
781751d8 PP |
894 | } |
895 | ||
ce8955eb | 896 | if (!res) { |
ccaf1ae5 SM |
897 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(trimmer_comp->self_comp, |
898 | "Cannot convert timestamp to date and time", | |
8e67b873 | 899 | ": ts=%" PRId64, (int64_t) time_seconds); |
781751d8 PP |
900 | ret = -1; |
901 | goto end; | |
902 | } | |
903 | ||
904 | ret = set_bound_ns_from_origin(bound, tm.tm_year + 1900, tm.tm_mon + 1, | |
905 | tm.tm_mday, bound->time.hour, bound->time.minute, | |
906 | bound->time.second, bound->time.ns, is_gmt); | |
907 | ||
908 | end: | |
cab3f160 JG |
909 | return ret; |
910 | } | |
781751d8 PP |
911 | |
912 | static | |
fb25b9e3 PP |
913 | bt_component_class_message_iterator_next_method_status |
914 | state_set_trimmer_iterator_bounds( | |
781751d8 PP |
915 | struct trimmer_iterator *trimmer_it) |
916 | { | |
fb25b9e3 | 917 | bt_message_iterator_next_status upstream_iter_status = |
621b4e7e | 918 | BT_MESSAGE_ITERATOR_NEXT_STATUS_OK; |
781751d8 PP |
919 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
920 | bt_message_array_const msgs; | |
921 | uint64_t count = 0; | |
922 | int64_t ns_from_origin = INT64_MIN; | |
923 | uint64_t i; | |
924 | int ret; | |
925 | ||
926 | BT_ASSERT(!trimmer_it->begin.is_set || | |
927 | !trimmer_it->end.is_set); | |
928 | ||
929 | while (true) { | |
930 | upstream_iter_status = | |
931 | bt_self_component_port_input_message_iterator_next( | |
932 | trimmer_it->upstream_iter, &msgs, &count); | |
fb25b9e3 | 933 | if (upstream_iter_status != BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) { |
781751d8 PP |
934 | goto end; |
935 | } | |
936 | ||
937 | for (i = 0; i < count; i++) { | |
938 | const bt_message *msg = msgs[i]; | |
b7cbc799 | 939 | bool has_ns_from_origin; |
781751d8 PP |
940 | int ret; |
941 | ||
942 | ret = get_msg_ns_from_origin(msg, &ns_from_origin, | |
b7cbc799 | 943 | &has_ns_from_origin); |
781751d8 PP |
944 | if (ret) { |
945 | goto error; | |
946 | } | |
947 | ||
b7cbc799 | 948 | if (!has_ns_from_origin) { |
781751d8 PP |
949 | continue; |
950 | } | |
951 | ||
952 | BT_ASSERT(ns_from_origin != INT64_MIN && | |
953 | ns_from_origin != INT64_MAX); | |
954 | put_messages(msgs, count); | |
955 | goto found; | |
956 | } | |
957 | ||
958 | put_messages(msgs, count); | |
959 | } | |
960 | ||
961 | found: | |
962 | if (!trimmer_it->begin.is_set) { | |
963 | BT_ASSERT(!trimmer_it->begin.is_infinite); | |
19f15b11 | 964 | ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->begin, |
781751d8 PP |
965 | ns_from_origin, trimmer_comp->is_gmt); |
966 | if (ret) { | |
967 | goto error; | |
968 | } | |
969 | } | |
970 | ||
971 | if (!trimmer_it->end.is_set) { | |
972 | BT_ASSERT(!trimmer_it->end.is_infinite); | |
19f15b11 | 973 | ret = set_trimmer_iterator_bound(trimmer_it, &trimmer_it->end, |
781751d8 PP |
974 | ns_from_origin, trimmer_comp->is_gmt); |
975 | if (ret) { | |
976 | goto error; | |
977 | } | |
978 | } | |
979 | ||
19f15b11 PP |
980 | ret = validate_trimmer_bounds(trimmer_it->trimmer_comp, |
981 | &trimmer_it->begin, &trimmer_it->end); | |
781751d8 PP |
982 | if (ret) { |
983 | goto error; | |
984 | } | |
985 | ||
986 | goto end; | |
987 | ||
988 | error: | |
989 | put_messages(msgs, count); | |
621b4e7e | 990 | upstream_iter_status = BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR; |
781751d8 PP |
991 | |
992 | end: | |
993 | return (int) upstream_iter_status; | |
994 | } | |
995 | ||
996 | static | |
fb25b9e3 | 997 | bt_component_class_message_iterator_next_method_status state_seek_initially( |
781751d8 PP |
998 | struct trimmer_iterator *trimmer_it) |
999 | { | |
19f15b11 | 1000 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; |
9e8e8b43 | 1001 | bt_component_class_message_iterator_next_method_status status; |
781751d8 PP |
1002 | |
1003 | BT_ASSERT(trimmer_it->begin.is_set); | |
1004 | ||
1005 | if (trimmer_it->begin.is_infinite) { | |
9e8e8b43 SM |
1006 | bt_bool can_seek; |
1007 | ||
1008 | status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1009 | trimmer_it->upstream_iter, &can_seek); | |
1010 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { | |
1011 | if (status < 0) { | |
1012 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1013 | "Cannot make upstream message iterator initially seek its beginning."); | |
1014 | } | |
1015 | ||
1016 | goto end; | |
1017 | } | |
1018 | ||
1019 | if (!can_seek) { | |
ccaf1ae5 SM |
1020 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1021 | "Cannot make upstream message iterator initially seek its beginning."); | |
fb25b9e3 | 1022 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1023 | goto end; |
1024 | } | |
1025 | ||
1026 | status = (int) bt_self_component_port_input_message_iterator_seek_beginning( | |
1027 | trimmer_it->upstream_iter); | |
1028 | } else { | |
9e8e8b43 SM |
1029 | bt_bool can_seek; |
1030 | ||
1031 | status = (int) bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1032 | trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin, | |
1033 | &can_seek); | |
1034 | ||
1035 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { | |
1036 | if (status < 0) { | |
1037 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, | |
1038 | "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64, | |
1039 | trimmer_it->begin.ns_from_origin); | |
1040 | } | |
1041 | ||
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | if (!can_seek) { | |
ccaf1ae5 SM |
1046 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1047 | "Cannot make upstream message iterator initially seek: seek-ns-from-origin=%" PRId64, | |
781751d8 | 1048 | trimmer_it->begin.ns_from_origin); |
fb25b9e3 | 1049 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1050 | goto end; |
1051 | } | |
1052 | ||
1053 | status = (int) bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1054 | trimmer_it->upstream_iter, trimmer_it->begin.ns_from_origin); | |
1055 | } | |
1056 | ||
fb25b9e3 | 1057 | if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1058 | trimmer_it->state = TRIMMER_ITERATOR_STATE_TRIM; |
1059 | } | |
1060 | ||
1061 | end: | |
1062 | return status; | |
1063 | } | |
1064 | ||
1065 | static inline | |
1066 | void push_message(struct trimmer_iterator *trimmer_it, const bt_message *msg) | |
1067 | { | |
1068 | g_queue_push_head(trimmer_it->output_messages, (void *) msg); | |
1069 | } | |
1070 | ||
1071 | static inline | |
1072 | const bt_message *pop_message(struct trimmer_iterator *trimmer_it) | |
1073 | { | |
1074 | return g_queue_pop_tail(trimmer_it->output_messages); | |
1075 | } | |
1076 | ||
1077 | static inline | |
1078 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, | |
1079 | int64_t ns_from_origin, uint64_t *raw_value) | |
1080 | { | |
1081 | ||
1082 | int64_t cc_offset_s; | |
1083 | uint64_t cc_offset_cycles; | |
1084 | uint64_t cc_freq; | |
1085 | ||
1086 | bt_clock_class_get_offset(clock_class, &cc_offset_s, &cc_offset_cycles); | |
1087 | cc_freq = bt_clock_class_get_frequency(clock_class); | |
1088 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, | |
1089 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); | |
1090 | } | |
1091 | ||
1092 | static inline | |
fb25b9e3 PP |
1093 | bt_component_class_message_iterator_next_method_status |
1094 | end_stream(struct trimmer_iterator *trimmer_it, | |
781751d8 PP |
1095 | struct trimmer_iterator_stream_state *sstate) |
1096 | { | |
fb25b9e3 PP |
1097 | bt_component_class_message_iterator_next_method_status status = |
1098 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
b7cbc799 SM |
1099 | /* Initialize to silence maybe-uninitialized warning. */ |
1100 | uint64_t raw_value = 0; | |
781751d8 PP |
1101 | bt_message *msg = NULL; |
1102 | ||
1103 | BT_ASSERT(!trimmer_it->end.is_infinite); | |
b7cbc799 | 1104 | BT_ASSERT(sstate->stream); |
781751d8 | 1105 | |
b7cbc799 SM |
1106 | /* |
1107 | * If we haven't seen a message with a clock snapshot, we don't know if the trimmer's end bound is within | |
1108 | * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value. | |
1109 | * | |
1110 | * Also, it would be a bit of a lie to generate a stream end message with the end bound as its | |
1111 | * clock snapshot, because we don't really know if the stream existed at that time. If we have | |
1112 | * seen a message with a clock snapshot and the stream is cut short by another message with a | |
1113 | * clock snapshot, then we are sure that the the end bound time is not below the clock range, | |
1114 | * and we know the stream was active at that time (and that we cut it short). | |
1115 | */ | |
1116 | if (sstate->seen_clock_snapshot) { | |
1117 | const bt_clock_class *clock_class; | |
1118 | int ret; | |
781751d8 | 1119 | |
781751d8 PP |
1120 | clock_class = bt_stream_class_borrow_default_clock_class_const( |
1121 | bt_stream_borrow_class_const(sstate->stream)); | |
1122 | BT_ASSERT(clock_class); | |
1123 | ret = clock_raw_value_from_ns_from_origin(clock_class, | |
1124 | trimmer_it->end.ns_from_origin, &raw_value); | |
1125 | if (ret) { | |
fb25b9e3 | 1126 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1127 | goto end; |
1128 | } | |
b7cbc799 SM |
1129 | } |
1130 | ||
1131 | if (sstate->cur_packet) { | |
1132 | /* | |
1133 | * Create and push a packet end message, making its time | |
1134 | * the trimming range's end time. | |
1135 | * | |
1136 | * We know that we must have seen a clock snapshot, the one in | |
1137 | * the packet beginning message, since trimmer currently | |
1138 | * requires packet messages to have clock snapshots (see comment | |
1139 | * in create_stream_state_entry). | |
1140 | */ | |
1141 | BT_ASSERT(sstate->seen_clock_snapshot); | |
781751d8 PP |
1142 | |
1143 | msg = bt_message_packet_end_create_with_default_clock_snapshot( | |
1144 | trimmer_it->self_msg_iter, sstate->cur_packet, | |
1145 | raw_value); | |
1146 | if (!msg) { | |
fb25b9e3 | 1147 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; |
781751d8 PP |
1148 | goto end; |
1149 | } | |
1150 | ||
1151 | push_message(trimmer_it, msg); | |
1152 | msg = NULL; | |
1153 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); | |
781751d8 PP |
1154 | } |
1155 | ||
b7cbc799 | 1156 | /* Create and push a stream end message. */ |
781751d8 PP |
1157 | msg = bt_message_stream_end_create(trimmer_it->self_msg_iter, |
1158 | sstate->stream); | |
1159 | if (!msg) { | |
fb25b9e3 | 1160 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; |
781751d8 PP |
1161 | goto end; |
1162 | } | |
1163 | ||
b7cbc799 SM |
1164 | if (sstate->seen_clock_snapshot) { |
1165 | bt_message_stream_end_set_default_clock_snapshot(msg, raw_value); | |
1166 | } | |
1167 | ||
781751d8 PP |
1168 | push_message(trimmer_it, msg); |
1169 | msg = NULL; | |
1170 | ||
1171 | /* | |
1172 | * Just to make sure that we don't use this stream state again | |
1173 | * in the future without an obvious error. | |
1174 | */ | |
1175 | sstate->stream = NULL; | |
1176 | ||
1177 | end: | |
1178 | bt_message_put_ref(msg); | |
1179 | return status; | |
1180 | } | |
1181 | ||
1182 | static inline | |
fb25b9e3 | 1183 | bt_component_class_message_iterator_next_method_status end_iterator_streams( |
781751d8 PP |
1184 | struct trimmer_iterator *trimmer_it) |
1185 | { | |
fb25b9e3 PP |
1186 | bt_component_class_message_iterator_next_method_status status = |
1187 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
781751d8 PP |
1188 | GHashTableIter iter; |
1189 | gpointer key, sstate; | |
1190 | ||
1191 | if (trimmer_it->end.is_infinite) { | |
1192 | /* | |
1193 | * An infinite trimming range's end time guarantees that | |
1194 | * we received (and pushed) all the appropriate end | |
1195 | * messages. | |
1196 | */ | |
1197 | goto remove_all; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * End each stream and then remove them from the hash table of | |
1202 | * stream states to release unneeded references. | |
1203 | */ | |
1204 | g_hash_table_iter_init(&iter, trimmer_it->stream_states); | |
1205 | ||
1206 | while (g_hash_table_iter_next(&iter, &key, &sstate)) { | |
1207 | status = end_stream(trimmer_it, sstate); | |
1208 | if (status) { | |
1209 | goto end; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | remove_all: | |
1214 | g_hash_table_remove_all(trimmer_it->stream_states); | |
1215 | ||
1216 | end: | |
1217 | return status; | |
1218 | } | |
1219 | ||
b7cbc799 SM |
1220 | static |
1221 | bt_component_class_message_iterator_next_method_status | |
1222 | create_stream_state_entry( | |
1223 | struct trimmer_iterator *trimmer_it, | |
1224 | const struct bt_stream *stream, | |
1225 | struct trimmer_iterator_stream_state **stream_state) | |
1226 | { | |
1227 | struct trimmer_comp *trimmer_comp = trimmer_it->trimmer_comp; | |
1228 | bt_component_class_message_iterator_next_method_status status; | |
1229 | struct trimmer_iterator_stream_state *sstate; | |
1230 | const bt_stream_class *sc; | |
1231 | ||
1232 | BT_ASSERT(!bt_g_hash_table_contains(trimmer_it->stream_states, stream)); | |
1233 | ||
1234 | /* | |
1235 | * Validate right now that the stream's class | |
1236 | * has a registered default clock class so that | |
1237 | * an existing stream state guarantees existing | |
1238 | * default clock snapshots for its associated | |
1239 | * messages. | |
1240 | * | |
1241 | * Also check that clock snapshots are always | |
1242 | * known. | |
1243 | */ | |
1244 | sc = bt_stream_borrow_class_const(stream); | |
1245 | if (!bt_stream_class_borrow_default_clock_class_const(sc)) { | |
ccaf1ae5 SM |
1246 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1247 | "Unsupported stream: stream class does " | |
b7cbc799 SM |
1248 | "not have a default clock class: " |
1249 | "stream-addr=%p, " | |
1250 | "stream-id=%" PRIu64 ", " | |
1251 | "stream-name=\"%s\"", | |
1252 | stream, bt_stream_get_id(stream), | |
1253 | bt_stream_get_name(stream)); | |
1254 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; | |
1255 | goto end; | |
1256 | } | |
1257 | ||
1258 | /* | |
1259 | * Temporary: make sure packet beginning, packet | |
1260 | * end, discarded events, and discarded packets | |
1261 | * messages have default clock snapshots until | |
1262 | * the support for not having them is | |
1263 | * implemented. | |
1264 | */ | |
1265 | if (!bt_stream_class_packets_have_beginning_default_clock_snapshot( | |
1266 | sc)) { | |
ccaf1ae5 SM |
1267 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1268 | "Unsupported stream: packets have no beginning clock snapshot: " | |
b7cbc799 SM |
1269 | "stream-addr=%p, " |
1270 | "stream-id=%" PRIu64 ", " | |
1271 | "stream-name=\"%s\"", | |
1272 | stream, bt_stream_get_id(stream), | |
1273 | bt_stream_get_name(stream)); | |
1274 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; | |
1275 | goto end; | |
1276 | } | |
1277 | ||
1278 | if (!bt_stream_class_packets_have_end_default_clock_snapshot( | |
1279 | sc)) { | |
ccaf1ae5 SM |
1280 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1281 | "Unsupported stream: packets have no end clock snapshot: " | |
b7cbc799 SM |
1282 | "stream-addr=%p, " |
1283 | "stream-id=%" PRIu64 ", " | |
1284 | "stream-name=\"%s\"", | |
1285 | stream, bt_stream_get_id(stream), | |
1286 | bt_stream_get_name(stream)); | |
1287 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; | |
1288 | goto end; | |
1289 | } | |
1290 | ||
1291 | if (bt_stream_class_supports_discarded_events(sc) && | |
1292 | !bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) { | |
ccaf1ae5 SM |
1293 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1294 | "Unsupported stream: discarded events have no clock snapshots: " | |
b7cbc799 SM |
1295 | "stream-addr=%p, " |
1296 | "stream-id=%" PRIu64 ", " | |
1297 | "stream-name=\"%s\"", | |
1298 | stream, bt_stream_get_id(stream), | |
1299 | bt_stream_get_name(stream)); | |
1300 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; | |
1301 | goto end; | |
1302 | } | |
1303 | ||
1304 | if (bt_stream_class_supports_discarded_packets(sc) && | |
1305 | !bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) { | |
ccaf1ae5 SM |
1306 | BT_COMP_LOGE_APPEND_CAUSE(trimmer_comp->self_comp, |
1307 | "Unsupported stream: discarded packets " | |
b7cbc799 SM |
1308 | "have no clock snapshots: " |
1309 | "stream-addr=%p, " | |
1310 | "stream-id=%" PRIu64 ", " | |
1311 | "stream-name=\"%s\"", | |
1312 | stream, bt_stream_get_id(stream), | |
1313 | bt_stream_get_name(stream)); | |
1314 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; | |
1315 | goto end; | |
1316 | } | |
1317 | ||
1318 | sstate = g_new0(struct trimmer_iterator_stream_state, 1); | |
1319 | if (!sstate) { | |
1320 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; | |
1321 | goto end; | |
1322 | } | |
1323 | ||
1324 | sstate->stream = stream; | |
1325 | ||
1326 | g_hash_table_insert(trimmer_it->stream_states, (void *) stream, sstate); | |
1327 | ||
1328 | *stream_state = sstate; | |
1329 | ||
1330 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
1331 | ||
1332 | end: | |
1333 | return status; | |
1334 | } | |
1335 | ||
1336 | static | |
1337 | struct trimmer_iterator_stream_state *get_stream_state_entry( | |
1338 | struct trimmer_iterator *trimmer_it, | |
1339 | const struct bt_stream *stream) | |
1340 | { | |
1341 | struct trimmer_iterator_stream_state *sstate; | |
1342 | ||
1343 | BT_ASSERT(stream); | |
1344 | sstate = g_hash_table_lookup(trimmer_it->stream_states, stream); | |
1345 | BT_ASSERT(sstate); | |
1346 | ||
1347 | return sstate; | |
1348 | } | |
1349 | ||
781751d8 PP |
1350 | /* |
1351 | * Handles a message which is associated to a given stream state. This | |
1352 | * _could_ make the iterator's output message queue grow; this could | |
1353 | * also consume the message without pushing anything to this queue, only | |
1354 | * modifying the stream state. | |
1355 | * | |
1356 | * This function consumes the `msg` reference, _whatever the outcome_. | |
1357 | * | |
b7cbc799 SM |
1358 | * If non-NULL, `ns_from_origin` is the message's time, as given by |
1359 | * get_msg_ns_from_origin(). If NULL, the message doesn't have a time. | |
781751d8 PP |
1360 | * |
1361 | * This function sets `reached_end` if handling this message made the | |
1362 | * iterator reach the end of the trimming range. Note that the output | |
1363 | * message queue could contain messages even if this function sets | |
1364 | * `reached_end`. | |
1365 | */ | |
b7cbc799 | 1366 | static |
fb25b9e3 | 1367 | bt_component_class_message_iterator_next_method_status |
b7cbc799 | 1368 | handle_message_with_stream( |
781751d8 | 1369 | struct trimmer_iterator *trimmer_it, const bt_message *msg, |
b7cbc799 SM |
1370 | const struct bt_stream *stream, const int64_t *ns_from_origin, |
1371 | bool *reached_end) | |
781751d8 | 1372 | { |
fb25b9e3 PP |
1373 | bt_component_class_message_iterator_next_method_status status = |
1374 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
781751d8 PP |
1375 | bt_message_type msg_type = bt_message_get_type(msg); |
1376 | int ret; | |
b7cbc799 SM |
1377 | struct trimmer_iterator_stream_state *sstate = NULL; |
1378 | ||
1379 | /* | |
1380 | * Retrieve the stream's state - except if the message is stream | |
1381 | * beginning, in which case we don't know about about this stream yet. | |
1382 | */ | |
1383 | if (msg_type != BT_MESSAGE_TYPE_STREAM_BEGINNING) { | |
1384 | sstate = get_stream_state_entry(trimmer_it, stream); | |
1385 | } | |
781751d8 PP |
1386 | |
1387 | switch (msg_type) { | |
1388 | case BT_MESSAGE_TYPE_EVENT: | |
b7cbc799 SM |
1389 | /* |
1390 | * Event messages always have a clock snapshot if the stream | |
1391 | * class has a clock class. And we know it has, otherwise we | |
1392 | * couldn't be using the trimmer component. | |
1393 | */ | |
1394 | BT_ASSERT(ns_from_origin); | |
b7cbc799 | 1395 | |
85e7137b | 1396 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
b7cbc799 | 1397 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
781751d8 PP |
1398 | status = end_iterator_streams(trimmer_it); |
1399 | *reached_end = true; | |
1400 | break; | |
1401 | } | |
1402 | ||
b7cbc799 SM |
1403 | sstate->seen_clock_snapshot = true; |
1404 | ||
781751d8 PP |
1405 | push_message(trimmer_it, msg); |
1406 | msg = NULL; | |
1407 | break; | |
b7cbc799 | 1408 | |
781751d8 | 1409 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
b7cbc799 SM |
1410 | /* |
1411 | * Packet beginning messages won't have a clock snapshot if | |
1412 | * stream_class->packets_have_beginning_default_clock_snapshot | |
1413 | * is false. But for now, assume they always do. | |
1414 | */ | |
1415 | BT_ASSERT(ns_from_origin); | |
1416 | BT_ASSERT(!sstate->cur_packet); | |
1417 | ||
85e7137b | 1418 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
b7cbc799 | 1419 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
781751d8 PP |
1420 | status = end_iterator_streams(trimmer_it); |
1421 | *reached_end = true; | |
1422 | break; | |
1423 | } | |
1424 | ||
781751d8 PP |
1425 | sstate->cur_packet = |
1426 | bt_message_packet_beginning_borrow_packet_const(msg); | |
1427 | bt_packet_get_ref(sstate->cur_packet); | |
b7cbc799 SM |
1428 | |
1429 | sstate->seen_clock_snapshot = true; | |
1430 | ||
781751d8 PP |
1431 | push_message(trimmer_it, msg); |
1432 | msg = NULL; | |
1433 | break; | |
b7cbc799 | 1434 | |
781751d8 | 1435 | case BT_MESSAGE_TYPE_PACKET_END: |
b7cbc799 SM |
1436 | /* |
1437 | * Packet end messages won't have a clock snapshot if | |
1438 | * stream_class->packets_have_end_default_clock_snapshot | |
1439 | * is false. But for now, assume they always do. | |
1440 | */ | |
1441 | BT_ASSERT(ns_from_origin); | |
1442 | BT_ASSERT(sstate->cur_packet); | |
781751d8 | 1443 | |
85e7137b | 1444 | if (G_UNLIKELY(!trimmer_it->end.is_infinite && |
b7cbc799 | 1445 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { |
781751d8 PP |
1446 | status = end_iterator_streams(trimmer_it); |
1447 | *reached_end = true; | |
1448 | break; | |
1449 | } | |
1450 | ||
781751d8 | 1451 | BT_PACKET_PUT_REF_AND_RESET(sstate->cur_packet); |
b7cbc799 SM |
1452 | |
1453 | sstate->seen_clock_snapshot = true; | |
1454 | ||
781751d8 PP |
1455 | push_message(trimmer_it, msg); |
1456 | msg = NULL; | |
1457 | break; | |
b7cbc799 | 1458 | |
781751d8 PP |
1459 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1460 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1461 | { | |
1462 | /* | |
1463 | * `ns_from_origin` is the message's time range's | |
1464 | * beginning time here. | |
1465 | */ | |
1466 | int64_t end_ns_from_origin; | |
1467 | const bt_clock_snapshot *end_cs; | |
1468 | ||
b7cbc799 SM |
1469 | BT_ASSERT(ns_from_origin); |
1470 | ||
1471 | sstate->seen_clock_snapshot = true; | |
1472 | ||
781751d8 PP |
1473 | if (bt_message_get_type(msg) == |
1474 | BT_MESSAGE_TYPE_DISCARDED_EVENTS) { | |
1475 | /* | |
1476 | * Safe to ignore the return value because we | |
1477 | * know there's a default clock and it's always | |
1478 | * known. | |
1479 | */ | |
5ef34326 | 1480 | end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
11ddb3ef | 1481 | msg); |
781751d8 PP |
1482 | } else { |
1483 | /* | |
1484 | * Safe to ignore the return value because we | |
1485 | * know there's a default clock and it's always | |
1486 | * known. | |
1487 | */ | |
5ef34326 | 1488 | end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
11ddb3ef | 1489 | msg); |
781751d8 PP |
1490 | } |
1491 | ||
1492 | if (bt_clock_snapshot_get_ns_from_origin(end_cs, | |
1493 | &end_ns_from_origin)) { | |
fb25b9e3 | 1494 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1495 | goto end; |
1496 | } | |
1497 | ||
781751d8 | 1498 | if (!trimmer_it->end.is_infinite && |
b7cbc799 | 1499 | *ns_from_origin > trimmer_it->end.ns_from_origin) { |
781751d8 PP |
1500 | status = end_iterator_streams(trimmer_it); |
1501 | *reached_end = true; | |
1502 | break; | |
1503 | } | |
1504 | ||
1505 | if (!trimmer_it->end.is_infinite && | |
1506 | end_ns_from_origin > trimmer_it->end.ns_from_origin) { | |
1507 | /* | |
1508 | * This message's end time is outside the | |
1509 | * trimming time range: replace it with a new | |
1510 | * message having an end time equal to the | |
1511 | * trimming time range's end and without a | |
1512 | * count. | |
1513 | */ | |
1514 | const bt_clock_class *clock_class = | |
1515 | bt_clock_snapshot_borrow_clock_class_const( | |
1516 | end_cs); | |
1517 | const bt_clock_snapshot *begin_cs; | |
1518 | bt_message *new_msg; | |
1519 | uint64_t end_raw_value; | |
1520 | ||
1521 | ret = clock_raw_value_from_ns_from_origin(clock_class, | |
1522 | trimmer_it->end.ns_from_origin, &end_raw_value); | |
1523 | if (ret) { | |
fb25b9e3 | 1524 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1525 | goto end; |
1526 | } | |
1527 | ||
1528 | if (msg_type == BT_MESSAGE_TYPE_DISCARDED_EVENTS) { | |
5ef34326 | 1529 | begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
11ddb3ef | 1530 | msg); |
781751d8 PP |
1531 | new_msg = bt_message_discarded_events_create_with_default_clock_snapshots( |
1532 | trimmer_it->self_msg_iter, | |
1533 | sstate->stream, | |
1534 | bt_clock_snapshot_get_value(begin_cs), | |
1535 | end_raw_value); | |
1536 | } else { | |
5ef34326 | 1537 | begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
11ddb3ef | 1538 | msg); |
781751d8 PP |
1539 | new_msg = bt_message_discarded_packets_create_with_default_clock_snapshots( |
1540 | trimmer_it->self_msg_iter, | |
1541 | sstate->stream, | |
1542 | bt_clock_snapshot_get_value(begin_cs), | |
1543 | end_raw_value); | |
1544 | } | |
1545 | ||
1546 | if (!new_msg) { | |
fb25b9e3 | 1547 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; |
781751d8 PP |
1548 | goto end; |
1549 | } | |
1550 | ||
1551 | /* Replace the original message */ | |
1552 | BT_MESSAGE_MOVE_REF(msg, new_msg); | |
1553 | } | |
1554 | ||
781751d8 PP |
1555 | push_message(trimmer_it, msg); |
1556 | msg = NULL; | |
1557 | break; | |
1558 | } | |
b7cbc799 SM |
1559 | |
1560 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1561 | /* | |
1562 | * If this message has a time and this time is greater than the | |
1563 | * trimmer's end bound, it triggers the end of the trim window. | |
1564 | */ | |
1565 | if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite && | |
1566 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { | |
781751d8 PP |
1567 | status = end_iterator_streams(trimmer_it); |
1568 | *reached_end = true; | |
1569 | break; | |
1570 | } | |
1571 | ||
b7cbc799 SM |
1572 | /* Learn about this stream. */ |
1573 | status = create_stream_state_entry(trimmer_it, stream, &sstate); | |
1574 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { | |
1575 | goto end; | |
781751d8 PP |
1576 | } |
1577 | ||
b7cbc799 SM |
1578 | if (ns_from_origin) { |
1579 | sstate->seen_clock_snapshot = true; | |
781751d8 PP |
1580 | } |
1581 | ||
143feff5 SM |
1582 | push_message(trimmer_it, msg); |
1583 | msg = NULL; | |
781751d8 PP |
1584 | break; |
1585 | case BT_MESSAGE_TYPE_STREAM_END: | |
b7cbc799 SM |
1586 | { |
1587 | gboolean removed; | |
1588 | ||
143feff5 | 1589 | /* |
b7cbc799 SM |
1590 | * If this message has a time and this time is greater than the |
1591 | * trimmer's end bound, it triggers the end of the trim window. | |
143feff5 | 1592 | */ |
b7cbc799 SM |
1593 | if (G_UNLIKELY(ns_from_origin && !trimmer_it->end.is_infinite && |
1594 | *ns_from_origin > trimmer_it->end.ns_from_origin)) { | |
1595 | status = end_iterator_streams(trimmer_it); | |
1596 | *reached_end = true; | |
1597 | break; | |
781751d8 | 1598 | } |
b7cbc799 SM |
1599 | |
1600 | /* | |
1601 | * Either the stream end message's time is within the trimmer's | |
1602 | * bounds, or it doesn't have a time. In both cases, pass | |
1603 | * the message unmodified. | |
1604 | */ | |
1605 | push_message(trimmer_it, msg); | |
1606 | msg = NULL; | |
1607 | ||
1608 | /* Forget about this stream. */ | |
1609 | removed = g_hash_table_remove(trimmer_it->stream_states, sstate->stream); | |
1610 | BT_ASSERT(removed); | |
781751d8 | 1611 | break; |
b7cbc799 | 1612 | } |
781751d8 PP |
1613 | default: |
1614 | break; | |
1615 | } | |
1616 | ||
1617 | end: | |
1618 | /* We release the message's reference whatever the outcome */ | |
1619 | bt_message_put_ref(msg); | |
b7cbc799 | 1620 | return status; |
781751d8 PP |
1621 | } |
1622 | ||
1623 | /* | |
1624 | * Handles an input message. This _could_ make the iterator's output | |
1625 | * message queue grow; this could also consume the message without | |
1626 | * pushing anything to this queue, only modifying the stream state. | |
1627 | * | |
1628 | * This function consumes the `msg` reference, _whatever the outcome_. | |
1629 | * | |
1630 | * This function sets `reached_end` if handling this message made the | |
1631 | * iterator reach the end of the trimming range. Note that the output | |
1632 | * message queue could contain messages even if this function sets | |
1633 | * `reached_end`. | |
1634 | */ | |
1635 | static inline | |
fb25b9e3 | 1636 | bt_component_class_message_iterator_next_method_status handle_message( |
781751d8 PP |
1637 | struct trimmer_iterator *trimmer_it, const bt_message *msg, |
1638 | bool *reached_end) | |
1639 | { | |
fb25b9e3 | 1640 | bt_component_class_message_iterator_next_method_status status; |
781751d8 PP |
1641 | const bt_stream *stream = NULL; |
1642 | int64_t ns_from_origin = INT64_MIN; | |
1564e3dc | 1643 | bool has_ns_from_origin = false; |
781751d8 | 1644 | int ret; |
781751d8 PP |
1645 | |
1646 | /* Find message's associated stream */ | |
1647 | switch (bt_message_get_type(msg)) { | |
1648 | case BT_MESSAGE_TYPE_EVENT: | |
1649 | stream = bt_event_borrow_stream_const( | |
1650 | bt_message_event_borrow_event_const(msg)); | |
1651 | break; | |
1652 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
1653 | stream = bt_packet_borrow_stream_const( | |
1654 | bt_message_packet_beginning_borrow_packet_const(msg)); | |
1655 | break; | |
1656 | case BT_MESSAGE_TYPE_PACKET_END: | |
1657 | stream = bt_packet_borrow_stream_const( | |
1658 | bt_message_packet_end_borrow_packet_const(msg)); | |
1659 | break; | |
1660 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1661 | stream = bt_message_discarded_events_borrow_stream_const(msg); | |
1662 | break; | |
1663 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1664 | stream = bt_message_discarded_packets_borrow_stream_const(msg); | |
1665 | break; | |
781751d8 PP |
1666 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
1667 | stream = bt_message_stream_beginning_borrow_stream_const(msg); | |
1668 | break; | |
1669 | case BT_MESSAGE_TYPE_STREAM_END: | |
1670 | stream = bt_message_stream_end_borrow_stream_const(msg); | |
1671 | break; | |
1672 | default: | |
1673 | break; | |
1674 | } | |
1675 | ||
781751d8 | 1676 | /* Retrieve the message's time */ |
b7cbc799 | 1677 | ret = get_msg_ns_from_origin(msg, &ns_from_origin, &has_ns_from_origin); |
85e7137b | 1678 | if (G_UNLIKELY(ret)) { |
fb25b9e3 | 1679 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
781751d8 PP |
1680 | goto end; |
1681 | } | |
1682 | ||
b7cbc799 | 1683 | if (G_LIKELY(stream)) { |
781751d8 | 1684 | /* Message associated to a stream */ |
b7cbc799 SM |
1685 | status = handle_message_with_stream(trimmer_it, msg, |
1686 | stream, has_ns_from_origin ? &ns_from_origin : NULL, reached_end); | |
781751d8 PP |
1687 | |
1688 | /* | |
1689 | * handle_message_with_stream_state() unconditionally | |
1690 | * consumes `msg`. | |
1691 | */ | |
1692 | msg = NULL; | |
1693 | } else { | |
1694 | /* | |
1695 | * Message not associated to a stream (message iterator | |
1696 | * inactivity). | |
1697 | */ | |
85e7137b | 1698 | if (G_UNLIKELY(ns_from_origin > trimmer_it->end.ns_from_origin)) { |
781751d8 PP |
1699 | BT_MESSAGE_PUT_REF_AND_RESET(msg); |
1700 | status = end_iterator_streams(trimmer_it); | |
1701 | *reached_end = true; | |
1702 | } else { | |
1703 | push_message(trimmer_it, msg); | |
fb25b9e3 | 1704 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; |
781751d8 PP |
1705 | msg = NULL; |
1706 | } | |
1707 | } | |
1708 | ||
1709 | end: | |
1710 | /* We release the message's reference whatever the outcome */ | |
1711 | bt_message_put_ref(msg); | |
1712 | return status; | |
1713 | } | |
1714 | ||
1715 | static inline | |
1716 | void fill_message_array_from_output_messages( | |
1717 | struct trimmer_iterator *trimmer_it, | |
1718 | bt_message_array_const msgs, uint64_t capacity, uint64_t *count) | |
1719 | { | |
1720 | *count = 0; | |
1721 | ||
1722 | /* | |
1723 | * Move auto-seek messages to the output array (which is this | |
1724 | * iterator's base message array). | |
1725 | */ | |
1726 | while (capacity > 0 && !g_queue_is_empty(trimmer_it->output_messages)) { | |
1727 | msgs[*count] = pop_message(trimmer_it); | |
1728 | capacity--; | |
1729 | (*count)++; | |
1730 | } | |
1731 | ||
1732 | BT_ASSERT(*count > 0); | |
1733 | } | |
1734 | ||
1735 | static inline | |
fb25b9e3 | 1736 | bt_component_class_message_iterator_next_method_status state_ending( |
781751d8 PP |
1737 | struct trimmer_iterator *trimmer_it, |
1738 | bt_message_array_const msgs, uint64_t capacity, | |
1739 | uint64_t *count) | |
1740 | { | |
fb25b9e3 PP |
1741 | bt_component_class_message_iterator_next_method_status status = |
1742 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
781751d8 PP |
1743 | |
1744 | if (g_queue_is_empty(trimmer_it->output_messages)) { | |
1745 | trimmer_it->state = TRIMMER_ITERATOR_STATE_ENDED; | |
fb25b9e3 | 1746 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
781751d8 PP |
1747 | goto end; |
1748 | } | |
1749 | ||
1750 | fill_message_array_from_output_messages(trimmer_it, msgs, | |
1751 | capacity, count); | |
1752 | ||
1753 | end: | |
1754 | return status; | |
1755 | } | |
1756 | ||
1757 | static inline | |
fb25b9e3 PP |
1758 | bt_component_class_message_iterator_next_method_status |
1759 | state_trim(struct trimmer_iterator *trimmer_it, | |
781751d8 PP |
1760 | bt_message_array_const msgs, uint64_t capacity, |
1761 | uint64_t *count) | |
1762 | { | |
fb25b9e3 PP |
1763 | bt_component_class_message_iterator_next_method_status status = |
1764 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
781751d8 PP |
1765 | bt_message_array_const my_msgs; |
1766 | uint64_t my_count; | |
1767 | uint64_t i; | |
1768 | bool reached_end = false; | |
1769 | ||
1770 | while (g_queue_is_empty(trimmer_it->output_messages)) { | |
1771 | status = (int) bt_self_component_port_input_message_iterator_next( | |
1772 | trimmer_it->upstream_iter, &my_msgs, &my_count); | |
fb25b9e3 PP |
1773 | if (G_UNLIKELY(status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK)) { |
1774 | if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END) { | |
781751d8 | 1775 | status = end_iterator_streams(trimmer_it); |
fb25b9e3 | 1776 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1777 | goto end; |
1778 | } | |
1779 | ||
1780 | trimmer_it->state = | |
1781 | TRIMMER_ITERATOR_STATE_ENDING; | |
1782 | status = state_ending(trimmer_it, msgs, | |
1783 | capacity, count); | |
1784 | } | |
1785 | ||
1786 | goto end; | |
1787 | } | |
1788 | ||
1789 | BT_ASSERT(my_count > 0); | |
1790 | ||
1791 | for (i = 0; i < my_count; i++) { | |
1792 | status = handle_message(trimmer_it, my_msgs[i], | |
1793 | &reached_end); | |
1794 | ||
1795 | /* | |
1796 | * handle_message() unconditionally consumes the | |
1797 | * message reference. | |
1798 | */ | |
1799 | my_msgs[i] = NULL; | |
1800 | ||
85e7137b | 1801 | if (G_UNLIKELY(status != |
fb25b9e3 | 1802 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK)) { |
781751d8 PP |
1803 | put_messages(my_msgs, my_count); |
1804 | goto end; | |
1805 | } | |
1806 | ||
85e7137b | 1807 | if (G_UNLIKELY(reached_end)) { |
781751d8 PP |
1808 | /* |
1809 | * This message's time was passed the | |
1810 | * trimming time range's end time: we | |
1811 | * are done. Their might still be | |
1812 | * messages in the output message queue, | |
1813 | * so move to the "ending" state and | |
1814 | * apply it immediately since | |
1815 | * state_trim() is called within the | |
1816 | * "next" method. | |
1817 | */ | |
1818 | put_messages(my_msgs, my_count); | |
1819 | trimmer_it->state = | |
1820 | TRIMMER_ITERATOR_STATE_ENDING; | |
1821 | status = state_ending(trimmer_it, msgs, | |
1822 | capacity, count); | |
1823 | goto end; | |
1824 | } | |
1825 | } | |
1826 | } | |
1827 | ||
1828 | /* | |
1829 | * There's at least one message in the output message queue: | |
1830 | * move the messages to the output message array. | |
1831 | */ | |
1832 | BT_ASSERT(!g_queue_is_empty(trimmer_it->output_messages)); | |
1833 | fill_message_array_from_output_messages(trimmer_it, msgs, | |
1834 | capacity, count); | |
1835 | ||
1836 | end: | |
1837 | return status; | |
1838 | } | |
1839 | ||
1840 | BT_HIDDEN | |
fb25b9e3 | 1841 | bt_component_class_message_iterator_next_method_status trimmer_msg_iter_next( |
781751d8 PP |
1842 | bt_self_message_iterator *self_msg_iter, |
1843 | bt_message_array_const msgs, uint64_t capacity, | |
1844 | uint64_t *count) | |
1845 | { | |
1846 | struct trimmer_iterator *trimmer_it = | |
1847 | bt_self_message_iterator_get_data(self_msg_iter); | |
fb25b9e3 PP |
1848 | bt_component_class_message_iterator_next_method_status status = |
1849 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
781751d8 PP |
1850 | |
1851 | BT_ASSERT(trimmer_it); | |
1852 | ||
85e7137b | 1853 | if (G_LIKELY(trimmer_it->state == TRIMMER_ITERATOR_STATE_TRIM)) { |
781751d8 | 1854 | status = state_trim(trimmer_it, msgs, capacity, count); |
fb25b9e3 | 1855 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1856 | goto end; |
1857 | } | |
1858 | } else { | |
1859 | switch (trimmer_it->state) { | |
1860 | case TRIMMER_ITERATOR_STATE_SET_BOUNDS_NS_FROM_ORIGIN: | |
1861 | status = state_set_trimmer_iterator_bounds(trimmer_it); | |
fb25b9e3 | 1862 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1863 | goto end; |
1864 | } | |
1865 | ||
1866 | status = state_seek_initially(trimmer_it); | |
fb25b9e3 | 1867 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1868 | goto end; |
1869 | } | |
1870 | ||
1871 | status = state_trim(trimmer_it, msgs, capacity, count); | |
fb25b9e3 | 1872 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1873 | goto end; |
1874 | } | |
1875 | ||
1876 | break; | |
1877 | case TRIMMER_ITERATOR_STATE_SEEK_INITIALLY: | |
1878 | status = state_seek_initially(trimmer_it); | |
fb25b9e3 | 1879 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1880 | goto end; |
1881 | } | |
1882 | ||
1883 | status = state_trim(trimmer_it, msgs, capacity, count); | |
fb25b9e3 | 1884 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1885 | goto end; |
1886 | } | |
1887 | ||
1888 | break; | |
1889 | case TRIMMER_ITERATOR_STATE_ENDING: | |
1890 | status = state_ending(trimmer_it, msgs, capacity, | |
1891 | count); | |
fb25b9e3 | 1892 | if (status != BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
781751d8 PP |
1893 | goto end; |
1894 | } | |
1895 | ||
1896 | break; | |
1897 | case TRIMMER_ITERATOR_STATE_ENDED: | |
fb25b9e3 | 1898 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
781751d8 PP |
1899 | break; |
1900 | default: | |
1901 | abort(); | |
1902 | } | |
1903 | } | |
1904 | ||
1905 | end: | |
1906 | return status; | |
1907 | } | |
1908 | ||
1909 | BT_HIDDEN | |
1910 | void trimmer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) | |
1911 | { | |
1912 | struct trimmer_iterator *trimmer_it = | |
1913 | bt_self_message_iterator_get_data(self_msg_iter); | |
1914 | ||
1915 | BT_ASSERT(trimmer_it); | |
1916 | destroy_trimmer_iterator(trimmer_it); | |
1917 | } |