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