| 1 | /* |
| 2 | * iterator.c |
| 3 | * |
| 4 | * Babeltrace Trace Trimmer Iterator |
| 5 | * |
| 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include "trimmer.h" |
| 30 | #include "iterator.h" |
| 31 | #include <babeltrace/component/notification/iterator.h> |
| 32 | #include <babeltrace/component/notification/private-iterator.h> |
| 33 | #include <babeltrace/component/notification/notification.h> |
| 34 | #include <babeltrace/component/notification/event.h> |
| 35 | #include <babeltrace/component/notification/stream.h> |
| 36 | #include <babeltrace/component/notification/packet.h> |
| 37 | #include <babeltrace/component/component-filter.h> |
| 38 | #include <babeltrace/component/private-component-filter.h> |
| 39 | #include <babeltrace/component/private-port.h> |
| 40 | #include <babeltrace/component/private-connection.h> |
| 41 | #include <babeltrace/component/private-component.h> |
| 42 | #include <babeltrace/ctf-ir/event.h> |
| 43 | #include <babeltrace/ctf-ir/stream.h> |
| 44 | #include <babeltrace/ctf-ir/stream-class.h> |
| 45 | #include <babeltrace/ctf-ir/clock-class.h> |
| 46 | #include <babeltrace/ctf-ir/packet.h> |
| 47 | #include <babeltrace/ctf-ir/trace.h> |
| 48 | #include <babeltrace/ctf-ir/fields.h> |
| 49 | #include <assert.h> |
| 50 | #include <plugins-common.h> |
| 51 | |
| 52 | BT_HIDDEN |
| 53 | void trimmer_iterator_destroy(struct bt_private_notification_iterator *it) |
| 54 | { |
| 55 | struct trimmer_iterator *it_data; |
| 56 | |
| 57 | it_data = bt_private_notification_iterator_get_user_data(it); |
| 58 | assert(it_data); |
| 59 | |
| 60 | bt_put(it_data->current_notification); |
| 61 | bt_put(it_data->input_iterator); |
| 62 | g_free(it_data); |
| 63 | } |
| 64 | |
| 65 | BT_HIDDEN |
| 66 | enum bt_notification_iterator_status trimmer_iterator_init( |
| 67 | struct bt_private_component *component, |
| 68 | struct bt_private_port *port, |
| 69 | struct bt_private_notification_iterator *iterator) |
| 70 | { |
| 71 | enum bt_notification_iterator_status ret = |
| 72 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 73 | enum bt_notification_iterator_status it_ret; |
| 74 | struct bt_private_port *input_port = NULL; |
| 75 | struct bt_private_connection *connection = NULL; |
| 76 | struct trimmer_iterator *it_data = g_new0(struct trimmer_iterator, 1); |
| 77 | |
| 78 | if (!it_data) { |
| 79 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 80 | goto end; |
| 81 | } |
| 82 | |
| 83 | /* Create a new iterator on the upstream component. */ |
| 84 | input_port = bt_private_component_filter_get_default_input_private_port( |
| 85 | component); |
| 86 | assert(input_port); |
| 87 | connection = bt_private_port_get_private_connection(input_port); |
| 88 | assert(connection); |
| 89 | |
| 90 | it_data->input_iterator = |
| 91 | bt_private_connection_create_notification_iterator(connection); |
| 92 | if (!it_data->input_iterator) { |
| 93 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 94 | goto end; |
| 95 | } |
| 96 | |
| 97 | it_ret = bt_private_notification_iterator_set_user_data(iterator, |
| 98 | it_data); |
| 99 | if (it_ret) { |
| 100 | goto end; |
| 101 | } |
| 102 | end: |
| 103 | bt_put(connection); |
| 104 | bt_put(input_port); |
| 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | BT_HIDDEN |
| 109 | struct bt_notification *trimmer_iterator_get( |
| 110 | struct bt_private_notification_iterator *iterator) |
| 111 | { |
| 112 | struct trimmer_iterator *trim_it; |
| 113 | |
| 114 | trim_it = bt_private_notification_iterator_get_user_data(iterator); |
| 115 | assert(trim_it); |
| 116 | |
| 117 | if (!trim_it->current_notification) { |
| 118 | enum bt_notification_iterator_status it_ret; |
| 119 | |
| 120 | it_ret = trimmer_iterator_next(iterator); |
| 121 | if (it_ret) { |
| 122 | goto end; |
| 123 | } |
| 124 | } |
| 125 | end: |
| 126 | return bt_get(trim_it->current_notification); |
| 127 | } |
| 128 | |
| 129 | static |
| 130 | int update_lazy_bound(struct trimmer_bound *bound, const char *name, |
| 131 | int64_t ts, bool *lazy_update) |
| 132 | { |
| 133 | struct tm tm; |
| 134 | int64_t value; |
| 135 | time_t timeval; |
| 136 | |
| 137 | *lazy_update = false; |
| 138 | |
| 139 | if (!bound->lazy) { |
| 140 | return 0; |
| 141 | } |
| 142 | tm.tm_isdst = -1; |
| 143 | timeval = ts / NSEC_PER_SEC; |
| 144 | |
| 145 | if (bound->lazy_values.gmt) { |
| 146 | /* Get day, month, year. */ |
| 147 | if (!gmtime_r(&timeval, &tm)) { |
| 148 | printf_error("Failure in gmtime_r()"); |
| 149 | goto error; |
| 150 | } |
| 151 | tm.tm_sec = bound->lazy_values.ss; |
| 152 | tm.tm_min = bound->lazy_values.mm; |
| 153 | tm.tm_hour = bound->lazy_values.hh; |
| 154 | timeval = timegm(&tm); |
| 155 | if (timeval < 0) { |
| 156 | printf_error("Failure in timegm(), incorrectly formatted %s timestamp", |
| 157 | name); |
| 158 | goto error; |
| 159 | } |
| 160 | } else { |
| 161 | /* Get day, month, year. */ |
| 162 | if (!localtime_r(&timeval, &tm)) { |
| 163 | printf_error("Failure in localtime_r()"); |
| 164 | goto error; |
| 165 | } |
| 166 | tm.tm_sec = bound->lazy_values.ss; |
| 167 | tm.tm_min = bound->lazy_values.mm; |
| 168 | tm.tm_hour = bound->lazy_values.hh; |
| 169 | timeval = mktime(&tm); |
| 170 | if (timeval < 0) { |
| 171 | printf_error("Failure in mktime(), incorrectly formatted %s timestamp", |
| 172 | name); |
| 173 | goto error; |
| 174 | } |
| 175 | } |
| 176 | value = (int64_t) timeval; |
| 177 | value *= NSEC_PER_SEC; |
| 178 | value += bound->lazy_values.ns; |
| 179 | bound->value = value; |
| 180 | bound->set = true; |
| 181 | bound->lazy = false; |
| 182 | *lazy_update = true; |
| 183 | return 0; |
| 184 | |
| 185 | error: |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | static |
| 190 | enum bt_notification_iterator_status |
| 191 | evaluate_event_notification(struct bt_notification *notification, |
| 192 | struct trimmer_bound *begin, struct trimmer_bound *end, |
| 193 | bool *_event_in_range) |
| 194 | { |
| 195 | int64_t ts; |
| 196 | int clock_ret; |
| 197 | struct bt_ctf_event *event = NULL; |
| 198 | bool in_range = true; |
| 199 | struct bt_ctf_clock_class *clock_class = NULL; |
| 200 | struct bt_ctf_trace *trace = NULL; |
| 201 | struct bt_ctf_stream *stream = NULL; |
| 202 | struct bt_ctf_stream_class *stream_class = NULL; |
| 203 | struct bt_ctf_clock_value *clock_value = NULL; |
| 204 | enum bt_notification_iterator_status ret = |
| 205 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 206 | bool lazy_update = false; |
| 207 | |
| 208 | event = bt_notification_event_get_event(notification); |
| 209 | assert(event); |
| 210 | |
| 211 | stream = bt_ctf_event_get_stream(event); |
| 212 | assert(stream); |
| 213 | |
| 214 | stream_class = bt_ctf_stream_get_class(stream); |
| 215 | assert(stream_class); |
| 216 | |
| 217 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 218 | assert(trace); |
| 219 | |
| 220 | /* FIXME multi-clock? */ |
| 221 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); |
| 222 | if (!clock_class) { |
| 223 | goto end; |
| 224 | } |
| 225 | |
| 226 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); |
| 227 | if (!clock_value) { |
| 228 | printf_error("Failed to retrieve clock value"); |
| 229 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | clock_ret = bt_ctf_clock_value_get_value_ns_from_epoch( |
| 234 | clock_value, &ts); |
| 235 | if (clock_ret) { |
| 236 | printf_error("Failed to retrieve clock value timestamp"); |
| 237 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 238 | goto end; |
| 239 | } |
| 240 | if (update_lazy_bound(begin, "begin", ts, &lazy_update)) { |
| 241 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 242 | goto end; |
| 243 | } |
| 244 | if (update_lazy_bound(end, "end", ts, &lazy_update)) { |
| 245 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 246 | goto end; |
| 247 | } |
| 248 | if (lazy_update && begin->set && end->set) { |
| 249 | if (begin->value > end->value) { |
| 250 | printf_error("Unexpected: time range begin value is above end value"); |
| 251 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 252 | goto end; |
| 253 | } |
| 254 | } |
| 255 | if (begin->set && ts < begin->value) { |
| 256 | in_range = false; |
| 257 | } |
| 258 | if (end->set && ts > end->value) { |
| 259 | in_range = false; |
| 260 | } |
| 261 | end: |
| 262 | bt_put(event); |
| 263 | bt_put(clock_class); |
| 264 | bt_put(trace); |
| 265 | bt_put(stream); |
| 266 | bt_put(stream_class); |
| 267 | bt_put(clock_value); |
| 268 | *_event_in_range = in_range; |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | static |
| 273 | int ns_from_integer_field(struct bt_ctf_field *integer, int64_t *ns) |
| 274 | { |
| 275 | int ret = 0; |
| 276 | int is_signed; |
| 277 | uint64_t raw_clock_value; |
| 278 | struct bt_ctf_field_type *integer_type = NULL; |
| 279 | struct bt_ctf_clock_class *clock_class = NULL; |
| 280 | struct bt_ctf_clock_value *clock_value = NULL; |
| 281 | |
| 282 | integer_type = bt_ctf_field_get_type(integer); |
| 283 | assert(integer_type); |
| 284 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class( |
| 285 | integer_type); |
| 286 | if (!clock_class) { |
| 287 | ret = -1; |
| 288 | goto end; |
| 289 | } |
| 290 | |
| 291 | is_signed = bt_ctf_field_type_integer_get_signed(integer_type); |
| 292 | if (!is_signed) { |
| 293 | ret = bt_ctf_field_unsigned_integer_get_value(integer, |
| 294 | &raw_clock_value); |
| 295 | if (ret) { |
| 296 | goto end; |
| 297 | } |
| 298 | } else { |
| 299 | /* Signed clock values are unsupported. */ |
| 300 | goto end; |
| 301 | } |
| 302 | |
| 303 | clock_value = bt_ctf_clock_value_create(clock_class, raw_clock_value); |
| 304 | if (!clock_value) { |
| 305 | goto end; |
| 306 | } |
| 307 | |
| 308 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns); |
| 309 | end: |
| 310 | bt_put(integer_type); |
| 311 | bt_put(clock_class); |
| 312 | bt_put(clock_value); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static |
| 317 | enum bt_notification_iterator_status evaluate_packet_notification( |
| 318 | struct bt_notification *notification, |
| 319 | struct trimmer_bound *begin, struct trimmer_bound *end, |
| 320 | bool *_packet_in_range) |
| 321 | { |
| 322 | enum bt_notification_iterator_status ret = |
| 323 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 324 | int64_t begin_ns, pkt_begin_ns, end_ns, pkt_end_ns; |
| 325 | bool in_range = true; |
| 326 | struct bt_ctf_packet *packet = NULL; |
| 327 | struct bt_ctf_field *packet_context = NULL, |
| 328 | *timestamp_begin = NULL, |
| 329 | *timestamp_end = NULL; |
| 330 | |
| 331 | switch (bt_notification_get_type(notification)) { |
| 332 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 333 | packet = bt_notification_packet_begin_get_packet(notification); |
| 334 | break; |
| 335 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 336 | packet = bt_notification_packet_end_get_packet(notification); |
| 337 | break; |
| 338 | default: |
| 339 | break; |
| 340 | } |
| 341 | assert(packet); |
| 342 | |
| 343 | packet_context = bt_ctf_packet_get_context(packet); |
| 344 | if (!packet_context) { |
| 345 | goto end; |
| 346 | } |
| 347 | |
| 348 | if (!bt_ctf_field_is_structure(packet_context)) { |
| 349 | goto end; |
| 350 | } |
| 351 | |
| 352 | timestamp_begin = bt_ctf_field_structure_get_field( |
| 353 | packet_context, "timestamp_begin"); |
| 354 | if (!timestamp_begin || !bt_ctf_field_is_integer(timestamp_begin)) { |
| 355 | goto end; |
| 356 | } |
| 357 | timestamp_end = bt_ctf_field_structure_get_field( |
| 358 | packet_context, "timestamp_end"); |
| 359 | if (!timestamp_end || !bt_ctf_field_is_integer(timestamp_end)) { |
| 360 | goto end; |
| 361 | } |
| 362 | |
| 363 | if (ns_from_integer_field(timestamp_begin, &pkt_begin_ns)) { |
| 364 | goto end; |
| 365 | } |
| 366 | if (ns_from_integer_field(timestamp_end, &pkt_end_ns)) { |
| 367 | goto end; |
| 368 | } |
| 369 | |
| 370 | begin_ns = begin->set ? begin->value : INT64_MIN; |
| 371 | end_ns = end->set ? end->value : INT64_MAX; |
| 372 | |
| 373 | /* |
| 374 | * Accept if there is any overlap between the selected region and the |
| 375 | * packet. |
| 376 | */ |
| 377 | in_range = (pkt_end_ns >= begin_ns) && (pkt_begin_ns <= end_ns); |
| 378 | end: |
| 379 | *_packet_in_range = in_range; |
| 380 | bt_put(packet); |
| 381 | bt_put(packet_context); |
| 382 | bt_put(timestamp_begin); |
| 383 | bt_put(timestamp_end); |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | /* Return true if the notification should be forwarded. */ |
| 388 | static |
| 389 | enum bt_notification_iterator_status evaluate_notification( |
| 390 | struct bt_notification *notification, |
| 391 | struct trimmer_bound *begin, struct trimmer_bound *end, |
| 392 | bool *in_range) |
| 393 | { |
| 394 | enum bt_notification_type type; |
| 395 | enum bt_notification_iterator_status ret = |
| 396 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 397 | |
| 398 | *in_range = true; |
| 399 | type = bt_notification_get_type(notification); |
| 400 | switch (type) { |
| 401 | case BT_NOTIFICATION_TYPE_EVENT: |
| 402 | ret = evaluate_event_notification(notification, begin, |
| 403 | end, in_range); |
| 404 | break; |
| 405 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 406 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 407 | ret = evaluate_packet_notification(notification, begin, |
| 408 | end, in_range); |
| 409 | break; |
| 410 | default: |
| 411 | /* Accept all other notifications. */ |
| 412 | break; |
| 413 | } |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | BT_HIDDEN |
| 418 | enum bt_notification_iterator_status trimmer_iterator_next( |
| 419 | struct bt_private_notification_iterator *iterator) |
| 420 | { |
| 421 | struct trimmer_iterator *trim_it = NULL; |
| 422 | struct bt_private_component *component = NULL; |
| 423 | struct trimmer *trimmer = NULL; |
| 424 | struct bt_notification_iterator *source_it = NULL; |
| 425 | enum bt_notification_iterator_status ret = |
| 426 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 427 | bool notification_in_range = false; |
| 428 | |
| 429 | trim_it = bt_private_notification_iterator_get_user_data(iterator); |
| 430 | assert(trim_it); |
| 431 | |
| 432 | component = bt_private_notification_iterator_get_private_component( |
| 433 | iterator); |
| 434 | assert(component); |
| 435 | trimmer = bt_private_component_get_user_data(component); |
| 436 | assert(trimmer); |
| 437 | |
| 438 | source_it = trim_it->input_iterator; |
| 439 | assert(source_it); |
| 440 | |
| 441 | while (!notification_in_range) { |
| 442 | struct bt_notification *notification; |
| 443 | |
| 444 | ret = bt_notification_iterator_next(source_it); |
| 445 | if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 446 | goto end; |
| 447 | } |
| 448 | |
| 449 | notification = bt_notification_iterator_get_notification( |
| 450 | source_it); |
| 451 | if (!notification) { |
| 452 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 453 | goto end; |
| 454 | } |
| 455 | |
| 456 | ret = evaluate_notification(notification, |
| 457 | &trimmer->begin, &trimmer->end, |
| 458 | ¬ification_in_range); |
| 459 | if (notification_in_range) { |
| 460 | BT_MOVE(trim_it->current_notification, notification); |
| 461 | } else { |
| 462 | bt_put(notification); |
| 463 | } |
| 464 | |
| 465 | if (ret != BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | end: |
| 470 | bt_put(component); |
| 471 | return ret; |
| 472 | } |
| 473 | |
| 474 | BT_HIDDEN |
| 475 | enum bt_notification_iterator_status trimmer_iterator_seek_time( |
| 476 | struct bt_private_notification_iterator *iterator, |
| 477 | int64_t time) |
| 478 | { |
| 479 | return BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 480 | } |