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