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