| 1 | /* |
| 2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp) |
| 24 | #define BT_LOG_OUTPUT_LEVEL (stream->log_level) |
| 25 | #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM" |
| 26 | #include "plugins/comp-logging.h" |
| 27 | |
| 28 | #include <babeltrace2/babeltrace.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <glib.h> |
| 32 | #include "common/assert.h" |
| 33 | #include "ctfser/ctfser.h" |
| 34 | #include "compat/endian.h" |
| 35 | |
| 36 | #include "fs-sink.h" |
| 37 | #include "fs-sink-trace.h" |
| 38 | #include "fs-sink-stream.h" |
| 39 | #include "translate-trace-ir-to-ctf-ir.h" |
| 40 | |
| 41 | BT_HIDDEN |
| 42 | void fs_sink_stream_destroy(struct fs_sink_stream *stream) |
| 43 | { |
| 44 | if (!stream) { |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | bt_ctfser_fini(&stream->ctfser); |
| 49 | |
| 50 | if (stream->file_name) { |
| 51 | g_string_free(stream->file_name, TRUE); |
| 52 | stream->file_name = NULL; |
| 53 | } |
| 54 | |
| 55 | bt_packet_put_ref(stream->packet_state.packet); |
| 56 | g_free(stream); |
| 57 | |
| 58 | end: |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | static |
| 63 | bool stream_file_name_exists(struct fs_sink_trace *trace, const char *name) |
| 64 | { |
| 65 | bool exists = false; |
| 66 | GHashTableIter iter; |
| 67 | gpointer key, value; |
| 68 | |
| 69 | g_hash_table_iter_init(&iter, trace->streams); |
| 70 | |
| 71 | while (g_hash_table_iter_next(&iter, &key, &value)) { |
| 72 | struct fs_sink_stream *stream = value; |
| 73 | |
| 74 | if (strcmp(name, stream->file_name->str) == 0) { |
| 75 | exists = true; |
| 76 | goto end; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | end: |
| 81 | return exists; |
| 82 | } |
| 83 | |
| 84 | static |
| 85 | GString *sanitize_stream_file_name(const char *file_name) |
| 86 | { |
| 87 | GString *san_file_name = g_string_new(NULL); |
| 88 | const char *ch; |
| 89 | gchar *basename; |
| 90 | |
| 91 | BT_ASSERT(san_file_name); |
| 92 | BT_ASSERT(file_name); |
| 93 | basename = g_path_get_basename(file_name); |
| 94 | |
| 95 | for (ch = basename; *ch != '\0'; ch++) { |
| 96 | if (*ch == '/') { |
| 97 | g_string_append_c(san_file_name, '_'); |
| 98 | } else { |
| 99 | g_string_append_c(san_file_name, *ch); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* Do not allow `.` and `..` either */ |
| 104 | if (strcmp(san_file_name->str, ".") == 0 || |
| 105 | strcmp(san_file_name->str, "..") == 0) { |
| 106 | g_string_assign(san_file_name, "stream"); |
| 107 | } |
| 108 | |
| 109 | g_free(basename); |
| 110 | return san_file_name; |
| 111 | } |
| 112 | |
| 113 | static |
| 114 | GString *make_unique_stream_file_name(struct fs_sink_trace *trace, |
| 115 | const char *base) |
| 116 | { |
| 117 | GString *san_base = sanitize_stream_file_name(base); |
| 118 | GString *name = g_string_new(san_base->str); |
| 119 | unsigned int suffix = 0; |
| 120 | |
| 121 | BT_ASSERT(name); |
| 122 | |
| 123 | while (stream_file_name_exists(trace, name->str) && |
| 124 | strcmp(name->str, "metadata") == 0) { |
| 125 | g_string_printf(name, "%s-%u", san_base->str, suffix); |
| 126 | suffix++; |
| 127 | } |
| 128 | |
| 129 | g_string_free(san_base, TRUE); |
| 130 | return name; |
| 131 | } |
| 132 | |
| 133 | static |
| 134 | void set_stream_file_name(struct fs_sink_stream *stream) |
| 135 | { |
| 136 | const char *base_name = bt_stream_get_name(stream->ir_stream); |
| 137 | |
| 138 | if (!base_name) { |
| 139 | base_name = "stream"; |
| 140 | } |
| 141 | |
| 142 | BT_ASSERT(!stream->file_name); |
| 143 | stream->file_name = make_unique_stream_file_name(stream->trace, |
| 144 | base_name); |
| 145 | } |
| 146 | |
| 147 | BT_HIDDEN |
| 148 | struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace, |
| 149 | const bt_stream *ir_stream) |
| 150 | { |
| 151 | struct fs_sink_stream *stream = g_new0(struct fs_sink_stream, 1); |
| 152 | int ret; |
| 153 | GString *path = g_string_new(trace->path->str); |
| 154 | |
| 155 | if (!stream) { |
| 156 | goto end; |
| 157 | } |
| 158 | |
| 159 | stream->log_level = trace->log_level; |
| 160 | stream->trace = trace; |
| 161 | stream->ir_stream = ir_stream; |
| 162 | stream->packet_state.beginning_cs = UINT64_C(-1); |
| 163 | stream->packet_state.end_cs = UINT64_C(-1); |
| 164 | stream->prev_packet_state.end_cs = UINT64_C(-1); |
| 165 | stream->prev_packet_state.discarded_events_counter = UINT64_C(-1); |
| 166 | stream->prev_packet_state.seq_num = UINT64_C(-1); |
| 167 | ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->fs_sink, |
| 168 | trace->trace, bt_stream_borrow_class_const(ir_stream), |
| 169 | &stream->sc); |
| 170 | if (ret) { |
| 171 | goto error; |
| 172 | } |
| 173 | |
| 174 | set_stream_file_name(stream); |
| 175 | g_string_append_printf(path, "/%s", stream->file_name->str); |
| 176 | ret = bt_ctfser_init(&stream->ctfser, path->str, |
| 177 | stream->log_level); |
| 178 | if (ret) { |
| 179 | goto error; |
| 180 | } |
| 181 | |
| 182 | g_hash_table_insert(trace->streams, (gpointer) ir_stream, stream); |
| 183 | goto end; |
| 184 | |
| 185 | error: |
| 186 | fs_sink_stream_destroy(stream); |
| 187 | stream = NULL; |
| 188 | |
| 189 | end: |
| 190 | if (path) { |
| 191 | g_string_free(path, TRUE); |
| 192 | } |
| 193 | |
| 194 | return stream; |
| 195 | } |
| 196 | |
| 197 | static |
| 198 | int write_field(struct fs_sink_stream *stream, |
| 199 | struct fs_sink_ctf_field_class *fc, const bt_field *field); |
| 200 | |
| 201 | static inline |
| 202 | int write_int_field(struct fs_sink_stream *stream, |
| 203 | struct fs_sink_ctf_field_class_int *fc, const bt_field *field) |
| 204 | { |
| 205 | int ret; |
| 206 | |
| 207 | if (fc->is_signed) { |
| 208 | ret = bt_ctfser_write_signed_int(&stream->ctfser, |
| 209 | bt_field_signed_integer_get_value(field), |
| 210 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
| 211 | } else { |
| 212 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, |
| 213 | bt_field_unsigned_integer_get_value(field), |
| 214 | fc->base.base.alignment, fc->base.size, BYTE_ORDER); |
| 215 | } |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | static inline |
| 221 | int write_float_field(struct fs_sink_stream *stream, |
| 222 | struct fs_sink_ctf_field_class_float *fc, const bt_field *field) |
| 223 | { |
| 224 | int ret; |
| 225 | double val = bt_field_real_get_value(field); |
| 226 | |
| 227 | if (fc->base.size == 32) { |
| 228 | ret = bt_ctfser_write_float32(&stream->ctfser, val, |
| 229 | fc->base.base.alignment, BYTE_ORDER); |
| 230 | } else { |
| 231 | ret = bt_ctfser_write_float64(&stream->ctfser, val, |
| 232 | fc->base.base.alignment, BYTE_ORDER); |
| 233 | } |
| 234 | |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static inline |
| 239 | int write_string_field(struct fs_sink_stream *stream, |
| 240 | struct fs_sink_ctf_field_class_string *fc, const bt_field *field) |
| 241 | { |
| 242 | return bt_ctfser_write_string(&stream->ctfser, |
| 243 | bt_field_string_get_value(field)); |
| 244 | } |
| 245 | |
| 246 | static inline |
| 247 | int write_array_field_elements(struct fs_sink_stream *stream, |
| 248 | struct fs_sink_ctf_field_class_array_base *fc, |
| 249 | const bt_field *field) |
| 250 | { |
| 251 | uint64_t i; |
| 252 | uint64_t len = bt_field_array_get_length(field); |
| 253 | int ret = 0; |
| 254 | |
| 255 | for (i = 0; i < len; i++) { |
| 256 | const bt_field *elem_field = |
| 257 | bt_field_array_borrow_element_field_by_index_const( |
| 258 | field, i); |
| 259 | ret = write_field(stream, fc->elem_fc, elem_field); |
| 260 | if (G_UNLIKELY(ret)) { |
| 261 | goto end; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | end: |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | static inline |
| 270 | int write_sequence_field(struct fs_sink_stream *stream, |
| 271 | struct fs_sink_ctf_field_class_sequence *fc, |
| 272 | const bt_field *field) |
| 273 | { |
| 274 | int ret; |
| 275 | |
| 276 | if (fc->length_is_before) { |
| 277 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, |
| 278 | bt_field_array_get_length(field), 8, 32, BYTE_ORDER); |
| 279 | if (G_UNLIKELY(ret)) { |
| 280 | goto end; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | ret = write_array_field_elements(stream, (void *) fc, field); |
| 285 | |
| 286 | end: |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static inline |
| 291 | int write_struct_field(struct fs_sink_stream *stream, |
| 292 | struct fs_sink_ctf_field_class_struct *fc, |
| 293 | const bt_field *field, bool align_struct) |
| 294 | { |
| 295 | int ret = 0; |
| 296 | uint64_t i; |
| 297 | |
| 298 | if (G_LIKELY(align_struct)) { |
| 299 | ret = bt_ctfser_align_offset_in_current_packet(&stream->ctfser, |
| 300 | fc->base.alignment); |
| 301 | if (G_UNLIKELY(ret)) { |
| 302 | goto end; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | for (i = 0; i < fc->members->len; i++) { |
| 307 | const bt_field *memb_field = |
| 308 | bt_field_structure_borrow_member_field_by_index_const( |
| 309 | field, i); |
| 310 | struct fs_sink_ctf_field_class *member_fc = |
| 311 | fs_sink_ctf_field_class_struct_borrow_member_by_index( |
| 312 | fc, i)->fc; |
| 313 | |
| 314 | ret = write_field(stream, member_fc, memb_field); |
| 315 | if (G_UNLIKELY(ret)) { |
| 316 | goto end; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | end: |
| 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static inline |
| 325 | int write_variant_field(struct fs_sink_stream *stream, |
| 326 | struct fs_sink_ctf_field_class_variant *fc, |
| 327 | const bt_field *field) |
| 328 | { |
| 329 | uint64_t opt_index = |
| 330 | bt_field_variant_get_selected_option_field_index(field); |
| 331 | int ret; |
| 332 | |
| 333 | if (fc->tag_is_before) { |
| 334 | ret = bt_ctfser_write_unsigned_int(&stream->ctfser, |
| 335 | opt_index, 8, 16, BYTE_ORDER); |
| 336 | if (G_UNLIKELY(ret)) { |
| 337 | goto end; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | ret = write_field(stream, |
| 342 | fs_sink_ctf_field_class_variant_borrow_option_by_index(fc, |
| 343 | opt_index)->fc, |
| 344 | bt_field_variant_borrow_selected_option_field_const(field)); |
| 345 | |
| 346 | end: |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static |
| 351 | int write_field(struct fs_sink_stream *stream, |
| 352 | struct fs_sink_ctf_field_class *fc, const bt_field *field) |
| 353 | { |
| 354 | int ret; |
| 355 | |
| 356 | switch (fc->type) { |
| 357 | case FS_SINK_CTF_FIELD_CLASS_TYPE_INT: |
| 358 | ret = write_int_field(stream, (void *) fc, field); |
| 359 | break; |
| 360 | case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT: |
| 361 | ret = write_float_field(stream, (void *) fc, field); |
| 362 | break; |
| 363 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING: |
| 364 | ret = write_string_field(stream, (void *) fc, field); |
| 365 | break; |
| 366 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: |
| 367 | ret = write_struct_field(stream, (void *) fc, field, true); |
| 368 | break; |
| 369 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: |
| 370 | ret = write_array_field_elements(stream, (void *) fc, field); |
| 371 | break; |
| 372 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: |
| 373 | ret = write_sequence_field(stream, (void *) fc, field); |
| 374 | break; |
| 375 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: |
| 376 | ret = write_variant_field(stream, (void *) fc, field); |
| 377 | break; |
| 378 | default: |
| 379 | abort(); |
| 380 | } |
| 381 | |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static inline |
| 386 | int write_event_header(struct fs_sink_stream *stream, |
| 387 | const bt_clock_snapshot *cs, struct fs_sink_ctf_event_class *ec) |
| 388 | { |
| 389 | int ret; |
| 390 | |
| 391 | /* Event class ID */ |
| 392 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 393 | bt_event_class_get_id(ec->ir_ec), 8, 64, BYTE_ORDER); |
| 394 | if (G_UNLIKELY(ret)) { |
| 395 | goto end; |
| 396 | } |
| 397 | |
| 398 | /* Time */ |
| 399 | if (stream->sc->default_clock_class) { |
| 400 | BT_ASSERT(cs); |
| 401 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 402 | bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER); |
| 403 | if (G_UNLIKELY(ret)) { |
| 404 | goto end; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | end: |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | BT_HIDDEN |
| 413 | int fs_sink_stream_write_event(struct fs_sink_stream *stream, |
| 414 | const bt_clock_snapshot *cs, const bt_event *event, |
| 415 | struct fs_sink_ctf_event_class *ec) |
| 416 | { |
| 417 | int ret; |
| 418 | const bt_field *field; |
| 419 | |
| 420 | /* Header */ |
| 421 | ret = write_event_header(stream, cs, ec); |
| 422 | if (G_UNLIKELY(ret)) { |
| 423 | goto end; |
| 424 | } |
| 425 | |
| 426 | /* Common context */ |
| 427 | if (stream->sc->event_common_context_fc) { |
| 428 | field = bt_event_borrow_common_context_field_const(event); |
| 429 | BT_ASSERT(field); |
| 430 | ret = write_struct_field(stream, |
| 431 | (void *) stream->sc->event_common_context_fc, |
| 432 | field, true); |
| 433 | if (G_UNLIKELY(ret)) { |
| 434 | goto end; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | /* Specific context */ |
| 439 | if (ec->spec_context_fc) { |
| 440 | field = bt_event_borrow_specific_context_field_const(event); |
| 441 | BT_ASSERT(field); |
| 442 | ret = write_struct_field(stream, (void *) ec->spec_context_fc, |
| 443 | field, true); |
| 444 | if (G_UNLIKELY(ret)) { |
| 445 | goto end; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* Specific context */ |
| 450 | if (ec->payload_fc) { |
| 451 | field = bt_event_borrow_payload_field_const(event); |
| 452 | BT_ASSERT(field); |
| 453 | ret = write_struct_field(stream, (void *) ec->payload_fc, |
| 454 | field, true); |
| 455 | if (G_UNLIKELY(ret)) { |
| 456 | goto end; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | end: |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static |
| 465 | int write_packet_context(struct fs_sink_stream *stream) |
| 466 | { |
| 467 | int ret; |
| 468 | |
| 469 | /* Packet total size */ |
| 470 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 471 | stream->packet_state.total_size, 8, 64, BYTE_ORDER); |
| 472 | if (ret) { |
| 473 | goto end; |
| 474 | } |
| 475 | |
| 476 | /* Packet content size */ |
| 477 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 478 | stream->packet_state.content_size, 8, 64, BYTE_ORDER); |
| 479 | if (ret) { |
| 480 | goto end; |
| 481 | } |
| 482 | |
| 483 | if (stream->sc->packets_have_ts_begin) { |
| 484 | /* Beginning time */ |
| 485 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 486 | stream->packet_state.beginning_cs, 8, 64, BYTE_ORDER); |
| 487 | if (ret) { |
| 488 | goto end; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (stream->sc->packets_have_ts_end) { |
| 493 | /* End time */ |
| 494 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 495 | stream->packet_state.end_cs, 8, 64, BYTE_ORDER); |
| 496 | if (ret) { |
| 497 | goto end; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (stream->sc->has_discarded_events) { |
| 502 | /* Discarded event counter */ |
| 503 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 504 | stream->packet_state.discarded_events_counter, 8, 64, |
| 505 | BYTE_ORDER); |
| 506 | if (ret) { |
| 507 | goto end; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* Sequence number */ |
| 512 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 513 | stream->packet_state.seq_num, 8, 64, BYTE_ORDER); |
| 514 | if (ret) { |
| 515 | goto end; |
| 516 | } |
| 517 | |
| 518 | /* Other members */ |
| 519 | if (stream->sc->packet_context_fc) { |
| 520 | const bt_field *packet_context_field = |
| 521 | bt_packet_borrow_context_field_const( |
| 522 | stream->packet_state.packet); |
| 523 | |
| 524 | BT_ASSERT(packet_context_field); |
| 525 | ret = write_struct_field(stream, |
| 526 | (void *) stream->sc->packet_context_fc, |
| 527 | packet_context_field, false); |
| 528 | if (ret) { |
| 529 | goto end; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | end: |
| 534 | return ret; |
| 535 | } |
| 536 | |
| 537 | BT_HIDDEN |
| 538 | int fs_sink_stream_open_packet(struct fs_sink_stream *stream, |
| 539 | const bt_clock_snapshot *cs, const bt_packet *packet) |
| 540 | { |
| 541 | int ret; |
| 542 | uint64_t i; |
| 543 | |
| 544 | BT_ASSERT(!stream->packet_state.is_open); |
| 545 | bt_packet_put_ref(stream->packet_state.packet); |
| 546 | stream->packet_state.packet = packet; |
| 547 | bt_packet_get_ref(stream->packet_state.packet); |
| 548 | if (cs) { |
| 549 | stream->packet_state.beginning_cs = |
| 550 | bt_clock_snapshot_get_value(cs); |
| 551 | } |
| 552 | |
| 553 | /* Open packet */ |
| 554 | ret = bt_ctfser_open_packet(&stream->ctfser); |
| 555 | if (ret) { |
| 556 | /* bt_ctfser_open_packet() logs errors */ |
| 557 | goto end; |
| 558 | } |
| 559 | |
| 560 | /* Packet header: magic */ |
| 561 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 562 | UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER); |
| 563 | if (ret) { |
| 564 | BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s", |
| 565 | stream->file_name->str); |
| 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | /* Packet header: UUID */ |
| 570 | for (i = 0; i < BT_UUID_LEN; i++) { |
| 571 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 572 | (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER); |
| 573 | if (ret) { |
| 574 | BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s", |
| 575 | stream->file_name->str); |
| 576 | goto end; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /* Packet header: stream class ID */ |
| 581 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 582 | bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER); |
| 583 | if (ret) { |
| 584 | BT_COMP_LOGE("Error writing packet header stream class id: " |
| 585 | "stream-file-name=%s, stream-class-id=%"PRIu64, |
| 586 | stream->file_name->str, |
| 587 | bt_stream_class_get_id(stream->sc->ir_sc)); |
| 588 | goto end; |
| 589 | } |
| 590 | |
| 591 | /* Packet header: stream ID */ |
| 592 | ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser, |
| 593 | bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER); |
| 594 | if (ret) { |
| 595 | BT_COMP_LOGE("Error writing packet header stream id: " |
| 596 | "stream-file-name=%s, stream-id=%"PRIu64, |
| 597 | stream->file_name->str, |
| 598 | bt_stream_get_id(stream->ir_stream)); |
| 599 | goto end; |
| 600 | } |
| 601 | |
| 602 | /* Save packet context's offset to rewrite it later */ |
| 603 | stream->packet_state.context_offset_bits = |
| 604 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); |
| 605 | |
| 606 | /* Write packet context just to advance to content (first event) */ |
| 607 | ret = write_packet_context(stream); |
| 608 | if (ret) { |
| 609 | goto end; |
| 610 | } |
| 611 | |
| 612 | stream->packet_state.is_open = true; |
| 613 | |
| 614 | end: |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | BT_HIDDEN |
| 619 | int fs_sink_stream_close_packet(struct fs_sink_stream *stream, |
| 620 | const bt_clock_snapshot *cs) |
| 621 | { |
| 622 | int ret; |
| 623 | |
| 624 | BT_ASSERT(stream->packet_state.is_open); |
| 625 | |
| 626 | if (cs) { |
| 627 | stream->packet_state.end_cs = bt_clock_snapshot_get_value(cs); |
| 628 | } |
| 629 | |
| 630 | stream->packet_state.content_size = |
| 631 | bt_ctfser_get_offset_in_current_packet_bits(&stream->ctfser); |
| 632 | stream->packet_state.total_size = |
| 633 | (stream->packet_state.content_size + 7) & ~UINT64_C(7); |
| 634 | |
| 635 | /* Rewrite packet context */ |
| 636 | bt_ctfser_set_offset_in_current_packet_bits(&stream->ctfser, |
| 637 | stream->packet_state.context_offset_bits); |
| 638 | ret = write_packet_context(stream); |
| 639 | if (ret) { |
| 640 | goto end; |
| 641 | } |
| 642 | |
| 643 | /* Close packet */ |
| 644 | bt_ctfser_close_current_packet(&stream->ctfser, |
| 645 | stream->packet_state.total_size / 8); |
| 646 | |
| 647 | /* Partially copy current packet state to previous packet state */ |
| 648 | stream->prev_packet_state.end_cs = stream->packet_state.end_cs; |
| 649 | stream->prev_packet_state.discarded_events_counter = |
| 650 | stream->packet_state.discarded_events_counter; |
| 651 | stream->prev_packet_state.seq_num = |
| 652 | stream->packet_state.seq_num; |
| 653 | |
| 654 | /* Reset current packet state */ |
| 655 | stream->packet_state.beginning_cs = UINT64_C(-1); |
| 656 | stream->packet_state.end_cs = UINT64_C(-1); |
| 657 | stream->packet_state.content_size = 0; |
| 658 | stream->packet_state.total_size = 0; |
| 659 | stream->packet_state.seq_num += 1; |
| 660 | stream->packet_state.context_offset_bits = 0; |
| 661 | stream->packet_state.is_open = false; |
| 662 | BT_PACKET_PUT_REF_AND_RESET(stream->packet_state.packet); |
| 663 | |
| 664 | end: |
| 665 | return ret; |
| 666 | } |