| 1 | /* |
| 2 | * writer.c |
| 3 | * |
| 4 | * Babeltrace CTF Writer Output Plugin Event Handling |
| 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/event.h> |
| 30 | #include <babeltrace/ctf-ir/packet.h> |
| 31 | #include <babeltrace/ctf-ir/event-class.h> |
| 32 | #include <babeltrace/ctf-ir/stream.h> |
| 33 | #include <babeltrace/ctf-ir/stream-class.h> |
| 34 | #include <babeltrace/ctf-ir/clock-class.h> |
| 35 | #include <babeltrace/ctf-ir/fields.h> |
| 36 | #include <babeltrace/ctf-writer/stream-class.h> |
| 37 | #include <babeltrace/ctf-writer/stream.h> |
| 38 | #include <assert.h> |
| 39 | |
| 40 | #include <ctfcopytrace.h> |
| 41 | |
| 42 | #include "writer.h" |
| 43 | |
| 44 | static |
| 45 | void unref_stream_class(struct bt_ctf_stream_class *writer_stream_class) |
| 46 | { |
| 47 | bt_put(writer_stream_class); |
| 48 | } |
| 49 | |
| 50 | static |
| 51 | void unref_stream(struct bt_ctf_stream_class *writer_stream) |
| 52 | { |
| 53 | bt_put(writer_stream); |
| 54 | } |
| 55 | |
| 56 | static |
| 57 | gboolean empty_ht(gpointer key, gpointer value, gpointer user_data) |
| 58 | { |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | gboolean empty_streams_ht(gpointer key, gpointer value, gpointer user_data) |
| 64 | { |
| 65 | struct bt_ctf_stream *writer_stream = value; |
| 66 | |
| 67 | bt_ctf_stream_flush(writer_stream); |
| 68 | |
| 69 | return TRUE; |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | void destroy_stream_state_key(gpointer key) |
| 74 | { |
| 75 | g_free((enum fs_writer_stream_state *) key); |
| 76 | } |
| 77 | |
| 78 | static |
| 79 | void check_completed_trace(gpointer key, gpointer value, gpointer user_data) |
| 80 | { |
| 81 | enum fs_writer_stream_state *state = value; |
| 82 | int *trace_completed = user_data; |
| 83 | |
| 84 | if (*state != FS_WRITER_COMPLETED_STREAM) { |
| 85 | *trace_completed = 0; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static |
| 90 | void trace_is_static_listener(struct bt_ctf_trace *trace, void *data) |
| 91 | { |
| 92 | struct fs_writer *fs_writer = data; |
| 93 | int trace_completed = 1; |
| 94 | |
| 95 | fs_writer->trace_static = 1; |
| 96 | |
| 97 | g_hash_table_foreach(fs_writer->stream_states, |
| 98 | check_completed_trace, &trace_completed); |
| 99 | if (trace_completed) { |
| 100 | writer_close(fs_writer->writer_component, fs_writer); |
| 101 | g_hash_table_remove(fs_writer->writer_component->trace_map, |
| 102 | fs_writer->trace); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static |
| 107 | struct bt_ctf_stream_class *insert_new_stream_class( |
| 108 | struct writer_component *writer_component, |
| 109 | struct fs_writer *fs_writer, |
| 110 | struct bt_ctf_stream_class *stream_class) |
| 111 | { |
| 112 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 113 | struct bt_ctf_trace *trace = NULL, *writer_trace = NULL; |
| 114 | struct bt_ctf_writer *ctf_writer = fs_writer->writer; |
| 115 | enum bt_component_status ret; |
| 116 | |
| 117 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 118 | if (!trace) { |
| 119 | fprintf(writer_component->err, |
| 120 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 121 | __LINE__); |
| 122 | goto error; |
| 123 | } |
| 124 | |
| 125 | writer_trace = bt_ctf_writer_get_trace(ctf_writer); |
| 126 | if (!writer_trace) { |
| 127 | fprintf(writer_component->err, |
| 128 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 129 | __LINE__); |
| 130 | goto error; |
| 131 | } |
| 132 | |
| 133 | ret = ctf_copy_clock_classes(writer_component->err, writer_trace, |
| 134 | writer_stream_class, trace); |
| 135 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 136 | fprintf(writer_component->err, |
| 137 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 138 | __LINE__); |
| 139 | goto error; |
| 140 | } |
| 141 | |
| 142 | writer_stream_class = ctf_copy_stream_class(writer_component->err, |
| 143 | stream_class, writer_trace, true); |
| 144 | if (!writer_stream_class) { |
| 145 | fprintf(writer_component->err, "[error] Failed to copy stream class\n"); |
| 146 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 147 | __func__, __FILE__, __LINE__); |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | g_hash_table_insert(fs_writer->stream_class_map, |
| 152 | (gpointer) stream_class, writer_stream_class); |
| 153 | |
| 154 | goto end; |
| 155 | |
| 156 | error: |
| 157 | BT_PUT(writer_stream_class); |
| 158 | end: |
| 159 | bt_put(writer_trace); |
| 160 | bt_put(trace); |
| 161 | return writer_stream_class; |
| 162 | } |
| 163 | |
| 164 | static |
| 165 | enum fs_writer_stream_state *insert_new_stream_state( |
| 166 | struct writer_component *writer_component, |
| 167 | struct fs_writer *fs_writer, struct bt_ctf_stream *stream) |
| 168 | { |
| 169 | enum fs_writer_stream_state *v = NULL; |
| 170 | |
| 171 | v = g_new0(enum fs_writer_stream_state, 1); |
| 172 | if (!v) { |
| 173 | fprintf(writer_component->err, |
| 174 | "[error] %s in %s:%d\n", __func__, |
| 175 | __FILE__, __LINE__); |
| 176 | } |
| 177 | *v = FS_WRITER_UNKNOWN_STREAM; |
| 178 | |
| 179 | g_hash_table_insert(fs_writer->stream_states, stream, v); |
| 180 | |
| 181 | return v; |
| 182 | } |
| 183 | |
| 184 | static |
| 185 | int make_trace_path(struct writer_component *writer_component, |
| 186 | struct bt_ctf_trace *trace, char *trace_path) |
| 187 | { |
| 188 | int ret; |
| 189 | const char *trace_name; |
| 190 | |
| 191 | trace_name = bt_ctf_trace_get_name(trace); |
| 192 | if (!trace_name) { |
| 193 | trace_name = writer_component->trace_name_base->str; |
| 194 | } |
| 195 | /* XXX: we might have to skip the first level, TBD. */ |
| 196 | |
| 197 | /* Sanitize the trace name. */ |
| 198 | if (strlen(trace_name) == 2 && !strcmp(trace_name, "..")) { |
| 199 | fprintf(writer_component->err, "[error] Trace name cannot " |
| 200 | "be \"..\"\n"); |
| 201 | goto error; |
| 202 | } |
| 203 | |
| 204 | if (strstr(trace_name, "../")) { |
| 205 | fprintf(writer_component->err, "[error] Trace name cannot " |
| 206 | "contain \"../\", received \"%s\"\n", |
| 207 | trace_name); |
| 208 | goto error; |
| 209 | |
| 210 | } |
| 211 | |
| 212 | snprintf(trace_path, PATH_MAX, "%s/%s", |
| 213 | writer_component->base_path->str, |
| 214 | trace_name); |
| 215 | if (g_file_test(trace_path, G_FILE_TEST_EXISTS)) { |
| 216 | int i = 0; |
| 217 | do { |
| 218 | snprintf(trace_path, PATH_MAX, "%s/%s-%d", |
| 219 | writer_component->base_path->str, |
| 220 | trace_name, ++i); |
| 221 | } while (g_file_test(trace_path, G_FILE_TEST_EXISTS) && i < INT_MAX); |
| 222 | if (i == INT_MAX) { |
| 223 | fprintf(writer_component->err, "[error] Unable to find " |
| 224 | "a unique trace path\n"); |
| 225 | goto error; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | ret = 0; |
| 230 | |
| 231 | goto end; |
| 232 | |
| 233 | error: |
| 234 | ret = -1; |
| 235 | end: |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static |
| 240 | struct fs_writer *insert_new_writer( |
| 241 | struct writer_component *writer_component, |
| 242 | struct bt_ctf_trace *trace) |
| 243 | { |
| 244 | struct bt_ctf_writer *ctf_writer = NULL; |
| 245 | struct bt_ctf_trace *writer_trace = NULL; |
| 246 | char trace_path[PATH_MAX]; |
| 247 | enum bt_component_status ret; |
| 248 | struct bt_ctf_stream *stream = NULL; |
| 249 | struct fs_writer *fs_writer = NULL; |
| 250 | int nr_stream, i; |
| 251 | |
| 252 | ret = make_trace_path(writer_component, trace, trace_path); |
| 253 | if (ret) { |
| 254 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 255 | __func__, __FILE__, __LINE__); |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | printf("ctf.fs sink creating trace in %s\n", trace_path); |
| 260 | |
| 261 | ctf_writer = bt_ctf_writer_create(trace_path); |
| 262 | if (!ctf_writer) { |
| 263 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 264 | __func__, __FILE__, __LINE__); |
| 265 | goto error; |
| 266 | } |
| 267 | |
| 268 | writer_trace = bt_ctf_writer_get_trace(ctf_writer); |
| 269 | if (!writer_trace) { |
| 270 | fprintf(writer_component->err, |
| 271 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 272 | __LINE__); |
| 273 | goto error; |
| 274 | } |
| 275 | |
| 276 | ret = ctf_copy_trace(writer_component->err, trace, writer_trace); |
| 277 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 278 | fprintf(writer_component->err, "[error] Failed to copy trace\n"); |
| 279 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 280 | __func__, __FILE__, __LINE__); |
| 281 | BT_PUT(ctf_writer); |
| 282 | goto error; |
| 283 | } |
| 284 | |
| 285 | fs_writer = g_new0(struct fs_writer, 1); |
| 286 | if (!fs_writer) { |
| 287 | fprintf(writer_component->err, |
| 288 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 289 | __LINE__); |
| 290 | goto error; |
| 291 | } |
| 292 | fs_writer->writer = ctf_writer; |
| 293 | fs_writer->trace = trace; |
| 294 | fs_writer->writer_trace = writer_trace; |
| 295 | fs_writer->writer_component = writer_component; |
| 296 | BT_PUT(writer_trace); |
| 297 | fs_writer->stream_class_map = g_hash_table_new_full(g_direct_hash, |
| 298 | g_direct_equal, NULL, (GDestroyNotify) unref_stream_class); |
| 299 | fs_writer->stream_map = g_hash_table_new_full(g_direct_hash, |
| 300 | g_direct_equal, NULL, (GDestroyNotify) unref_stream); |
| 301 | fs_writer->stream_states = g_hash_table_new_full(g_direct_hash, |
| 302 | g_direct_equal, NULL, destroy_stream_state_key); |
| 303 | |
| 304 | /* Set all the existing streams in the unknown state. */ |
| 305 | nr_stream = bt_ctf_trace_get_stream_count(trace); |
| 306 | for (i = 0; i < nr_stream; i++) { |
| 307 | stream = bt_ctf_trace_get_stream_by_index(trace, i); |
| 308 | if (!stream) { |
| 309 | fprintf(writer_component->err, |
| 310 | "[error] %s in %s:%d\n", __func__, |
| 311 | __FILE__, __LINE__); |
| 312 | goto error; |
| 313 | } |
| 314 | insert_new_stream_state(writer_component, fs_writer, stream); |
| 315 | BT_PUT(stream); |
| 316 | } |
| 317 | |
| 318 | /* Check if the trace is already static or register a listener. */ |
| 319 | if (bt_ctf_trace_is_static(trace)) { |
| 320 | fs_writer->trace_static = 1; |
| 321 | fs_writer->static_listener_id = -1; |
| 322 | } else { |
| 323 | ret = bt_ctf_trace_add_is_static_listener(trace, |
| 324 | trace_is_static_listener, fs_writer); |
| 325 | if (ret < 0) { |
| 326 | fprintf(writer_component->err, |
| 327 | "[error] %s in %s:%d\n", __func__, __FILE__, |
| 328 | __LINE__); |
| 329 | goto error; |
| 330 | } |
| 331 | fs_writer->static_listener_id = ret; |
| 332 | } |
| 333 | |
| 334 | g_hash_table_insert(writer_component->trace_map, (gpointer) trace, |
| 335 | fs_writer); |
| 336 | |
| 337 | goto end; |
| 338 | |
| 339 | error: |
| 340 | g_free(fs_writer); |
| 341 | fs_writer = NULL; |
| 342 | bt_put(writer_trace); |
| 343 | bt_put(stream); |
| 344 | BT_PUT(ctf_writer); |
| 345 | end: |
| 346 | return fs_writer; |
| 347 | } |
| 348 | |
| 349 | static |
| 350 | struct fs_writer *get_fs_writer(struct writer_component *writer_component, |
| 351 | struct bt_ctf_stream_class *stream_class) |
| 352 | { |
| 353 | struct bt_ctf_trace *trace = NULL; |
| 354 | struct fs_writer *fs_writer; |
| 355 | |
| 356 | trace = bt_ctf_stream_class_get_trace(stream_class); |
| 357 | if (!trace) { |
| 358 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 359 | __func__, __FILE__, __LINE__); |
| 360 | goto error; |
| 361 | } |
| 362 | |
| 363 | fs_writer = g_hash_table_lookup(writer_component->trace_map, |
| 364 | (gpointer) trace); |
| 365 | if (!fs_writer) { |
| 366 | fs_writer = insert_new_writer(writer_component, trace); |
| 367 | } |
| 368 | BT_PUT(trace); |
| 369 | goto end; |
| 370 | |
| 371 | error: |
| 372 | fs_writer = NULL; |
| 373 | end: |
| 374 | return fs_writer; |
| 375 | } |
| 376 | |
| 377 | static |
| 378 | struct fs_writer *get_fs_writer_from_stream( |
| 379 | struct writer_component *writer_component, |
| 380 | struct bt_ctf_stream *stream) |
| 381 | { |
| 382 | struct bt_ctf_stream_class *stream_class = NULL; |
| 383 | struct fs_writer *fs_writer; |
| 384 | |
| 385 | stream_class = bt_ctf_stream_get_class(stream); |
| 386 | if (!stream_class) { |
| 387 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 388 | __func__, __FILE__, __LINE__); |
| 389 | goto error; |
| 390 | } |
| 391 | |
| 392 | fs_writer = get_fs_writer(writer_component, stream_class); |
| 393 | goto end; |
| 394 | |
| 395 | error: |
| 396 | fs_writer = NULL; |
| 397 | |
| 398 | end: |
| 399 | bt_put(stream_class); |
| 400 | return fs_writer; |
| 401 | } |
| 402 | |
| 403 | static |
| 404 | struct bt_ctf_stream_class *lookup_stream_class( |
| 405 | struct writer_component *writer_component, |
| 406 | struct bt_ctf_stream_class *stream_class) |
| 407 | { |
| 408 | struct fs_writer *fs_writer = get_fs_writer( |
| 409 | writer_component, stream_class); |
| 410 | assert(fs_writer); |
| 411 | return (struct bt_ctf_stream_class *) g_hash_table_lookup( |
| 412 | fs_writer->stream_class_map, (gpointer) stream_class); |
| 413 | } |
| 414 | |
| 415 | static |
| 416 | struct bt_ctf_stream *lookup_stream(struct writer_component *writer_component, |
| 417 | struct bt_ctf_stream *stream) |
| 418 | { |
| 419 | struct fs_writer *fs_writer = get_fs_writer_from_stream( |
| 420 | writer_component, stream); |
| 421 | assert(fs_writer); |
| 422 | return (struct bt_ctf_stream *) g_hash_table_lookup( |
| 423 | fs_writer->stream_map, (gpointer) stream); |
| 424 | } |
| 425 | |
| 426 | static |
| 427 | struct bt_ctf_stream *insert_new_stream( |
| 428 | struct writer_component *writer_component, |
| 429 | struct fs_writer *fs_writer, |
| 430 | struct bt_ctf_stream_class *stream_class, |
| 431 | struct bt_ctf_stream *stream) |
| 432 | { |
| 433 | struct bt_ctf_stream *writer_stream = NULL; |
| 434 | struct bt_ctf_stream_class *writer_stream_class = NULL; |
| 435 | struct bt_ctf_writer *ctf_writer = bt_get(fs_writer->writer); |
| 436 | |
| 437 | writer_stream_class = lookup_stream_class(writer_component, |
| 438 | stream_class); |
| 439 | if (!writer_stream_class) { |
| 440 | writer_stream_class = insert_new_stream_class( |
| 441 | writer_component, fs_writer, stream_class); |
| 442 | if (!writer_stream_class) { |
| 443 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 444 | __func__, __FILE__, __LINE__); |
| 445 | goto error; |
| 446 | } |
| 447 | } |
| 448 | bt_get(writer_stream_class); |
| 449 | |
| 450 | writer_stream = bt_ctf_writer_create_stream(ctf_writer, |
| 451 | writer_stream_class); |
| 452 | if (!writer_stream) { |
| 453 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 454 | __func__, __FILE__, __LINE__); |
| 455 | goto error; |
| 456 | } |
| 457 | |
| 458 | g_hash_table_insert(fs_writer->stream_map, (gpointer) stream, |
| 459 | writer_stream); |
| 460 | |
| 461 | goto end; |
| 462 | |
| 463 | error: |
| 464 | BT_PUT(writer_stream); |
| 465 | end: |
| 466 | bt_put(ctf_writer); |
| 467 | bt_put(writer_stream_class); |
| 468 | return writer_stream; |
| 469 | } |
| 470 | |
| 471 | static |
| 472 | struct bt_ctf_event_class *get_event_class(struct writer_component *writer_component, |
| 473 | struct bt_ctf_stream_class *writer_stream_class, |
| 474 | struct bt_ctf_event_class *event_class) |
| 475 | { |
| 476 | return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class, |
| 477 | bt_ctf_event_class_get_id(event_class)); |
| 478 | } |
| 479 | |
| 480 | static |
| 481 | struct bt_ctf_stream *get_writer_stream( |
| 482 | struct writer_component *writer_component, |
| 483 | struct bt_ctf_packet *packet, struct bt_ctf_stream *stream) |
| 484 | { |
| 485 | struct bt_ctf_stream *writer_stream = NULL; |
| 486 | |
| 487 | writer_stream = lookup_stream(writer_component, stream); |
| 488 | if (!writer_stream) { |
| 489 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 490 | __func__, __FILE__, __LINE__); |
| 491 | goto error; |
| 492 | } |
| 493 | bt_get(writer_stream); |
| 494 | |
| 495 | goto end; |
| 496 | |
| 497 | error: |
| 498 | BT_PUT(writer_stream); |
| 499 | end: |
| 500 | return writer_stream; |
| 501 | } |
| 502 | |
| 503 | BT_HIDDEN |
| 504 | void writer_close(struct writer_component *writer_component, |
| 505 | struct fs_writer *fs_writer) |
| 506 | { |
| 507 | if (fs_writer->static_listener_id >= 0) { |
| 508 | bt_ctf_trace_remove_is_static_listener(fs_writer->trace, |
| 509 | fs_writer->static_listener_id); |
| 510 | } |
| 511 | |
| 512 | /* Empty the stream class HT. */ |
| 513 | g_hash_table_foreach_remove(fs_writer->stream_class_map, |
| 514 | empty_ht, NULL); |
| 515 | g_hash_table_destroy(fs_writer->stream_class_map); |
| 516 | |
| 517 | /* Empty the stream HT. */ |
| 518 | g_hash_table_foreach_remove(fs_writer->stream_map, |
| 519 | empty_streams_ht, NULL); |
| 520 | g_hash_table_destroy(fs_writer->stream_map); |
| 521 | |
| 522 | /* Empty the stream state HT. */ |
| 523 | g_hash_table_foreach_remove(fs_writer->stream_states, |
| 524 | empty_ht, NULL); |
| 525 | g_hash_table_destroy(fs_writer->stream_states); |
| 526 | } |
| 527 | |
| 528 | BT_HIDDEN |
| 529 | enum bt_component_status writer_stream_begin( |
| 530 | struct writer_component *writer_component, |
| 531 | struct bt_ctf_stream *stream) |
| 532 | { |
| 533 | struct bt_ctf_stream_class *stream_class = NULL; |
| 534 | struct fs_writer *fs_writer; |
| 535 | struct bt_ctf_stream *writer_stream = NULL; |
| 536 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 537 | enum fs_writer_stream_state *state; |
| 538 | |
| 539 | stream_class = bt_ctf_stream_get_class(stream); |
| 540 | if (!stream_class) { |
| 541 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 542 | __func__, __FILE__, __LINE__); |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | fs_writer = get_fs_writer(writer_component, stream_class); |
| 547 | if (!fs_writer) { |
| 548 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 549 | __func__, __FILE__, __LINE__); |
| 550 | goto error; |
| 551 | } |
| 552 | |
| 553 | /* Set the stream as active */ |
| 554 | state = g_hash_table_lookup(fs_writer->stream_states, stream); |
| 555 | if (!state) { |
| 556 | if (fs_writer->trace_static) { |
| 557 | fprintf(writer_component->err, "[error] Adding a new " |
| 558 | "stream on a static trace\n"); |
| 559 | goto error; |
| 560 | } |
| 561 | state = insert_new_stream_state(writer_component, fs_writer, |
| 562 | stream); |
| 563 | } |
| 564 | if (*state != FS_WRITER_UNKNOWN_STREAM) { |
| 565 | fprintf(writer_component->err, "[error] Unexpected stream " |
| 566 | "state %d\n", *state); |
| 567 | goto error; |
| 568 | } |
| 569 | *state = FS_WRITER_ACTIVE_STREAM; |
| 570 | |
| 571 | writer_stream = insert_new_stream(writer_component, fs_writer, |
| 572 | stream_class, stream); |
| 573 | if (!writer_stream) { |
| 574 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 575 | __func__, __FILE__, __LINE__); |
| 576 | goto error; |
| 577 | } |
| 578 | |
| 579 | goto end; |
| 580 | |
| 581 | error: |
| 582 | ret = BT_COMPONENT_STATUS_ERROR; |
| 583 | end: |
| 584 | bt_put(stream_class); |
| 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | BT_HIDDEN |
| 589 | enum bt_component_status writer_stream_end( |
| 590 | struct writer_component *writer_component, |
| 591 | struct bt_ctf_stream *stream) |
| 592 | { |
| 593 | struct bt_ctf_stream_class *stream_class = NULL; |
| 594 | struct fs_writer *fs_writer; |
| 595 | struct bt_ctf_trace *trace = NULL; |
| 596 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 597 | enum fs_writer_stream_state *state; |
| 598 | |
| 599 | stream_class = bt_ctf_stream_get_class(stream); |
| 600 | if (!stream_class) { |
| 601 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 602 | __func__, __FILE__, __LINE__); |
| 603 | goto error; |
| 604 | } |
| 605 | |
| 606 | fs_writer = get_fs_writer(writer_component, stream_class); |
| 607 | if (!fs_writer) { |
| 608 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 609 | __func__, __FILE__, __LINE__); |
| 610 | goto error; |
| 611 | } |
| 612 | |
| 613 | state = g_hash_table_lookup(fs_writer->stream_states, stream); |
| 614 | if (*state != FS_WRITER_ACTIVE_STREAM) { |
| 615 | fprintf(writer_component->err, "[error] Unexpected stream " |
| 616 | "state %d\n", *state); |
| 617 | goto error; |
| 618 | } |
| 619 | *state = FS_WRITER_COMPLETED_STREAM; |
| 620 | |
| 621 | g_hash_table_remove(fs_writer->stream_map, stream); |
| 622 | |
| 623 | if (fs_writer->trace_static) { |
| 624 | int trace_completed = 1; |
| 625 | |
| 626 | g_hash_table_foreach(fs_writer->stream_states, |
| 627 | check_completed_trace, &trace_completed); |
| 628 | if (trace_completed) { |
| 629 | writer_close(writer_component, fs_writer); |
| 630 | g_hash_table_remove(writer_component->trace_map, |
| 631 | fs_writer->trace); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | goto end; |
| 636 | |
| 637 | error: |
| 638 | ret = BT_COMPONENT_STATUS_ERROR; |
| 639 | end: |
| 640 | BT_PUT(trace); |
| 641 | BT_PUT(stream_class); |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | BT_HIDDEN |
| 646 | enum bt_component_status writer_new_packet( |
| 647 | struct writer_component *writer_component, |
| 648 | struct bt_ctf_packet *packet) |
| 649 | { |
| 650 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
| 651 | struct bt_ctf_field *writer_packet_context = NULL; |
| 652 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 653 | int int_ret; |
| 654 | |
| 655 | stream = bt_ctf_packet_get_stream(packet); |
| 656 | if (!stream) { |
| 657 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 658 | __func__, __FILE__, __LINE__); |
| 659 | goto error; |
| 660 | } |
| 661 | |
| 662 | writer_stream = get_writer_stream(writer_component, packet, stream); |
| 663 | if (!writer_stream) { |
| 664 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 665 | __func__, __FILE__, __LINE__); |
| 666 | goto error; |
| 667 | } |
| 668 | BT_PUT(stream); |
| 669 | |
| 670 | writer_packet_context = ctf_copy_packet_context(writer_component->err, |
| 671 | packet, writer_stream); |
| 672 | if (!writer_packet_context) { |
| 673 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 674 | __func__, __FILE__, __LINE__); |
| 675 | goto error; |
| 676 | } |
| 677 | |
| 678 | int_ret = bt_ctf_stream_set_packet_context(writer_stream, |
| 679 | writer_packet_context); |
| 680 | if (int_ret < 0) { |
| 681 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 682 | __FILE__, __LINE__); |
| 683 | goto error; |
| 684 | } |
| 685 | BT_PUT(writer_stream); |
| 686 | BT_PUT(writer_packet_context); |
| 687 | |
| 688 | goto end; |
| 689 | |
| 690 | error: |
| 691 | ret = BT_COMPONENT_STATUS_ERROR; |
| 692 | end: |
| 693 | bt_put(writer_stream); |
| 694 | bt_put(writer_packet_context); |
| 695 | bt_put(stream); |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | BT_HIDDEN |
| 700 | enum bt_component_status writer_close_packet( |
| 701 | struct writer_component *writer_component, |
| 702 | struct bt_ctf_packet *packet) |
| 703 | { |
| 704 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
| 705 | enum bt_component_status ret; |
| 706 | |
| 707 | stream = bt_ctf_packet_get_stream(packet); |
| 708 | if (!stream) { |
| 709 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 710 | __func__, __FILE__, __LINE__); |
| 711 | goto error; |
| 712 | } |
| 713 | |
| 714 | writer_stream = lookup_stream(writer_component, stream); |
| 715 | if (!writer_stream) { |
| 716 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 717 | __func__, __FILE__, __LINE__); |
| 718 | goto error; |
| 719 | } |
| 720 | BT_PUT(stream); |
| 721 | |
| 722 | bt_get(writer_stream); |
| 723 | |
| 724 | ret = bt_ctf_stream_flush(writer_stream); |
| 725 | if (ret < 0) { |
| 726 | fprintf(writer_component->err, |
| 727 | "[error] Failed to flush packet\n"); |
| 728 | goto error; |
| 729 | } |
| 730 | BT_PUT(writer_stream); |
| 731 | |
| 732 | ret = BT_COMPONENT_STATUS_OK; |
| 733 | goto end; |
| 734 | |
| 735 | error: |
| 736 | ret = BT_COMPONENT_STATUS_ERROR; |
| 737 | end: |
| 738 | bt_put(writer_stream); |
| 739 | bt_put(stream); |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | BT_HIDDEN |
| 744 | enum bt_component_status writer_output_event( |
| 745 | struct writer_component *writer_component, |
| 746 | struct bt_ctf_event *event) |
| 747 | { |
| 748 | enum bt_component_status ret; |
| 749 | struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL; |
| 750 | struct bt_ctf_stream *stream = NULL, *writer_stream = NULL; |
| 751 | struct bt_ctf_stream_class *stream_class = NULL, *writer_stream_class = NULL; |
| 752 | struct bt_ctf_event *writer_event = NULL; |
| 753 | const char *event_name; |
| 754 | int int_ret; |
| 755 | |
| 756 | event_class = bt_ctf_event_get_class(event); |
| 757 | if (!event_class) { |
| 758 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 759 | __FILE__, __LINE__); |
| 760 | goto error; |
| 761 | } |
| 762 | |
| 763 | event_name = bt_ctf_event_class_get_name(event_class); |
| 764 | if (!event_name) { |
| 765 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 766 | __FILE__, __LINE__); |
| 767 | goto error; |
| 768 | } |
| 769 | |
| 770 | stream = bt_ctf_event_get_stream(event); |
| 771 | if (!stream) { |
| 772 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 773 | __FILE__, __LINE__); |
| 774 | goto error; |
| 775 | } |
| 776 | |
| 777 | writer_stream = lookup_stream(writer_component, stream); |
| 778 | if (!writer_stream || !bt_get(writer_stream)) { |
| 779 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 780 | __FILE__, __LINE__); |
| 781 | goto error; |
| 782 | } |
| 783 | |
| 784 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 785 | if (!stream_class) { |
| 786 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 787 | __FILE__, __LINE__); |
| 788 | goto error; |
| 789 | } |
| 790 | |
| 791 | writer_stream_class = lookup_stream_class(writer_component, stream_class); |
| 792 | if (!writer_stream_class || !bt_get(writer_stream_class)) { |
| 793 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 794 | __FILE__, __LINE__); |
| 795 | goto error; |
| 796 | } |
| 797 | |
| 798 | writer_event_class = get_event_class(writer_component, |
| 799 | writer_stream_class, event_class); |
| 800 | if (!writer_event_class) { |
| 801 | writer_event_class = ctf_copy_event_class(writer_component->err, |
| 802 | event_class); |
| 803 | if (!writer_event_class) { |
| 804 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 805 | __func__, __FILE__, __LINE__); |
| 806 | goto error; |
| 807 | } |
| 808 | int_ret = bt_ctf_stream_class_add_event_class( |
| 809 | writer_stream_class, writer_event_class); |
| 810 | if (int_ret) { |
| 811 | fprintf(writer_component->err, "[error] %s in %s:%d\n", |
| 812 | __func__, __FILE__, __LINE__); |
| 813 | goto error; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | writer_event = ctf_copy_event(writer_component->err, event, |
| 818 | writer_event_class, true); |
| 819 | if (!writer_event) { |
| 820 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 821 | __FILE__, __LINE__); |
| 822 | fprintf(writer_component->err, "[error] Failed to copy event %s\n", |
| 823 | bt_ctf_event_class_get_name(writer_event_class)); |
| 824 | goto error; |
| 825 | } |
| 826 | |
| 827 | int_ret = bt_ctf_stream_append_event(writer_stream, writer_event); |
| 828 | if (int_ret < 0) { |
| 829 | fprintf(writer_component->err, "[error] %s in %s:%d\n", __func__, |
| 830 | __FILE__, __LINE__); |
| 831 | fprintf(writer_component->err, "[error] Failed to append event %s\n", |
| 832 | bt_ctf_event_class_get_name(writer_event_class)); |
| 833 | goto error; |
| 834 | } |
| 835 | |
| 836 | ret = BT_COMPONENT_STATUS_OK; |
| 837 | goto end; |
| 838 | |
| 839 | error: |
| 840 | ret = BT_COMPONENT_STATUS_ERROR; |
| 841 | end: |
| 842 | bt_put(writer_event); |
| 843 | bt_put(writer_event_class); |
| 844 | bt_put(writer_stream_class); |
| 845 | bt_put(stream_class); |
| 846 | bt_put(writer_stream); |
| 847 | bt_put(stream); |
| 848 | bt_put(event_class); |
| 849 | return ret; |
| 850 | } |