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.h>
45 #include <babeltrace/align.h>
46 #include <babeltrace/endian.h>
49 void bt_ctf_stream_class_destroy(struct bt_object
*obj
);
51 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
53 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
55 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
58 struct bt_ctf_stream_class
*stream_class
= NULL
;
60 if (name
&& bt_ctf_validate_identifier(name
)) {
64 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
69 stream_class
->name
= g_string_new(name
);
70 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
71 (GDestroyNotify
) bt_object_release
);
72 if (!stream_class
->event_classes
) {
76 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
77 g_int64_equal
, g_free
, NULL
);
79 ret
= init_event_header(stream_class
);
84 ret
= init_packet_context(stream_class
);
89 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
97 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
98 struct bt_ctf_stream_class
*stream_class
)
100 return (struct bt_ctf_trace
*) bt_object_get_parent(
104 const char *bt_ctf_stream_class_get_name(
105 struct bt_ctf_stream_class
*stream_class
)
107 const char *name
= NULL
;
113 name
= stream_class
->name
->str
;
118 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
123 if (!stream_class
|| stream_class
->frozen
) {
128 g_string_assign(stream_class
->name
, name
);
133 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
134 struct bt_ctf_stream_class
*stream_class
)
136 struct bt_ctf_clock
*clock
= NULL
;
138 if (!stream_class
|| !stream_class
->clock
) {
142 clock
= bt_get(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
|| !clock
|| stream_class
->frozen
) {
159 * Look for a "timestamp" integer field type in the stream
160 * class's event header field type and map the stream class's
161 * clock's class to that field type if there's no current
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_class
*mapped_clock_class
=
168 bt_ctf_field_type_integer_get_mapped_clock_class(
171 if (!mapped_clock_class
) {
172 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(
173 timestamp_field
, clock
->clock_class
);
179 BT_PUT(mapped_clock_class
);
182 /* Replace the current clock of this stream class. */
183 bt_put(stream_class
->clock
);
184 stream_class
->clock
= bt_get(clock
);
187 bt_put(timestamp_field
);
191 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
195 if (!stream_class
|| !stream_class
->id_set
) {
200 ret
= (int64_t) stream_class
->id
;
206 int _bt_ctf_stream_class_set_id(
207 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
209 stream_class
->id
= id
;
210 stream_class
->id_set
= 1;
214 struct event_class_set_stream_id_data
{
220 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
222 struct event_class_set_stream_id_data
*typed_data
= data
;
224 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
225 typed_data
->stream_id
);
229 int bt_ctf_stream_class_set_id_no_check(
230 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
233 struct event_class_set_stream_id_data data
=
234 { .stream_id
= id
, .ret
= 0 };
237 * Make sure all event classes have their "stream_id" attribute
240 g_ptr_array_foreach(stream_class
->event_classes
,
241 event_class_set_stream_id
, &data
);
247 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
255 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
260 if (!stream_class
|| stream_class
->frozen
) {
265 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
271 void event_class_exists(gpointer element
, gpointer query
)
273 struct bt_ctf_event_class
*event_class_a
= element
;
274 struct search_query
*search_query
= query
;
275 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
278 if (search_query
->value
== element
) {
279 search_query
->found
= 1;
284 * Two event classes cannot share the same name in a given
287 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
288 bt_ctf_event_class_get_name(event_class_b
))) {
289 search_query
->found
= 1;
294 * Two event classes cannot share the same ID in a given
297 id_a
= bt_ctf_event_class_get_id(event_class_a
);
298 id_b
= bt_ctf_event_class_get_id(event_class_b
);
300 if (id_a
< 0 || id_b
< 0) {
301 /* at least one ID is not set: will be automatically set later */
306 search_query
->found
= 1;
314 int bt_ctf_stream_class_add_event_class(
315 struct bt_ctf_stream_class
*stream_class
,
316 struct bt_ctf_event_class
*event_class
)
319 int64_t *event_id
= NULL
;
320 struct bt_ctf_trace
*trace
= NULL
;
321 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
322 struct bt_ctf_validation_output validation_output
= { 0 };
323 struct bt_ctf_field_type
*packet_header_type
= NULL
;
324 struct bt_ctf_field_type
*packet_context_type
= NULL
;
325 struct bt_ctf_field_type
*event_header_type
= NULL
;
326 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
327 struct bt_ctf_field_type
*event_context_type
= NULL
;
328 struct bt_ctf_field_type
*event_payload_type
= NULL
;
329 const enum bt_ctf_validation_flag validation_flags
=
330 BT_CTF_VALIDATION_FLAG_EVENT
;
332 if (!stream_class
|| !event_class
) {
337 event_id
= g_new(int64_t, 1);
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
,
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. */
359 trace
= bt_ctf_stream_class_get_trace(stream_class
);
362 * If the stream class is associated with a trace, then
363 * both those objects are frozen. Also, this event class
364 * is about to be frozen.
366 * Therefore the event class must be validated here.
367 * The trace and stream class should be valid at this
370 assert(trace
->valid
);
371 assert(stream_class
->valid
);
373 bt_ctf_trace_get_packet_header_type(trace
);
374 packet_context_type
=
375 bt_ctf_stream_class_get_packet_context_type(
378 bt_ctf_stream_class_get_event_header_type(stream_class
);
379 stream_event_ctx_type
=
380 bt_ctf_stream_class_get_event_context_type(
383 bt_ctf_event_class_get_context_type(event_class
);
385 bt_ctf_event_class_get_payload_type(event_class
);
386 ret
= bt_ctf_validate_class_types(
387 trace
->environment
, packet_header_type
,
388 packet_context_type
, event_header_type
,
389 stream_event_ctx_type
, event_context_type
,
390 event_payload_type
, trace
->valid
,
391 stream_class
->valid
, event_class
->valid
,
392 &validation_output
, validation_flags
);
393 BT_PUT(packet_header_type
);
394 BT_PUT(packet_context_type
);
395 BT_PUT(event_header_type
);
396 BT_PUT(stream_event_ctx_type
);
397 BT_PUT(event_context_type
);
398 BT_PUT(event_payload_type
);
402 * This means something went wrong during the
403 * validation process, not that the objects are
409 if ((validation_output
.valid_flags
& validation_flags
) !=
411 /* Invalid event class */
417 /* Only set an event ID if none was explicitly set before */
418 *event_id
= bt_ctf_event_class_get_id(event_class
);
420 if (bt_ctf_event_class_set_id(event_class
,
421 stream_class
->next_event_id
++)) {
425 *event_id
= stream_class
->next_event_id
;
428 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
433 bt_object_set_parent(event_class
, stream_class
);
437 * At this point we know that the function will be
438 * successful. Therefore we can replace the event
439 * class's field types with what's in the validation
440 * output structure and mark this event class as valid.
442 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
443 &validation_output
, validation_flags
);
444 event_class
->valid
= 1;
447 * Put what was not moved in
448 * bt_ctf_validation_replace_types().
450 bt_ctf_validation_output_put_types(&validation_output
);
453 /* Add to the event classes of the stream class */
454 g_ptr_array_add(stream_class
->event_classes
, event_class
);
455 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
459 /* Freeze the event class */
460 bt_ctf_event_class_freeze(event_class
);
462 if (stream_class
->byte_order
) {
464 * Only set native byte order if it has been initialized
465 * when the stream class was added to a trace.
467 * If not set here, this will be set when the stream
468 * class is added to a trace.
470 bt_ctf_event_class_set_native_byte_order(event_class
,
471 stream_class
->byte_order
);
474 /* Notifiy listeners of the trace's schema modification. */
476 struct bt_ctf_object obj
= { .object
= event_class
,
477 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
479 (void) bt_ctf_trace_object_modification(&obj
, trace
);
483 BT_PUT(old_stream_class
);
484 bt_ctf_validation_output_put_types(&validation_output
);
485 assert(!packet_header_type
);
486 assert(!packet_context_type
);
487 assert(!event_header_type
);
488 assert(!stream_event_ctx_type
);
489 assert(!event_context_type
);
490 assert(!event_payload_type
);
496 int bt_ctf_stream_class_get_event_class_count(
497 struct bt_ctf_stream_class
*stream_class
)
506 ret
= (int) stream_class
->event_classes
->len
;
511 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
512 struct bt_ctf_stream_class
*stream_class
, int index
)
514 struct bt_ctf_event_class
*event_class
= NULL
;
516 if (!stream_class
|| index
< 0 ||
517 index
>= stream_class
->event_classes
->len
) {
521 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
527 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
528 struct bt_ctf_stream_class
*stream_class
, const char *name
)
531 struct bt_ctf_event_class
*event_class
= NULL
;
533 if (!stream_class
|| !name
) {
537 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
538 struct bt_ctf_event_class
*cur_event_class
=
539 g_ptr_array_index(stream_class
->event_classes
, i
);
540 const char *cur_event_class_name
=
541 bt_ctf_event_class_get_name(cur_event_class
);
543 if (!strcmp(name
, cur_event_class_name
)) {
544 event_class
= cur_event_class
;
553 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
554 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
557 struct bt_ctf_event_class
*event_class
= NULL
;
563 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
570 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
571 struct bt_ctf_stream_class
*stream_class
)
573 struct bt_ctf_field_type
*ret
= NULL
;
579 bt_get(stream_class
->packet_context_type
);
580 ret
= stream_class
->packet_context_type
;
585 int bt_ctf_stream_class_set_packet_context_type(
586 struct bt_ctf_stream_class
*stream_class
,
587 struct bt_ctf_field_type
*packet_context_type
)
591 if (!stream_class
|| stream_class
->frozen
) {
596 if (packet_context_type
&&
597 bt_ctf_field_type_get_type_id(packet_context_type
) !=
598 BT_CTF_TYPE_ID_STRUCT
) {
599 /* A packet context must be a structure. */
604 bt_put(stream_class
->packet_context_type
);
605 bt_get(packet_context_type
);
606 stream_class
->packet_context_type
= packet_context_type
;
611 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
612 struct bt_ctf_stream_class
*stream_class
)
614 struct bt_ctf_field_type
*ret
= NULL
;
616 if (!stream_class
|| !stream_class
->event_header_type
) {
620 bt_get(stream_class
->event_header_type
);
621 ret
= stream_class
->event_header_type
;
626 int bt_ctf_stream_class_set_event_header_type(
627 struct bt_ctf_stream_class
*stream_class
,
628 struct bt_ctf_field_type
*event_header_type
)
632 if (!stream_class
|| stream_class
->frozen
) {
637 if (event_header_type
&&
638 bt_ctf_field_type_get_type_id(event_header_type
) !=
639 BT_CTF_TYPE_ID_STRUCT
) {
640 /* An event header must be a structure. */
645 bt_put(stream_class
->event_header_type
);
646 stream_class
->event_header_type
= bt_get(event_header_type
);
651 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
652 struct bt_ctf_stream_class
*stream_class
)
654 struct bt_ctf_field_type
*ret
= NULL
;
656 if (!stream_class
|| !stream_class
->event_context_type
) {
660 bt_get(stream_class
->event_context_type
);
661 ret
= stream_class
->event_context_type
;
666 int bt_ctf_stream_class_set_event_context_type(
667 struct bt_ctf_stream_class
*stream_class
,
668 struct bt_ctf_field_type
*event_context_type
)
672 if (!stream_class
|| stream_class
->frozen
) {
677 if (event_context_type
&&
678 bt_ctf_field_type_get_type_id(event_context_type
) !=
679 BT_CTF_TYPE_ID_STRUCT
) {
680 /* A packet context must be a structure. */
685 bt_put(stream_class
->event_context_type
);
686 stream_class
->event_context_type
= bt_get(event_context_type
);
691 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
693 bt_get(stream_class
);
696 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
698 bt_put(stream_class
);
702 int get_event_class_count(void *element
)
704 return bt_ctf_stream_class_get_event_class_count(
705 (struct bt_ctf_stream_class
*) element
);
709 void *get_event_class(void *element
, int i
)
711 return bt_ctf_stream_class_get_event_class(
712 (struct bt_ctf_stream_class
*) element
, i
);
716 int visit_event_class(void *object
, bt_ctf_visitor visitor
,void *data
)
718 struct bt_ctf_object obj
=
720 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
722 return visitor(&obj
, data
);
725 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
726 bt_ctf_visitor visitor
, void *data
)
729 struct bt_ctf_object obj
=
730 { .object
= stream_class
,
731 .type
= BT_CTF_OBJECT_TYPE_STREAM_CLASS
};
733 if (!stream_class
|| !visitor
) {
738 ret
= visitor_helper(&obj
, get_event_class_count
,
740 visit_event_class
, visitor
, data
);
746 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
752 stream_class
->frozen
= 1;
753 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
754 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
755 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
757 if (stream_class
->clock
) {
758 bt_ctf_clock_class_freeze(stream_class
->clock
->clock_class
);
763 void bt_ctf_stream_class_set_byte_order(
764 struct bt_ctf_stream_class
*stream_class
, int byte_order
)
768 assert(stream_class
);
769 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
770 stream_class
->byte_order
= byte_order
;
772 /* Set native byte order to little or big endian */
773 bt_ctf_field_type_set_native_byte_order(
774 stream_class
->event_header_type
, byte_order
);
775 bt_ctf_field_type_set_native_byte_order(
776 stream_class
->packet_context_type
, byte_order
);
777 bt_ctf_field_type_set_native_byte_order(
778 stream_class
->event_context_type
, byte_order
);
780 /* Set all events' native byte order */
781 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
782 struct bt_ctf_event_class
*event_class
=
783 g_ptr_array_index(stream_class
->event_classes
, i
);
785 bt_ctf_event_class_set_native_byte_order(event_class
,
791 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
792 struct metadata_context
*context
)
797 g_string_assign(context
->field_name
, "");
798 context
->current_indentation_level
= 1;
799 if (!stream_class
->id_set
) {
804 g_string_append_printf(context
->string
,
805 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
807 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
813 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
814 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
820 if (stream_class
->event_context_type
) {
821 g_string_append(context
->string
, ";\n\n\tevent.context := ");
822 ret
= bt_ctf_field_type_serialize(
823 stream_class
->event_context_type
, context
);
829 g_string_append(context
->string
, ";\n};\n\n");
830 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
831 struct bt_ctf_event_class
*event_class
=
832 stream_class
->event_classes
->pdata
[i
];
834 ret
= bt_ctf_event_class_serialize(event_class
, context
);
840 context
->current_indentation_level
= 0;
845 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
847 struct bt_ctf_stream_class
*stream_class
;
849 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
850 bt_put(stream_class
->clock
);
852 if (stream_class
->event_classes_ht
) {
853 g_hash_table_destroy(stream_class
->event_classes_ht
);
855 if (stream_class
->event_classes
) {
856 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
859 if (stream_class
->name
) {
860 g_string_free(stream_class
->name
, TRUE
);
863 bt_put(stream_class
->event_header_type
);
864 bt_put(stream_class
->packet_context_type
);
865 bt_put(stream_class
->event_context_type
);
866 g_free(stream_class
);
870 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
873 struct bt_ctf_field_type
*event_header_type
=
874 bt_ctf_field_type_structure_create();
875 struct bt_ctf_field_type
*_uint32_t
=
876 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
877 struct bt_ctf_field_type
*_uint64_t
=
878 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
880 if (!event_header_type
) {
885 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
891 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
892 _uint64_t
, "timestamp");
897 if (stream_class
->event_header_type
) {
898 bt_put(stream_class
->event_header_type
);
900 stream_class
->event_header_type
= event_header_type
;
903 bt_put(event_header_type
);
912 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
915 struct bt_ctf_field_type
*packet_context_type
=
916 bt_ctf_field_type_structure_create();
917 struct bt_ctf_field_type
*_uint64_t
=
918 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
920 if (!packet_context_type
) {
926 * We create a stream packet context as proposed in the CTF
929 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
930 _uint64_t
, "timestamp_begin");
935 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
936 _uint64_t
, "timestamp_end");
941 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
942 _uint64_t
, "content_size");
947 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
948 _uint64_t
, "packet_size");
953 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
954 _uint64_t
, "events_discarded");
959 bt_put(stream_class
->packet_context_type
);
960 stream_class
->packet_context_type
= packet_context_type
;
963 bt_put(packet_context_type
);