| 1 | /* |
| 2 | * stream-class.c |
| 3 | * |
| 4 | * Babeltrace CTF IR - Stream Class |
| 5 | * |
| 6 | * Copyright 2013, 2014 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-writer/clock.h> |
| 30 | #include <babeltrace/ctf-ir/clock-internal.h> |
| 31 | #include <babeltrace/ctf-writer/event.h> |
| 32 | #include <babeltrace/ctf-ir/event-internal.h> |
| 33 | #include <babeltrace/ctf-ir/event-types-internal.h> |
| 34 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
| 35 | #include <babeltrace/ctf-writer/stream.h> |
| 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
| 37 | #include <babeltrace/ctf-writer/functor-internal.h> |
| 38 | #include <babeltrace/ctf-ir/utils.h> |
| 39 | #include <babeltrace/compiler.h> |
| 40 | #include <babeltrace/align.h> |
| 41 | |
| 42 | static |
| 43 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref); |
| 44 | static |
| 45 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
| 46 | static |
| 47 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
| 48 | |
| 49 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) |
| 50 | { |
| 51 | int ret; |
| 52 | struct bt_ctf_stream_class *stream_class = NULL; |
| 53 | |
| 54 | if (name && bt_ctf_validate_identifier(name)) { |
| 55 | goto error; |
| 56 | } |
| 57 | |
| 58 | stream_class = g_new0(struct bt_ctf_stream_class, 1); |
| 59 | if (!stream_class) { |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | stream_class->name = g_string_new(name); |
| 64 | stream_class->event_classes = g_ptr_array_new_with_free_func( |
| 65 | (GDestroyNotify)bt_ctf_event_class_put); |
| 66 | if (!stream_class->event_classes) { |
| 67 | goto error_destroy; |
| 68 | } |
| 69 | |
| 70 | ret = init_event_header(stream_class); |
| 71 | if (ret) { |
| 72 | goto error_destroy; |
| 73 | } |
| 74 | |
| 75 | ret = init_packet_context(stream_class); |
| 76 | if (ret) { |
| 77 | goto error_destroy; |
| 78 | } |
| 79 | |
| 80 | bt_ctf_ref_init(&stream_class->ref_count); |
| 81 | return stream_class; |
| 82 | |
| 83 | error_destroy: |
| 84 | bt_ctf_stream_class_destroy(&stream_class->ref_count); |
| 85 | stream_class = NULL; |
| 86 | error: |
| 87 | return stream_class; |
| 88 | } |
| 89 | |
| 90 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
| 91 | struct bt_ctf_stream_class *stream_class) |
| 92 | { |
| 93 | struct bt_ctf_trace *trace = NULL; |
| 94 | |
| 95 | if (!stream_class) { |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | trace = stream_class->trace; |
| 100 | if (trace) { |
| 101 | bt_ctf_trace_get(trace); |
| 102 | } |
| 103 | end: |
| 104 | return trace; |
| 105 | } |
| 106 | |
| 107 | const char *bt_ctf_stream_class_get_name( |
| 108 | struct bt_ctf_stream_class *stream_class) |
| 109 | { |
| 110 | const char *name = NULL; |
| 111 | |
| 112 | if (!stream_class) { |
| 113 | goto end; |
| 114 | } |
| 115 | |
| 116 | name = stream_class->name->str; |
| 117 | end: |
| 118 | return name; |
| 119 | } |
| 120 | |
| 121 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
| 122 | const char *name) |
| 123 | { |
| 124 | int ret = 0; |
| 125 | |
| 126 | if (!stream_class || stream_class->frozen) { |
| 127 | ret = -1; |
| 128 | goto end; |
| 129 | } |
| 130 | |
| 131 | g_string_assign(stream_class->name, name); |
| 132 | end: |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
| 137 | struct bt_ctf_stream_class *stream_class) |
| 138 | { |
| 139 | struct bt_ctf_clock *clock = NULL; |
| 140 | |
| 141 | if (!stream_class || !stream_class->clock) { |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | clock = stream_class->clock; |
| 146 | bt_ctf_clock_get(clock); |
| 147 | end: |
| 148 | return clock; |
| 149 | } |
| 150 | |
| 151 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
| 152 | struct bt_ctf_clock *clock) |
| 153 | { |
| 154 | int ret = 0; |
| 155 | struct bt_ctf_field_type *timestamp_field = NULL; |
| 156 | |
| 157 | if (!stream_class || !clock || stream_class->frozen) { |
| 158 | ret = -1; |
| 159 | goto end; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Look for a "timestamp" field in the stream class' event header type |
| 164 | * and map the stream's clock to that field if no current mapping is |
| 165 | * currently set. |
| 166 | */ |
| 167 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( |
| 168 | stream_class->event_header_type, "timestamp"); |
| 169 | if (timestamp_field) { |
| 170 | struct bt_ctf_clock *mapped_clock; |
| 171 | |
| 172 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( |
| 173 | timestamp_field); |
| 174 | if (mapped_clock) { |
| 175 | bt_ctf_clock_put(mapped_clock); |
| 176 | goto end; |
| 177 | } |
| 178 | |
| 179 | ret = bt_ctf_field_type_integer_set_mapped_clock( |
| 180 | timestamp_field, clock); |
| 181 | if (ret) { |
| 182 | goto end; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (stream_class->clock) { |
| 187 | bt_ctf_clock_put(stream_class->clock); |
| 188 | } |
| 189 | |
| 190 | stream_class->clock = clock; |
| 191 | bt_ctf_clock_get(clock); |
| 192 | end: |
| 193 | if (timestamp_field) { |
| 194 | bt_ctf_field_type_put(timestamp_field); |
| 195 | } |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
| 200 | { |
| 201 | int64_t ret; |
| 202 | |
| 203 | if (!stream_class || !stream_class->id_set) { |
| 204 | ret = -1; |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | ret = (int64_t) stream_class->id; |
| 209 | end: |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | BT_HIDDEN |
| 214 | int _bt_ctf_stream_class_set_id( |
| 215 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 216 | { |
| 217 | stream_class->id = id; |
| 218 | stream_class->id_set = 1; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
| 223 | uint32_t id) |
| 224 | { |
| 225 | int ret = 0; |
| 226 | |
| 227 | if (!stream_class || stream_class->frozen) { |
| 228 | ret = -1; |
| 229 | goto end; |
| 230 | } |
| 231 | |
| 232 | ret = _bt_ctf_stream_class_set_id(stream_class, id); |
| 233 | if (ret) { |
| 234 | goto end; |
| 235 | } |
| 236 | end: |
| 237 | return ret; |
| 238 | } |
| 239 | |
| 240 | static |
| 241 | void event_class_exists(gpointer element, gpointer query) |
| 242 | { |
| 243 | struct bt_ctf_event_class *event_class_a = element; |
| 244 | struct search_query *search_query = query; |
| 245 | struct bt_ctf_event_class *event_class_b = search_query->value; |
| 246 | int64_t id_a, id_b; |
| 247 | |
| 248 | if (search_query->value == element) { |
| 249 | search_query->found = 1; |
| 250 | goto end; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Two event classes cannot share the same name in a given |
| 255 | * stream class. |
| 256 | */ |
| 257 | if (!strcmp(bt_ctf_event_class_get_name(event_class_a), |
| 258 | bt_ctf_event_class_get_name(event_class_b))) { |
| 259 | search_query->found = 1; |
| 260 | goto end; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Two event classes cannot share the same ID in a given |
| 265 | * stream class. |
| 266 | */ |
| 267 | id_a = bt_ctf_event_class_get_id(event_class_a); |
| 268 | id_b = bt_ctf_event_class_get_id(event_class_b); |
| 269 | |
| 270 | if (id_a < 0 || id_b < 0) { |
| 271 | /* at least one ID is not set: will be automatically set later */ |
| 272 | goto end; |
| 273 | } |
| 274 | |
| 275 | if (id_a == id_b) { |
| 276 | search_query->found = 1; |
| 277 | goto end; |
| 278 | } |
| 279 | |
| 280 | end: |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | int bt_ctf_stream_class_add_event_class( |
| 285 | struct bt_ctf_stream_class *stream_class, |
| 286 | struct bt_ctf_event_class *event_class) |
| 287 | { |
| 288 | int ret = 0; |
| 289 | int64_t event_id; |
| 290 | |
| 291 | if (!stream_class || !event_class) { |
| 292 | ret = -1; |
| 293 | goto end; |
| 294 | } |
| 295 | |
| 296 | /* Check for duplicate event classes */ |
| 297 | struct search_query query = { .value = event_class, .found = 0 }; |
| 298 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
| 299 | &query); |
| 300 | if (query.found) { |
| 301 | ret = -1; |
| 302 | goto end; |
| 303 | } |
| 304 | |
| 305 | /* Only set an event id if none was explicitly set before */ |
| 306 | event_id = bt_ctf_event_class_get_id(event_class); |
| 307 | if (event_id < 0) { |
| 308 | if (bt_ctf_event_class_set_id(event_class, |
| 309 | stream_class->next_event_id++)) { |
| 310 | ret = -1; |
| 311 | goto end; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | ret = bt_ctf_event_class_set_stream_class(event_class, stream_class); |
| 316 | if (ret) { |
| 317 | goto end; |
| 318 | } |
| 319 | |
| 320 | bt_ctf_event_class_get(event_class); |
| 321 | g_ptr_array_add(stream_class->event_classes, event_class); |
| 322 | bt_ctf_event_class_freeze(event_class); |
| 323 | |
| 324 | if (stream_class->byte_order) { |
| 325 | /* |
| 326 | * Only set native byte order if it has been initialized |
| 327 | * when the stream class was added to a trace. |
| 328 | * |
| 329 | * If not set here, this will be set when the stream |
| 330 | * classe will be added to a trace. |
| 331 | */ |
| 332 | bt_ctf_event_class_set_native_byte_order(event_class, |
| 333 | stream_class->byte_order); |
| 334 | } |
| 335 | end: |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | int bt_ctf_stream_class_get_event_class_count( |
| 340 | struct bt_ctf_stream_class *stream_class) |
| 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | if (!stream_class) { |
| 345 | ret = -1; |
| 346 | goto end; |
| 347 | } |
| 348 | |
| 349 | ret = (int) stream_class->event_classes->len; |
| 350 | end: |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( |
| 355 | struct bt_ctf_stream_class *stream_class, int index) |
| 356 | { |
| 357 | struct bt_ctf_event_class *event_class = NULL; |
| 358 | |
| 359 | if (!stream_class || index < 0 || |
| 360 | index >= stream_class->event_classes->len) { |
| 361 | goto end; |
| 362 | } |
| 363 | |
| 364 | event_class = g_ptr_array_index(stream_class->event_classes, index); |
| 365 | bt_ctf_event_class_get(event_class); |
| 366 | end: |
| 367 | return event_class; |
| 368 | } |
| 369 | |
| 370 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( |
| 371 | struct bt_ctf_stream_class *stream_class, const char *name) |
| 372 | { |
| 373 | size_t i; |
| 374 | struct bt_ctf_event_class *event_class = NULL; |
| 375 | |
| 376 | if (!stream_class || !name) { |
| 377 | goto end; |
| 378 | } |
| 379 | |
| 380 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 381 | struct bt_ctf_event_class *cur_event_class = |
| 382 | g_ptr_array_index(stream_class->event_classes, i); |
| 383 | const char *cur_event_class_name = |
| 384 | bt_ctf_event_class_get_name(cur_event_class); |
| 385 | |
| 386 | if (!strcmp(name, cur_event_class_name)) { |
| 387 | event_class = cur_event_class; |
| 388 | bt_ctf_event_class_get(event_class); |
| 389 | goto end; |
| 390 | } |
| 391 | } |
| 392 | end: |
| 393 | return event_class; |
| 394 | } |
| 395 | |
| 396 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
| 397 | struct bt_ctf_stream_class *stream_class, uint32_t id) |
| 398 | { |
| 399 | size_t i; |
| 400 | struct bt_ctf_event_class *event_class = NULL; |
| 401 | |
| 402 | if (!stream_class) { |
| 403 | goto end; |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 407 | struct bt_ctf_event_class *current_event_class = |
| 408 | g_ptr_array_index(stream_class->event_classes, i); |
| 409 | |
| 410 | if (bt_ctf_event_class_get_id(current_event_class) == id) { |
| 411 | event_class = current_event_class; |
| 412 | bt_ctf_event_class_get(event_class); |
| 413 | goto end; |
| 414 | } |
| 415 | } |
| 416 | end: |
| 417 | return event_class; |
| 418 | } |
| 419 | |
| 420 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
| 421 | struct bt_ctf_stream_class *stream_class) |
| 422 | { |
| 423 | struct bt_ctf_field_type *ret = NULL; |
| 424 | |
| 425 | if (!stream_class) { |
| 426 | goto end; |
| 427 | } |
| 428 | |
| 429 | assert(stream_class->packet_context_type); |
| 430 | bt_ctf_field_type_get(stream_class->packet_context_type); |
| 431 | ret = stream_class->packet_context_type; |
| 432 | end: |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | int bt_ctf_stream_class_set_packet_context_type( |
| 437 | struct bt_ctf_stream_class *stream_class, |
| 438 | struct bt_ctf_field_type *packet_context_type) |
| 439 | { |
| 440 | int ret = 0; |
| 441 | |
| 442 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
| 443 | ret = -1; |
| 444 | goto end; |
| 445 | } |
| 446 | |
| 447 | assert(stream_class->packet_context_type); |
| 448 | if (stream_class->packet_context_type == packet_context_type) { |
| 449 | goto end; |
| 450 | } |
| 451 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
| 452 | CTF_TYPE_STRUCT) { |
| 453 | /* A packet context must be a structure */ |
| 454 | ret = -1; |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 459 | bt_ctf_field_type_get(packet_context_type); |
| 460 | stream_class->packet_context_type = packet_context_type; |
| 461 | end: |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
| 466 | struct bt_ctf_stream_class *stream_class) |
| 467 | { |
| 468 | struct bt_ctf_field_type *ret = NULL; |
| 469 | |
| 470 | if (!stream_class || !stream_class->event_header_type) { |
| 471 | goto end; |
| 472 | } |
| 473 | |
| 474 | assert(stream_class->event_header_type); |
| 475 | bt_ctf_field_type_get(stream_class->event_header_type); |
| 476 | ret = stream_class->event_header_type; |
| 477 | end: |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | int bt_ctf_stream_class_set_event_header_type( |
| 482 | struct bt_ctf_stream_class *stream_class, |
| 483 | struct bt_ctf_field_type *event_header_type) |
| 484 | { |
| 485 | int ret = 0; |
| 486 | |
| 487 | if (!stream_class || !event_header_type || stream_class->frozen) { |
| 488 | ret = -1; |
| 489 | goto end; |
| 490 | } |
| 491 | |
| 492 | assert(stream_class->event_header_type); |
| 493 | if (stream_class->event_header_type == event_header_type) { |
| 494 | goto end; |
| 495 | } |
| 496 | if (bt_ctf_field_type_get_type_id(event_header_type) != |
| 497 | CTF_TYPE_STRUCT) { |
| 498 | /* An event header must be a structure */ |
| 499 | ret = -1; |
| 500 | goto end; |
| 501 | } |
| 502 | |
| 503 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 504 | bt_ctf_field_type_get(event_header_type); |
| 505 | stream_class->event_header_type = event_header_type; |
| 506 | end: |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
| 511 | struct bt_ctf_stream_class *stream_class) |
| 512 | { |
| 513 | struct bt_ctf_field_type *ret = NULL; |
| 514 | |
| 515 | if (!stream_class || !stream_class->event_context_type) { |
| 516 | goto end; |
| 517 | } |
| 518 | |
| 519 | assert(stream_class->event_context_type); |
| 520 | bt_ctf_field_type_get(stream_class->event_context_type); |
| 521 | ret = stream_class->event_context_type; |
| 522 | end: |
| 523 | return ret; |
| 524 | } |
| 525 | |
| 526 | int bt_ctf_stream_class_set_event_context_type( |
| 527 | struct bt_ctf_stream_class *stream_class, |
| 528 | struct bt_ctf_field_type *event_context_type) |
| 529 | { |
| 530 | int ret = 0; |
| 531 | |
| 532 | if (!stream_class || !event_context_type || stream_class->frozen) { |
| 533 | ret = -1; |
| 534 | goto end; |
| 535 | } |
| 536 | |
| 537 | if (bt_ctf_field_type_get_type_id(event_context_type) != |
| 538 | CTF_TYPE_STRUCT) { |
| 539 | /* A packet context must be a structure */ |
| 540 | ret = -1; |
| 541 | goto end; |
| 542 | } |
| 543 | |
| 544 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 545 | bt_ctf_field_type_get(event_context_type); |
| 546 | stream_class->event_context_type = event_context_type; |
| 547 | end: |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
| 552 | { |
| 553 | if (!stream_class) { |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | bt_ctf_ref_get(&stream_class->ref_count); |
| 558 | } |
| 559 | |
| 560 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
| 561 | { |
| 562 | if (!stream_class) { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy); |
| 567 | } |
| 568 | |
| 569 | BT_HIDDEN |
| 570 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) |
| 571 | { |
| 572 | if (!stream_class) { |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | stream_class->frozen = 1; |
| 577 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
| 578 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
| 579 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
| 580 | bt_ctf_clock_freeze(stream_class->clock); |
| 581 | } |
| 582 | |
| 583 | BT_HIDDEN |
| 584 | int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class, |
| 585 | enum bt_ctf_byte_order byte_order) |
| 586 | { |
| 587 | int i, ret = 0; |
| 588 | int internal_byte_order; |
| 589 | |
| 590 | /* Note that "NATIVE" means the trace's endianness, not the host's. */ |
| 591 | if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN || |
| 592 | byte_order > BT_CTF_BYTE_ORDER_NETWORK) { |
| 593 | ret = -1; |
| 594 | goto end; |
| 595 | } |
| 596 | |
| 597 | switch (byte_order) { |
| 598 | case BT_CTF_BYTE_ORDER_NETWORK: |
| 599 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
| 600 | internal_byte_order = BIG_ENDIAN; |
| 601 | break; |
| 602 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
| 603 | internal_byte_order = LITTLE_ENDIAN; |
| 604 | break; |
| 605 | default: |
| 606 | ret = -1; |
| 607 | goto end; |
| 608 | } |
| 609 | |
| 610 | stream_class->byte_order = internal_byte_order; |
| 611 | |
| 612 | /* Set native byte order to little or big endian */ |
| 613 | bt_ctf_field_type_set_native_byte_order( |
| 614 | stream_class->event_header_type, stream_class->byte_order); |
| 615 | bt_ctf_field_type_set_native_byte_order( |
| 616 | stream_class->packet_context_type, stream_class->byte_order); |
| 617 | bt_ctf_field_type_set_native_byte_order( |
| 618 | stream_class->event_context_type, stream_class->byte_order); |
| 619 | |
| 620 | /* Set all events' native byte order */ |
| 621 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 622 | bt_ctf_event_class_set_native_byte_order( |
| 623 | g_ptr_array_index(stream_class->event_classes, i), |
| 624 | stream_class->byte_order); |
| 625 | bt_ctf_event_class_freeze( |
| 626 | g_ptr_array_index(stream_class->event_classes, i)); |
| 627 | } |
| 628 | end: |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | BT_HIDDEN |
| 633 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, |
| 634 | struct metadata_context *context) |
| 635 | { |
| 636 | int64_t ret = 0; |
| 637 | size_t i; |
| 638 | |
| 639 | g_string_assign(context->field_name, ""); |
| 640 | context->current_indentation_level = 1; |
| 641 | if (!stream_class->id_set) { |
| 642 | ret = -1; |
| 643 | goto end; |
| 644 | } |
| 645 | |
| 646 | g_string_append_printf(context->string, |
| 647 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", |
| 648 | stream_class->id); |
| 649 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, |
| 650 | context); |
| 651 | if (ret) { |
| 652 | goto end; |
| 653 | } |
| 654 | |
| 655 | g_string_append(context->string, ";\n\n\tpacket.context := "); |
| 656 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
| 657 | context); |
| 658 | if (ret) { |
| 659 | goto end; |
| 660 | } |
| 661 | |
| 662 | if (stream_class->event_context_type) { |
| 663 | g_string_append(context->string, ";\n\n\tevent.context := "); |
| 664 | ret = bt_ctf_field_type_serialize( |
| 665 | stream_class->event_context_type, context); |
| 666 | if (ret) { |
| 667 | goto end; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | g_string_append(context->string, ";\n};\n\n"); |
| 672 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 673 | struct bt_ctf_event_class *event_class = |
| 674 | stream_class->event_classes->pdata[i]; |
| 675 | |
| 676 | ret = bt_ctf_event_class_serialize(event_class, context); |
| 677 | if (ret) { |
| 678 | goto end; |
| 679 | } |
| 680 | } |
| 681 | end: |
| 682 | context->current_indentation_level = 0; |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | BT_HIDDEN |
| 687 | int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class, |
| 688 | struct bt_ctf_trace *trace) |
| 689 | { |
| 690 | int ret = 0; |
| 691 | |
| 692 | if (!stream_class) { |
| 693 | ret = -1; |
| 694 | goto end; |
| 695 | } |
| 696 | |
| 697 | if (stream_class->trace && trace) { |
| 698 | /* Already attached to a trace */ |
| 699 | ret = -1; |
| 700 | goto end; |
| 701 | } |
| 702 | |
| 703 | stream_class->trace = trace; |
| 704 | end: |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | static |
| 709 | void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref) |
| 710 | { |
| 711 | struct bt_ctf_stream_class *stream_class; |
| 712 | |
| 713 | if (!ref) { |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count); |
| 718 | bt_ctf_clock_put(stream_class->clock); |
| 719 | |
| 720 | if (stream_class->event_classes) { |
| 721 | size_t i; |
| 722 | |
| 723 | /* Unregister this stream class from the event classes */ |
| 724 | for (i = 0; i < stream_class->event_classes->len; i++) { |
| 725 | struct bt_ctf_event_class *event_class = |
| 726 | g_ptr_array_index(stream_class->event_classes, |
| 727 | i); |
| 728 | |
| 729 | bt_ctf_event_class_set_stream_class(event_class, NULL); |
| 730 | } |
| 731 | |
| 732 | g_ptr_array_free(stream_class->event_classes, TRUE); |
| 733 | } |
| 734 | |
| 735 | if (stream_class->name) { |
| 736 | g_string_free(stream_class->name, TRUE); |
| 737 | } |
| 738 | |
| 739 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 740 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 741 | if (stream_class->event_context_type) { |
| 742 | bt_ctf_field_type_put(stream_class->event_context_type); |
| 743 | } |
| 744 | g_free(stream_class); |
| 745 | } |
| 746 | |
| 747 | static |
| 748 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
| 749 | { |
| 750 | int ret = 0; |
| 751 | struct bt_ctf_field_type *event_header_type = |
| 752 | bt_ctf_field_type_structure_create(); |
| 753 | struct bt_ctf_field_type *_uint32_t = |
| 754 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); |
| 755 | struct bt_ctf_field_type *_uint64_t = |
| 756 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 757 | |
| 758 | if (!event_header_type) { |
| 759 | ret = -1; |
| 760 | goto end; |
| 761 | } |
| 762 | |
| 763 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 764 | _uint32_t, "id"); |
| 765 | if (ret) { |
| 766 | goto end; |
| 767 | } |
| 768 | |
| 769 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 770 | _uint64_t, "timestamp"); |
| 771 | if (ret) { |
| 772 | goto end; |
| 773 | } |
| 774 | |
| 775 | if (stream_class->event_header_type) { |
| 776 | bt_ctf_field_type_put(stream_class->event_header_type); |
| 777 | } |
| 778 | stream_class->event_header_type = event_header_type; |
| 779 | end: |
| 780 | if (ret) { |
| 781 | bt_ctf_field_type_put(event_header_type); |
| 782 | } |
| 783 | |
| 784 | bt_ctf_field_type_put(_uint32_t); |
| 785 | bt_ctf_field_type_put(_uint64_t); |
| 786 | return ret; |
| 787 | } |
| 788 | |
| 789 | static |
| 790 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
| 791 | { |
| 792 | int ret = 0; |
| 793 | struct bt_ctf_field_type *packet_context_type = |
| 794 | bt_ctf_field_type_structure_create(); |
| 795 | struct bt_ctf_field_type *_uint64_t = |
| 796 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); |
| 797 | |
| 798 | if (!packet_context_type) { |
| 799 | ret = -1; |
| 800 | goto end; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * We create a stream packet context as proposed in the CTF |
| 805 | * specification. |
| 806 | */ |
| 807 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 808 | _uint64_t, "timestamp_begin"); |
| 809 | if (ret) { |
| 810 | goto end; |
| 811 | } |
| 812 | |
| 813 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 814 | _uint64_t, "timestamp_end"); |
| 815 | if (ret) { |
| 816 | goto end; |
| 817 | } |
| 818 | |
| 819 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 820 | _uint64_t, "content_size"); |
| 821 | if (ret) { |
| 822 | goto end; |
| 823 | } |
| 824 | |
| 825 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 826 | _uint64_t, "packet_size"); |
| 827 | if (ret) { |
| 828 | goto end; |
| 829 | } |
| 830 | |
| 831 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 832 | _uint64_t, "events_discarded"); |
| 833 | if (ret) { |
| 834 | goto end; |
| 835 | } |
| 836 | |
| 837 | if (stream_class->packet_context_type) { |
| 838 | bt_ctf_field_type_put(stream_class->packet_context_type); |
| 839 | } |
| 840 | stream_class->packet_context_type = packet_context_type; |
| 841 | end: |
| 842 | if (ret) { |
| 843 | bt_ctf_field_type_put(packet_context_type); |
| 844 | goto end; |
| 845 | } |
| 846 | |
| 847 | bt_ctf_field_type_put(_uint64_t); |
| 848 | return ret; |
| 849 | } |