4 * Babeltrace CTF IR - Stream Class
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
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>
51 void bt_ctf_stream_class_destroy(struct bt_object
*obj
);
53 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
55 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
57 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
60 struct bt_ctf_stream_class
*stream_class
= NULL
;
62 if (name
&& bt_ctf_validate_identifier(name
)) {
66 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
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
) {
78 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
79 g_int64_equal
, g_free
, NULL
);
81 ret
= init_event_header(stream_class
);
86 ret
= init_packet_context(stream_class
);
91 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
99 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
100 struct bt_ctf_stream_class
*stream_class
)
102 return stream_class
?
103 bt_get(bt_ctf_stream_class_borrow_trace(stream_class
)) :
107 const char *bt_ctf_stream_class_get_name(
108 struct bt_ctf_stream_class
*stream_class
)
110 const char *name
= NULL
;
116 name
= stream_class
->name
->str
;
121 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
126 if (!stream_class
|| stream_class
->frozen
) {
131 g_string_assign(stream_class
->name
, name
);
136 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
137 struct bt_ctf_stream_class
*stream_class
)
139 struct bt_ctf_clock
*clock
= NULL
;
141 if (!stream_class
|| !stream_class
->clock
) {
145 clock
= bt_get(stream_class
->clock
);
150 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
151 struct bt_ctf_clock
*clock
)
154 struct bt_ctf_field_type
*timestamp_field
= NULL
;
156 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
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
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(
174 if (!mapped_clock_class
) {
175 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(
176 timestamp_field
, clock
->clock_class
);
182 BT_PUT(mapped_clock_class
);
185 /* Replace the current clock of this stream class. */
186 bt_put(stream_class
->clock
);
187 stream_class
->clock
= bt_get(clock
);
190 bt_put(timestamp_field
);
194 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
198 if (!stream_class
|| !stream_class
->id_set
) {
203 ret
= (int64_t) stream_class
->id
;
209 int _bt_ctf_stream_class_set_id(
210 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
212 stream_class
->id
= id
;
213 stream_class
->id_set
= 1;
217 struct event_class_set_stream_id_data
{
223 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
225 struct event_class_set_stream_id_data
*typed_data
= data
;
227 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
228 typed_data
->stream_id
);
232 int bt_ctf_stream_class_set_id_no_check(
233 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
236 struct event_class_set_stream_id_data data
=
237 { .stream_id
= id
, .ret
= 0 };
240 * Make sure all event classes have their "stream_id" attribute
243 g_ptr_array_foreach(stream_class
->event_classes
,
244 event_class_set_stream_id
, &data
);
250 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
258 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
263 if (!stream_class
|| stream_class
->frozen
) {
268 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
274 void event_class_exists(gpointer element
, gpointer query
)
276 struct bt_ctf_event_class
*event_class_a
= element
;
277 struct search_query
*search_query
= query
;
278 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
281 if (search_query
->value
== element
) {
282 search_query
->found
= 1;
287 * Two event classes cannot share the same name in a given
290 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
291 bt_ctf_event_class_get_name(event_class_b
))) {
292 search_query
->found
= 1;
297 * Two event classes cannot share the same ID in a given
300 id_a
= bt_ctf_event_class_get_id(event_class_a
);
301 id_b
= bt_ctf_event_class_get_id(event_class_b
);
303 if (id_a
< 0 || id_b
< 0) {
304 /* at least one ID is not set: will be automatically set later */
309 search_query
->found
= 1;
317 int bt_ctf_stream_class_add_event_class(
318 struct bt_ctf_stream_class
*stream_class
,
319 struct bt_ctf_event_class
*event_class
)
322 int64_t *event_id
= NULL
;
323 struct bt_ctf_trace
*trace
= NULL
;
324 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
325 struct bt_ctf_validation_output validation_output
= { 0 };
326 struct bt_ctf_field_type
*packet_header_type
= NULL
;
327 struct bt_ctf_field_type
*packet_context_type
= NULL
;
328 struct bt_ctf_field_type
*event_header_type
= NULL
;
329 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
330 struct bt_ctf_field_type
*event_context_type
= NULL
;
331 struct bt_ctf_field_type
*event_payload_type
= NULL
;
332 const enum bt_ctf_validation_flag validation_flags
=
333 BT_CTF_VALIDATION_FLAG_EVENT
;
335 if (!stream_class
|| !event_class
) {
340 trace
= bt_ctf_stream_class_get_trace(stream_class
);
341 if (trace
&& trace
->is_static
) {
346 event_id
= g_new(int64_t, 1);
352 /* Check for duplicate event classes */
353 struct search_query query
= { .value
= event_class
, .found
= 0 };
354 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
361 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
362 if (old_stream_class
) {
363 /* Event class is already associated to a stream class. */
370 * If the stream class is associated with a trace, then
371 * both those objects are frozen. Also, this event class
372 * is about to be frozen.
374 * Therefore the event class must be validated here.
375 * The trace and stream class should be valid at this
378 assert(trace
->valid
);
379 assert(stream_class
->valid
);
381 bt_ctf_trace_get_packet_header_type(trace
);
382 packet_context_type
=
383 bt_ctf_stream_class_get_packet_context_type(
386 bt_ctf_stream_class_get_event_header_type(stream_class
);
387 stream_event_ctx_type
=
388 bt_ctf_stream_class_get_event_context_type(
391 bt_ctf_event_class_get_context_type(event_class
);
393 bt_ctf_event_class_get_payload_type(event_class
);
394 ret
= bt_ctf_validate_class_types(
395 trace
->environment
, packet_header_type
,
396 packet_context_type
, event_header_type
,
397 stream_event_ctx_type
, event_context_type
,
398 event_payload_type
, trace
->valid
,
399 stream_class
->valid
, event_class
->valid
,
400 &validation_output
, validation_flags
);
401 BT_PUT(packet_header_type
);
402 BT_PUT(packet_context_type
);
403 BT_PUT(event_header_type
);
404 BT_PUT(stream_event_ctx_type
);
405 BT_PUT(event_context_type
);
406 BT_PUT(event_payload_type
);
410 * This means something went wrong during the
411 * validation process, not that the objects are
417 if ((validation_output
.valid_flags
& validation_flags
) !=
419 /* Invalid event class */
425 /* Only set an event ID if none was explicitly set before */
426 *event_id
= bt_ctf_event_class_get_id(event_class
);
428 if (bt_ctf_event_class_set_id(event_class
,
429 stream_class
->next_event_id
++)) {
433 *event_id
= stream_class
->next_event_id
;
436 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
441 bt_object_set_parent(event_class
, stream_class
);
445 * At this point we know that the function will be
446 * successful. Therefore we can replace the event
447 * class's field types with what's in the validation
448 * output structure and mark this event class as valid.
450 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
451 &validation_output
, validation_flags
);
452 event_class
->valid
= 1;
455 * Put what was not moved in
456 * bt_ctf_validation_replace_types().
458 bt_ctf_validation_output_put_types(&validation_output
);
461 /* Add to the event classes of the stream class */
462 g_ptr_array_add(stream_class
->event_classes
, event_class
);
463 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
467 /* Freeze the event class */
468 bt_ctf_event_class_freeze(event_class
);
470 /* Notifiy listeners of the trace's schema modification. */
472 struct bt_ctf_object obj
= { .object
= event_class
,
473 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
475 (void) bt_ctf_trace_object_modification(&obj
, trace
);
479 BT_PUT(old_stream_class
);
480 bt_ctf_validation_output_put_types(&validation_output
);
481 assert(!packet_header_type
);
482 assert(!packet_context_type
);
483 assert(!event_header_type
);
484 assert(!stream_event_ctx_type
);
485 assert(!event_context_type
);
486 assert(!event_payload_type
);
492 int64_t bt_ctf_stream_class_get_event_class_count(
493 struct bt_ctf_stream_class
*stream_class
)
502 ret
= (int) stream_class
->event_classes
->len
;
507 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
508 struct bt_ctf_stream_class
*stream_class
, int index
)
510 struct bt_ctf_event_class
*event_class
= NULL
;
512 if (!stream_class
|| index
< 0 ||
513 index
>= stream_class
->event_classes
->len
) {
517 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
523 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
524 struct bt_ctf_stream_class
*stream_class
, const char *name
)
527 struct bt_ctf_event_class
*event_class
= NULL
;
529 if (!stream_class
|| !name
) {
533 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
534 struct bt_ctf_event_class
*cur_event_class
=
535 g_ptr_array_index(stream_class
->event_classes
, i
);
536 const char *cur_event_class_name
=
537 bt_ctf_event_class_get_name(cur_event_class
);
539 if (!strcmp(name
, cur_event_class_name
)) {
540 event_class
= cur_event_class
;
549 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
550 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
553 struct bt_ctf_event_class
*event_class
= NULL
;
559 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
566 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
567 struct bt_ctf_stream_class
*stream_class
)
569 struct bt_ctf_field_type
*ret
= NULL
;
575 bt_get(stream_class
->packet_context_type
);
576 ret
= stream_class
->packet_context_type
;
581 int bt_ctf_stream_class_set_packet_context_type(
582 struct bt_ctf_stream_class
*stream_class
,
583 struct bt_ctf_field_type
*packet_context_type
)
587 if (!stream_class
|| stream_class
->frozen
) {
592 if (packet_context_type
&&
593 bt_ctf_field_type_get_type_id(packet_context_type
) !=
594 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
595 /* A packet context must be a structure. */
600 bt_put(stream_class
->packet_context_type
);
601 bt_get(packet_context_type
);
602 stream_class
->packet_context_type
= packet_context_type
;
607 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
608 struct bt_ctf_stream_class
*stream_class
)
610 struct bt_ctf_field_type
*ret
= NULL
;
612 if (!stream_class
|| !stream_class
->event_header_type
) {
616 bt_get(stream_class
->event_header_type
);
617 ret
= stream_class
->event_header_type
;
622 int bt_ctf_stream_class_set_event_header_type(
623 struct bt_ctf_stream_class
*stream_class
,
624 struct bt_ctf_field_type
*event_header_type
)
628 if (!stream_class
|| stream_class
->frozen
) {
633 if (event_header_type
&&
634 bt_ctf_field_type_get_type_id(event_header_type
) !=
635 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
636 /* An event header must be a structure. */
641 bt_put(stream_class
->event_header_type
);
642 stream_class
->event_header_type
= bt_get(event_header_type
);
647 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
648 struct bt_ctf_stream_class
*stream_class
)
650 struct bt_ctf_field_type
*ret
= NULL
;
652 if (!stream_class
|| !stream_class
->event_context_type
) {
656 bt_get(stream_class
->event_context_type
);
657 ret
= stream_class
->event_context_type
;
662 int bt_ctf_stream_class_set_event_context_type(
663 struct bt_ctf_stream_class
*stream_class
,
664 struct bt_ctf_field_type
*event_context_type
)
668 if (!stream_class
|| stream_class
->frozen
) {
673 if (event_context_type
&&
674 bt_ctf_field_type_get_type_id(event_context_type
) !=
675 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
676 /* A packet context must be a structure. */
681 bt_put(stream_class
->event_context_type
);
682 stream_class
->event_context_type
= bt_get(event_context_type
);
687 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
689 bt_get(stream_class
);
692 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
694 bt_put(stream_class
);
698 int64_t get_event_class_count(void *element
)
700 return bt_ctf_stream_class_get_event_class_count(
701 (struct bt_ctf_stream_class
*) element
);
705 void *get_event_class(void *element
, int i
)
707 return bt_ctf_stream_class_get_event_class(
708 (struct bt_ctf_stream_class
*) element
, i
);
712 int visit_event_class(void *object
, bt_ctf_visitor visitor
,void *data
)
714 struct bt_ctf_object obj
=
716 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
718 return visitor(&obj
, data
);
721 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
722 bt_ctf_visitor visitor
, void *data
)
725 struct bt_ctf_object obj
=
726 { .object
= stream_class
,
727 .type
= BT_CTF_OBJECT_TYPE_STREAM_CLASS
};
729 if (!stream_class
|| !visitor
) {
734 ret
= visitor_helper(&obj
, get_event_class_count
,
736 visit_event_class
, visitor
, data
);
742 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
748 stream_class
->frozen
= 1;
749 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
750 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
751 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
753 if (stream_class
->clock
) {
754 bt_ctf_clock_class_freeze(stream_class
->clock
->clock_class
);
759 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
760 struct metadata_context
*context
)
765 g_string_assign(context
->field_name
, "");
766 context
->current_indentation_level
= 1;
767 if (!stream_class
->id_set
) {
772 g_string_append_printf(context
->string
,
773 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
775 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
781 if (stream_class
->packet_context_type
) {
782 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
783 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
790 if (stream_class
->event_context_type
) {
791 g_string_append(context
->string
, ";\n\n\tevent.context := ");
792 ret
= bt_ctf_field_type_serialize(
793 stream_class
->event_context_type
, context
);
799 g_string_append(context
->string
, ";\n};\n\n");
800 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
801 struct bt_ctf_event_class
*event_class
=
802 stream_class
->event_classes
->pdata
[i
];
804 ret
= bt_ctf_event_class_serialize(event_class
, context
);
810 context
->current_indentation_level
= 0;
815 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
817 struct bt_ctf_stream_class
*stream_class
;
819 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
820 bt_put(stream_class
->clock
);
822 if (stream_class
->event_classes_ht
) {
823 g_hash_table_destroy(stream_class
->event_classes_ht
);
825 if (stream_class
->event_classes
) {
826 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
829 if (stream_class
->name
) {
830 g_string_free(stream_class
->name
, TRUE
);
833 bt_put(stream_class
->event_header_type
);
834 bt_put(stream_class
->packet_context_type
);
835 bt_put(stream_class
->event_context_type
);
836 g_free(stream_class
);
840 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
843 struct bt_ctf_field_type
*event_header_type
=
844 bt_ctf_field_type_structure_create();
845 struct bt_ctf_field_type
*_uint32_t
=
846 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
847 struct bt_ctf_field_type
*_uint64_t
=
848 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
850 if (!event_header_type
) {
855 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
861 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
862 _uint64_t
, "timestamp");
867 if (stream_class
->event_header_type
) {
868 bt_put(stream_class
->event_header_type
);
870 stream_class
->event_header_type
= event_header_type
;
873 bt_put(event_header_type
);
882 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
885 struct bt_ctf_field_type
*packet_context_type
=
886 bt_ctf_field_type_structure_create();
887 struct bt_ctf_field_type
*_uint64_t
=
888 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
890 if (!packet_context_type
) {
896 * We create a stream packet context as proposed in the CTF
899 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
900 _uint64_t
, "timestamp_begin");
905 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
906 _uint64_t
, "timestamp_end");
911 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
912 _uint64_t
, "content_size");
917 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
918 _uint64_t
, "packet_size");
923 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
924 _uint64_t
, "events_discarded");
929 bt_put(stream_class
->packet_context_type
);
930 stream_class
->packet_context_type
= packet_context_type
;
933 bt_put(packet_context_type
);