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