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-internal.h>
33 #include <babeltrace/ctf-ir/event-types-internal.h>
34 #include <babeltrace/ctf-ir/event-fields-internal.h>
35 #include <babeltrace/ctf-writer/stream.h>
36 #include <babeltrace/ctf-ir/stream-class-internal.h>
37 #include <babeltrace/ctf-ir/visitor-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/ctf-ir/utils.h>
40 #include <babeltrace/ctf-ir/ref.h>
41 #include <babeltrace/ctf-ir/common-internal.h>
42 #include <babeltrace/compiler.h>
43 #include <babeltrace/align.h>
46 void bt_ctf_stream_class_destroy(struct bt_ref
*ref
);
48 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
50 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
52 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
55 struct bt_ctf_stream_class
*stream_class
= NULL
;
57 if (name
&& bt_ctf_validate_identifier(name
)) {
61 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
66 stream_class
->name
= g_string_new(name
);
67 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
68 (GDestroyNotify
)bt_ctf_event_class_put
);
69 if (!stream_class
->event_classes
) {
73 ret
= init_event_header(stream_class
);
78 ret
= init_packet_context(stream_class
);
83 bt_ctf_base_init(stream_class
, bt_ctf_stream_class_destroy
);
87 bt_ctf_stream_class_destroy(&stream_class
->base
.ref_count
);
93 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
94 struct bt_ctf_stream_class
*stream_class
)
96 struct bt_ctf_trace
*trace
= NULL
;
102 trace
= stream_class
->trace
;
104 bt_ctf_trace_get(trace
);
110 const char *bt_ctf_stream_class_get_name(
111 struct bt_ctf_stream_class
*stream_class
)
113 const char *name
= NULL
;
119 name
= stream_class
->name
->str
;
124 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
129 if (!stream_class
|| stream_class
->frozen
) {
134 g_string_assign(stream_class
->name
, name
);
139 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
140 struct bt_ctf_stream_class
*stream_class
)
142 struct bt_ctf_clock
*clock
= NULL
;
144 if (!stream_class
|| !stream_class
->clock
) {
148 clock
= stream_class
->clock
;
149 bt_ctf_clock_get(clock
);
154 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
155 struct bt_ctf_clock
*clock
)
158 struct bt_ctf_field_type
*timestamp_field
= NULL
;
160 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
166 * Look for a "timestamp" field in the stream class' event header type
167 * and map the stream's clock to that field if no current mapping is
170 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
171 stream_class
->event_header_type
, "timestamp");
172 if (timestamp_field
) {
173 struct bt_ctf_clock
*mapped_clock
;
175 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
178 bt_ctf_clock_put(mapped_clock
);
182 ret
= bt_ctf_field_type_integer_set_mapped_clock(
183 timestamp_field
, clock
);
189 if (stream_class
->clock
) {
190 bt_ctf_clock_put(stream_class
->clock
);
193 stream_class
->clock
= clock
;
194 bt_ctf_clock_get(clock
);
196 if (timestamp_field
) {
197 bt_ctf_field_type_put(timestamp_field
);
202 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
206 if (!stream_class
|| !stream_class
->id_set
) {
211 ret
= (int64_t) stream_class
->id
;
217 int _bt_ctf_stream_class_set_id(
218 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
220 stream_class
->id
= id
;
221 stream_class
->id_set
= 1;
225 struct event_class_set_stream_id_data
{
231 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
233 struct event_class_set_stream_id_data
*typed_data
= data
;
235 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
236 typed_data
->stream_id
);
240 int bt_ctf_stream_class_set_id_no_check(
241 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
244 struct event_class_set_stream_id_data data
=
245 { .stream_id
= id
, .ret
= 0 };
248 * Make sure all event classes have their "stream_id" attribute
251 g_ptr_array_foreach(stream_class
->event_classes
,
252 event_class_set_stream_id
, &data
);
258 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
266 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
271 if (!stream_class
|| stream_class
->frozen
) {
276 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
282 void event_class_exists(gpointer element
, gpointer query
)
284 struct bt_ctf_event_class
*event_class_a
= element
;
285 struct search_query
*search_query
= query
;
286 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
289 if (search_query
->value
== element
) {
290 search_query
->found
= 1;
295 * Two event classes cannot share the same name in a given
298 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
299 bt_ctf_event_class_get_name(event_class_b
))) {
300 search_query
->found
= 1;
305 * Two event classes cannot share the same ID in a given
308 id_a
= bt_ctf_event_class_get_id(event_class_a
);
309 id_b
= bt_ctf_event_class_get_id(event_class_b
);
311 if (id_a
< 0 || id_b
< 0) {
312 /* at least one ID is not set: will be automatically set later */
317 search_query
->found
= 1;
325 int bt_ctf_stream_class_add_event_class(
326 struct bt_ctf_stream_class
*stream_class
,
327 struct bt_ctf_event_class
*event_class
)
332 if (!stream_class
|| !event_class
) {
337 /* Check for duplicate event classes */
338 struct search_query query
= { .value
= event_class
, .found
= 0 };
339 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
347 * Resolve the event's sequence length and variant tags if the
348 * stream is already associated with a trace. Otherwise, this
349 * validation will be performed once the stream is registered
352 if (stream_class
->trace
) {
353 ret
= bt_ctf_event_class_resolve_types(event_class
,
354 stream_class
->trace
, stream_class
);
360 /* Only set an event id if none was explicitly set before */
361 event_id
= bt_ctf_event_class_get_id(event_class
);
363 if (bt_ctf_event_class_set_id(event_class
,
364 stream_class
->next_event_id
++)) {
370 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
375 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
380 bt_ctf_event_class_get(event_class
);
381 g_ptr_array_add(stream_class
->event_classes
, event_class
);
382 bt_ctf_event_class_freeze(event_class
);
384 if (stream_class
->byte_order
) {
386 * Only set native byte order if it has been initialized
387 * when the stream class was added to a trace.
389 * If not set here, this will be set when the stream
390 * classe will be added to a trace.
392 bt_ctf_event_class_set_native_byte_order(event_class
,
393 stream_class
->byte_order
);
399 int bt_ctf_stream_class_get_event_class_count(
400 struct bt_ctf_stream_class
*stream_class
)
409 ret
= (int) stream_class
->event_classes
->len
;
414 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
415 struct bt_ctf_stream_class
*stream_class
, int index
)
417 struct bt_ctf_event_class
*event_class
= NULL
;
419 if (!stream_class
|| index
< 0 ||
420 index
>= stream_class
->event_classes
->len
) {
424 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
425 bt_ctf_event_class_get(event_class
);
430 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
431 struct bt_ctf_stream_class
*stream_class
, const char *name
)
434 struct bt_ctf_event_class
*event_class
= NULL
;
436 if (!stream_class
|| !name
) {
440 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
441 struct bt_ctf_event_class
*cur_event_class
=
442 g_ptr_array_index(stream_class
->event_classes
, i
);
443 const char *cur_event_class_name
=
444 bt_ctf_event_class_get_name(cur_event_class
);
446 if (!strcmp(name
, cur_event_class_name
)) {
447 event_class
= cur_event_class
;
448 bt_ctf_event_class_get(event_class
);
456 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
457 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
460 struct bt_ctf_event_class
*event_class
= NULL
;
466 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
467 struct bt_ctf_event_class
*current_event_class
=
468 g_ptr_array_index(stream_class
->event_classes
, i
);
470 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
471 event_class
= current_event_class
;
472 bt_ctf_event_class_get(event_class
);
480 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
481 struct bt_ctf_stream_class
*stream_class
)
483 struct bt_ctf_field_type
*ret
= NULL
;
489 assert(stream_class
->packet_context_type
);
490 bt_ctf_field_type_get(stream_class
->packet_context_type
);
491 ret
= stream_class
->packet_context_type
;
496 int bt_ctf_stream_class_set_packet_context_type(
497 struct bt_ctf_stream_class
*stream_class
,
498 struct bt_ctf_field_type
*packet_context_type
)
502 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
507 assert(stream_class
->packet_context_type
);
508 if (stream_class
->packet_context_type
== packet_context_type
) {
511 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
513 /* A packet context must be a structure */
518 bt_ctf_field_type_put(stream_class
->packet_context_type
);
519 bt_ctf_field_type_get(packet_context_type
);
520 stream_class
->packet_context_type
= packet_context_type
;
525 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
526 struct bt_ctf_stream_class
*stream_class
)
528 struct bt_ctf_field_type
*ret
= NULL
;
530 if (!stream_class
|| !stream_class
->event_header_type
) {
534 assert(stream_class
->event_header_type
);
535 bt_ctf_field_type_get(stream_class
->event_header_type
);
536 ret
= stream_class
->event_header_type
;
541 int bt_ctf_stream_class_set_event_header_type(
542 struct bt_ctf_stream_class
*stream_class
,
543 struct bt_ctf_field_type
*event_header_type
)
547 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
552 assert(stream_class
->event_header_type
);
553 if (stream_class
->event_header_type
== event_header_type
) {
556 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
558 /* An event header must be a structure */
563 bt_ctf_field_type_put(stream_class
->event_header_type
);
564 bt_ctf_field_type_get(event_header_type
);
565 stream_class
->event_header_type
= event_header_type
;
570 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
571 struct bt_ctf_stream_class
*stream_class
)
573 struct bt_ctf_field_type
*ret
= NULL
;
575 if (!stream_class
|| !stream_class
->event_context_type
) {
579 assert(stream_class
->event_context_type
);
580 bt_ctf_field_type_get(stream_class
->event_context_type
);
581 ret
= stream_class
->event_context_type
;
586 int bt_ctf_stream_class_set_event_context_type(
587 struct bt_ctf_stream_class
*stream_class
,
588 struct bt_ctf_field_type
*event_context_type
)
592 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
597 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
599 /* A packet context must be a structure */
604 bt_ctf_field_type_put(stream_class
->event_context_type
);
605 bt_ctf_field_type_get(event_context_type
);
606 stream_class
->event_context_type
= event_context_type
;
611 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
613 bt_ctf_get(stream_class
);
616 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
618 bt_ctf_put(stream_class
);
622 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
628 stream_class
->frozen
= 1;
629 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
630 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
631 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
632 bt_ctf_clock_freeze(stream_class
->clock
);
636 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
637 enum bt_ctf_byte_order byte_order
)
640 int internal_byte_order
;
642 /* Note that "NATIVE" means the trace's endianness, not the host's. */
643 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
644 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
649 switch (byte_order
) {
650 case BT_CTF_BYTE_ORDER_NETWORK
:
651 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
652 internal_byte_order
= BIG_ENDIAN
;
654 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
655 internal_byte_order
= LITTLE_ENDIAN
;
662 stream_class
->byte_order
= internal_byte_order
;
664 /* Set native byte order to little or big endian */
665 bt_ctf_field_type_set_native_byte_order(
666 stream_class
->event_header_type
, stream_class
->byte_order
);
667 bt_ctf_field_type_set_native_byte_order(
668 stream_class
->packet_context_type
, stream_class
->byte_order
);
669 bt_ctf_field_type_set_native_byte_order(
670 stream_class
->event_context_type
, stream_class
->byte_order
);
672 /* Set all events' native byte order */
673 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
674 bt_ctf_event_class_set_native_byte_order(
675 g_ptr_array_index(stream_class
->event_classes
, i
),
676 stream_class
->byte_order
);
677 bt_ctf_event_class_freeze(
678 g_ptr_array_index(stream_class
->event_classes
, i
));
685 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
686 struct metadata_context
*context
)
691 g_string_assign(context
->field_name
, "");
692 context
->current_indentation_level
= 1;
693 if (!stream_class
->id_set
) {
698 g_string_append_printf(context
->string
,
699 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
701 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
707 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
708 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
714 if (stream_class
->event_context_type
) {
715 g_string_append(context
->string
, ";\n\n\tevent.context := ");
716 ret
= bt_ctf_field_type_serialize(
717 stream_class
->event_context_type
, context
);
723 g_string_append(context
->string
, ";\n};\n\n");
724 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
725 struct bt_ctf_event_class
*event_class
=
726 stream_class
->event_classes
->pdata
[i
];
728 ret
= bt_ctf_event_class_serialize(event_class
, context
);
734 context
->current_indentation_level
= 0;
739 int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class
*stream_class
,
740 struct bt_ctf_trace
*trace
)
749 if (stream_class
->trace
&& trace
) {
750 /* Already attached to a trace */
755 stream_class
->trace
= trace
;
761 void bt_ctf_stream_class_destroy(struct bt_ref
*ref
)
763 struct bt_ctf_stream_class
*stream_class
;
764 struct bt_ctf_base
*base
;
770 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
771 stream_class
= container_of(base
, struct bt_ctf_stream_class
, base
);
772 bt_ctf_clock_put(stream_class
->clock
);
774 if (stream_class
->event_classes
) {
777 /* Unregister this stream class from the event classes */
778 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
779 struct bt_ctf_event_class
*event_class
=
780 g_ptr_array_index(stream_class
->event_classes
,
783 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
786 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
789 if (stream_class
->name
) {
790 g_string_free(stream_class
->name
, TRUE
);
793 bt_ctf_field_type_put(stream_class
->event_header_type
);
794 bt_ctf_field_type_put(stream_class
->packet_context_type
);
795 if (stream_class
->event_context_type
) {
796 bt_ctf_field_type_put(stream_class
->event_context_type
);
798 g_free(stream_class
);
802 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
805 struct bt_ctf_field_type
*event_header_type
=
806 bt_ctf_field_type_structure_create();
807 struct bt_ctf_field_type
*_uint32_t
=
808 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
809 struct bt_ctf_field_type
*_uint64_t
=
810 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
812 if (!event_header_type
) {
817 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
823 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
824 _uint64_t
, "timestamp");
829 if (stream_class
->event_header_type
) {
830 bt_ctf_field_type_put(stream_class
->event_header_type
);
832 stream_class
->event_header_type
= event_header_type
;
835 bt_ctf_field_type_put(event_header_type
);
838 bt_ctf_field_type_put(_uint32_t
);
839 bt_ctf_field_type_put(_uint64_t
);
844 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
847 struct bt_ctf_field_type
*packet_context_type
=
848 bt_ctf_field_type_structure_create();
849 struct bt_ctf_field_type
*_uint64_t
=
850 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
852 if (!packet_context_type
) {
858 * We create a stream packet context as proposed in the CTF
861 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
862 _uint64_t
, "timestamp_begin");
867 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
868 _uint64_t
, "timestamp_end");
873 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
874 _uint64_t
, "content_size");
879 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
880 _uint64_t
, "packet_size");
885 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
886 _uint64_t
, "events_discarded");
891 if (stream_class
->packet_context_type
) {
892 bt_ctf_field_type_put(stream_class
->packet_context_type
);
894 stream_class
->packet_context_type
= packet_context_type
;
897 bt_ctf_field_type_put(packet_context_type
);
901 bt_ctf_field_type_put(_uint64_t
);