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