| 1 | /* |
| 2 | * trimmer.c |
| 3 | * |
| 4 | * Babeltrace Trace Trimmer |
| 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 | #define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT" |
| 30 | #include "logging.h" |
| 31 | |
| 32 | #include <babeltrace/compat/utc-internal.h> |
| 33 | #include <babeltrace/plugin/plugin-dev.h> |
| 34 | #include <babeltrace/graph/component.h> |
| 35 | #include <babeltrace/graph/private-component.h> |
| 36 | #include <babeltrace/graph/component-filter.h> |
| 37 | #include <babeltrace/graph/notification.h> |
| 38 | #include <babeltrace/graph/notification-iterator.h> |
| 39 | #include <babeltrace/graph/private-notification-iterator.h> |
| 40 | #include <babeltrace/graph/private-component-filter.h> |
| 41 | #include <babeltrace/graph/notification-event.h> |
| 42 | #include <plugins-common.h> |
| 43 | #include "trimmer.h" |
| 44 | #include "iterator.h" |
| 45 | #include <assert.h> |
| 46 | |
| 47 | static |
| 48 | void destroy_trimmer_data(struct trimmer *trimmer) |
| 49 | { |
| 50 | g_free(trimmer); |
| 51 | } |
| 52 | |
| 53 | static |
| 54 | struct trimmer *create_trimmer_data(void) |
| 55 | { |
| 56 | struct trimmer *trimmer; |
| 57 | |
| 58 | trimmer = g_new0(struct trimmer, 1); |
| 59 | if (!trimmer) { |
| 60 | goto end; |
| 61 | } |
| 62 | end: |
| 63 | return trimmer; |
| 64 | } |
| 65 | |
| 66 | void finalize_trimmer(struct bt_private_component *component) |
| 67 | { |
| 68 | void *data = bt_private_component_get_user_data(component); |
| 69 | |
| 70 | destroy_trimmer_data(data); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Parses a timestamp, figuring out its format. |
| 75 | * |
| 76 | * Returns a negative value if anything goes wrong. |
| 77 | * |
| 78 | * Expected formats: |
| 79 | * |
| 80 | * YYYY-MM-DD hh:mm:ss.ns |
| 81 | * hh:mm:ss.ns |
| 82 | * -ss.ns |
| 83 | * ss.ns |
| 84 | * YYYY-MM-DD hh:mm:ss |
| 85 | * hh:mm:ss |
| 86 | * -ss |
| 87 | * ss |
| 88 | */ |
| 89 | static |
| 90 | int timestamp_from_arg(const char *arg, struct trimmer *trimmer, |
| 91 | struct trimmer_bound *result_bound, bt_bool gmt) |
| 92 | { |
| 93 | int ret; |
| 94 | int64_t value; |
| 95 | unsigned int year, month, day, hh, mm, ss, ns; |
| 96 | |
| 97 | /* YYYY-MM-DD hh:mm:ss.ns */ |
| 98 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u.%u", |
| 99 | &year, &month, &day, &hh, &mm, &ss, &ns); |
| 100 | if (ret == 7) { |
| 101 | struct tm tm = { |
| 102 | .tm_sec = ss, |
| 103 | .tm_min = mm, |
| 104 | .tm_hour = hh, |
| 105 | .tm_mday = day, |
| 106 | .tm_mon = month - 1, |
| 107 | .tm_year = year - 1900, |
| 108 | .tm_isdst = -1, |
| 109 | }; |
| 110 | time_t result; |
| 111 | |
| 112 | if (gmt) { |
| 113 | result = bt_timegm(&tm); |
| 114 | if (result < 0) { |
| 115 | return -1; |
| 116 | } |
| 117 | } else { |
| 118 | result = mktime(&tm); |
| 119 | if (result < 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | } |
| 123 | value = (int64_t) result; |
| 124 | value *= NSEC_PER_SEC; |
| 125 | value += ns; |
| 126 | if (!trimmer->date) { |
| 127 | trimmer->year = year; |
| 128 | trimmer->month = month; |
| 129 | trimmer->day = day; |
| 130 | trimmer->date = true; |
| 131 | } |
| 132 | goto set; |
| 133 | } |
| 134 | /* hh:mm:ss.ns */ |
| 135 | ret = sscanf(arg, "%u:%u:%u.%u", |
| 136 | &hh, &mm, &ss, &ns); |
| 137 | if (ret == 4) { |
| 138 | if (!trimmer->date) { |
| 139 | /* We don't know which day until we get an event. */ |
| 140 | result_bound->lazy_values.hh = hh; |
| 141 | result_bound->lazy_values.mm = mm; |
| 142 | result_bound->lazy_values.ss = ss; |
| 143 | result_bound->lazy_values.ns = ns; |
| 144 | result_bound->lazy_values.gmt = gmt; |
| 145 | goto lazy; |
| 146 | } else { |
| 147 | struct tm tm = { |
| 148 | .tm_sec = ss, |
| 149 | .tm_min = mm, |
| 150 | .tm_hour = hh, |
| 151 | .tm_mday = trimmer->day, |
| 152 | .tm_mon = trimmer->month - 1, |
| 153 | .tm_year = trimmer->year - 1900, |
| 154 | .tm_isdst = -1, |
| 155 | }; |
| 156 | time_t result; |
| 157 | |
| 158 | if (gmt) { |
| 159 | result = bt_timegm(&tm); |
| 160 | if (result < 0) { |
| 161 | return -1; |
| 162 | } |
| 163 | } else { |
| 164 | result = mktime(&tm); |
| 165 | if (result < 0) { |
| 166 | return -1; |
| 167 | } |
| 168 | } |
| 169 | value = (int64_t) result; |
| 170 | value *= NSEC_PER_SEC; |
| 171 | value += ns; |
| 172 | goto set; |
| 173 | } |
| 174 | } |
| 175 | /* -ss.ns */ |
| 176 | ret = sscanf(arg, "-%u.%u", |
| 177 | &ss, &ns); |
| 178 | if (ret == 2) { |
| 179 | value = -ss * NSEC_PER_SEC; |
| 180 | value -= ns; |
| 181 | goto set; |
| 182 | } |
| 183 | /* ss.ns */ |
| 184 | ret = sscanf(arg, "%u.%u", |
| 185 | &ss, &ns); |
| 186 | if (ret == 2) { |
| 187 | value = ss * NSEC_PER_SEC; |
| 188 | value += ns; |
| 189 | goto set; |
| 190 | } |
| 191 | |
| 192 | /* YYYY-MM-DD hh:mm:ss */ |
| 193 | ret = sscanf(arg, "%u-%u-%u %u:%u:%u", |
| 194 | &year, &month, &day, &hh, &mm, &ss); |
| 195 | if (ret == 6) { |
| 196 | struct tm tm = { |
| 197 | .tm_sec = ss, |
| 198 | .tm_min = mm, |
| 199 | .tm_hour = hh, |
| 200 | .tm_mday = day, |
| 201 | .tm_mon = month - 1, |
| 202 | .tm_year = year - 1900, |
| 203 | .tm_isdst = -1, |
| 204 | }; |
| 205 | |
| 206 | if (gmt) { |
| 207 | value = bt_timegm(&tm); |
| 208 | if (value < 0) { |
| 209 | return -1; |
| 210 | } |
| 211 | } else { |
| 212 | value = mktime(&tm); |
| 213 | if (value < 0) { |
| 214 | return -1; |
| 215 | } |
| 216 | } |
| 217 | value *= NSEC_PER_SEC; |
| 218 | if (!trimmer->date) { |
| 219 | trimmer->year = year; |
| 220 | trimmer->month = month; |
| 221 | trimmer->day = day; |
| 222 | trimmer->date = true; |
| 223 | } |
| 224 | goto set; |
| 225 | } |
| 226 | /* hh:mm:ss */ |
| 227 | ret = sscanf(arg, "%u:%u:%u", |
| 228 | &hh, &mm, &ss); |
| 229 | if (ret == 3) { |
| 230 | if (!trimmer->date) { |
| 231 | /* We don't know which day until we get an event. */ |
| 232 | result_bound->lazy_values.hh = hh; |
| 233 | result_bound->lazy_values.mm = mm; |
| 234 | result_bound->lazy_values.ss = ss; |
| 235 | result_bound->lazy_values.ns = 0; |
| 236 | result_bound->lazy_values.gmt = gmt; |
| 237 | goto lazy; |
| 238 | } else { |
| 239 | struct tm tm = { |
| 240 | .tm_sec = ss, |
| 241 | .tm_min = mm, |
| 242 | .tm_hour = hh, |
| 243 | .tm_mday = trimmer->day, |
| 244 | .tm_mon = trimmer->month - 1, |
| 245 | .tm_year = trimmer->year - 1900, |
| 246 | .tm_isdst = -1, |
| 247 | }; |
| 248 | time_t result; |
| 249 | |
| 250 | if (gmt) { |
| 251 | result = bt_timegm(&tm); |
| 252 | if (result < 0) { |
| 253 | return -1; |
| 254 | } |
| 255 | } else { |
| 256 | result = mktime(&tm); |
| 257 | if (result < 0) { |
| 258 | return -1; |
| 259 | } |
| 260 | } |
| 261 | value = (int64_t) result; |
| 262 | value *= NSEC_PER_SEC; |
| 263 | goto set; |
| 264 | } |
| 265 | } |
| 266 | /* -ss */ |
| 267 | ret = sscanf(arg, "-%u", |
| 268 | &ss); |
| 269 | if (ret == 1) { |
| 270 | value = -ss * NSEC_PER_SEC; |
| 271 | goto set; |
| 272 | } |
| 273 | /* ss */ |
| 274 | ret = sscanf(arg, "%u", |
| 275 | &ss); |
| 276 | if (ret == 1) { |
| 277 | value = ss * NSEC_PER_SEC; |
| 278 | goto set; |
| 279 | } |
| 280 | |
| 281 | /* Not found. */ |
| 282 | return -1; |
| 283 | |
| 284 | set: |
| 285 | result_bound->value = value; |
| 286 | result_bound->set = true; |
| 287 | return 0; |
| 288 | |
| 289 | lazy: |
| 290 | result_bound->lazy = true; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static |
| 295 | enum bt_component_status init_from_params(struct trimmer *trimmer, |
| 296 | struct bt_value *params) |
| 297 | { |
| 298 | struct bt_value *value = NULL; |
| 299 | bt_bool gmt = BT_FALSE; |
| 300 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 301 | |
| 302 | assert(params); |
| 303 | |
| 304 | value = bt_value_map_get(params, "clock-gmt"); |
| 305 | if (value) { |
| 306 | enum bt_value_status value_ret; |
| 307 | |
| 308 | value_ret = bt_value_bool_get(value, &gmt); |
| 309 | if (value_ret) { |
| 310 | ret = BT_COMPONENT_STATUS_INVALID; |
| 311 | BT_LOGE_STR("Failed to retrieve clock-gmt value. Expecting a boolean"); |
| 312 | } |
| 313 | } |
| 314 | bt_put(value); |
| 315 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 316 | goto end; |
| 317 | } |
| 318 | |
| 319 | value = bt_value_map_get(params, "begin"); |
| 320 | if (value) { |
| 321 | enum bt_value_status value_ret; |
| 322 | const char *str; |
| 323 | |
| 324 | value_ret = bt_value_string_get(value, &str); |
| 325 | if (value_ret || timestamp_from_arg(str, |
| 326 | trimmer, &trimmer->begin, gmt)) { |
| 327 | ret = BT_COMPONENT_STATUS_INVALID; |
| 328 | BT_LOGE_STR("Failed to retrieve begin value. Expecting a timestamp string"); |
| 329 | } |
| 330 | } |
| 331 | bt_put(value); |
| 332 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 333 | goto end; |
| 334 | } |
| 335 | |
| 336 | value = bt_value_map_get(params, "end"); |
| 337 | if (value) { |
| 338 | enum bt_value_status value_ret; |
| 339 | const char *str; |
| 340 | |
| 341 | value_ret = bt_value_string_get(value, &str); |
| 342 | if (value_ret || timestamp_from_arg(str, |
| 343 | trimmer, &trimmer->end, gmt)) { |
| 344 | ret = BT_COMPONENT_STATUS_INVALID; |
| 345 | BT_LOGE_STR("Failed to retrieve end value. Expecting a timestamp string"); |
| 346 | } |
| 347 | } |
| 348 | bt_put(value); |
| 349 | end: |
| 350 | if (trimmer->begin.set && trimmer->end.set) { |
| 351 | if (trimmer->begin.value > trimmer->end.value) { |
| 352 | BT_LOGE_STR("Unexpected: time range begin value is above end value"); |
| 353 | ret = BT_COMPONENT_STATUS_INVALID; |
| 354 | } |
| 355 | } |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | enum bt_component_status trimmer_component_init( |
| 360 | struct bt_private_component *component, struct bt_value *params, |
| 361 | UNUSED_VAR void *init_method_data) |
| 362 | { |
| 363 | enum bt_component_status ret; |
| 364 | struct trimmer *trimmer = create_trimmer_data(); |
| 365 | |
| 366 | if (!trimmer) { |
| 367 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 368 | goto end; |
| 369 | } |
| 370 | |
| 371 | /* Create input and output ports */ |
| 372 | ret = bt_private_component_filter_add_input_private_port( |
| 373 | component, "in", NULL, NULL); |
| 374 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | ret = bt_private_component_filter_add_output_private_port( |
| 379 | component, "out", NULL, NULL); |
| 380 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 381 | goto error; |
| 382 | } |
| 383 | |
| 384 | ret = bt_private_component_set_user_data(component, trimmer); |
| 385 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 386 | goto error; |
| 387 | } |
| 388 | |
| 389 | ret = init_from_params(trimmer, params); |
| 390 | end: |
| 391 | return ret; |
| 392 | error: |
| 393 | destroy_trimmer_data(trimmer); |
| 394 | ret = BT_COMPONENT_STATUS_ERROR; |
| 395 | return ret; |
| 396 | } |