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