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/compiler.h>
41 #include <babeltrace/align.h>
44 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
);
46 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
48 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
50 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
53 struct bt_ctf_stream_class
*stream_class
= NULL
;
55 if (name
&& bt_ctf_validate_identifier(name
)) {
59 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
64 stream_class
->name
= g_string_new(name
);
65 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
66 (GDestroyNotify
)bt_ctf_event_class_put
);
67 if (!stream_class
->event_classes
) {
71 ret
= init_event_header(stream_class
);
76 ret
= init_packet_context(stream_class
);
81 bt_ctf_ref_init(&stream_class
->ref_count
);
85 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
91 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
92 struct bt_ctf_stream_class
*stream_class
)
94 struct bt_ctf_trace
*trace
= NULL
;
100 trace
= stream_class
->trace
;
102 bt_ctf_trace_get(trace
);
108 const char *bt_ctf_stream_class_get_name(
109 struct bt_ctf_stream_class
*stream_class
)
111 const char *name
= NULL
;
117 name
= stream_class
->name
->str
;
122 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
127 if (!stream_class
|| stream_class
->frozen
) {
132 g_string_assign(stream_class
->name
, name
);
137 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
138 struct bt_ctf_stream_class
*stream_class
)
140 struct bt_ctf_clock
*clock
= NULL
;
142 if (!stream_class
|| !stream_class
->clock
) {
146 clock
= stream_class
->clock
;
147 bt_ctf_clock_get(clock
);
152 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
153 struct bt_ctf_clock
*clock
)
156 struct bt_ctf_field_type
*timestamp_field
= NULL
;
158 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
164 * Look for a "timestamp" field in the stream class' event header type
165 * and map the stream's clock to that field if no current mapping is
168 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
169 stream_class
->event_header_type
, "timestamp");
170 if (timestamp_field
) {
171 struct bt_ctf_clock
*mapped_clock
;
173 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
176 bt_ctf_clock_put(mapped_clock
);
180 ret
= bt_ctf_field_type_integer_set_mapped_clock(
181 timestamp_field
, clock
);
187 if (stream_class
->clock
) {
188 bt_ctf_clock_put(stream_class
->clock
);
191 stream_class
->clock
= clock
;
192 bt_ctf_clock_get(clock
);
194 if (timestamp_field
) {
195 bt_ctf_field_type_put(timestamp_field
);
200 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
204 if (!stream_class
|| !stream_class
->id_set
) {
209 ret
= (int64_t) stream_class
->id
;
215 int _bt_ctf_stream_class_set_id(
216 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
218 stream_class
->id
= id
;
219 stream_class
->id_set
= 1;
223 struct event_class_set_stream_id_data
{
229 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
231 struct event_class_set_stream_id_data
*typed_data
= data
;
233 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
234 typed_data
->stream_id
);
238 int bt_ctf_stream_class_set_id_no_check(
239 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
242 struct event_class_set_stream_id_data data
=
243 { .stream_id
= id
, .ret
= 0 };
246 * Make sure all event classes have their "stream_id" attribute
249 g_ptr_array_foreach(stream_class
->event_classes
,
250 event_class_set_stream_id
, &data
);
256 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
264 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
269 if (!stream_class
|| stream_class
->frozen
) {
274 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
280 void event_class_exists(gpointer element
, gpointer query
)
282 struct bt_ctf_event_class
*event_class_a
= element
;
283 struct search_query
*search_query
= query
;
284 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
287 if (search_query
->value
== element
) {
288 search_query
->found
= 1;
293 * Two event classes cannot share the same name in a given
296 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
297 bt_ctf_event_class_get_name(event_class_b
))) {
298 search_query
->found
= 1;
303 * Two event classes cannot share the same ID in a given
306 id_a
= bt_ctf_event_class_get_id(event_class_a
);
307 id_b
= bt_ctf_event_class_get_id(event_class_b
);
309 if (id_a
< 0 || id_b
< 0) {
310 /* at least one ID is not set: will be automatically set later */
315 search_query
->found
= 1;
323 int bt_ctf_stream_class_add_event_class(
324 struct bt_ctf_stream_class
*stream_class
,
325 struct bt_ctf_event_class
*event_class
)
330 if (!stream_class
|| !event_class
) {
335 /* Check for duplicate event classes */
336 struct search_query query
= { .value
= event_class
, .found
= 0 };
337 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
345 * Resolve the event's sequence length and variant tags if the
346 * stream is already associated with a trace. Otherwise, this
347 * validation will be performed once the stream is registered
350 if (stream_class
->trace
) {
351 ret
= bt_ctf_event_class_resolve_types(event_class
,
352 stream_class
->trace
, stream_class
);
358 /* Only set an event id if none was explicitly set before */
359 event_id
= bt_ctf_event_class_get_id(event_class
);
361 if (bt_ctf_event_class_set_id(event_class
,
362 stream_class
->next_event_id
++)) {
368 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
373 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
378 bt_ctf_event_class_get(event_class
);
379 g_ptr_array_add(stream_class
->event_classes
, event_class
);
380 bt_ctf_event_class_freeze(event_class
);
382 if (stream_class
->byte_order
) {
384 * Only set native byte order if it has been initialized
385 * when the stream class was added to a trace.
387 * If not set here, this will be set when the stream
388 * classe will be added to a trace.
390 bt_ctf_event_class_set_native_byte_order(event_class
,
391 stream_class
->byte_order
);
397 int bt_ctf_stream_class_get_event_class_count(
398 struct bt_ctf_stream_class
*stream_class
)
407 ret
= (int) stream_class
->event_classes
->len
;
412 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
413 struct bt_ctf_stream_class
*stream_class
, int index
)
415 struct bt_ctf_event_class
*event_class
= NULL
;
417 if (!stream_class
|| index
< 0 ||
418 index
>= stream_class
->event_classes
->len
) {
422 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
423 bt_ctf_event_class_get(event_class
);
428 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
429 struct bt_ctf_stream_class
*stream_class
, const char *name
)
432 struct bt_ctf_event_class
*event_class
= NULL
;
434 if (!stream_class
|| !name
) {
438 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
439 struct bt_ctf_event_class
*cur_event_class
=
440 g_ptr_array_index(stream_class
->event_classes
, i
);
441 const char *cur_event_class_name
=
442 bt_ctf_event_class_get_name(cur_event_class
);
444 if (!strcmp(name
, cur_event_class_name
)) {
445 event_class
= cur_event_class
;
446 bt_ctf_event_class_get(event_class
);
454 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
455 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
458 struct bt_ctf_event_class
*event_class
= NULL
;
464 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
465 struct bt_ctf_event_class
*current_event_class
=
466 g_ptr_array_index(stream_class
->event_classes
, i
);
468 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
469 event_class
= current_event_class
;
470 bt_ctf_event_class_get(event_class
);
478 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
479 struct bt_ctf_stream_class
*stream_class
)
481 struct bt_ctf_field_type
*ret
= NULL
;
487 assert(stream_class
->packet_context_type
);
488 bt_ctf_field_type_get(stream_class
->packet_context_type
);
489 ret
= stream_class
->packet_context_type
;
494 int bt_ctf_stream_class_set_packet_context_type(
495 struct bt_ctf_stream_class
*stream_class
,
496 struct bt_ctf_field_type
*packet_context_type
)
500 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
505 assert(stream_class
->packet_context_type
);
506 if (stream_class
->packet_context_type
== packet_context_type
) {
509 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
511 /* A packet context must be a structure */
516 bt_ctf_field_type_put(stream_class
->packet_context_type
);
517 bt_ctf_field_type_get(packet_context_type
);
518 stream_class
->packet_context_type
= packet_context_type
;
523 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
524 struct bt_ctf_stream_class
*stream_class
)
526 struct bt_ctf_field_type
*ret
= NULL
;
528 if (!stream_class
|| !stream_class
->event_header_type
) {
532 assert(stream_class
->event_header_type
);
533 bt_ctf_field_type_get(stream_class
->event_header_type
);
534 ret
= stream_class
->event_header_type
;
539 int bt_ctf_stream_class_set_event_header_type(
540 struct bt_ctf_stream_class
*stream_class
,
541 struct bt_ctf_field_type
*event_header_type
)
545 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
550 assert(stream_class
->event_header_type
);
551 if (stream_class
->event_header_type
== event_header_type
) {
554 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
556 /* An event header must be a structure */
561 bt_ctf_field_type_put(stream_class
->event_header_type
);
562 bt_ctf_field_type_get(event_header_type
);
563 stream_class
->event_header_type
= event_header_type
;
568 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
569 struct bt_ctf_stream_class
*stream_class
)
571 struct bt_ctf_field_type
*ret
= NULL
;
573 if (!stream_class
|| !stream_class
->event_context_type
) {
577 assert(stream_class
->event_context_type
);
578 bt_ctf_field_type_get(stream_class
->event_context_type
);
579 ret
= stream_class
->event_context_type
;
584 int bt_ctf_stream_class_set_event_context_type(
585 struct bt_ctf_stream_class
*stream_class
,
586 struct bt_ctf_field_type
*event_context_type
)
590 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
595 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
597 /* A packet context must be a structure */
602 bt_ctf_field_type_put(stream_class
->event_context_type
);
603 bt_ctf_field_type_get(event_context_type
);
604 stream_class
->event_context_type
= event_context_type
;
609 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
615 bt_ctf_ref_get(&stream_class
->ref_count
);
618 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
624 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
628 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
634 stream_class
->frozen
= 1;
635 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
636 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
637 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
638 bt_ctf_clock_freeze(stream_class
->clock
);
642 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
643 enum bt_ctf_byte_order byte_order
)
646 int internal_byte_order
;
648 /* Note that "NATIVE" means the trace's endianness, not the host's. */
649 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
650 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
655 switch (byte_order
) {
656 case BT_CTF_BYTE_ORDER_NETWORK
:
657 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
658 internal_byte_order
= BIG_ENDIAN
;
660 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
661 internal_byte_order
= LITTLE_ENDIAN
;
668 stream_class
->byte_order
= internal_byte_order
;
670 /* Set native byte order to little or big endian */
671 bt_ctf_field_type_set_native_byte_order(
672 stream_class
->event_header_type
, stream_class
->byte_order
);
673 bt_ctf_field_type_set_native_byte_order(
674 stream_class
->packet_context_type
, stream_class
->byte_order
);
675 bt_ctf_field_type_set_native_byte_order(
676 stream_class
->event_context_type
, stream_class
->byte_order
);
678 /* Set all events' native byte order */
679 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
680 bt_ctf_event_class_set_native_byte_order(
681 g_ptr_array_index(stream_class
->event_classes
, i
),
682 stream_class
->byte_order
);
683 bt_ctf_event_class_freeze(
684 g_ptr_array_index(stream_class
->event_classes
, i
));
691 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
692 struct metadata_context
*context
)
697 g_string_assign(context
->field_name
, "");
698 context
->current_indentation_level
= 1;
699 if (!stream_class
->id_set
) {
704 g_string_append_printf(context
->string
,
705 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
707 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
713 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
714 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
720 if (stream_class
->event_context_type
) {
721 g_string_append(context
->string
, ";\n\n\tevent.context := ");
722 ret
= bt_ctf_field_type_serialize(
723 stream_class
->event_context_type
, context
);
729 g_string_append(context
->string
, ";\n};\n\n");
730 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
731 struct bt_ctf_event_class
*event_class
=
732 stream_class
->event_classes
->pdata
[i
];
734 ret
= bt_ctf_event_class_serialize(event_class
, context
);
740 context
->current_indentation_level
= 0;
745 int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class
*stream_class
,
746 struct bt_ctf_trace
*trace
)
755 if (stream_class
->trace
&& trace
) {
756 /* Already attached to a trace */
761 stream_class
->trace
= trace
;
767 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
769 struct bt_ctf_stream_class
*stream_class
;
775 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
776 bt_ctf_clock_put(stream_class
->clock
);
778 if (stream_class
->event_classes
) {
781 /* Unregister this stream class from the event classes */
782 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
783 struct bt_ctf_event_class
*event_class
=
784 g_ptr_array_index(stream_class
->event_classes
,
787 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
790 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
793 if (stream_class
->name
) {
794 g_string_free(stream_class
->name
, TRUE
);
797 bt_ctf_field_type_put(stream_class
->event_header_type
);
798 bt_ctf_field_type_put(stream_class
->packet_context_type
);
799 if (stream_class
->event_context_type
) {
800 bt_ctf_field_type_put(stream_class
->event_context_type
);
802 g_free(stream_class
);
806 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
809 struct bt_ctf_field_type
*event_header_type
=
810 bt_ctf_field_type_structure_create();
811 struct bt_ctf_field_type
*_uint32_t
=
812 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
813 struct bt_ctf_field_type
*_uint64_t
=
814 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
816 if (!event_header_type
) {
821 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
827 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
828 _uint64_t
, "timestamp");
833 if (stream_class
->event_header_type
) {
834 bt_ctf_field_type_put(stream_class
->event_header_type
);
836 stream_class
->event_header_type
= event_header_type
;
839 bt_ctf_field_type_put(event_header_type
);
842 bt_ctf_field_type_put(_uint32_t
);
843 bt_ctf_field_type_put(_uint64_t
);
848 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
851 struct bt_ctf_field_type
*packet_context_type
=
852 bt_ctf_field_type_structure_create();
853 struct bt_ctf_field_type
*_uint64_t
=
854 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
856 if (!packet_context_type
) {
862 * We create a stream packet context as proposed in the CTF
865 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
866 _uint64_t
, "timestamp_begin");
871 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
872 _uint64_t
, "timestamp_end");
877 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
878 _uint64_t
, "content_size");
883 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
884 _uint64_t
, "packet_size");
889 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
890 _uint64_t
, "events_discarded");
895 if (stream_class
->packet_context_type
) {
896 bt_ctf_field_type_put(stream_class
->packet_context_type
);
898 stream_class
->packet_context_type
= packet_context_type
;
901 bt_ctf_field_type_put(packet_context_type
);
905 bt_ctf_field_type_put(_uint64_t
);