| 1 | /* |
| 2 | * query.c |
| 3 | * |
| 4 | * Babeltrace CTF file system Reader Component queries |
| 5 | * |
| 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include "query.h" |
| 28 | #include <stdbool.h> |
| 29 | #include <babeltrace/assert-internal.h> |
| 30 | #include "metadata.h" |
| 31 | #include "../common/metadata/decoder.h" |
| 32 | #include <babeltrace/common-internal.h> |
| 33 | #include <babeltrace/babeltrace-internal.h> |
| 34 | #include <babeltrace/babeltrace.h> |
| 35 | #include "fs.h" |
| 36 | |
| 37 | #define BT_LOG_TAG "PLUGIN-CTF-FS-QUERY-SRC" |
| 38 | #include "logging.h" |
| 39 | |
| 40 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
| 41 | |
| 42 | struct range { |
| 43 | int64_t begin_ns; |
| 44 | int64_t end_ns; |
| 45 | bool set; |
| 46 | }; |
| 47 | |
| 48 | BT_HIDDEN |
| 49 | enum bt_query_status metadata_info_query( |
| 50 | bt_self_component_class_source *comp_class, |
| 51 | const bt_value *params, |
| 52 | const bt_value **user_result) |
| 53 | { |
| 54 | enum bt_query_status status = BT_QUERY_STATUS_OK; |
| 55 | bt_value *result = NULL; |
| 56 | const bt_value *path_value = NULL; |
| 57 | char *metadata_text = NULL; |
| 58 | FILE *metadata_fp = NULL; |
| 59 | GString *g_metadata_text = NULL; |
| 60 | int ret; |
| 61 | int bo; |
| 62 | const char *path; |
| 63 | bool is_packetized; |
| 64 | |
| 65 | result = bt_value_map_create(); |
| 66 | if (!result) { |
| 67 | status = BT_QUERY_STATUS_NOMEM; |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | BT_ASSERT(params); |
| 72 | |
| 73 | if (!bt_value_is_map(params)) { |
| 74 | BT_LOGE_STR("Query parameters is not a map value object."); |
| 75 | status = BT_QUERY_STATUS_INVALID_PARAMS; |
| 76 | goto error; |
| 77 | } |
| 78 | |
| 79 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
| 80 | path = bt_value_string_get(path_value); |
| 81 | |
| 82 | BT_ASSERT(path); |
| 83 | metadata_fp = ctf_fs_metadata_open_file(path); |
| 84 | if (!metadata_fp) { |
| 85 | BT_LOGE("Cannot open trace metadata: path=\"%s\".", path); |
| 86 | goto error; |
| 87 | } |
| 88 | |
| 89 | is_packetized = ctf_metadata_decoder_is_packetized(metadata_fp, |
| 90 | &bo); |
| 91 | |
| 92 | if (is_packetized) { |
| 93 | ret = ctf_metadata_decoder_packetized_file_stream_to_buf( |
| 94 | metadata_fp, &metadata_text, bo); |
| 95 | if (ret) { |
| 96 | BT_LOGE("Cannot decode packetized metadata file: path=\"%s\"", |
| 97 | path); |
| 98 | goto error; |
| 99 | } |
| 100 | } else { |
| 101 | long filesize; |
| 102 | |
| 103 | ret = fseek(metadata_fp, 0, SEEK_END); |
| 104 | if (ret) { |
| 105 | BT_LOGE_ERRNO("Failed to seek to the end of the metadata file", |
| 106 | ": path=\"%s\"", path); |
| 107 | goto error; |
| 108 | } |
| 109 | filesize = ftell(metadata_fp); |
| 110 | if (filesize < 0) { |
| 111 | BT_LOGE_ERRNO("Failed to get the current position in the metadata file", |
| 112 | ": path=\"%s\"", path); |
| 113 | goto error; |
| 114 | } |
| 115 | rewind(metadata_fp); |
| 116 | metadata_text = malloc(filesize + 1); |
| 117 | if (!metadata_text) { |
| 118 | BT_LOGE_STR("Cannot allocate buffer for metadata text."); |
| 119 | goto error; |
| 120 | } |
| 121 | |
| 122 | if (fread(metadata_text, filesize, 1, metadata_fp) != 1) { |
| 123 | BT_LOGE_ERRNO("Cannot read metadata file", ": path=\"%s\"", |
| 124 | path); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | metadata_text[filesize] = '\0'; |
| 129 | } |
| 130 | |
| 131 | g_metadata_text = g_string_new(NULL); |
| 132 | if (!g_metadata_text) { |
| 133 | goto error; |
| 134 | } |
| 135 | |
| 136 | if (strncmp(metadata_text, METADATA_TEXT_SIG, |
| 137 | sizeof(METADATA_TEXT_SIG) - 1) != 0) { |
| 138 | g_string_assign(g_metadata_text, METADATA_TEXT_SIG); |
| 139 | g_string_append(g_metadata_text, " */\n\n"); |
| 140 | } |
| 141 | |
| 142 | g_string_append(g_metadata_text, metadata_text); |
| 143 | |
| 144 | ret = bt_value_map_insert_string_entry(result, "text", |
| 145 | g_metadata_text->str); |
| 146 | if (ret) { |
| 147 | BT_LOGE_STR("Cannot insert metadata text into query result."); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | ret = bt_value_map_insert_bool_entry(result, "is-packetized", |
| 152 | is_packetized); |
| 153 | if (ret) { |
| 154 | BT_LOGE_STR("Cannot insert \"is-packetized\" attribute into query result."); |
| 155 | goto error; |
| 156 | } |
| 157 | |
| 158 | goto end; |
| 159 | |
| 160 | error: |
| 161 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 162 | result = NULL; |
| 163 | |
| 164 | if (status >= 0) { |
| 165 | status = BT_QUERY_STATUS_ERROR; |
| 166 | } |
| 167 | |
| 168 | end: |
| 169 | free(metadata_text); |
| 170 | |
| 171 | if (g_metadata_text) { |
| 172 | g_string_free(g_metadata_text, TRUE); |
| 173 | } |
| 174 | |
| 175 | if (metadata_fp) { |
| 176 | fclose(metadata_fp); |
| 177 | } |
| 178 | |
| 179 | *user_result = result; |
| 180 | return status; |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | int add_range(bt_value *info, struct range *range, |
| 185 | const char *range_name) |
| 186 | { |
| 187 | int ret = 0; |
| 188 | enum bt_value_status status; |
| 189 | bt_value *range_map = NULL; |
| 190 | |
| 191 | if (!range->set) { |
| 192 | /* Not an error. */ |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | range_map = bt_value_map_create(); |
| 197 | if (!range_map) { |
| 198 | ret = -1; |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | status = bt_value_map_insert_integer_entry(range_map, "begin", |
| 203 | range->begin_ns); |
| 204 | if (status != BT_VALUE_STATUS_OK) { |
| 205 | ret = -1; |
| 206 | goto end; |
| 207 | } |
| 208 | |
| 209 | status = bt_value_map_insert_integer_entry(range_map, "end", |
| 210 | range->end_ns); |
| 211 | if (status != BT_VALUE_STATUS_OK) { |
| 212 | ret = -1; |
| 213 | goto end; |
| 214 | } |
| 215 | |
| 216 | status = bt_value_map_insert_entry(info, range_name, |
| 217 | range_map); |
| 218 | if (status != BT_VALUE_STATUS_OK) { |
| 219 | ret = -1; |
| 220 | goto end; |
| 221 | } |
| 222 | |
| 223 | end: |
| 224 | bt_value_put_ref(range_map); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static |
| 229 | int add_stream_ids(bt_value *info, const bt_stream *stream) |
| 230 | { |
| 231 | int ret = 0; |
| 232 | int64_t stream_class_id, stream_instance_id; |
| 233 | enum bt_value_status status; |
| 234 | const bt_stream_class *stream_class = NULL; |
| 235 | |
| 236 | stream_instance_id = bt_stream_get_id(stream); |
| 237 | if (stream_instance_id != -1) { |
| 238 | status = bt_value_map_insert_integer_entry(info, "id", |
| 239 | stream_instance_id); |
| 240 | if (status != BT_VALUE_STATUS_OK) { |
| 241 | ret = -1; |
| 242 | goto end; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | stream_class = bt_stream_borrow_class_const(stream); |
| 247 | if (!stream_class) { |
| 248 | ret = -1; |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | stream_class_id = bt_stream_class_get_id(stream_class); |
| 253 | if (stream_class_id == -1) { |
| 254 | ret = -1; |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | status = bt_value_map_insert_integer_entry(info, "class-id", stream_class_id); |
| 259 | if (status != BT_VALUE_STATUS_OK) { |
| 260 | ret = -1; |
| 261 | goto end; |
| 262 | } |
| 263 | |
| 264 | end: |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static |
| 269 | int populate_stream_info(struct ctf_fs_ds_file_group *group, |
| 270 | bt_value *group_info, struct range *stream_range) |
| 271 | { |
| 272 | int ret = 0; |
| 273 | size_t file_idx; |
| 274 | enum bt_value_status status; |
| 275 | bt_value *file_paths; |
| 276 | |
| 277 | stream_range->begin_ns = INT64_MAX; |
| 278 | stream_range->end_ns = 0; |
| 279 | |
| 280 | file_paths = bt_value_array_create(); |
| 281 | if (!file_paths) { |
| 282 | ret = -1; |
| 283 | goto end; |
| 284 | } |
| 285 | |
| 286 | for (file_idx = 0; file_idx < group->ds_file_infos->len; file_idx++) { |
| 287 | int64_t file_begin_epoch, file_end_epoch; |
| 288 | struct ctf_fs_ds_file_info *info = |
| 289 | g_ptr_array_index(group->ds_file_infos, |
| 290 | file_idx); |
| 291 | |
| 292 | if (!info->index || info->index->entries->len == 0) { |
| 293 | BT_LOGW("Cannot determine range of unindexed stream file \'%s\'", |
| 294 | info->path->str); |
| 295 | ret = -1; |
| 296 | goto end; |
| 297 | } |
| 298 | |
| 299 | status = bt_value_array_append_string_element(file_paths, |
| 300 | info->path->str); |
| 301 | if (status != BT_VALUE_STATUS_OK) { |
| 302 | ret = -1; |
| 303 | goto end; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * file range is from timestamp_begin of the first entry to the |
| 308 | * timestamp_end of the last entry. |
| 309 | */ |
| 310 | file_begin_epoch = ((struct ctf_fs_ds_index_entry *) &g_array_index(info->index->entries, |
| 311 | struct ctf_fs_ds_index_entry, 0))->timestamp_begin_ns; |
| 312 | file_end_epoch = ((struct ctf_fs_ds_index_entry *) &g_array_index(info->index->entries, |
| 313 | struct ctf_fs_ds_index_entry, info->index->entries->len - 1))->timestamp_end_ns; |
| 314 | |
| 315 | stream_range->begin_ns = min(stream_range->begin_ns, file_begin_epoch); |
| 316 | stream_range->end_ns = max(stream_range->end_ns, file_end_epoch); |
| 317 | stream_range->set = true; |
| 318 | } |
| 319 | |
| 320 | if (stream_range->set) { |
| 321 | ret = add_range(group_info, stream_range, "range-ns"); |
| 322 | if (ret) { |
| 323 | goto end; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | status = bt_value_map_insert_entry(group_info, "paths", |
| 328 | file_paths); |
| 329 | if (status != BT_VALUE_STATUS_OK) { |
| 330 | ret = -1; |
| 331 | goto end; |
| 332 | } |
| 333 | |
| 334 | ret = add_stream_ids(group_info, group->stream); |
| 335 | if (ret) { |
| 336 | goto end; |
| 337 | } |
| 338 | end: |
| 339 | bt_value_put_ref(file_paths); |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | static |
| 344 | int populate_trace_info(const char *trace_path, const char *trace_name, |
| 345 | bt_value *trace_info) |
| 346 | { |
| 347 | int ret = 0; |
| 348 | size_t group_idx; |
| 349 | struct ctf_fs_trace *trace = NULL; |
| 350 | enum bt_value_status status; |
| 351 | bt_value *file_groups; |
| 352 | struct range trace_range = { |
| 353 | .begin_ns = INT64_MAX, |
| 354 | .end_ns = 0, |
| 355 | .set = false, |
| 356 | }; |
| 357 | struct range trace_intersection = { |
| 358 | .begin_ns = 0, |
| 359 | .end_ns = INT64_MAX, |
| 360 | .set = false, |
| 361 | }; |
| 362 | |
| 363 | file_groups = bt_value_array_create(); |
| 364 | if (!file_groups) { |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | status = bt_value_map_insert_string_entry(trace_info, "name", |
| 369 | trace_name); |
| 370 | if (status != BT_VALUE_STATUS_OK) { |
| 371 | ret = -1; |
| 372 | goto end; |
| 373 | } |
| 374 | status = bt_value_map_insert_string_entry(trace_info, "path", |
| 375 | trace_path); |
| 376 | if (status != BT_VALUE_STATUS_OK) { |
| 377 | ret = -1; |
| 378 | goto end; |
| 379 | } |
| 380 | |
| 381 | trace = ctf_fs_trace_create(trace_path, trace_name, NULL); |
| 382 | if (!trace) { |
| 383 | BT_LOGE("Failed to create fs trace at \'%s\'", trace_path); |
| 384 | ret = -1; |
| 385 | goto end; |
| 386 | } |
| 387 | |
| 388 | BT_ASSERT(trace->ds_file_groups); |
| 389 | /* Add trace range info only if it contains streams. */ |
| 390 | if (trace->ds_file_groups->len == 0) { |
| 391 | ret = -1; |
| 392 | goto end; |
| 393 | } |
| 394 | |
| 395 | /* Find range of all stream groups, and of the trace. */ |
| 396 | for (group_idx = 0; group_idx < trace->ds_file_groups->len; |
| 397 | group_idx++) { |
| 398 | bt_value *group_info; |
| 399 | struct range group_range = { .set = false }; |
| 400 | struct ctf_fs_ds_file_group *group = g_ptr_array_index( |
| 401 | trace->ds_file_groups, group_idx); |
| 402 | |
| 403 | group_info = bt_value_map_create(); |
| 404 | if (!group_info) { |
| 405 | ret = -1; |
| 406 | goto end; |
| 407 | } |
| 408 | |
| 409 | ret = populate_stream_info(group, group_info, &group_range); |
| 410 | if (ret) { |
| 411 | bt_value_put_ref(group_info); |
| 412 | goto end; |
| 413 | } |
| 414 | |
| 415 | if (group_range.set) { |
| 416 | trace_range.begin_ns = min(trace_range.begin_ns, |
| 417 | group_range.begin_ns); |
| 418 | trace_range.end_ns = max(trace_range.end_ns, |
| 419 | group_range.end_ns); |
| 420 | trace_range.set = true; |
| 421 | |
| 422 | trace_intersection.begin_ns = max(trace_intersection.begin_ns, |
| 423 | group_range.begin_ns); |
| 424 | trace_intersection.end_ns = min(trace_intersection.end_ns, |
| 425 | group_range.end_ns); |
| 426 | trace_intersection.set = true; |
| 427 | status = bt_value_array_append_element( |
| 428 | file_groups, |
| 429 | group_info); |
| 430 | bt_value_put_ref(group_info); |
| 431 | if (status != BT_VALUE_STATUS_OK) { |
| 432 | goto end; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | ret = add_range(trace_info, &trace_range, "range-ns"); |
| 438 | if (ret) { |
| 439 | goto end; |
| 440 | } |
| 441 | |
| 442 | if (trace_intersection.begin_ns < trace_intersection.end_ns) { |
| 443 | ret = add_range(trace_info, &trace_intersection, |
| 444 | "intersection-range-ns"); |
| 445 | if (ret) { |
| 446 | goto end; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | status = bt_value_map_insert_entry(trace_info, "streams", |
| 451 | file_groups); |
| 452 | BT_VALUE_PUT_REF_AND_RESET(file_groups); |
| 453 | if (status != BT_VALUE_STATUS_OK) { |
| 454 | ret = -1; |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | end: |
| 459 | bt_value_put_ref(file_groups); |
| 460 | ctf_fs_trace_destroy(trace); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | BT_HIDDEN |
| 465 | enum bt_query_status trace_info_query( |
| 466 | bt_self_component_class_source *comp_class, |
| 467 | const bt_value *params, |
| 468 | const bt_value **user_result) |
| 469 | { |
| 470 | enum bt_query_status status = BT_QUERY_STATUS_OK; |
| 471 | bt_value *result = NULL; |
| 472 | const bt_value *path_value = NULL; |
| 473 | int ret = 0; |
| 474 | const char *path = NULL; |
| 475 | GList *trace_paths = NULL; |
| 476 | GList *trace_names = NULL; |
| 477 | GList *tp_node = NULL; |
| 478 | GList *tn_node = NULL; |
| 479 | GString *normalized_path = NULL; |
| 480 | |
| 481 | BT_ASSERT(params); |
| 482 | |
| 483 | if (!bt_value_is_map(params)) { |
| 484 | BT_LOGE("Query parameters is not a map value object."); |
| 485 | status = BT_QUERY_STATUS_INVALID_PARAMS; |
| 486 | goto error; |
| 487 | } |
| 488 | |
| 489 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
| 490 | path = bt_value_string_get(path_value); |
| 491 | |
| 492 | normalized_path = bt_common_normalize_path(path, NULL); |
| 493 | if (!normalized_path) { |
| 494 | BT_LOGE("Failed to normalize path: `%s`.", path); |
| 495 | goto error; |
| 496 | } |
| 497 | BT_ASSERT(path); |
| 498 | |
| 499 | ret = ctf_fs_find_traces(&trace_paths, normalized_path->str); |
| 500 | if (ret) { |
| 501 | goto error; |
| 502 | } |
| 503 | |
| 504 | trace_names = ctf_fs_create_trace_names(trace_paths, |
| 505 | normalized_path->str); |
| 506 | if (!trace_names) { |
| 507 | BT_LOGE("Cannot create trace names from trace paths."); |
| 508 | goto error; |
| 509 | } |
| 510 | |
| 511 | result = bt_value_array_create(); |
| 512 | if (!result) { |
| 513 | status = BT_QUERY_STATUS_NOMEM; |
| 514 | goto error; |
| 515 | } |
| 516 | |
| 517 | /* Iterates over both trace paths and names simultaneously. */ |
| 518 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; |
| 519 | tp_node = g_list_next(tp_node), |
| 520 | tn_node = g_list_next(tn_node)) { |
| 521 | GString *trace_path = tp_node->data; |
| 522 | GString *trace_name = tn_node->data; |
| 523 | enum bt_value_status status; |
| 524 | bt_value *trace_info; |
| 525 | |
| 526 | trace_info = bt_value_map_create(); |
| 527 | if (!trace_info) { |
| 528 | BT_LOGE("Failed to create trace info map."); |
| 529 | goto error; |
| 530 | } |
| 531 | |
| 532 | ret = populate_trace_info(trace_path->str, trace_name->str, |
| 533 | trace_info); |
| 534 | if (ret) { |
| 535 | bt_value_put_ref(trace_info); |
| 536 | goto error; |
| 537 | } |
| 538 | |
| 539 | status = bt_value_array_append_element(result, trace_info); |
| 540 | bt_value_put_ref(trace_info); |
| 541 | if (status != BT_VALUE_STATUS_OK) { |
| 542 | goto error; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | goto end; |
| 547 | |
| 548 | error: |
| 549 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 550 | result = NULL; |
| 551 | |
| 552 | if (status >= 0) { |
| 553 | status = BT_QUERY_STATUS_ERROR; |
| 554 | } |
| 555 | |
| 556 | end: |
| 557 | if (normalized_path) { |
| 558 | g_string_free(normalized_path, TRUE); |
| 559 | } |
| 560 | if (trace_paths) { |
| 561 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { |
| 562 | if (tp_node->data) { |
| 563 | g_string_free(tp_node->data, TRUE); |
| 564 | } |
| 565 | } |
| 566 | g_list_free(trace_paths); |
| 567 | } |
| 568 | if (trace_names) { |
| 569 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { |
| 570 | if (tn_node->data) { |
| 571 | g_string_free(tn_node->data, TRUE); |
| 572 | } |
| 573 | } |
| 574 | g_list_free(trace_names); |
| 575 | } |
| 576 | |
| 577 | *user_result = result; |
| 578 | return status; |
| 579 | } |