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