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