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