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-ir/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-ir/event-class-internal.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/fields-internal.h>
36 #include <babeltrace/ctf-writer/stream.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-ir/validation-internal.h>
39 #include <babeltrace/ctf-ir/visitor-internal.h>
40 #include <babeltrace/ctf-writer/functor-internal.h>
41 #include <babeltrace/ctf-ir/utils.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/compiler.h>
44 #include <babeltrace/align.h>
45 #include <babeltrace/endian.h>
48 void bt_ctf_stream_class_destroy(struct bt_object
*obj
);
50 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
52 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
54 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
57 struct bt_ctf_stream_class
*stream_class
= NULL
;
59 if (name
&& bt_ctf_validate_identifier(name
)) {
63 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
68 stream_class
->name
= g_string_new(name
);
69 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
70 (GDestroyNotify
) bt_object_release
);
71 if (!stream_class
->event_classes
) {
75 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
76 g_int64_equal
, g_free
, NULL
);
78 ret
= init_event_header(stream_class
);
83 ret
= init_packet_context(stream_class
);
88 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
96 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
97 struct bt_ctf_stream_class
*stream_class
)
99 return (struct bt_ctf_trace
*) bt_object_get_parent(
103 const char *bt_ctf_stream_class_get_name(
104 struct bt_ctf_stream_class
*stream_class
)
106 const char *name
= NULL
;
112 name
= stream_class
->name
->str
;
117 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
122 if (!stream_class
|| stream_class
->frozen
) {
127 g_string_assign(stream_class
->name
, name
);
132 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
133 struct bt_ctf_stream_class
*stream_class
)
135 struct bt_ctf_clock
*clock
= NULL
;
137 if (!stream_class
|| !stream_class
->clock
) {
141 clock
= stream_class
->clock
;
147 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
148 struct bt_ctf_clock
*clock
)
151 struct bt_ctf_field_type
*timestamp_field
= NULL
;
153 if (!stream_class
|| stream_class
->frozen
||
154 !bt_ctf_clock_is_valid(clock
)) {
160 * Look for a "timestamp" field in the stream class' event header type
161 * and map the stream's clock to that field if no current mapping is
164 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
165 stream_class
->event_header_type
, "timestamp");
166 if (timestamp_field
) {
167 struct bt_ctf_clock
*mapped_clock
;
169 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
172 bt_put(mapped_clock
);
176 ret
= bt_ctf_field_type_integer_set_mapped_clock(
177 timestamp_field
, clock
);
183 if (stream_class
->clock
) {
184 bt_put(stream_class
->clock
);
187 stream_class
->clock
= clock
;
190 if (timestamp_field
) {
191 bt_put(timestamp_field
);
196 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
200 if (!stream_class
|| !stream_class
->id_set
) {
205 ret
= (int64_t) stream_class
->id
;
211 int _bt_ctf_stream_class_set_id(
212 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
214 stream_class
->id
= id
;
215 stream_class
->id_set
= 1;
219 struct event_class_set_stream_id_data
{
225 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
227 struct event_class_set_stream_id_data
*typed_data
= data
;
229 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
230 typed_data
->stream_id
);
234 int bt_ctf_stream_class_set_id_no_check(
235 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
238 struct event_class_set_stream_id_data data
=
239 { .stream_id
= id
, .ret
= 0 };
242 * Make sure all event classes have their "stream_id" attribute
245 g_ptr_array_foreach(stream_class
->event_classes
,
246 event_class_set_stream_id
, &data
);
252 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
260 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
265 if (!stream_class
|| stream_class
->frozen
) {
270 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
276 void event_class_exists(gpointer element
, gpointer query
)
278 struct bt_ctf_event_class
*event_class_a
= element
;
279 struct search_query
*search_query
= query
;
280 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
283 if (search_query
->value
== element
) {
284 search_query
->found
= 1;
289 * Two event classes cannot share the same name in a given
292 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
293 bt_ctf_event_class_get_name(event_class_b
))) {
294 search_query
->found
= 1;
299 * Two event classes cannot share the same ID in a given
302 id_a
= bt_ctf_event_class_get_id(event_class_a
);
303 id_b
= bt_ctf_event_class_get_id(event_class_b
);
305 if (id_a
< 0 || id_b
< 0) {
306 /* at least one ID is not set: will be automatically set later */
311 search_query
->found
= 1;
319 int bt_ctf_stream_class_add_event_class(
320 struct bt_ctf_stream_class
*stream_class
,
321 struct bt_ctf_event_class
*event_class
)
324 int64_t *event_id
= NULL
;
325 struct bt_ctf_trace
*trace
= NULL
;
326 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
327 struct bt_ctf_validation_output validation_output
= { 0 };
328 struct bt_ctf_field_type
*packet_header_type
= NULL
;
329 struct bt_ctf_field_type
*packet_context_type
= NULL
;
330 struct bt_ctf_field_type
*event_header_type
= NULL
;
331 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
332 struct bt_ctf_field_type
*event_context_type
= NULL
;
333 struct bt_ctf_field_type
*event_payload_type
= NULL
;
334 const enum bt_ctf_validation_flag validation_flags
=
335 BT_CTF_VALIDATION_FLAG_EVENT
;
337 if (!stream_class
|| !event_class
) {
342 event_id
= g_new(int64_t, 1);
348 /* Check for duplicate event classes */
349 struct search_query query
= { .value
= event_class
, .found
= 0 };
350 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
357 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
358 if (old_stream_class
) {
359 /* Event class is already associated to a stream class. */
364 trace
= bt_ctf_stream_class_get_trace(stream_class
);
367 * If the stream class is associated with a trace, then
368 * both those objects are frozen. Also, this event class
369 * is about to be frozen.
371 * Therefore the event class must be validated here.
372 * The trace and stream class should be valid at this
375 assert(trace
->valid
);
376 assert(stream_class
->valid
);
378 bt_ctf_trace_get_packet_header_type(trace
);
379 packet_context_type
=
380 bt_ctf_stream_class_get_packet_context_type(
383 bt_ctf_stream_class_get_event_header_type(stream_class
);
384 stream_event_ctx_type
=
385 bt_ctf_stream_class_get_event_context_type(
388 bt_ctf_event_class_get_context_type(event_class
);
390 bt_ctf_event_class_get_payload_type(event_class
);
391 ret
= bt_ctf_validate_class_types(
392 trace
->environment
, packet_header_type
,
393 packet_context_type
, event_header_type
,
394 stream_event_ctx_type
, event_context_type
,
395 event_payload_type
, trace
->valid
,
396 stream_class
->valid
, event_class
->valid
,
397 &validation_output
, validation_flags
);
398 BT_PUT(packet_header_type
);
399 BT_PUT(packet_context_type
);
400 BT_PUT(event_header_type
);
401 BT_PUT(stream_event_ctx_type
);
402 BT_PUT(event_context_type
);
403 BT_PUT(event_payload_type
);
407 * This means something went wrong during the
408 * validation process, not that the objects are
414 if ((validation_output
.valid_flags
& validation_flags
) !=
416 /* Invalid event class */
422 /* Only set an event ID if none was explicitly set before */
423 *event_id
= bt_ctf_event_class_get_id(event_class
);
425 if (bt_ctf_event_class_set_id(event_class
,
426 stream_class
->next_event_id
++)) {
430 *event_id
= stream_class
->next_event_id
;
433 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
438 bt_object_set_parent(event_class
, stream_class
);
442 * At this point we know that the function will be
443 * successful. Therefore we can replace the event
444 * class's field types with what's in the validation
445 * output structure and mark this event class as valid.
447 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
448 &validation_output
, validation_flags
);
449 event_class
->valid
= 1;
452 * Put what was not moved in
453 * bt_ctf_validation_replace_types().
455 bt_ctf_validation_output_put_types(&validation_output
);
458 /* Add to the event classes of the stream class */
459 g_ptr_array_add(stream_class
->event_classes
, event_class
);
460 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
464 /* Freeze the event class */
465 bt_ctf_event_class_freeze(event_class
);
467 if (stream_class
->byte_order
) {
469 * Only set native byte order if it has been initialized
470 * when the stream class was added to a trace.
472 * If not set here, this will be set when the stream
473 * class is added to a trace.
475 bt_ctf_event_class_set_native_byte_order(event_class
,
476 stream_class
->byte_order
);
479 /* Notifiy listeners of the trace's schema modification. */
481 struct bt_ctf_object obj
= { .object
= event_class
,
482 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
484 (void) bt_ctf_trace_object_modification(&obj
, trace
);
488 BT_PUT(old_stream_class
);
489 bt_ctf_validation_output_put_types(&validation_output
);
490 assert(!packet_header_type
);
491 assert(!packet_context_type
);
492 assert(!event_header_type
);
493 assert(!stream_event_ctx_type
);
494 assert(!event_context_type
);
495 assert(!event_payload_type
);
501 int bt_ctf_stream_class_get_event_class_count(
502 struct bt_ctf_stream_class
*stream_class
)
511 ret
= (int) stream_class
->event_classes
->len
;
516 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
517 struct bt_ctf_stream_class
*stream_class
, int index
)
519 struct bt_ctf_event_class
*event_class
= NULL
;
521 if (!stream_class
|| index
< 0 ||
522 index
>= stream_class
->event_classes
->len
) {
526 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
532 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
533 struct bt_ctf_stream_class
*stream_class
, const char *name
)
536 struct bt_ctf_event_class
*event_class
= NULL
;
538 if (!stream_class
|| !name
) {
542 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
543 struct bt_ctf_event_class
*cur_event_class
=
544 g_ptr_array_index(stream_class
->event_classes
, i
);
545 const char *cur_event_class_name
=
546 bt_ctf_event_class_get_name(cur_event_class
);
548 if (!strcmp(name
, cur_event_class_name
)) {
549 event_class
= cur_event_class
;
558 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
559 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
562 struct bt_ctf_event_class
*event_class
= NULL
;
568 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
575 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
576 struct bt_ctf_stream_class
*stream_class
)
578 struct bt_ctf_field_type
*ret
= NULL
;
584 bt_get(stream_class
->packet_context_type
);
585 ret
= stream_class
->packet_context_type
;
590 int bt_ctf_stream_class_set_packet_context_type(
591 struct bt_ctf_stream_class
*stream_class
,
592 struct bt_ctf_field_type
*packet_context_type
)
596 if (!stream_class
|| stream_class
->frozen
) {
601 if (packet_context_type
&&
602 bt_ctf_field_type_get_type_id(packet_context_type
) !=
603 BT_CTF_TYPE_ID_STRUCT
) {
604 /* A packet context must be a structure. */
609 bt_put(stream_class
->packet_context_type
);
610 bt_get(packet_context_type
);
611 stream_class
->packet_context_type
= packet_context_type
;
616 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
617 struct bt_ctf_stream_class
*stream_class
)
619 struct bt_ctf_field_type
*ret
= NULL
;
621 if (!stream_class
|| !stream_class
->event_header_type
) {
625 assert(stream_class
->event_header_type
);
626 bt_get(stream_class
->event_header_type
);
627 ret
= stream_class
->event_header_type
;
632 int bt_ctf_stream_class_set_event_header_type(
633 struct bt_ctf_stream_class
*stream_class
,
634 struct bt_ctf_field_type
*event_header_type
)
638 if (!stream_class
|| stream_class
->frozen
) {
643 if (event_header_type
&&
644 bt_ctf_field_type_get_type_id(event_header_type
) !=
645 BT_CTF_TYPE_ID_STRUCT
) {
646 /* An event header must be a structure. */
651 bt_put(stream_class
->event_header_type
);
652 stream_class
->event_header_type
= bt_get(event_header_type
);
657 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
658 struct bt_ctf_stream_class
*stream_class
)
660 struct bt_ctf_field_type
*ret
= NULL
;
662 if (!stream_class
|| !stream_class
->event_context_type
) {
666 bt_get(stream_class
->event_context_type
);
667 ret
= stream_class
->event_context_type
;
672 int bt_ctf_stream_class_set_event_context_type(
673 struct bt_ctf_stream_class
*stream_class
,
674 struct bt_ctf_field_type
*event_context_type
)
678 if (!stream_class
|| stream_class
->frozen
) {
683 if (event_context_type
&&
684 bt_ctf_field_type_get_type_id(event_context_type
) !=
685 BT_CTF_TYPE_ID_STRUCT
) {
686 /* A packet context must be a structure. */
691 bt_put(stream_class
->event_context_type
);
692 stream_class
->event_context_type
= bt_get(event_context_type
);
697 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
699 bt_get(stream_class
);
702 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
704 bt_put(stream_class
);
708 int get_event_class_count(void *element
)
710 return bt_ctf_stream_class_get_event_class_count(
711 (struct bt_ctf_stream_class
*) element
);
715 void *get_event_class(void *element
, int i
)
717 return bt_ctf_stream_class_get_event_class(
718 (struct bt_ctf_stream_class
*) element
, i
);
722 int visit_event_class(void *object
, bt_ctf_visitor visitor
,void *data
)
724 struct bt_ctf_object obj
=
726 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
728 return visitor(&obj
, data
);
731 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
732 bt_ctf_visitor visitor
, void *data
)
735 struct bt_ctf_object obj
=
736 { .object
= stream_class
,
737 .type
= BT_CTF_OBJECT_TYPE_STREAM_CLASS
};
739 if (!stream_class
|| !visitor
) {
744 ret
= visitor_helper(&obj
, get_event_class_count
,
746 visit_event_class
, visitor
, data
);
752 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
758 stream_class
->frozen
= 1;
759 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
760 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
761 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
762 bt_ctf_clock_freeze(stream_class
->clock
);
766 void bt_ctf_stream_class_set_byte_order(
767 struct bt_ctf_stream_class
*stream_class
, int byte_order
)
771 assert(stream_class
);
772 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
773 stream_class
->byte_order
= byte_order
;
775 /* Set native byte order to little or big endian */
776 bt_ctf_field_type_set_native_byte_order(
777 stream_class
->event_header_type
, byte_order
);
778 bt_ctf_field_type_set_native_byte_order(
779 stream_class
->packet_context_type
, byte_order
);
780 bt_ctf_field_type_set_native_byte_order(
781 stream_class
->event_context_type
, byte_order
);
783 /* Set all events' native byte order */
784 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
785 struct bt_ctf_event_class
*event_class
=
786 g_ptr_array_index(stream_class
->event_classes
, i
);
788 bt_ctf_event_class_set_native_byte_order(event_class
,
794 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
795 struct metadata_context
*context
)
800 g_string_assign(context
->field_name
, "");
801 context
->current_indentation_level
= 1;
802 if (!stream_class
->id_set
) {
807 g_string_append_printf(context
->string
,
808 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
810 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
816 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
817 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
823 if (stream_class
->event_context_type
) {
824 g_string_append(context
->string
, ";\n\n\tevent.context := ");
825 ret
= bt_ctf_field_type_serialize(
826 stream_class
->event_context_type
, context
);
832 g_string_append(context
->string
, ";\n};\n\n");
833 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
834 struct bt_ctf_event_class
*event_class
=
835 stream_class
->event_classes
->pdata
[i
];
837 ret
= bt_ctf_event_class_serialize(event_class
, context
);
843 context
->current_indentation_level
= 0;
848 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
850 struct bt_ctf_stream_class
*stream_class
;
852 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
853 bt_put(stream_class
->clock
);
855 if (stream_class
->event_classes_ht
) {
856 g_hash_table_destroy(stream_class
->event_classes_ht
);
858 if (stream_class
->event_classes
) {
859 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
862 if (stream_class
->name
) {
863 g_string_free(stream_class
->name
, TRUE
);
866 bt_put(stream_class
->event_header_type
);
867 bt_put(stream_class
->packet_context_type
);
868 bt_put(stream_class
->event_context_type
);
869 g_free(stream_class
);
873 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
876 struct bt_ctf_field_type
*event_header_type
=
877 bt_ctf_field_type_structure_create();
878 struct bt_ctf_field_type
*_uint32_t
=
879 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
880 struct bt_ctf_field_type
*_uint64_t
=
881 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
883 if (!event_header_type
) {
888 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
894 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
895 _uint64_t
, "timestamp");
900 if (stream_class
->event_header_type
) {
901 bt_put(stream_class
->event_header_type
);
903 stream_class
->event_header_type
= event_header_type
;
906 bt_put(event_header_type
);
915 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
918 struct bt_ctf_field_type
*packet_context_type
=
919 bt_ctf_field_type_structure_create();
920 struct bt_ctf_field_type
*_uint64_t
=
921 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
923 if (!packet_context_type
) {
929 * We create a stream packet context as proposed in the CTF
932 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
933 _uint64_t
, "timestamp_begin");
938 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
939 _uint64_t
, "timestamp_end");
944 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
945 _uint64_t
, "content_size");
950 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
951 _uint64_t
, "packet_size");
956 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
957 _uint64_t
, "events_discarded");
962 bt_put(stream_class
->packet_context_type
);
963 stream_class
->packet_context_type
= packet_context_type
;
966 bt_put(packet_context_type
);