| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 5 | * |
| 6 | * Babeltrace CTF file system Reader Component queries |
| 7 | */ |
| 8 | |
| 9 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 10 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY" |
| 11 | #include "logging/log.h" |
| 12 | |
| 13 | #include "query.hpp" |
| 14 | #include <stdbool.h> |
| 15 | #include <glib.h> |
| 16 | #include <glib/gstdio.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include "common/assert.h" |
| 21 | #include "metadata.hpp" |
| 22 | #include "../common/metadata/decoder.hpp" |
| 23 | #include "common/common.h" |
| 24 | #include "common/macros.h" |
| 25 | #include <babeltrace2/babeltrace.h> |
| 26 | #include "fs.hpp" |
| 27 | #include "logging/comp-logging.h" |
| 28 | |
| 29 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
| 30 | |
| 31 | struct range |
| 32 | { |
| 33 | int64_t begin_ns = 0; |
| 34 | int64_t end_ns = 0; |
| 35 | bool set = false; |
| 36 | }; |
| 37 | |
| 38 | bt_component_class_query_method_status |
| 39 | metadata_info_query(bt_self_component_class_source *self_comp_class_src, const bt_value *params, |
| 40 | bt_logging_level log_level, const bt_value **user_result) |
| 41 | { |
| 42 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 43 | bt_self_component_class *self_comp_class = |
| 44 | bt_self_component_class_source_as_self_component_class(self_comp_class_src); |
| 45 | bt_value *result = NULL; |
| 46 | const bt_value *path_value = NULL; |
| 47 | FILE *metadata_fp = NULL; |
| 48 | int ret; |
| 49 | int bo; |
| 50 | const char *path; |
| 51 | bool is_packetized; |
| 52 | struct ctf_metadata_decoder *decoder = NULL; |
| 53 | ctf_metadata_decoder_config decoder_cfg {}; |
| 54 | enum ctf_metadata_decoder_status decoder_status; |
| 55 | |
| 56 | result = bt_value_map_create(); |
| 57 | if (!result) { |
| 58 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | BT_ASSERT(params); |
| 63 | |
| 64 | if (!bt_value_is_map(params)) { |
| 65 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 66 | "Query parameters is not a map value object."); |
| 67 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 68 | goto error; |
| 69 | } |
| 70 | |
| 71 | path_value = bt_value_map_borrow_entry_value_const(params, "path"); |
| 72 | if (!path_value) { |
| 73 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Mandatory `path` parameter missing"); |
| 74 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 75 | goto error; |
| 76 | } |
| 77 | |
| 78 | if (!bt_value_is_string(path_value)) { |
| 79 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 80 | "`path` parameter is required to be a string value"); |
| 81 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | path = bt_value_string_get(path_value); |
| 86 | |
| 87 | BT_ASSERT(path); |
| 88 | metadata_fp = ctf_fs_metadata_open_file(path); |
| 89 | if (!metadata_fp) { |
| 90 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Cannot open trace metadata: path=\"%s\".", |
| 91 | path); |
| 92 | goto error; |
| 93 | } |
| 94 | |
| 95 | ret = ctf_metadata_decoder_is_packetized(metadata_fp, &is_packetized, &bo, log_level, NULL); |
| 96 | if (ret) { |
| 97 | BT_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 98 | self_comp_class, |
| 99 | "Cannot check whether or not the metadata stream is packetized: path=\"%s\".", path); |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | decoder_cfg.log_level = log_level; |
| 104 | decoder_cfg.self_comp_class = self_comp_class; |
| 105 | decoder_cfg.keep_plain_text = true; |
| 106 | decoder = ctf_metadata_decoder_create(&decoder_cfg); |
| 107 | if (!decoder) { |
| 108 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 109 | "Cannot create metadata decoder: path=\"%s\".", path); |
| 110 | goto error; |
| 111 | } |
| 112 | |
| 113 | rewind(metadata_fp); |
| 114 | decoder_status = ctf_metadata_decoder_append_content(decoder, metadata_fp); |
| 115 | if (decoder_status) { |
| 116 | BT_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 117 | self_comp_class, "Cannot update metadata decoder's content: path=\"%s\".", path); |
| 118 | goto error; |
| 119 | } |
| 120 | |
| 121 | ret = bt_value_map_insert_string_entry(result, "text", ctf_metadata_decoder_get_text(decoder)); |
| 122 | if (ret) { |
| 123 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 124 | "Cannot insert metadata text into query result."); |
| 125 | goto error; |
| 126 | } |
| 127 | |
| 128 | ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized); |
| 129 | if (ret) { |
| 130 | BT_COMP_CLASS_LOGE_APPEND_CAUSE( |
| 131 | self_comp_class, "Cannot insert \"is-packetized\" attribute into query result."); |
| 132 | goto error; |
| 133 | } |
| 134 | |
| 135 | goto end; |
| 136 | |
| 137 | error: |
| 138 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 139 | result = NULL; |
| 140 | |
| 141 | if (status >= 0) { |
| 142 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 143 | } |
| 144 | |
| 145 | end: |
| 146 | ctf_metadata_decoder_destroy(decoder); |
| 147 | |
| 148 | if (metadata_fp) { |
| 149 | ret = fclose(metadata_fp); |
| 150 | if (ret) { |
| 151 | BT_LOGE_ERRNO("Cannot close metatada file stream", ": path=\"%s\"", path); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | *user_result = result; |
| 156 | return status; |
| 157 | } |
| 158 | |
| 159 | static int add_range(bt_value *info, struct range *range, const char *range_name) |
| 160 | { |
| 161 | int ret = 0; |
| 162 | bt_value_map_insert_entry_status status; |
| 163 | bt_value *range_map; |
| 164 | |
| 165 | if (!range->set) { |
| 166 | /* Not an error. */ |
| 167 | goto end; |
| 168 | } |
| 169 | |
| 170 | status = bt_value_map_insert_empty_map_entry(info, range_name, &range_map); |
| 171 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 172 | ret = -1; |
| 173 | goto end; |
| 174 | } |
| 175 | |
| 176 | status = bt_value_map_insert_signed_integer_entry(range_map, "begin", range->begin_ns); |
| 177 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 178 | ret = -1; |
| 179 | goto end; |
| 180 | } |
| 181 | |
| 182 | status = bt_value_map_insert_signed_integer_entry(range_map, "end", range->end_ns); |
| 183 | if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 184 | ret = -1; |
| 185 | goto end; |
| 186 | } |
| 187 | |
| 188 | end: |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *group_info, |
| 193 | struct range *stream_range) |
| 194 | { |
| 195 | int ret = 0; |
| 196 | bt_value_map_insert_entry_status insert_status; |
| 197 | struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry; |
| 198 | gchar *port_name = NULL; |
| 199 | |
| 200 | /* |
| 201 | * Since each `struct ctf_fs_ds_file_group` has a sorted array of |
| 202 | * `struct ctf_fs_ds_index_entry`, we can compute the stream range from |
| 203 | * the timestamp_begin of the first index entry and the timestamp_end |
| 204 | * of the last index entry. |
| 205 | */ |
| 206 | BT_ASSERT(group->index); |
| 207 | BT_ASSERT(group->index->entries); |
| 208 | BT_ASSERT(group->index->entries->len > 0); |
| 209 | |
| 210 | /* First entry. */ |
| 211 | first_ds_index_entry = |
| 212 | (struct ctf_fs_ds_index_entry *) g_ptr_array_index(group->index->entries, 0); |
| 213 | |
| 214 | /* Last entry. */ |
| 215 | last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index( |
| 216 | group->index->entries, group->index->entries->len - 1); |
| 217 | |
| 218 | stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns; |
| 219 | stream_range->end_ns = last_ds_index_entry->timestamp_end_ns; |
| 220 | |
| 221 | /* |
| 222 | * If any of the begin and end timestamps is not set it means that |
| 223 | * packets don't include `timestamp_begin` _and_ `timestamp_end` fields |
| 224 | * in their packet context so we can't set the range. |
| 225 | */ |
| 226 | stream_range->set = |
| 227 | stream_range->begin_ns != UINT64_C(-1) && stream_range->end_ns != UINT64_C(-1); |
| 228 | |
| 229 | ret = add_range(group_info, stream_range, "range-ns"); |
| 230 | if (ret) { |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | port_name = ctf_fs_make_port_name(group); |
| 235 | if (!port_name) { |
| 236 | ret = -1; |
| 237 | goto end; |
| 238 | } |
| 239 | |
| 240 | insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name); |
| 241 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 242 | ret = -1; |
| 243 | goto end; |
| 244 | } |
| 245 | |
| 246 | end: |
| 247 | g_free(port_name); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info, |
| 252 | bt_logging_level log_level, bt_self_component_class *self_comp_class) |
| 253 | { |
| 254 | int ret = 0; |
| 255 | size_t group_idx; |
| 256 | bt_value_map_insert_entry_status insert_status; |
| 257 | bt_value_array_append_element_status append_status; |
| 258 | bt_value *file_groups = NULL; |
| 259 | |
| 260 | BT_ASSERT(trace->ds_file_groups); |
| 261 | /* Add trace range info only if it contains streams. */ |
| 262 | if (trace->ds_file_groups->len == 0) { |
| 263 | ret = -1; |
| 264 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Trace has no streams: trace-path=%s", |
| 265 | trace->path->str); |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 269 | insert_status = bt_value_map_insert_empty_array_entry(trace_info, "stream-infos", &file_groups); |
| 270 | if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 271 | ret = -1; |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | /* Find range of all stream groups, and of the trace. */ |
| 276 | for (group_idx = 0; group_idx < trace->ds_file_groups->len; group_idx++) { |
| 277 | bt_value *group_info; |
| 278 | range group_range; |
| 279 | ctf_fs_ds_file_group *group = |
| 280 | (ctf_fs_ds_file_group *) g_ptr_array_index(trace->ds_file_groups, group_idx); |
| 281 | |
| 282 | append_status = bt_value_array_append_empty_map_element(file_groups, &group_info); |
| 283 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 284 | ret = -1; |
| 285 | goto end; |
| 286 | } |
| 287 | |
| 288 | ret = populate_stream_info(group, group_info, &group_range); |
| 289 | if (ret) { |
| 290 | goto end; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | end: |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | bt_component_class_query_method_status |
| 299 | trace_infos_query(bt_self_component_class_source *self_comp_class_src, const bt_value *params, |
| 300 | bt_logging_level log_level, const bt_value **user_result) |
| 301 | { |
| 302 | struct ctf_fs_component *ctf_fs = NULL; |
| 303 | bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 304 | bt_self_component_class *self_comp_class = |
| 305 | bt_self_component_class_source_as_self_component_class(self_comp_class_src); |
| 306 | bt_value *result = NULL; |
| 307 | const bt_value *inputs_value = NULL; |
| 308 | const bt_value *trace_name_value; |
| 309 | int ret = 0; |
| 310 | bt_value *trace_info = NULL; |
| 311 | bt_value_array_append_element_status append_status; |
| 312 | |
| 313 | BT_ASSERT(params); |
| 314 | |
| 315 | if (!bt_value_is_map(params)) { |
| 316 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, |
| 317 | "Query parameters is not a map value object."); |
| 318 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | ctf_fs = ctf_fs_component_create(log_level, NULL); |
| 323 | if (!ctf_fs) { |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs, NULL, |
| 328 | self_comp_class)) { |
| 329 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 330 | goto error; |
| 331 | } |
| 332 | |
| 333 | if (ctf_fs_component_create_ctf_fs_trace(ctf_fs, inputs_value, trace_name_value, NULL, |
| 334 | self_comp_class)) { |
| 335 | goto error; |
| 336 | } |
| 337 | |
| 338 | result = bt_value_array_create(); |
| 339 | if (!result) { |
| 340 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 341 | goto error; |
| 342 | } |
| 343 | |
| 344 | append_status = bt_value_array_append_empty_map_element(result, &trace_info); |
| 345 | if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 346 | BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Failed to create trace info map."); |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | ret = populate_trace_info(ctf_fs->trace, trace_info, log_level, self_comp_class); |
| 351 | if (ret) { |
| 352 | goto error; |
| 353 | } |
| 354 | |
| 355 | goto end; |
| 356 | |
| 357 | error: |
| 358 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 359 | |
| 360 | if (status >= 0) { |
| 361 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 362 | } |
| 363 | |
| 364 | end: |
| 365 | if (ctf_fs) { |
| 366 | ctf_fs_destroy(ctf_fs); |
| 367 | ctf_fs = NULL; |
| 368 | } |
| 369 | |
| 370 | *user_result = result; |
| 371 | return status; |
| 372 | } |
| 373 | |
| 374 | bt_component_class_query_method_status |
| 375 | support_info_query(bt_self_component_class_source *comp_class, const bt_value *params, |
| 376 | bt_logging_level log_level, const bt_value **user_result) |
| 377 | { |
| 378 | const bt_value *input_type_value; |
| 379 | const char *input_type; |
| 380 | bt_component_class_query_method_status status; |
| 381 | bt_value_map_insert_entry_status insert_entry_status; |
| 382 | double weight = 0; |
| 383 | gchar *metadata_path = NULL; |
| 384 | bt_value *result = NULL; |
| 385 | struct ctf_metadata_decoder *metadata_decoder = NULL; |
| 386 | FILE *metadata_file = NULL; |
| 387 | char uuid_str[BT_UUID_STR_LEN + 1]; |
| 388 | bool has_uuid = false; |
| 389 | const bt_value *input_value; |
| 390 | const char *input; |
| 391 | |
| 392 | input_type_value = bt_value_map_borrow_entry_value_const(params, "type"); |
| 393 | BT_ASSERT(input_type_value); |
| 394 | BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING); |
| 395 | input_type = bt_value_string_get(input_type_value); |
| 396 | |
| 397 | if (strcmp(input_type, "directory") != 0) { |
| 398 | goto create_result; |
| 399 | } |
| 400 | |
| 401 | input_value = bt_value_map_borrow_entry_value_const(params, "input"); |
| 402 | BT_ASSERT(input_value); |
| 403 | BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING); |
| 404 | input = bt_value_string_get(input_value); |
| 405 | |
| 406 | metadata_path = g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL); |
| 407 | if (!metadata_path) { |
| 408 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 409 | goto end; |
| 410 | } |
| 411 | |
| 412 | metadata_file = g_fopen(metadata_path, "rb"); |
| 413 | if (metadata_file) { |
| 414 | ctf_metadata_decoder_config metadata_decoder_config {}; |
| 415 | enum ctf_metadata_decoder_status decoder_status; |
| 416 | bt_uuid_t uuid; |
| 417 | |
| 418 | metadata_decoder_config.log_level = log_level; |
| 419 | metadata_decoder_config.self_comp_class = |
| 420 | bt_self_component_class_source_as_self_component_class(comp_class); |
| 421 | |
| 422 | metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config); |
| 423 | if (!metadata_decoder) { |
| 424 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 425 | goto end; |
| 426 | } |
| 427 | |
| 428 | decoder_status = ctf_metadata_decoder_append_content(metadata_decoder, metadata_file); |
| 429 | if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) { |
| 430 | BT_LOGW("cannot append metadata content: metadata-decoder-status=%d", decoder_status); |
| 431 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; |
| 432 | goto end; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * We were able to parse the metadata file, so we are |
| 437 | * confident it's a CTF trace. |
| 438 | */ |
| 439 | weight = 0.75; |
| 440 | |
| 441 | /* If the trace has a UUID, return the stringified UUID as the group. */ |
| 442 | if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder, uuid) == 0) { |
| 443 | bt_uuid_to_str(uuid, uuid_str); |
| 444 | has_uuid = true; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | create_result: |
| 449 | result = bt_value_map_create(); |
| 450 | if (!result) { |
| 451 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; |
| 452 | goto end; |
| 453 | } |
| 454 | |
| 455 | insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight); |
| 456 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 457 | status = (bt_component_class_query_method_status) insert_entry_status; |
| 458 | goto end; |
| 459 | } |
| 460 | |
| 461 | /* We are not supposed to have weight == 0 and a UUID. */ |
| 462 | BT_ASSERT(weight > 0 || !has_uuid); |
| 463 | |
| 464 | if (weight > 0 && has_uuid) { |
| 465 | insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str); |
| 466 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 467 | status = (bt_component_class_query_method_status) insert_entry_status; |
| 468 | goto end; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | *user_result = result; |
| 473 | result = NULL; |
| 474 | status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 475 | |
| 476 | end: |
| 477 | g_free(metadata_path); |
| 478 | bt_value_put_ref(result); |
| 479 | ctf_metadata_decoder_destroy(metadata_decoder); |
| 480 | |
| 481 | return status; |
| 482 | } |