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