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