| 1 | /* |
| 2 | * fs.c |
| 3 | * |
| 4 | * Babeltrace CTF file system Reader Component |
| 5 | * |
| 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * |
| 8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 | * SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/ctf-ir/packet.h> |
| 30 | #include <babeltrace/ctf-ir/clock-class.h> |
| 31 | #include <babeltrace/component/notification/iterator.h> |
| 32 | #include <babeltrace/component/notification/stream.h> |
| 33 | #include <babeltrace/component/notification/event.h> |
| 34 | #include <babeltrace/component/notification/packet.h> |
| 35 | #include <babeltrace/component/notification/heap.h> |
| 36 | #include <plugins-common.h> |
| 37 | #include <glib.h> |
| 38 | #include <assert.h> |
| 39 | #include <unistd.h> |
| 40 | #include "fs.h" |
| 41 | #include "metadata.h" |
| 42 | #include "data-stream.h" |
| 43 | #include "file.h" |
| 44 | |
| 45 | #define PRINT_ERR_STREAM ctf_fs->error_fp |
| 46 | #define PRINT_PREFIX "ctf-fs" |
| 47 | #include "print.h" |
| 48 | |
| 49 | BT_HIDDEN |
| 50 | bool ctf_fs_debug; |
| 51 | |
| 52 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
| 53 | struct bt_notification_iterator *iterator); |
| 54 | |
| 55 | struct bt_notification *ctf_fs_iterator_get( |
| 56 | struct bt_notification_iterator *iterator) |
| 57 | { |
| 58 | struct ctf_fs_iterator *ctf_it = |
| 59 | bt_notification_iterator_get_private_data(iterator); |
| 60 | |
| 61 | if (!ctf_it->current_notification) { |
| 62 | (void) ctf_fs_iterator_next(iterator); |
| 63 | } |
| 64 | |
| 65 | return bt_get(ctf_it->current_notification); |
| 66 | } |
| 67 | |
| 68 | static |
| 69 | enum bt_notification_iterator_status ctf_fs_iterator_get_next_notification( |
| 70 | struct ctf_fs_iterator *it, |
| 71 | struct ctf_fs_stream *stream, |
| 72 | struct bt_notification **notification) |
| 73 | { |
| 74 | enum bt_ctf_notif_iter_status status; |
| 75 | enum bt_notification_iterator_status ret; |
| 76 | |
| 77 | if (stream->end_reached) { |
| 78 | status = BT_CTF_NOTIF_ITER_STATUS_EOF; |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter, |
| 83 | notification); |
| 84 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK && |
| 85 | status != BT_CTF_NOTIF_ITER_STATUS_EOF) { |
| 86 | goto end; |
| 87 | } |
| 88 | |
| 89 | /* Should be handled in bt_ctf_notif_iter_get_next_notification. */ |
| 90 | if (status == BT_CTF_NOTIF_ITER_STATUS_EOF) { |
| 91 | *notification = bt_notification_stream_end_create( |
| 92 | stream->stream); |
| 93 | if (!*notification) { |
| 94 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; |
| 95 | } |
| 96 | status = BT_CTF_NOTIF_ITER_STATUS_OK; |
| 97 | stream->end_reached = true; |
| 98 | } |
| 99 | end: |
| 100 | switch (status) { |
| 101 | case BT_CTF_NOTIF_ITER_STATUS_EOF: |
| 102 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 103 | break; |
| 104 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
| 105 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 106 | break; |
| 107 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: |
| 108 | /* |
| 109 | * Should not make it this far as this is medium-specific; |
| 110 | * there is nothing for the user to do and it should have been |
| 111 | * handled upstream. |
| 112 | */ |
| 113 | assert(0); |
| 114 | case BT_CTF_NOTIF_ITER_STATUS_INVAL: |
| 115 | /* No argument provided by the user, so don't return INVAL. */ |
| 116 | case BT_CTF_NOTIF_ITER_STATUS_ERROR: |
| 117 | default: |
| 118 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
| 119 | break; |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * Remove me. This is a temporary work-around due to our inhability to use |
| 126 | * libbabeltrace-ctf from libbabeltrace-plugin. |
| 127 | */ |
| 128 | static |
| 129 | struct bt_ctf_stream *internal_bt_notification_get_stream( |
| 130 | struct bt_notification *notification) |
| 131 | { |
| 132 | struct bt_ctf_stream *stream = NULL; |
| 133 | |
| 134 | assert(notification); |
| 135 | switch (bt_notification_get_type(notification)) { |
| 136 | case BT_NOTIFICATION_TYPE_EVENT: |
| 137 | { |
| 138 | struct bt_ctf_event *event; |
| 139 | |
| 140 | event = bt_notification_event_get_event(notification); |
| 141 | stream = bt_ctf_event_get_stream(event); |
| 142 | bt_put(event); |
| 143 | break; |
| 144 | } |
| 145 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 146 | { |
| 147 | struct bt_ctf_packet *packet; |
| 148 | |
| 149 | packet = bt_notification_packet_begin_get_packet(notification); |
| 150 | stream = bt_ctf_packet_get_stream(packet); |
| 151 | bt_put(packet); |
| 152 | break; |
| 153 | } |
| 154 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 155 | { |
| 156 | struct bt_ctf_packet *packet; |
| 157 | |
| 158 | packet = bt_notification_packet_end_get_packet(notification); |
| 159 | stream = bt_ctf_packet_get_stream(packet); |
| 160 | bt_put(packet); |
| 161 | break; |
| 162 | } |
| 163 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 164 | stream = bt_notification_stream_end_get_stream(notification); |
| 165 | break; |
| 166 | default: |
| 167 | goto end; |
| 168 | } |
| 169 | end: |
| 170 | return stream; |
| 171 | } |
| 172 | |
| 173 | static |
| 174 | enum bt_notification_iterator_status populate_heap(struct ctf_fs_iterator *it) |
| 175 | { |
| 176 | size_t i, pending_streams_count = it->pending_streams->len; |
| 177 | enum bt_notification_iterator_status ret = |
| 178 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 179 | |
| 180 | /* Insert one stream-associated notification for each stream. */ |
| 181 | for (i = 0; i < pending_streams_count; i++) { |
| 182 | struct bt_notification *notification; |
| 183 | struct ctf_fs_stream *fs_stream; |
| 184 | struct bt_ctf_stream *stream; |
| 185 | size_t pending_stream_index = pending_streams_count - 1 - i; |
| 186 | |
| 187 | fs_stream = g_ptr_array_index(it->pending_streams, |
| 188 | pending_stream_index); |
| 189 | |
| 190 | do { |
| 191 | int heap_ret; |
| 192 | |
| 193 | ret = ctf_fs_iterator_get_next_notification( |
| 194 | it, fs_stream, ¬ification); |
| 195 | if (ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END) { |
| 196 | printf_debug("Failed to populate heap at stream %zu\n", |
| 197 | pending_stream_index); |
| 198 | goto end; |
| 199 | } |
| 200 | |
| 201 | stream = internal_bt_notification_get_stream( |
| 202 | notification); |
| 203 | if (stream) { |
| 204 | gboolean inserted; |
| 205 | |
| 206 | /* |
| 207 | * Associate pending ctf_fs_stream to |
| 208 | * bt_ctf_stream. Ownership of stream |
| 209 | * is passed to the stream ht. |
| 210 | */ |
| 211 | inserted = g_hash_table_insert(it->stream_ht, |
| 212 | stream, fs_stream); |
| 213 | if (!inserted) { |
| 214 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 215 | printf_debug("Failed to associate fs stream to ctf stream\n"); |
| 216 | goto end; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | heap_ret = bt_notification_heap_insert( |
| 221 | it->pending_notifications, |
| 222 | notification); |
| 223 | bt_put(notification); |
| 224 | if (heap_ret) { |
| 225 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 226 | printf_debug("Failed to insert notification in heap\n"); |
| 227 | goto end; |
| 228 | } |
| 229 | } while (!stream && ret != BT_NOTIFICATION_ITERATOR_STATUS_END); |
| 230 | /* |
| 231 | * Set NULL so the destruction callback registered with the |
| 232 | * array is not invoked on the stream (its ownership was |
| 233 | * transferred to the streams hashtable). |
| 234 | */ |
| 235 | g_ptr_array_index(it->pending_streams, |
| 236 | pending_stream_index) = NULL; |
| 237 | g_ptr_array_remove_index(it->pending_streams, |
| 238 | pending_stream_index); |
| 239 | } |
| 240 | |
| 241 | g_ptr_array_free(it->pending_streams, TRUE); |
| 242 | it->pending_streams = NULL; |
| 243 | end: |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | enum bt_notification_iterator_status ctf_fs_iterator_next( |
| 248 | struct bt_notification_iterator *iterator) |
| 249 | { |
| 250 | int heap_ret; |
| 251 | struct bt_ctf_stream *stream = NULL; |
| 252 | struct ctf_fs_stream *fs_stream; |
| 253 | struct bt_notification *notification; |
| 254 | struct bt_notification *next_stream_notification; |
| 255 | enum bt_notification_iterator_status ret = |
| 256 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 257 | struct ctf_fs_iterator *ctf_it = |
| 258 | bt_notification_iterator_get_private_data(iterator); |
| 259 | |
| 260 | notification = bt_notification_heap_pop(ctf_it->pending_notifications); |
| 261 | if (!notification && !ctf_it->pending_streams) { |
| 262 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | if (!notification && ctf_it->pending_streams) { |
| 267 | /* |
| 268 | * Insert at one notification per stream in the heap and pop |
| 269 | * one. |
| 270 | */ |
| 271 | ret = populate_heap(ctf_it); |
| 272 | if (ret) { |
| 273 | goto end; |
| 274 | } |
| 275 | |
| 276 | notification = bt_notification_heap_pop( |
| 277 | ctf_it->pending_notifications); |
| 278 | if (!notification) { |
| 279 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; |
| 280 | goto end; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /* notification is set from here. */ |
| 285 | |
| 286 | stream = internal_bt_notification_get_stream(notification); |
| 287 | if (!stream) { |
| 288 | /* |
| 289 | * The current notification is not associated to a particular |
| 290 | * stream, there is no need to insert a new notification from |
| 291 | * a stream in the heap. |
| 292 | */ |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | fs_stream = g_hash_table_lookup(ctf_it->stream_ht, stream); |
| 297 | if (!fs_stream) { |
| 298 | /* We have reached this stream's end. */ |
| 299 | goto end; |
| 300 | } |
| 301 | |
| 302 | ret = ctf_fs_iterator_get_next_notification(ctf_it, fs_stream, |
| 303 | &next_stream_notification); |
| 304 | if ((ret && ret != BT_NOTIFICATION_ITERATOR_STATUS_END)) { |
| 305 | heap_ret = bt_notification_heap_insert( |
| 306 | ctf_it->pending_notifications, notification); |
| 307 | |
| 308 | assert(!next_stream_notification); |
| 309 | if (heap_ret) { |
| 310 | /* |
| 311 | * We're dropping the most recent notification, but at |
| 312 | * this point, something is seriously wrong... |
| 313 | */ |
| 314 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 315 | } |
| 316 | BT_PUT(notification); |
| 317 | goto end; |
| 318 | } |
| 319 | |
| 320 | if (ret == BT_NOTIFICATION_ITERATOR_STATUS_END) { |
| 321 | gboolean success; |
| 322 | |
| 323 | /* Remove stream. */ |
| 324 | success = g_hash_table_remove(ctf_it->stream_ht, stream); |
| 325 | assert(success); |
| 326 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 327 | } else { |
| 328 | heap_ret = bt_notification_heap_insert(ctf_it->pending_notifications, |
| 329 | next_stream_notification); |
| 330 | BT_PUT(next_stream_notification); |
| 331 | if (heap_ret) { |
| 332 | /* |
| 333 | * We're dropping the most recent notification... |
| 334 | */ |
| 335 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Ensure that the stream is removed from both pending_streams and |
| 341 | * the streams hashtable on reception of the "end of stream" |
| 342 | * notification. |
| 343 | */ |
| 344 | end: |
| 345 | BT_MOVE(ctf_it->current_notification, notification); |
| 346 | bt_put(stream); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static |
| 351 | void ctf_fs_iterator_destroy_data(struct ctf_fs_iterator *ctf_it) |
| 352 | { |
| 353 | if (!ctf_it) { |
| 354 | return; |
| 355 | } |
| 356 | bt_put(ctf_it->current_notification); |
| 357 | bt_put(ctf_it->pending_notifications); |
| 358 | if (ctf_it->pending_streams) { |
| 359 | g_ptr_array_free(ctf_it->pending_streams, TRUE); |
| 360 | } |
| 361 | if (ctf_it->stream_ht) { |
| 362 | g_hash_table_destroy(ctf_it->stream_ht); |
| 363 | } |
| 364 | g_free(ctf_it); |
| 365 | } |
| 366 | |
| 367 | void ctf_fs_iterator_destroy(struct bt_notification_iterator *it) |
| 368 | { |
| 369 | void *data = bt_notification_iterator_get_private_data(it); |
| 370 | |
| 371 | ctf_fs_iterator_destroy_data(data); |
| 372 | } |
| 373 | |
| 374 | static |
| 375 | bool compare_event_notifications(struct bt_notification *a, |
| 376 | struct bt_notification *b) |
| 377 | { |
| 378 | int ret; |
| 379 | struct bt_ctf_clock_class *clock_class; |
| 380 | struct bt_ctf_clock_value *a_clock_value, *b_clock_value; |
| 381 | struct bt_ctf_stream_class *a_stream_class; |
| 382 | struct bt_ctf_stream *a_stream; |
| 383 | struct bt_ctf_event *a_event, *b_event; |
| 384 | struct bt_ctf_trace *trace; |
| 385 | int64_t a_ts, b_ts; |
| 386 | |
| 387 | // FIXME - assumes only one clock |
| 388 | a_event = bt_notification_event_get_event(a); |
| 389 | b_event = bt_notification_event_get_event(b); |
| 390 | assert(a_event); |
| 391 | assert(b_event); |
| 392 | |
| 393 | a_stream = bt_ctf_event_get_stream(a_event); |
| 394 | assert(a_stream); |
| 395 | a_stream_class = bt_ctf_stream_get_class(a_stream); |
| 396 | assert(a_stream_class); |
| 397 | trace = bt_ctf_stream_class_get_trace(a_stream_class); |
| 398 | assert(trace); |
| 399 | |
| 400 | clock_class = bt_ctf_trace_get_clock_class(trace, 0); |
| 401 | a_clock_value = bt_ctf_event_get_clock_value(a_event, clock_class); |
| 402 | b_clock_value = bt_ctf_event_get_clock_value(b_event, clock_class); |
| 403 | assert(a_clock_value); |
| 404 | assert(b_clock_value); |
| 405 | |
| 406 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(a_clock_value, &a_ts); |
| 407 | assert(!ret); |
| 408 | ret = bt_ctf_clock_value_get_value_ns_from_epoch(b_clock_value, &b_ts); |
| 409 | assert(!ret); |
| 410 | |
| 411 | bt_put(a_event); |
| 412 | bt_put(b_event); |
| 413 | bt_put(a_clock_value); |
| 414 | bt_put(b_clock_value); |
| 415 | bt_put(a_stream); |
| 416 | bt_put(a_stream_class); |
| 417 | bt_put(clock_class); |
| 418 | bt_put(trace); |
| 419 | return a_ts < b_ts; |
| 420 | } |
| 421 | |
| 422 | static |
| 423 | bool compare_notifications(struct bt_notification *a, struct bt_notification *b, |
| 424 | void *unused) |
| 425 | { |
| 426 | static int notification_priorities[] = { |
| 427 | [BT_NOTIFICATION_TYPE_NEW_TRACE] = 0, |
| 428 | [BT_NOTIFICATION_TYPE_NEW_STREAM_CLASS] = 1, |
| 429 | [BT_NOTIFICATION_TYPE_NEW_EVENT_CLASS] = 2, |
| 430 | [BT_NOTIFICATION_TYPE_PACKET_BEGIN] = 3, |
| 431 | [BT_NOTIFICATION_TYPE_PACKET_END] = 4, |
| 432 | [BT_NOTIFICATION_TYPE_EVENT] = 5, |
| 433 | [BT_NOTIFICATION_TYPE_END_OF_TRACE] = 6, |
| 434 | }; |
| 435 | int a_prio, b_prio; |
| 436 | enum bt_notification_type a_type, b_type; |
| 437 | |
| 438 | assert(a && b); |
| 439 | a_type = bt_notification_get_type(a); |
| 440 | b_type = bt_notification_get_type(b); |
| 441 | assert(a_type > BT_NOTIFICATION_TYPE_ALL); |
| 442 | assert(a_type < BT_NOTIFICATION_TYPE_NR); |
| 443 | assert(b_type > BT_NOTIFICATION_TYPE_ALL); |
| 444 | assert(b_type < BT_NOTIFICATION_TYPE_NR); |
| 445 | |
| 446 | a_prio = notification_priorities[a_type]; |
| 447 | b_prio = notification_priorities[b_type]; |
| 448 | |
| 449 | if (likely((a_type == b_type) && a_type == BT_NOTIFICATION_TYPE_EVENT)) { |
| 450 | return compare_event_notifications(a, b); |
| 451 | } |
| 452 | |
| 453 | if (unlikely(a_prio != b_prio)) { |
| 454 | return a_prio < b_prio; |
| 455 | } |
| 456 | |
| 457 | /* Notification types are equal, but not of type "event". */ |
| 458 | switch (a_type) { |
| 459 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: |
| 460 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 461 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 462 | { |
| 463 | int64_t a_sc_id, b_sc_id; |
| 464 | struct bt_ctf_stream *a_stream, *b_stream; |
| 465 | struct bt_ctf_stream_class *a_sc, *b_sc; |
| 466 | |
| 467 | a_stream = internal_bt_notification_get_stream(a); |
| 468 | b_stream = internal_bt_notification_get_stream(b); |
| 469 | assert(a_stream && b_stream); |
| 470 | |
| 471 | a_sc = bt_ctf_stream_get_class(a_stream); |
| 472 | b_sc = bt_ctf_stream_get_class(b_stream); |
| 473 | assert(a_sc && b_sc); |
| 474 | |
| 475 | a_sc_id = bt_ctf_stream_class_get_id(a_sc); |
| 476 | b_sc_id = bt_ctf_stream_class_get_id(b_sc); |
| 477 | assert(a_sc_id >= 0 && b_sc_id >= 0); |
| 478 | bt_put(a_sc); |
| 479 | bt_put(a_stream); |
| 480 | bt_put(b_sc); |
| 481 | bt_put(b_stream); |
| 482 | return a_sc_id < b_sc_id; |
| 483 | } |
| 484 | case BT_NOTIFICATION_TYPE_NEW_TRACE: |
| 485 | case BT_NOTIFICATION_TYPE_END_OF_TRACE: |
| 486 | /* Impossible to have two separate traces. */ |
| 487 | default: |
| 488 | assert(0); |
| 489 | } |
| 490 | |
| 491 | assert(0); |
| 492 | return a < b; |
| 493 | } |
| 494 | |
| 495 | static |
| 496 | void stream_destroy(void *stream) |
| 497 | { |
| 498 | ctf_fs_stream_destroy((struct ctf_fs_stream *) stream); |
| 499 | } |
| 500 | |
| 501 | static |
| 502 | int open_trace_streams(struct ctf_fs_component *ctf_fs, |
| 503 | struct ctf_fs_iterator *ctf_it) |
| 504 | { |
| 505 | int ret = 0; |
| 506 | const char *name; |
| 507 | GError *error = NULL; |
| 508 | GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error); |
| 509 | |
| 510 | if (!dir) { |
| 511 | PERR("Cannot open directory \"%s\": %s (code %d)\n", |
| 512 | ctf_fs->trace_path->str, error->message, |
| 513 | error->code); |
| 514 | goto error; |
| 515 | } |
| 516 | |
| 517 | while ((name = g_dir_read_name(dir))) { |
| 518 | struct ctf_fs_file *file = NULL; |
| 519 | struct ctf_fs_stream *stream = NULL; |
| 520 | |
| 521 | if (!strcmp(name, CTF_FS_METADATA_FILENAME)) { |
| 522 | /* Ignore the metadata stream. */ |
| 523 | PDBG("Ignoring metadata file \"%s\"\n", |
| 524 | name); |
| 525 | continue; |
| 526 | } |
| 527 | |
| 528 | if (name[0] == '.') { |
| 529 | PDBG("Ignoring hidden file \"%s\"\n", |
| 530 | name); |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | /* Create the file. */ |
| 535 | file = ctf_fs_file_create(ctf_fs); |
| 536 | if (!file) { |
| 537 | PERR("Cannot create stream file object\n"); |
| 538 | goto error; |
| 539 | } |
| 540 | |
| 541 | /* Create full path string. */ |
| 542 | g_string_append_printf(file->path, "%s/%s", |
| 543 | ctf_fs->trace_path->str, name); |
| 544 | if (!g_file_test(file->path->str, G_FILE_TEST_IS_REGULAR)) { |
| 545 | PDBG("Ignoring non-regular file \"%s\"\n", name); |
| 546 | ctf_fs_file_destroy(file); |
| 547 | continue; |
| 548 | } |
| 549 | |
| 550 | /* Open the file. */ |
| 551 | if (ctf_fs_file_open(ctf_fs, file, "rb")) { |
| 552 | ctf_fs_file_destroy(file); |
| 553 | goto error; |
| 554 | } |
| 555 | |
| 556 | if (file->size == 0) { |
| 557 | /* Skip empty stream. */ |
| 558 | ctf_fs_file_destroy(file); |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | /* Create a private stream; file ownership is passed to it. */ |
| 563 | stream = ctf_fs_stream_create(ctf_fs, file); |
| 564 | if (!stream) { |
| 565 | ctf_fs_file_destroy(file); |
| 566 | goto error; |
| 567 | } |
| 568 | |
| 569 | g_ptr_array_add(ctf_it->pending_streams, stream); |
| 570 | } |
| 571 | |
| 572 | goto end; |
| 573 | error: |
| 574 | ret = -1; |
| 575 | end: |
| 576 | if (dir) { |
| 577 | g_dir_close(dir); |
| 578 | dir = NULL; |
| 579 | } |
| 580 | if (error) { |
| 581 | g_error_free(error); |
| 582 | } |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | enum bt_notification_iterator_status ctf_fs_iterator_init(struct bt_component *source, |
| 587 | struct bt_notification_iterator *it, |
| 588 | UNUSED_VAR void *init_method_data) |
| 589 | { |
| 590 | struct ctf_fs_iterator *ctf_it; |
| 591 | struct ctf_fs_component *ctf_fs; |
| 592 | enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 593 | |
| 594 | assert(source && it); |
| 595 | |
| 596 | ctf_fs = bt_component_get_private_data(source); |
| 597 | if (!ctf_fs) { |
| 598 | ret = BT_NOTIFICATION_ITERATOR_STATUS_INVAL; |
| 599 | goto end; |
| 600 | } |
| 601 | |
| 602 | ctf_it = g_new0(struct ctf_fs_iterator, 1); |
| 603 | if (!ctf_it) { |
| 604 | ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; |
| 605 | goto end; |
| 606 | } |
| 607 | |
| 608 | ctf_it->stream_ht = g_hash_table_new_full(g_direct_hash, |
| 609 | g_direct_equal, bt_put, stream_destroy); |
| 610 | if (!ctf_it->stream_ht) { |
| 611 | goto error; |
| 612 | } |
| 613 | ctf_it->pending_streams = g_ptr_array_new_with_free_func( |
| 614 | stream_destroy); |
| 615 | if (!ctf_it->pending_streams) { |
| 616 | goto error; |
| 617 | } |
| 618 | ctf_it->pending_notifications = bt_notification_heap_create( |
| 619 | compare_notifications, NULL); |
| 620 | if (!ctf_it->pending_notifications) { |
| 621 | goto error; |
| 622 | } |
| 623 | |
| 624 | ret = open_trace_streams(ctf_fs, ctf_it); |
| 625 | if (ret) { |
| 626 | goto error; |
| 627 | } |
| 628 | |
| 629 | ret = bt_notification_iterator_set_private_data(it, ctf_it); |
| 630 | if (ret) { |
| 631 | goto error; |
| 632 | } |
| 633 | |
| 634 | end: |
| 635 | return ret; |
| 636 | error: |
| 637 | (void) bt_notification_iterator_set_private_data(it, NULL); |
| 638 | ctf_fs_iterator_destroy_data(ctf_it); |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | static |
| 643 | void ctf_fs_destroy_data(struct ctf_fs_component *ctf_fs) |
| 644 | { |
| 645 | if (!ctf_fs) { |
| 646 | return; |
| 647 | } |
| 648 | if (ctf_fs->trace_path) { |
| 649 | g_string_free(ctf_fs->trace_path, TRUE); |
| 650 | } |
| 651 | if (ctf_fs->metadata) { |
| 652 | ctf_fs_metadata_fini(ctf_fs->metadata); |
| 653 | g_free(ctf_fs->metadata); |
| 654 | } |
| 655 | g_free(ctf_fs); |
| 656 | } |
| 657 | |
| 658 | void ctf_fs_destroy(struct bt_component *component) |
| 659 | { |
| 660 | void *data = bt_component_get_private_data(component); |
| 661 | |
| 662 | ctf_fs_destroy_data(data); |
| 663 | } |
| 664 | |
| 665 | static |
| 666 | struct ctf_fs_component *ctf_fs_create(struct bt_value *params) |
| 667 | { |
| 668 | struct ctf_fs_component *ctf_fs; |
| 669 | struct bt_value *value = NULL; |
| 670 | const char *path; |
| 671 | enum bt_value_status ret; |
| 672 | |
| 673 | ctf_fs = g_new0(struct ctf_fs_component, 1); |
| 674 | if (!ctf_fs) { |
| 675 | goto end; |
| 676 | } |
| 677 | |
| 678 | /* FIXME: should probably look for a source URI */ |
| 679 | value = bt_value_map_get(params, "path"); |
| 680 | if (!value || bt_value_is_null(value) || !bt_value_is_string(value)) { |
| 681 | goto error; |
| 682 | } |
| 683 | |
| 684 | ret = bt_value_string_get(value, &path); |
| 685 | if (ret != BT_VALUE_STATUS_OK) { |
| 686 | goto error; |
| 687 | } |
| 688 | |
| 689 | ctf_fs->trace_path = g_string_new(path); |
| 690 | if (!ctf_fs->trace_path) { |
| 691 | goto error; |
| 692 | } |
| 693 | ctf_fs->error_fp = stderr; |
| 694 | ctf_fs->page_size = (size_t) getpagesize(); |
| 695 | |
| 696 | // FIXME: check error. |
| 697 | ctf_fs->metadata = g_new0(struct ctf_fs_metadata, 1); |
| 698 | if (!ctf_fs->metadata) { |
| 699 | goto error; |
| 700 | } |
| 701 | ctf_fs_metadata_set_trace(ctf_fs); |
| 702 | goto end; |
| 703 | |
| 704 | error: |
| 705 | ctf_fs_destroy_data(ctf_fs); |
| 706 | ctf_fs = NULL; |
| 707 | end: |
| 708 | BT_PUT(value); |
| 709 | return ctf_fs; |
| 710 | } |
| 711 | |
| 712 | BT_HIDDEN |
| 713 | enum bt_component_status ctf_fs_init(struct bt_component *source, |
| 714 | struct bt_value *params, UNUSED_VAR void *init_method_data) |
| 715 | { |
| 716 | struct ctf_fs_component *ctf_fs; |
| 717 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 718 | |
| 719 | assert(source); |
| 720 | ctf_fs_debug = g_strcmp0(getenv("CTF_FS_DEBUG"), "1") == 0; |
| 721 | ctf_fs = ctf_fs_create(params); |
| 722 | if (!ctf_fs) { |
| 723 | ret = BT_COMPONENT_STATUS_NOMEM; |
| 724 | goto end; |
| 725 | } |
| 726 | |
| 727 | ret = bt_component_set_private_data(source, ctf_fs); |
| 728 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 729 | goto error; |
| 730 | } |
| 731 | end: |
| 732 | return ret; |
| 733 | error: |
| 734 | (void) bt_component_set_private_data(source, NULL); |
| 735 | ctf_fs_destroy_data(ctf_fs); |
| 736 | return ret; |
| 737 | } |