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 ret
= init_event_header(stream_class
);
80 ret
= init_packet_context(stream_class
);
85 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
93 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
94 struct bt_ctf_stream_class
*stream_class
)
96 return (struct bt_ctf_trace
*) bt_object_get_parent(
100 const char *bt_ctf_stream_class_get_name(
101 struct bt_ctf_stream_class
*stream_class
)
103 const char *name
= NULL
;
109 name
= stream_class
->name
->str
;
114 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
119 if (!stream_class
|| stream_class
->frozen
) {
124 g_string_assign(stream_class
->name
, name
);
129 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
130 struct bt_ctf_stream_class
*stream_class
)
132 struct bt_ctf_clock
*clock
= NULL
;
134 if (!stream_class
|| !stream_class
->clock
) {
138 clock
= stream_class
->clock
;
144 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
145 struct bt_ctf_clock
*clock
)
148 struct bt_ctf_field_type
*timestamp_field
= NULL
;
150 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
156 * Look for a "timestamp" field in the stream class' event header type
157 * and map the stream's clock to that field if no current mapping is
160 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
161 stream_class
->event_header_type
, "timestamp");
162 if (timestamp_field
) {
163 struct bt_ctf_clock
*mapped_clock
;
165 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
168 bt_put(mapped_clock
);
172 ret
= bt_ctf_field_type_integer_set_mapped_clock(
173 timestamp_field
, clock
);
179 if (stream_class
->clock
) {
180 bt_put(stream_class
->clock
);
183 stream_class
->clock
= clock
;
186 if (timestamp_field
) {
187 bt_put(timestamp_field
);
192 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
196 if (!stream_class
|| !stream_class
->id_set
) {
201 ret
= (int64_t) stream_class
->id
;
207 int _bt_ctf_stream_class_set_id(
208 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
210 stream_class
->id
= id
;
211 stream_class
->id_set
= 1;
215 struct event_class_set_stream_id_data
{
221 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
223 struct event_class_set_stream_id_data
*typed_data
= data
;
225 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
226 typed_data
->stream_id
);
230 int bt_ctf_stream_class_set_id_no_check(
231 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
234 struct event_class_set_stream_id_data data
=
235 { .stream_id
= id
, .ret
= 0 };
238 * Make sure all event classes have their "stream_id" attribute
241 g_ptr_array_foreach(stream_class
->event_classes
,
242 event_class_set_stream_id
, &data
);
248 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
256 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
261 if (!stream_class
|| stream_class
->frozen
) {
266 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
272 void event_class_exists(gpointer element
, gpointer query
)
274 struct bt_ctf_event_class
*event_class_a
= element
;
275 struct search_query
*search_query
= query
;
276 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
279 if (search_query
->value
== element
) {
280 search_query
->found
= 1;
285 * Two event classes cannot share the same name in a given
288 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
289 bt_ctf_event_class_get_name(event_class_b
))) {
290 search_query
->found
= 1;
295 * Two event classes cannot share the same ID in a given
298 id_a
= bt_ctf_event_class_get_id(event_class_a
);
299 id_b
= bt_ctf_event_class_get_id(event_class_b
);
301 if (id_a
< 0 || id_b
< 0) {
302 /* at least one ID is not set: will be automatically set later */
307 search_query
->found
= 1;
315 int bt_ctf_stream_class_add_event_class(
316 struct bt_ctf_stream_class
*stream_class
,
317 struct bt_ctf_event_class
*event_class
)
321 struct bt_ctf_trace
*trace
= NULL
;
322 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
323 struct bt_ctf_validation_output validation_output
= { 0 };
324 struct bt_ctf_field_type
*packet_header_type
= NULL
;
325 struct bt_ctf_field_type
*packet_context_type
= NULL
;
326 struct bt_ctf_field_type
*event_header_type
= NULL
;
327 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
328 struct bt_ctf_field_type
*event_context_type
= NULL
;
329 struct bt_ctf_field_type
*event_payload_type
= NULL
;
330 const enum bt_ctf_validation_flag validation_flags
=
331 BT_CTF_VALIDATION_FLAG_EVENT
;
333 if (!stream_class
|| !event_class
) {
338 /* Check for duplicate event classes */
339 struct search_query query
= { .value
= event_class
, .found
= 0 };
340 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
347 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
348 if (old_stream_class
) {
349 /* Event class is already associated to a stream class. */
354 trace
= bt_ctf_stream_class_get_trace(stream_class
);
357 * If the stream class is associated with a trace, then
358 * both those objects are frozen. Also, this event class
359 * is about to be frozen.
361 * Therefore the event class must be validated here.
362 * The trace and stream class should be valid at this
365 assert(trace
->valid
);
366 assert(stream_class
->valid
);
368 bt_ctf_trace_get_packet_header_type(trace
);
369 packet_context_type
=
370 bt_ctf_stream_class_get_packet_context_type(
373 bt_ctf_stream_class_get_event_header_type(stream_class
);
374 stream_event_ctx_type
=
375 bt_ctf_stream_class_get_event_context_type(
378 bt_ctf_event_class_get_context_type(event_class
);
380 bt_ctf_event_class_get_payload_type(event_class
);
381 ret
= bt_ctf_validate_class_types(
382 trace
->environment
, packet_header_type
,
383 packet_context_type
, event_header_type
,
384 stream_event_ctx_type
, event_context_type
,
385 event_payload_type
, trace
->valid
,
386 stream_class
->valid
, event_class
->valid
,
387 &validation_output
, validation_flags
);
388 BT_PUT(packet_header_type
);
389 BT_PUT(packet_context_type
);
390 BT_PUT(event_header_type
);
391 BT_PUT(stream_event_ctx_type
);
392 BT_PUT(event_context_type
);
393 BT_PUT(event_payload_type
);
397 * This means something went wrong during the
398 * validation process, not that the objects are
404 if ((validation_output
.valid_flags
& validation_flags
) !=
406 /* Invalid event class */
412 /* Only set an event ID if none was explicitly set before */
413 event_id
= bt_ctf_event_class_get_id(event_class
);
415 if (bt_ctf_event_class_set_id(event_class
,
416 stream_class
->next_event_id
++)) {
422 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
427 bt_object_set_parent(event_class
, stream_class
);
431 * At this point we know that the function will be
432 * successful. Therefore we can replace the event
433 * class's field types with what's in the validation
434 * output structure and mark this event class as valid.
436 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
437 &validation_output
, validation_flags
);
438 event_class
->valid
= 1;
441 * Put what was not moved in
442 * bt_ctf_validation_replace_types().
444 bt_ctf_validation_output_put_types(&validation_output
);
447 /* Add to the event classes of the stream class */
448 g_ptr_array_add(stream_class
->event_classes
, event_class
);
450 /* Freeze the event class */
451 bt_ctf_event_class_freeze(event_class
);
453 if (stream_class
->byte_order
) {
455 * Only set native byte order if it has been initialized
456 * when the stream class was added to a trace.
458 * If not set here, this will be set when the stream
459 * class is added to a trace.
461 bt_ctf_event_class_set_native_byte_order(event_class
,
462 stream_class
->byte_order
);
465 /* Notifiy listeners of the trace's schema modification. */
467 struct bt_ctf_ir_element element
= { .element
= event_class
,
468 .type
= BT_CTF_IR_TYPE_EVENT_CLASS
};
470 (void) bt_ctf_trace_element_modification(&element
, trace
);
474 BT_PUT(old_stream_class
);
475 bt_ctf_validation_output_put_types(&validation_output
);
476 assert(!packet_header_type
);
477 assert(!packet_context_type
);
478 assert(!event_header_type
);
479 assert(!stream_event_ctx_type
);
480 assert(!event_context_type
);
481 assert(!event_payload_type
);
486 int bt_ctf_stream_class_get_event_class_count(
487 struct bt_ctf_stream_class
*stream_class
)
496 ret
= (int) stream_class
->event_classes
->len
;
501 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
502 struct bt_ctf_stream_class
*stream_class
, int index
)
504 struct bt_ctf_event_class
*event_class
= NULL
;
506 if (!stream_class
|| index
< 0 ||
507 index
>= stream_class
->event_classes
->len
) {
511 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
517 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
518 struct bt_ctf_stream_class
*stream_class
, const char *name
)
521 struct bt_ctf_event_class
*event_class
= NULL
;
523 if (!stream_class
|| !name
) {
527 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
528 struct bt_ctf_event_class
*cur_event_class
=
529 g_ptr_array_index(stream_class
->event_classes
, i
);
530 const char *cur_event_class_name
=
531 bt_ctf_event_class_get_name(cur_event_class
);
533 if (!strcmp(name
, cur_event_class_name
)) {
534 event_class
= cur_event_class
;
543 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
544 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
547 struct bt_ctf_event_class
*event_class
= NULL
;
553 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
554 struct bt_ctf_event_class
*current_event_class
=
555 g_ptr_array_index(stream_class
->event_classes
, i
);
557 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
558 event_class
= current_event_class
;
567 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
568 struct bt_ctf_stream_class
*stream_class
)
570 struct bt_ctf_field_type
*ret
= NULL
;
576 assert(stream_class
->packet_context_type
);
577 bt_get(stream_class
->packet_context_type
);
578 ret
= stream_class
->packet_context_type
;
583 int bt_ctf_stream_class_set_packet_context_type(
584 struct bt_ctf_stream_class
*stream_class
,
585 struct bt_ctf_field_type
*packet_context_type
)
589 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
594 assert(stream_class
->packet_context_type
);
595 if (stream_class
->packet_context_type
== packet_context_type
) {
598 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
599 BT_CTF_TYPE_ID_STRUCT
) {
600 /* A packet context must be a structure */
605 bt_put(stream_class
->packet_context_type
);
606 bt_get(packet_context_type
);
607 stream_class
->packet_context_type
= packet_context_type
;
612 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
613 struct bt_ctf_stream_class
*stream_class
)
615 struct bt_ctf_field_type
*ret
= NULL
;
617 if (!stream_class
|| !stream_class
->event_header_type
) {
621 assert(stream_class
->event_header_type
);
622 bt_get(stream_class
->event_header_type
);
623 ret
= stream_class
->event_header_type
;
628 int bt_ctf_stream_class_set_event_header_type(
629 struct bt_ctf_stream_class
*stream_class
,
630 struct bt_ctf_field_type
*event_header_type
)
634 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
639 assert(stream_class
->event_header_type
);
640 if (stream_class
->event_header_type
== event_header_type
) {
643 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
644 BT_CTF_TYPE_ID_STRUCT
) {
645 /* An event header must be a structure */
650 bt_put(stream_class
->event_header_type
);
651 bt_get(event_header_type
);
652 stream_class
->event_header_type
= 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 assert(stream_class
->event_context_type
);
667 bt_get(stream_class
->event_context_type
);
668 ret
= stream_class
->event_context_type
;
673 int bt_ctf_stream_class_set_event_context_type(
674 struct bt_ctf_stream_class
*stream_class
,
675 struct bt_ctf_field_type
*event_context_type
)
679 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
684 if (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 bt_get(event_context_type
);
693 stream_class
->event_context_type
= event_context_type
;
698 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
700 bt_get(stream_class
);
703 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
705 bt_put(stream_class
);
709 int get_event_class_count(void *element
)
711 return bt_ctf_stream_class_get_event_class_count(
712 (struct bt_ctf_stream_class
*) element
);
716 void *get_event_class(void *element
, int i
)
718 return bt_ctf_stream_class_get_event_class(
719 (struct bt_ctf_stream_class
*) element
, i
);
723 int visit_event_class(void *element
, bt_ctf_ir_visitor visitor
,void *data
)
725 struct bt_ctf_ir_element ir_element
=
726 { .element
= element
,
727 .type
= BT_CTF_IR_TYPE_EVENT_CLASS
};
729 return visitor(&ir_element
, data
);
732 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
733 bt_ctf_ir_visitor visitor
, void *data
)
736 struct bt_ctf_ir_element element
=
737 { .element
= stream_class
,
738 .type
= BT_CTF_IR_TYPE_STREAM_CLASS
};
740 if (!stream_class
|| !visitor
) {
745 ret
= visitor_helper(&element
, get_event_class_count
,
747 visit_event_class
, visitor
, data
);
753 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
759 stream_class
->frozen
= 1;
760 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
761 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
762 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
763 bt_ctf_clock_freeze(stream_class
->clock
);
767 void bt_ctf_stream_class_set_byte_order(
768 struct bt_ctf_stream_class
*stream_class
, int byte_order
)
772 assert(stream_class
);
773 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
774 stream_class
->byte_order
= byte_order
;
776 /* Set native byte order to little or big endian */
777 bt_ctf_field_type_set_native_byte_order(
778 stream_class
->event_header_type
, byte_order
);
779 bt_ctf_field_type_set_native_byte_order(
780 stream_class
->packet_context_type
, byte_order
);
781 bt_ctf_field_type_set_native_byte_order(
782 stream_class
->event_context_type
, byte_order
);
784 /* Set all events' native byte order */
785 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
786 struct bt_ctf_event_class
*event_class
=
787 g_ptr_array_index(stream_class
->event_classes
, i
);
789 bt_ctf_event_class_set_native_byte_order(event_class
,
795 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
796 struct metadata_context
*context
)
801 g_string_assign(context
->field_name
, "");
802 context
->current_indentation_level
= 1;
803 if (!stream_class
->id_set
) {
808 g_string_append_printf(context
->string
,
809 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
811 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
817 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
818 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
824 if (stream_class
->event_context_type
) {
825 g_string_append(context
->string
, ";\n\n\tevent.context := ");
826 ret
= bt_ctf_field_type_serialize(
827 stream_class
->event_context_type
, context
);
833 g_string_append(context
->string
, ";\n};\n\n");
834 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
835 struct bt_ctf_event_class
*event_class
=
836 stream_class
->event_classes
->pdata
[i
];
838 ret
= bt_ctf_event_class_serialize(event_class
, context
);
844 context
->current_indentation_level
= 0;
849 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
851 struct bt_ctf_stream_class
*stream_class
;
853 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
854 bt_put(stream_class
->clock
);
856 if (stream_class
->event_classes
) {
857 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
860 if (stream_class
->name
) {
861 g_string_free(stream_class
->name
, TRUE
);
864 bt_put(stream_class
->event_header_type
);
865 bt_put(stream_class
->packet_context_type
);
866 bt_put(stream_class
->event_context_type
);
867 g_free(stream_class
);
871 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
874 struct bt_ctf_field_type
*event_header_type
=
875 bt_ctf_field_type_structure_create();
876 struct bt_ctf_field_type
*_uint32_t
=
877 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
878 struct bt_ctf_field_type
*_uint64_t
=
879 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
881 if (!event_header_type
) {
886 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
892 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
893 _uint64_t
, "timestamp");
898 if (stream_class
->event_header_type
) {
899 bt_put(stream_class
->event_header_type
);
901 stream_class
->event_header_type
= event_header_type
;
904 bt_put(event_header_type
);
913 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
916 struct bt_ctf_field_type
*packet_context_type
=
917 bt_ctf_field_type_structure_create();
918 struct bt_ctf_field_type
*_uint64_t
=
919 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
921 if (!packet_context_type
) {
927 * We create a stream packet context as proposed in the CTF
930 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
931 _uint64_t
, "timestamp_begin");
936 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
937 _uint64_t
, "timestamp_end");
942 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
943 _uint64_t
, "content_size");
948 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
949 _uint64_t
, "packet_size");
954 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
955 _uint64_t
, "events_discarded");
960 bt_put(stream_class
->packet_context_type
);
961 stream_class
->packet_context_type
= packet_context_type
;
964 bt_put(packet_context_type
);