| 1 | /* |
| 2 | * fs.c |
| 3 | * |
| 4 | * Babeltrace CTF file system Reader Component |
| 5 | * |
| 6 | * Copyright 2015-2017 Philippe Proulx <pproulx@efficios.com> |
| 7 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | * SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include <babeltrace/common-internal.h> |
| 29 | #include <babeltrace/ctf-ir/packet.h> |
| 30 | #include <babeltrace/ctf-ir/clock-class.h> |
| 31 | #include <babeltrace/ctf-ir/stream.h> |
| 32 | #include <babeltrace/ctf-ir/fields.h> |
| 33 | #include <babeltrace/graph/private-port.h> |
| 34 | #include <babeltrace/graph/private-component.h> |
| 35 | #include <babeltrace/graph/private-component-source.h> |
| 36 | #include <babeltrace/graph/private-notification-iterator.h> |
| 37 | #include <babeltrace/graph/component.h> |
| 38 | #include <babeltrace/graph/notification-iterator.h> |
| 39 | #include <babeltrace/graph/clock-class-priority-map.h> |
| 40 | #include <plugins-common.h> |
| 41 | #include <glib.h> |
| 42 | #include <assert.h> |
| 43 | #include <inttypes.h> |
| 44 | #include <stdbool.h> |
| 45 | #include "fs.h" |
| 46 | #include "metadata.h" |
| 47 | #include "data-stream-file.h" |
| 48 | #include "file.h" |
| 49 | #include "../common/metadata/decoder.h" |
| 50 | #include "query.h" |
| 51 | |
| 52 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC" |
| 53 | #include "logging.h" |
| 54 | |
| 55 | static |
| 56 | int notif_iter_data_set_current_ds_file(struct ctf_fs_notif_iter_data *notif_iter_data) |
| 57 | { |
| 58 | struct ctf_fs_ds_file_info *ds_file_info; |
| 59 | int ret = 0; |
| 60 | |
| 61 | assert(notif_iter_data->ds_file_info_index < |
| 62 | notif_iter_data->ds_file_group->ds_file_infos->len); |
| 63 | ds_file_info = g_ptr_array_index( |
| 64 | notif_iter_data->ds_file_group->ds_file_infos, |
| 65 | notif_iter_data->ds_file_info_index); |
| 66 | |
| 67 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); |
| 68 | notif_iter_data->ds_file = ctf_fs_ds_file_create( |
| 69 | notif_iter_data->ds_file_group->ctf_fs_trace, |
| 70 | notif_iter_data->ds_file_group->stream, |
| 71 | ds_file_info->path->str); |
| 72 | if (!notif_iter_data->ds_file) { |
| 73 | ret = -1; |
| 74 | } |
| 75 | |
| 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static |
| 80 | void ctf_fs_notif_iter_data_destroy( |
| 81 | struct ctf_fs_notif_iter_data *notif_iter_data) |
| 82 | { |
| 83 | if (!notif_iter_data) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | ctf_fs_ds_file_destroy(notif_iter_data->ds_file); |
| 88 | g_free(notif_iter_data); |
| 89 | } |
| 90 | |
| 91 | struct bt_notification_iterator_next_return ctf_fs_iterator_next( |
| 92 | struct bt_private_notification_iterator *iterator) |
| 93 | { |
| 94 | struct bt_notification_iterator_next_return next_ret; |
| 95 | struct ctf_fs_notif_iter_data *notif_iter_data = |
| 96 | bt_private_notification_iterator_get_user_data(iterator); |
| 97 | int ret; |
| 98 | |
| 99 | assert(notif_iter_data->ds_file); |
| 100 | next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file); |
| 101 | if (next_ret.status == BT_NOTIFICATION_ITERATOR_STATUS_END) { |
| 102 | assert(!next_ret.notification); |
| 103 | notif_iter_data->ds_file_info_index++; |
| 104 | |
| 105 | if (notif_iter_data->ds_file_info_index == |
| 106 | notif_iter_data->ds_file_group->ds_file_infos->len) { |
| 107 | /* |
| 108 | * No more stream files to read: we reached the |
| 109 | * real end. |
| 110 | */ |
| 111 | goto end; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Open and start reading the next stream file within |
| 116 | * our stream file group. |
| 117 | */ |
| 118 | ret = notif_iter_data_set_current_ds_file(notif_iter_data); |
| 119 | if (ret) { |
| 120 | next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | next_ret = ctf_fs_ds_file_next(notif_iter_data->ds_file); |
| 125 | |
| 126 | /* |
| 127 | * We should not get BT_NOTIFICATION_ITERATOR_STATUS_END |
| 128 | * with a brand new stream file because empty stream |
| 129 | * files are not even part of stream file groups, which |
| 130 | * means we're sure to get at least one pair of "packet |
| 131 | * begin" and "packet end" notifications in the case of |
| 132 | * a single, empty packet. |
| 133 | */ |
| 134 | assert(next_ret.status != BT_NOTIFICATION_ITERATOR_STATUS_END); |
| 135 | } |
| 136 | |
| 137 | end: |
| 138 | return next_ret; |
| 139 | } |
| 140 | |
| 141 | void ctf_fs_iterator_finalize(struct bt_private_notification_iterator *it) |
| 142 | { |
| 143 | void *notif_iter_data = |
| 144 | bt_private_notification_iterator_get_user_data(it); |
| 145 | |
| 146 | ctf_fs_notif_iter_data_destroy(notif_iter_data); |
| 147 | } |
| 148 | |
| 149 | enum bt_notification_iterator_status ctf_fs_iterator_init( |
| 150 | struct bt_private_notification_iterator *it, |
| 151 | struct bt_private_port *port) |
| 152 | { |
| 153 | struct ctf_fs_port_data *port_data; |
| 154 | struct ctf_fs_notif_iter_data *notif_iter_data = NULL; |
| 155 | enum bt_notification_iterator_status ret = |
| 156 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 157 | int iret; |
| 158 | |
| 159 | port_data = bt_private_port_get_user_data(port); |
| 160 | if (!port_data) { |
| 161 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVALID; |
| 162 | goto error; |
| 163 | } |
| 164 | |
| 165 | notif_iter_data = g_new0(struct ctf_fs_notif_iter_data, 1); |
| 166 | if (!notif_iter_data) { |
| 167 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 168 | goto error; |
| 169 | } |
| 170 | |
| 171 | notif_iter_data->ds_file_group = port_data->ds_file_group; |
| 172 | iret = notif_iter_data_set_current_ds_file(notif_iter_data); |
| 173 | if (iret) { |
| 174 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | ret = bt_private_notification_iterator_set_user_data(it, notif_iter_data); |
| 179 | if (ret) { |
| 180 | goto error; |
| 181 | } |
| 182 | |
| 183 | notif_iter_data = NULL; |
| 184 | goto end; |
| 185 | |
| 186 | error: |
| 187 | (void) bt_private_notification_iterator_set_user_data(it, NULL); |
| 188 | |
| 189 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 190 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 191 | } |
| 192 | |
| 193 | end: |
| 194 | ctf_fs_notif_iter_data_destroy(notif_iter_data); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | static |
| 199 | void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) |
| 200 | { |
| 201 | if (!ctf_fs) { |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (ctf_fs->traces) { |
| 206 | g_ptr_array_free(ctf_fs->traces, TRUE); |
| 207 | } |
| 208 | |
| 209 | if (ctf_fs->port_data) { |
| 210 | g_ptr_array_free(ctf_fs->port_data, TRUE); |
| 211 | } |
| 212 | |
| 213 | g_free(ctf_fs); |
| 214 | } |
| 215 | |
| 216 | BT_HIDDEN |
| 217 | void ctf_fs_trace_destroy(struct ctf_fs_trace *ctf_fs_trace) |
| 218 | { |
| 219 | if (!ctf_fs_trace) { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (ctf_fs_trace->ds_file_groups) { |
| 224 | g_ptr_array_free(ctf_fs_trace->ds_file_groups, TRUE); |
| 225 | } |
| 226 | |
| 227 | if (ctf_fs_trace->path) { |
| 228 | g_string_free(ctf_fs_trace->path, TRUE); |
| 229 | } |
| 230 | |
| 231 | if (ctf_fs_trace->name) { |
| 232 | g_string_free(ctf_fs_trace->name, TRUE); |
| 233 | } |
| 234 | |
| 235 | if (ctf_fs_trace->metadata) { |
| 236 | ctf_fs_metadata_fini(ctf_fs_trace->metadata); |
| 237 | g_free(ctf_fs_trace->metadata); |
| 238 | } |
| 239 | |
| 240 | bt_put(ctf_fs_trace->cc_prio_map); |
| 241 | g_free(ctf_fs_trace); |
| 242 | } |
| 243 | |
| 244 | static |
| 245 | void ctf_fs_trace_destroy_notifier(void *data) |
| 246 | { |
| 247 | struct ctf_fs_trace *trace = data; |
| 248 | ctf_fs_trace_destroy(trace); |
| 249 | } |
| 250 | |
| 251 | void ctf_fs_finalize(struct bt_private_component *component) |
| 252 | { |
| 253 | void *data = bt_private_component_get_user_data(component); |
| 254 | |
| 255 | ctf_fs_destroy(data); |
| 256 | } |
| 257 | |
| 258 | static |
| 259 | void port_data_destroy(void *data) { |
| 260 | struct ctf_fs_port_data *port_data = data; |
| 261 | |
| 262 | if (!port_data) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | g_free(port_data); |
| 267 | } |
| 268 | |
| 269 | static |
| 270 | int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, |
| 271 | struct ctf_fs_trace *ctf_fs_trace, |
| 272 | struct ctf_fs_ds_file_group *ds_file_group) |
| 273 | { |
| 274 | int ret = 0; |
| 275 | struct ctf_fs_port_data *port_data = NULL; |
| 276 | GString *port_name = NULL; |
| 277 | struct ctf_fs_ds_file_info *ds_file_info = |
| 278 | g_ptr_array_index(ds_file_group->ds_file_infos, 0); |
| 279 | |
| 280 | port_name = g_string_new(NULL); |
| 281 | if (!port_name) { |
| 282 | goto error; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Assign the name for the new output port. If there's more than |
| 287 | * one stream file in the stream file group, the first |
| 288 | * (earliest) stream file's path is used. |
| 289 | */ |
| 290 | assert(ds_file_group->ds_file_infos->len > 0); |
| 291 | ds_file_info = g_ptr_array_index(ds_file_group->ds_file_infos, 0); |
| 292 | g_string_assign(port_name, ds_file_info->path->str); |
| 293 | BT_LOGD("Creating one port named `%s`", port_name->str); |
| 294 | |
| 295 | /* Create output port for this file */ |
| 296 | port_data = g_new0(struct ctf_fs_port_data, 1); |
| 297 | if (!port_data) { |
| 298 | goto error; |
| 299 | } |
| 300 | |
| 301 | port_data->ds_file_group = ds_file_group; |
| 302 | ret = bt_private_component_source_add_output_private_port( |
| 303 | ctf_fs->priv_comp, port_name->str, port_data, NULL); |
| 304 | if (ret) { |
| 305 | goto error; |
| 306 | } |
| 307 | |
| 308 | g_ptr_array_add(ctf_fs->port_data, port_data); |
| 309 | port_data = NULL; |
| 310 | goto end; |
| 311 | |
| 312 | error: |
| 313 | ret = -1; |
| 314 | |
| 315 | end: |
| 316 | if (port_name) { |
| 317 | g_string_free(port_name, TRUE); |
| 318 | } |
| 319 | |
| 320 | port_data_destroy(port_data); |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static |
| 325 | int create_ports_for_trace(struct ctf_fs_component *ctf_fs, |
| 326 | struct ctf_fs_trace *ctf_fs_trace) |
| 327 | { |
| 328 | int ret = 0; |
| 329 | size_t i; |
| 330 | |
| 331 | /* Create one output port for each stream file group */ |
| 332 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { |
| 333 | struct ctf_fs_ds_file_group *ds_file_group = |
| 334 | g_ptr_array_index(ctf_fs_trace->ds_file_groups, i); |
| 335 | |
| 336 | ret = create_one_port_for_trace(ctf_fs, ctf_fs_trace, |
| 337 | ds_file_group); |
| 338 | if (ret) { |
| 339 | BT_LOGE("Cannot create output port."); |
| 340 | goto end; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | end: |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | uint64_t get_packet_header_stream_instance_id(struct ctf_fs_trace *ctf_fs_trace, |
| 349 | struct bt_ctf_field *packet_header_field) |
| 350 | { |
| 351 | struct bt_ctf_field *stream_instance_id_field = NULL; |
| 352 | uint64_t stream_instance_id = -1ULL; |
| 353 | int ret; |
| 354 | |
| 355 | if (!packet_header_field) { |
| 356 | goto end; |
| 357 | } |
| 358 | |
| 359 | stream_instance_id_field = bt_ctf_field_structure_get_field_by_name( |
| 360 | packet_header_field, "stream_instance_id"); |
| 361 | if (!stream_instance_id_field) { |
| 362 | goto end; |
| 363 | } |
| 364 | |
| 365 | ret = bt_ctf_field_unsigned_integer_get_value(stream_instance_id_field, |
| 366 | &stream_instance_id); |
| 367 | if (ret) { |
| 368 | stream_instance_id = -1ULL; |
| 369 | goto end; |
| 370 | } |
| 371 | |
| 372 | end: |
| 373 | bt_put(stream_instance_id_field); |
| 374 | return stream_instance_id; |
| 375 | } |
| 376 | |
| 377 | struct bt_ctf_stream_class *stream_class_from_packet_header( |
| 378 | struct ctf_fs_trace *ctf_fs_trace, |
| 379 | struct bt_ctf_field *packet_header_field) |
| 380 | { |
| 381 | struct bt_ctf_field *stream_id_field = NULL; |
| 382 | struct bt_ctf_stream_class *stream_class = NULL; |
| 383 | uint64_t stream_id = -1ULL; |
| 384 | int ret; |
| 385 | |
| 386 | if (!packet_header_field) { |
| 387 | goto single_stream_class; |
| 388 | } |
| 389 | |
| 390 | stream_id_field = bt_ctf_field_structure_get_field_by_name( |
| 391 | packet_header_field, "stream_id"); |
| 392 | if (!stream_id_field) { |
| 393 | goto single_stream_class; |
| 394 | } |
| 395 | |
| 396 | ret = bt_ctf_field_unsigned_integer_get_value(stream_id_field, |
| 397 | &stream_id); |
| 398 | if (ret) { |
| 399 | stream_id = -1ULL; |
| 400 | } |
| 401 | |
| 402 | if (stream_id == -1ULL) { |
| 403 | single_stream_class: |
| 404 | /* Single stream class */ |
| 405 | if (bt_ctf_trace_get_stream_class_count( |
| 406 | ctf_fs_trace->metadata->trace) == 0) { |
| 407 | goto end; |
| 408 | } |
| 409 | |
| 410 | stream_class = bt_ctf_trace_get_stream_class_by_index( |
| 411 | ctf_fs_trace->metadata->trace, 0); |
| 412 | } else { |
| 413 | stream_class = bt_ctf_trace_get_stream_class_by_id( |
| 414 | ctf_fs_trace->metadata->trace, stream_id); |
| 415 | } |
| 416 | |
| 417 | end: |
| 418 | bt_put(stream_id_field); |
| 419 | return stream_class; |
| 420 | } |
| 421 | |
| 422 | uint64_t get_packet_context_timestamp_begin_ns( |
| 423 | struct ctf_fs_trace *ctf_fs_trace, |
| 424 | struct bt_ctf_field *packet_context_field) |
| 425 | { |
| 426 | int ret; |
| 427 | struct bt_ctf_field *timestamp_begin_field = NULL; |
| 428 | struct bt_ctf_field_type *timestamp_begin_ft = NULL; |
| 429 | uint64_t timestamp_begin_raw_value = -1ULL; |
| 430 | uint64_t timestamp_begin_ns = -1ULL; |
| 431 | int64_t timestamp_begin_ns_signed; |
| 432 | struct bt_ctf_clock_class *timestamp_begin_clock_class = NULL; |
| 433 | struct bt_ctf_clock_value *clock_value = NULL; |
| 434 | |
| 435 | if (!packet_context_field) { |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | timestamp_begin_field = bt_ctf_field_structure_get_field_by_name( |
| 440 | packet_context_field, "timestamp_begin"); |
| 441 | if (!timestamp_begin_field) { |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | timestamp_begin_ft = bt_ctf_field_get_type(timestamp_begin_field); |
| 446 | assert(timestamp_begin_ft); |
| 447 | timestamp_begin_clock_class = |
| 448 | bt_ctf_field_type_integer_get_mapped_clock_class(timestamp_begin_ft); |
| 449 | if (!timestamp_begin_clock_class) { |
| 450 | goto end; |
| 451 | } |
| 452 | |
| 453 | ret = bt_ctf_field_unsigned_integer_get_value(timestamp_begin_field, |
| 454 | ×tamp_begin_raw_value); |
| 455 | if (ret) { |
| 456 | goto end; |
| 457 | } |
| 458 | |
| 459 | clock_value = bt_ctf_clock_value_create(timestamp_begin_clock_class, |
| 460 | timestamp_begin_raw_value); |
| 461 | if (!clock_value) { |
| 462 | goto end; |
| 463 | } |
| 464 | |
| 465 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, |
| 466 | ×tamp_begin_ns_signed); |
| 467 | if (ret) { |
| 468 | goto end; |
| 469 | } |
| 470 | |
| 471 | timestamp_begin_ns = (uint64_t) timestamp_begin_ns_signed; |
| 472 | |
| 473 | end: |
| 474 | bt_put(timestamp_begin_field); |
| 475 | bt_put(timestamp_begin_ft); |
| 476 | bt_put(timestamp_begin_clock_class); |
| 477 | bt_put(clock_value); |
| 478 | return timestamp_begin_ns; |
| 479 | } |
| 480 | |
| 481 | static |
| 482 | void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info) |
| 483 | { |
| 484 | if (!ds_file_info) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | if (ds_file_info->path) { |
| 489 | g_string_free(ds_file_info->path, TRUE); |
| 490 | } |
| 491 | |
| 492 | ctf_fs_ds_index_destroy(ds_file_info->index); |
| 493 | g_free(ds_file_info); |
| 494 | } |
| 495 | |
| 496 | static |
| 497 | struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, |
| 498 | uint64_t begin_ns, struct ctf_fs_ds_index *index) |
| 499 | { |
| 500 | struct ctf_fs_ds_file_info *ds_file_info; |
| 501 | |
| 502 | ds_file_info = g_new0(struct ctf_fs_ds_file_info, 1); |
| 503 | if (!ds_file_info) { |
| 504 | goto end; |
| 505 | } |
| 506 | |
| 507 | ds_file_info->path = g_string_new(path); |
| 508 | if (!ds_file_info->path) { |
| 509 | ctf_fs_ds_file_info_destroy(ds_file_info); |
| 510 | ds_file_info = NULL; |
| 511 | goto end; |
| 512 | } |
| 513 | |
| 514 | ds_file_info->begin_ns = begin_ns; |
| 515 | ds_file_info->index = index; |
| 516 | index = NULL; |
| 517 | |
| 518 | end: |
| 519 | ctf_fs_ds_index_destroy(index); |
| 520 | return ds_file_info; |
| 521 | } |
| 522 | |
| 523 | static |
| 524 | void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) |
| 525 | { |
| 526 | if (!ds_file_group) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | if (ds_file_group->ds_file_infos) { |
| 531 | g_ptr_array_free(ds_file_group->ds_file_infos, TRUE); |
| 532 | } |
| 533 | |
| 534 | bt_put(ds_file_group->stream); |
| 535 | g_free(ds_file_group); |
| 536 | } |
| 537 | |
| 538 | static |
| 539 | struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create( |
| 540 | struct ctf_fs_trace *ctf_fs_trace, |
| 541 | struct bt_ctf_stream_class *stream_class, |
| 542 | uint64_t stream_instance_id) |
| 543 | { |
| 544 | struct ctf_fs_ds_file_group *ds_file_group; |
| 545 | |
| 546 | assert(stream_class); |
| 547 | ds_file_group = g_new0(struct ctf_fs_ds_file_group, 1); |
| 548 | if (!ds_file_group) { |
| 549 | goto error; |
| 550 | } |
| 551 | |
| 552 | ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func( |
| 553 | (GDestroyNotify) ctf_fs_ds_file_info_destroy); |
| 554 | if (!ds_file_group->ds_file_infos) { |
| 555 | goto error; |
| 556 | } |
| 557 | |
| 558 | if (stream_instance_id == -1ULL) { |
| 559 | ds_file_group->stream = bt_ctf_stream_create( |
| 560 | stream_class, NULL); |
| 561 | } else { |
| 562 | ds_file_group->stream = bt_ctf_stream_create_with_id( |
| 563 | stream_class, NULL, stream_instance_id); |
| 564 | } |
| 565 | |
| 566 | if (!ds_file_group->stream) { |
| 567 | goto error; |
| 568 | } |
| 569 | |
| 570 | ds_file_group->ctf_fs_trace = ctf_fs_trace; |
| 571 | |
| 572 | goto end; |
| 573 | |
| 574 | error: |
| 575 | ctf_fs_ds_file_group_destroy(ds_file_group); |
| 576 | ds_file_group = NULL; |
| 577 | |
| 578 | end: |
| 579 | return ds_file_group; |
| 580 | } |
| 581 | |
| 582 | static |
| 583 | int ctf_fs_ds_file_group_add_ds_file_info( |
| 584 | struct ctf_fs_ds_file_group *ds_file_group, |
| 585 | const char *path, uint64_t begin_ns, |
| 586 | struct ctf_fs_ds_index *index) |
| 587 | { |
| 588 | struct ctf_fs_ds_file_info *ds_file_info; |
| 589 | gint i = 0; |
| 590 | int ret = 0; |
| 591 | |
| 592 | /* Onwership of index is transferred. */ |
| 593 | ds_file_info = ctf_fs_ds_file_info_create(path, begin_ns, index); |
| 594 | index = NULL; |
| 595 | if (!ds_file_info) { |
| 596 | goto error; |
| 597 | } |
| 598 | |
| 599 | /* Find a spot to insert this one */ |
| 600 | for (i = 0; i < ds_file_group->ds_file_infos->len; i++) { |
| 601 | struct ctf_fs_ds_file_info *other_ds_file_info = |
| 602 | g_ptr_array_index(ds_file_group->ds_file_infos, i); |
| 603 | |
| 604 | if (begin_ns < other_ds_file_info->begin_ns) { |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (i == ds_file_group->ds_file_infos->len) { |
| 610 | /* Append instead */ |
| 611 | i = -1; |
| 612 | } |
| 613 | |
| 614 | g_ptr_array_insert(ds_file_group->ds_file_infos, i, ds_file_info); |
| 615 | ds_file_info = NULL; |
| 616 | goto end; |
| 617 | |
| 618 | error: |
| 619 | ctf_fs_ds_file_info_destroy(ds_file_info); |
| 620 | ctf_fs_ds_index_destroy(index); |
| 621 | ret = -1; |
| 622 | end: |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static |
| 627 | int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, |
| 628 | const char *path) |
| 629 | { |
| 630 | struct bt_ctf_field *packet_header_field = NULL; |
| 631 | struct bt_ctf_field *packet_context_field = NULL; |
| 632 | struct bt_ctf_stream_class *stream_class = NULL; |
| 633 | uint64_t stream_instance_id = -1ULL; |
| 634 | uint64_t begin_ns = -1ULL; |
| 635 | struct ctf_fs_ds_file_group *ds_file_group = NULL; |
| 636 | bool add_group = false; |
| 637 | int ret; |
| 638 | size_t i; |
| 639 | struct ctf_fs_ds_file *ds_file; |
| 640 | struct ctf_fs_ds_index *index = NULL; |
| 641 | |
| 642 | ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path); |
| 643 | if (!ds_file) { |
| 644 | goto error; |
| 645 | } |
| 646 | |
| 647 | ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file, |
| 648 | &packet_header_field, &packet_context_field); |
| 649 | if (ret) { |
| 650 | BT_LOGE("Cannot get stream file's first packet's header and context fields (`%s`).", |
| 651 | path); |
| 652 | goto error; |
| 653 | } |
| 654 | |
| 655 | stream_instance_id = get_packet_header_stream_instance_id(ctf_fs_trace, |
| 656 | packet_header_field); |
| 657 | begin_ns = get_packet_context_timestamp_begin_ns(ctf_fs_trace, |
| 658 | packet_context_field); |
| 659 | stream_class = stream_class_from_packet_header(ctf_fs_trace, |
| 660 | packet_header_field); |
| 661 | if (!stream_class) { |
| 662 | goto error; |
| 663 | } |
| 664 | |
| 665 | index = ctf_fs_ds_file_build_index(ds_file); |
| 666 | if (!index) { |
| 667 | BT_LOGW("Failed to index CTF stream file \'%s\'", |
| 668 | ds_file->file->path->str); |
| 669 | } |
| 670 | |
| 671 | if (begin_ns == -1ULL) { |
| 672 | /* |
| 673 | * No beggining timestamp to sort the stream files |
| 674 | * within a stream file group, so consider that this |
| 675 | * file must be the only one within its group. |
| 676 | */ |
| 677 | stream_instance_id = -1ULL; |
| 678 | } |
| 679 | |
| 680 | if (stream_instance_id == -1ULL) { |
| 681 | /* |
| 682 | * No stream instance ID or no beginning timestamp: |
| 683 | * create a unique stream file group for this stream |
| 684 | * file because, even if there's a stream instance ID, |
| 685 | * there's no timestamp to order the file within its |
| 686 | * group. |
| 687 | */ |
| 688 | |
| 689 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
| 690 | stream_class, stream_instance_id); |
| 691 | if (!ds_file_group) { |
| 692 | goto error; |
| 693 | } |
| 694 | |
| 695 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, |
| 696 | path, begin_ns, index); |
| 697 | /* Ownership of index is transferred. */ |
| 698 | index = NULL; |
| 699 | if (ret) { |
| 700 | goto error; |
| 701 | } |
| 702 | |
| 703 | add_group = true; |
| 704 | goto end; |
| 705 | } |
| 706 | |
| 707 | assert(stream_instance_id != -1ULL); |
| 708 | assert(begin_ns != -1ULL); |
| 709 | |
| 710 | /* Find an existing stream file group with this ID */ |
| 711 | for (i = 0; i < ctf_fs_trace->ds_file_groups->len; i++) { |
| 712 | int64_t id; |
| 713 | struct bt_ctf_stream_class *cand_stream_class; |
| 714 | |
| 715 | ds_file_group = g_ptr_array_index( |
| 716 | ctf_fs_trace->ds_file_groups, i); |
| 717 | id = bt_ctf_stream_get_id(ds_file_group->stream); |
| 718 | cand_stream_class = bt_ctf_stream_get_class( |
| 719 | ds_file_group->stream); |
| 720 | |
| 721 | assert(cand_stream_class); |
| 722 | bt_put(cand_stream_class); |
| 723 | |
| 724 | if (cand_stream_class == stream_class && |
| 725 | (uint64_t) id == stream_instance_id) { |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | ds_file_group = NULL; |
| 730 | } |
| 731 | |
| 732 | if (!ds_file_group) { |
| 733 | ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, |
| 734 | stream_class, stream_instance_id); |
| 735 | if (!ds_file_group) { |
| 736 | goto error; |
| 737 | } |
| 738 | |
| 739 | add_group = true; |
| 740 | } |
| 741 | |
| 742 | ret = ctf_fs_ds_file_group_add_ds_file_info(ds_file_group, |
| 743 | path, begin_ns, index); |
| 744 | index = NULL; |
| 745 | if (ret) { |
| 746 | goto error; |
| 747 | } |
| 748 | |
| 749 | goto end; |
| 750 | |
| 751 | error: |
| 752 | ctf_fs_ds_file_group_destroy(ds_file_group); |
| 753 | ret = -1; |
| 754 | |
| 755 | end: |
| 756 | if (add_group && ds_file_group) { |
| 757 | g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); |
| 758 | } |
| 759 | ctf_fs_ds_file_destroy(ds_file); |
| 760 | ctf_fs_ds_index_destroy(index); |
| 761 | bt_put(packet_header_field); |
| 762 | bt_put(packet_context_field); |
| 763 | bt_put(stream_class); |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static |
| 768 | int create_ds_file_groups(struct ctf_fs_trace *ctf_fs_trace) |
| 769 | { |
| 770 | int ret = 0; |
| 771 | const char *basename; |
| 772 | GError *error = NULL; |
| 773 | GDir *dir = NULL; |
| 774 | |
| 775 | /* Check each file in the path directory, except specific ones */ |
| 776 | dir = g_dir_open(ctf_fs_trace->path->str, 0, &error); |
| 777 | if (!dir) { |
| 778 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
| 779 | ctf_fs_trace->path->str, error->message, |
| 780 | error->code); |
| 781 | goto error; |
| 782 | } |
| 783 | |
| 784 | while ((basename = g_dir_read_name(dir))) { |
| 785 | struct ctf_fs_file *file; |
| 786 | |
| 787 | if (!strcmp(basename, CTF_FS_METADATA_FILENAME)) { |
| 788 | /* Ignore the metadata stream. */ |
| 789 | BT_LOGD("Ignoring metadata file `%s/%s`", |
| 790 | ctf_fs_trace->path->str, basename); |
| 791 | continue; |
| 792 | } |
| 793 | |
| 794 | if (basename[0] == '.') { |
| 795 | BT_LOGD("Ignoring hidden file `%s/%s`", |
| 796 | ctf_fs_trace->path->str, basename); |
| 797 | continue; |
| 798 | } |
| 799 | |
| 800 | /* Create the file. */ |
| 801 | file = ctf_fs_file_create(); |
| 802 | if (!file) { |
| 803 | BT_LOGE("Cannot create stream file object for file `%s/%s`", |
| 804 | ctf_fs_trace->path->str, basename); |
| 805 | goto error; |
| 806 | } |
| 807 | |
| 808 | /* Create full path string. */ |
| 809 | g_string_append_printf(file->path, "%s/%s", |
| 810 | ctf_fs_trace->path->str, basename); |
| 811 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
| 812 | BT_LOGD("Ignoring non-regular file `%s`", |
| 813 | file->path->str); |
| 814 | ctf_fs_file_destroy(file); |
| 815 | file = NULL; |
| 816 | continue; |
| 817 | } |
| 818 | |
| 819 | ret = ctf_fs_file_open(file, "rb"); |
| 820 | if (ret) { |
| 821 | BT_LOGE("Cannot open stream file `%s`", file->path->str); |
| 822 | goto error; |
| 823 | } |
| 824 | |
| 825 | if (file->size == 0) { |
| 826 | /* Skip empty stream. */ |
| 827 | BT_LOGD("Ignoring empty file `%s`", file->path->str); |
| 828 | ctf_fs_file_destroy(file); |
| 829 | continue; |
| 830 | } |
| 831 | |
| 832 | ret = add_ds_file_to_ds_file_group(ctf_fs_trace, |
| 833 | file->path->str); |
| 834 | if (ret) { |
| 835 | BT_LOGE("Cannot add stream file `%s` to stream file group", |
| 836 | file->path->str); |
| 837 | ctf_fs_file_destroy(file); |
| 838 | goto error; |
| 839 | } |
| 840 | |
| 841 | ctf_fs_file_destroy(file); |
| 842 | } |
| 843 | |
| 844 | goto end; |
| 845 | |
| 846 | error: |
| 847 | ret = -1; |
| 848 | |
| 849 | end: |
| 850 | if (dir) { |
| 851 | g_dir_close(dir); |
| 852 | dir = NULL; |
| 853 | } |
| 854 | |
| 855 | if (error) { |
| 856 | g_error_free(error); |
| 857 | } |
| 858 | |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | static |
| 863 | int create_cc_prio_map(struct ctf_fs_trace *ctf_fs_trace) |
| 864 | { |
| 865 | int ret = 0; |
| 866 | size_t i; |
| 867 | int count; |
| 868 | |
| 869 | assert(ctf_fs_trace); |
| 870 | ctf_fs_trace->cc_prio_map = bt_clock_class_priority_map_create(); |
| 871 | if (!ctf_fs_trace->cc_prio_map) { |
| 872 | ret = -1; |
| 873 | goto end; |
| 874 | } |
| 875 | |
| 876 | count = bt_ctf_trace_get_clock_class_count( |
| 877 | ctf_fs_trace->metadata->trace); |
| 878 | assert(count >= 0); |
| 879 | |
| 880 | for (i = 0; i < count; i++) { |
| 881 | struct bt_ctf_clock_class *clock_class = |
| 882 | bt_ctf_trace_get_clock_class_by_index( |
| 883 | ctf_fs_trace->metadata->trace, i); |
| 884 | |
| 885 | assert(clock_class); |
| 886 | ret = bt_clock_class_priority_map_add_clock_class( |
| 887 | ctf_fs_trace->cc_prio_map, clock_class, 0); |
| 888 | BT_PUT(clock_class); |
| 889 | |
| 890 | if (ret) { |
| 891 | goto end; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | end: |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | BT_HIDDEN |
| 900 | struct ctf_fs_trace *ctf_fs_trace_create(const char *path, const char *name, |
| 901 | struct ctf_fs_metadata_config *metadata_config) |
| 902 | { |
| 903 | struct ctf_fs_trace *ctf_fs_trace; |
| 904 | int ret; |
| 905 | |
| 906 | ctf_fs_trace = g_new0(struct ctf_fs_trace, 1); |
| 907 | if (!ctf_fs_trace) { |
| 908 | goto end; |
| 909 | } |
| 910 | |
| 911 | ctf_fs_trace->path = g_string_new(path); |
| 912 | if (!ctf_fs_trace->path) { |
| 913 | goto error; |
| 914 | } |
| 915 | |
| 916 | ctf_fs_trace->name = g_string_new(name); |
| 917 | if (!ctf_fs_trace->name) { |
| 918 | goto error; |
| 919 | } |
| 920 | |
| 921 | ctf_fs_trace->metadata = g_new0(struct ctf_fs_metadata, 1); |
| 922 | if (!ctf_fs_trace->metadata) { |
| 923 | goto error; |
| 924 | } |
| 925 | |
| 926 | ctf_fs_trace->ds_file_groups = g_ptr_array_new_with_free_func( |
| 927 | (GDestroyNotify) ctf_fs_ds_file_group_destroy); |
| 928 | if (!ctf_fs_trace->ds_file_groups) { |
| 929 | goto error; |
| 930 | } |
| 931 | |
| 932 | ret = ctf_fs_metadata_set_trace(ctf_fs_trace, metadata_config); |
| 933 | if (ret) { |
| 934 | goto error; |
| 935 | } |
| 936 | |
| 937 | ret = create_ds_file_groups(ctf_fs_trace); |
| 938 | if (ret) { |
| 939 | goto error; |
| 940 | } |
| 941 | |
| 942 | ret = create_cc_prio_map(ctf_fs_trace); |
| 943 | if (ret) { |
| 944 | goto error; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * create_ds_file_groups() created all the streams that this |
| 949 | * trace needs. There won't be any more. Therefore it is safe to |
| 950 | * make this trace static. |
| 951 | */ |
| 952 | (void) bt_ctf_trace_set_is_static(ctf_fs_trace->metadata->trace); |
| 953 | |
| 954 | goto end; |
| 955 | |
| 956 | error: |
| 957 | ctf_fs_trace_destroy(ctf_fs_trace); |
| 958 | ctf_fs_trace = NULL; |
| 959 | end: |
| 960 | return ctf_fs_trace; |
| 961 | } |
| 962 | |
| 963 | static |
| 964 | int path_is_ctf_trace(const char *path) |
| 965 | { |
| 966 | GString *metadata_path = g_string_new(NULL); |
| 967 | int ret = 0; |
| 968 | |
| 969 | if (!metadata_path) { |
| 970 | ret = -1; |
| 971 | goto end; |
| 972 | } |
| 973 | |
| 974 | g_string_printf(metadata_path, "%s/%s", path, CTF_FS_METADATA_FILENAME); |
| 975 | |
| 976 | if (g_file_test(metadata_path->str, G_FILE_TEST_IS_REGULAR)) { |
| 977 | ret = 1; |
| 978 | goto end; |
| 979 | } |
| 980 | |
| 981 | end: |
| 982 | g_string_free(metadata_path, TRUE); |
| 983 | return ret; |
| 984 | } |
| 985 | |
| 986 | static |
| 987 | int add_trace_path(GList **trace_paths, const char *path) |
| 988 | { |
| 989 | GString *norm_path = NULL; |
| 990 | int ret = 0; |
| 991 | |
| 992 | norm_path = bt_common_normalize_path(path, NULL); |
| 993 | if (!norm_path) { |
| 994 | BT_LOGE("Failed to normalize path `%s`.", path); |
| 995 | ret = -1; |
| 996 | goto end; |
| 997 | } |
| 998 | |
| 999 | if (strcmp(norm_path->str, "/") == 0) { |
| 1000 | BT_LOGE("Opening a trace in `/` is not supported."); |
| 1001 | ret = -1; |
| 1002 | goto end; |
| 1003 | } |
| 1004 | |
| 1005 | *trace_paths = g_list_prepend(*trace_paths, norm_path); |
| 1006 | assert(*trace_paths); |
| 1007 | norm_path = NULL; |
| 1008 | |
| 1009 | end: |
| 1010 | if (norm_path) { |
| 1011 | g_string_free(norm_path, TRUE); |
| 1012 | } |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | |
| 1017 | BT_HIDDEN |
| 1018 | int ctf_fs_find_traces(GList **trace_paths, const char *start_path) |
| 1019 | { |
| 1020 | int ret; |
| 1021 | GError *error = NULL; |
| 1022 | GDir *dir = NULL; |
| 1023 | const char *basename = NULL; |
| 1024 | |
| 1025 | /* Check if the starting path is a CTF trace itself */ |
| 1026 | ret = path_is_ctf_trace(start_path); |
| 1027 | if (ret < 0) { |
| 1028 | goto end; |
| 1029 | } |
| 1030 | |
| 1031 | if (ret) { |
| 1032 | /* |
| 1033 | * Stop recursion: a CTF trace cannot contain another |
| 1034 | * CTF trace. |
| 1035 | */ |
| 1036 | ret = add_trace_path(trace_paths, start_path); |
| 1037 | goto end; |
| 1038 | } |
| 1039 | |
| 1040 | /* Look for subdirectories */ |
| 1041 | if (!g_file_test(start_path, G_FILE_TEST_IS_DIR)) { |
| 1042 | /* Starting path is not a directory: end of recursion */ |
| 1043 | goto end; |
| 1044 | } |
| 1045 | |
| 1046 | dir = g_dir_open(start_path, 0, &error); |
| 1047 | if (!dir) { |
| 1048 | if (error->code == G_FILE_ERROR_ACCES) { |
| 1049 | BT_LOGD("Cannot open directory `%s`: %s (code %d): continuing", |
| 1050 | start_path, error->message, error->code); |
| 1051 | goto end; |
| 1052 | } |
| 1053 | |
| 1054 | BT_LOGE("Cannot open directory `%s`: %s (code %d)", |
| 1055 | start_path, error->message, error->code); |
| 1056 | ret = -1; |
| 1057 | goto end; |
| 1058 | } |
| 1059 | |
| 1060 | while ((basename = g_dir_read_name(dir))) { |
| 1061 | GString *sub_path = g_string_new(NULL); |
| 1062 | |
| 1063 | if (!sub_path) { |
| 1064 | ret = -1; |
| 1065 | goto end; |
| 1066 | } |
| 1067 | |
| 1068 | g_string_printf(sub_path, "%s/%s", start_path, basename); |
| 1069 | ret = ctf_fs_find_traces(trace_paths, sub_path->str); |
| 1070 | g_string_free(sub_path, TRUE); |
| 1071 | if (ret) { |
| 1072 | goto end; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | end: |
| 1077 | if (dir) { |
| 1078 | g_dir_close(dir); |
| 1079 | } |
| 1080 | |
| 1081 | if (error) { |
| 1082 | g_error_free(error); |
| 1083 | } |
| 1084 | |
| 1085 | return ret; |
| 1086 | } |
| 1087 | |
| 1088 | BT_HIDDEN |
| 1089 | GList *ctf_fs_create_trace_names(GList *trace_paths, const char *base_path) { |
| 1090 | GList *trace_names = NULL; |
| 1091 | GList *node; |
| 1092 | const char *last_sep; |
| 1093 | size_t base_dist; |
| 1094 | |
| 1095 | /* |
| 1096 | * At this point we know that all the trace paths are |
| 1097 | * normalized, and so is the base path. This means that |
| 1098 | * they are absolute and they don't end with a separator. |
| 1099 | * We can simply find the location of the last separator |
| 1100 | * in the base path, which gives us the name of the actual |
| 1101 | * directory to look into, and use this location as the |
| 1102 | * start of each trace name within each trace path. |
| 1103 | * |
| 1104 | * For example: |
| 1105 | * |
| 1106 | * Base path: /home/user/my-traces/some-trace |
| 1107 | * Trace paths: |
| 1108 | * - /home/user/my-traces/some-trace/host1/trace1 |
| 1109 | * - /home/user/my-traces/some-trace/host1/trace2 |
| 1110 | * - /home/user/my-traces/some-trace/host2/trace |
| 1111 | * - /home/user/my-traces/some-trace/other-trace |
| 1112 | * |
| 1113 | * In this case the trace names are: |
| 1114 | * |
| 1115 | * - some-trace/host1/trace1 |
| 1116 | * - some-trace/host1/trace2 |
| 1117 | * - some-trace/host2/trace |
| 1118 | * - some-trace/other-trace |
| 1119 | */ |
| 1120 | last_sep = strrchr(base_path, G_DIR_SEPARATOR); |
| 1121 | |
| 1122 | /* We know there's at least one separator */ |
| 1123 | assert(last_sep); |
| 1124 | |
| 1125 | /* Distance to base */ |
| 1126 | base_dist = last_sep - base_path + 1; |
| 1127 | |
| 1128 | /* Create the trace names */ |
| 1129 | for (node = trace_paths; node; node = g_list_next(node)) { |
| 1130 | GString *trace_name = g_string_new(NULL); |
| 1131 | GString *trace_path = node->data; |
| 1132 | |
| 1133 | assert(trace_name); |
| 1134 | g_string_assign(trace_name, &trace_path->str[base_dist]); |
| 1135 | trace_names = g_list_append(trace_names, trace_name); |
| 1136 | } |
| 1137 | |
| 1138 | return trace_names; |
| 1139 | } |
| 1140 | |
| 1141 | static |
| 1142 | int create_ctf_fs_traces(struct ctf_fs_component *ctf_fs, |
| 1143 | const char *path_param) |
| 1144 | { |
| 1145 | struct ctf_fs_trace *ctf_fs_trace = NULL; |
| 1146 | int ret = 0; |
| 1147 | GString *norm_path = NULL; |
| 1148 | GList *trace_paths = NULL; |
| 1149 | GList *trace_names = NULL; |
| 1150 | GList *tp_node; |
| 1151 | GList *tn_node; |
| 1152 | |
| 1153 | norm_path = bt_common_normalize_path(path_param, NULL); |
| 1154 | if (!norm_path) { |
| 1155 | BT_LOGE("Failed to normalize path: `%s`.", |
| 1156 | path_param); |
| 1157 | goto error; |
| 1158 | } |
| 1159 | |
| 1160 | ret = ctf_fs_find_traces(&trace_paths, norm_path->str); |
| 1161 | if (ret) { |
| 1162 | goto error; |
| 1163 | } |
| 1164 | |
| 1165 | if (!trace_paths) { |
| 1166 | BT_LOGE("No CTF traces recursively found in `%s`.", |
| 1167 | path_param); |
| 1168 | goto error; |
| 1169 | } |
| 1170 | |
| 1171 | trace_names = ctf_fs_create_trace_names(trace_paths, norm_path->str); |
| 1172 | if (!trace_names) { |
| 1173 | BT_LOGE("Cannot create trace names from trace paths."); |
| 1174 | goto error; |
| 1175 | } |
| 1176 | |
| 1177 | for (tp_node = trace_paths, tn_node = trace_names; tp_node; |
| 1178 | tp_node = g_list_next(tp_node), |
| 1179 | tn_node = g_list_next(tn_node)) { |
| 1180 | GString *trace_path = tp_node->data; |
| 1181 | GString *trace_name = tn_node->data; |
| 1182 | |
| 1183 | ctf_fs_trace = ctf_fs_trace_create(trace_path->str, |
| 1184 | trace_name->str, &ctf_fs->metadata_config); |
| 1185 | if (!ctf_fs_trace) { |
| 1186 | BT_LOGE("Cannot create trace for `%s`.", |
| 1187 | trace_path->str); |
| 1188 | goto error; |
| 1189 | } |
| 1190 | |
| 1191 | ret = create_ports_for_trace(ctf_fs, ctf_fs_trace); |
| 1192 | if (ret) { |
| 1193 | goto error; |
| 1194 | } |
| 1195 | |
| 1196 | g_ptr_array_add(ctf_fs->traces, ctf_fs_trace); |
| 1197 | ctf_fs_trace = NULL; |
| 1198 | } |
| 1199 | |
| 1200 | goto end; |
| 1201 | |
| 1202 | error: |
| 1203 | ret = -1; |
| 1204 | ctf_fs_trace_destroy(ctf_fs_trace); |
| 1205 | |
| 1206 | end: |
| 1207 | for (tp_node = trace_paths; tp_node; tp_node = g_list_next(tp_node)) { |
| 1208 | if (tp_node->data) { |
| 1209 | g_string_free(tp_node->data, TRUE); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | for (tn_node = trace_names; tn_node; tn_node = g_list_next(tn_node)) { |
| 1214 | if (tn_node->data) { |
| 1215 | g_string_free(tn_node->data, TRUE); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | if (trace_paths) { |
| 1220 | g_list_free(trace_paths); |
| 1221 | } |
| 1222 | |
| 1223 | if (trace_names) { |
| 1224 | g_list_free(trace_names); |
| 1225 | } |
| 1226 | |
| 1227 | if (norm_path) { |
| 1228 | g_string_free(norm_path, TRUE); |
| 1229 | } |
| 1230 | |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | static |
| 1235 | struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp, |
| 1236 | struct bt_value *params) |
| 1237 | { |
| 1238 | struct ctf_fs_component *ctf_fs; |
| 1239 | struct bt_value *value = NULL; |
| 1240 | const char *path_param; |
| 1241 | int ret; |
| 1242 | |
| 1243 | ctf_fs = g_new0(struct ctf_fs_component, 1); |
| 1244 | if (!ctf_fs) { |
| 1245 | goto end; |
| 1246 | } |
| 1247 | |
| 1248 | ret = bt_private_component_set_user_data(priv_comp, ctf_fs); |
| 1249 | assert(ret == 0); |
| 1250 | |
| 1251 | /* |
| 1252 | * We don't need to get a new reference here because as long as |
| 1253 | * our private ctf_fs_component object exists, the containing |
| 1254 | * private component should also exist. |
| 1255 | */ |
| 1256 | ctf_fs->priv_comp = priv_comp; |
| 1257 | value = bt_value_map_get(params, "path"); |
| 1258 | if (!bt_value_is_string(value)) { |
| 1259 | goto error; |
| 1260 | } |
| 1261 | |
| 1262 | ret = bt_value_string_get(value, &path_param); |
| 1263 | assert(ret == 0); |
| 1264 | BT_PUT(value); |
| 1265 | value = bt_value_map_get(params, "clock-class-offset-s"); |
| 1266 | if (value) { |
| 1267 | if (!bt_value_is_integer(value)) { |
| 1268 | BT_LOGE("clock-class-offset-s should be an integer"); |
| 1269 | goto error; |
| 1270 | } |
| 1271 | ret = bt_value_integer_get(value, |
| 1272 | &ctf_fs->metadata_config.clock_class_offset_s); |
| 1273 | assert(ret == 0); |
| 1274 | BT_PUT(value); |
| 1275 | } |
| 1276 | |
| 1277 | value = bt_value_map_get(params, "clock-class-offset-ns"); |
| 1278 | if (value) { |
| 1279 | if (!bt_value_is_integer(value)) { |
| 1280 | BT_LOGE("clock-class-offset-ns should be an integer"); |
| 1281 | goto error; |
| 1282 | } |
| 1283 | ret = bt_value_integer_get(value, |
| 1284 | &ctf_fs->metadata_config.clock_class_offset_ns); |
| 1285 | assert(ret == 0); |
| 1286 | BT_PUT(value); |
| 1287 | } |
| 1288 | |
| 1289 | ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy); |
| 1290 | if (!ctf_fs->port_data) { |
| 1291 | goto error; |
| 1292 | } |
| 1293 | |
| 1294 | ctf_fs->traces = g_ptr_array_new_with_free_func( |
| 1295 | ctf_fs_trace_destroy_notifier); |
| 1296 | if (!ctf_fs->traces) { |
| 1297 | goto error; |
| 1298 | } |
| 1299 | |
| 1300 | ret = create_ctf_fs_traces(ctf_fs, path_param); |
| 1301 | if (ret) { |
| 1302 | goto error; |
| 1303 | } |
| 1304 | |
| 1305 | goto end; |
| 1306 | |
| 1307 | error: |
| 1308 | ctf_fs_destroy(ctf_fs); |
| 1309 | ctf_fs = NULL; |
| 1310 | ret = bt_private_component_set_user_data(priv_comp, NULL); |
| 1311 | assert(ret == 0); |
| 1312 | |
| 1313 | end: |
| 1314 | bt_put(value); |
| 1315 | return ctf_fs; |
| 1316 | } |
| 1317 | |
| 1318 | BT_HIDDEN |
| 1319 | enum bt_component_status ctf_fs_init(struct bt_private_component *priv_comp, |
| 1320 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
| 1321 | { |
| 1322 | struct ctf_fs_component *ctf_fs; |
| 1323 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1324 | |
| 1325 | ctf_fs = ctf_fs_create(priv_comp, params); |
| 1326 | if (!ctf_fs) { |
| 1327 | ret = BT_COMPONENT_STATUS_ERROR; |
| 1328 | } |
| 1329 | |
| 1330 | return ret; |
| 1331 | } |
| 1332 | |
| 1333 | BT_HIDDEN |
| 1334 | struct bt_value *ctf_fs_query(struct bt_component_class *comp_class, |
| 1335 | const char *object, struct bt_value *params) |
| 1336 | { |
| 1337 | struct bt_value *result = NULL; |
| 1338 | |
| 1339 | if (!strcmp(object, "metadata-info")) { |
| 1340 | result = metadata_info_query(comp_class, params); |
| 1341 | } else if (!strcmp(object, "trace-info")) { |
| 1342 | result = trace_info_query(comp_class, params); |
| 1343 | } else { |
| 1344 | BT_LOGE("Unknown query object `%s`", object); |
| 1345 | goto end; |
| 1346 | } |
| 1347 | end: |
| 1348 | return result; |
| 1349 | } |