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/ref.h>
41 #include <babeltrace/compiler.h>
42 #include <babeltrace/align.h>
43 #include <babeltrace/endian.h>
46 void bt_ctf_stream_class_destroy(struct bt_object
*obj
);
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_put
);
69 if (!stream_class
->event_classes
) {
73 ret
= init_event_header(stream_class
);
78 ret
= init_packet_context(stream_class
);
83 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
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
;
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
;
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_put(mapped_clock
);
180 ret
= bt_ctf_field_type_integer_set_mapped_clock(
181 timestamp_field
, clock
);
187 if (stream_class
->clock
) {
188 bt_put(stream_class
->clock
);
191 stream_class
->clock
= clock
;
194 if (timestamp_field
) {
195 bt_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
);
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
);
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
;
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
;
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_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_put(stream_class
->packet_context_type
);
517 bt_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_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_put(stream_class
->event_header_type
);
562 bt_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_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_put(stream_class
->event_context_type
);
603 bt_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
)
611 bt_get(stream_class
);
614 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
616 bt_put(stream_class
);
620 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
626 stream_class
->frozen
= 1;
627 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
628 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
629 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
630 bt_ctf_clock_freeze(stream_class
->clock
);
634 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
635 enum bt_ctf_byte_order byte_order
)
638 int internal_byte_order
;
640 /* Note that "NATIVE" means the trace's endianness, not the host's. */
641 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
642 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
647 switch (byte_order
) {
648 case BT_CTF_BYTE_ORDER_NETWORK
:
649 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
650 internal_byte_order
= BIG_ENDIAN
;
652 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
653 internal_byte_order
= LITTLE_ENDIAN
;
660 stream_class
->byte_order
= internal_byte_order
;
662 /* Set native byte order to little or big endian */
663 bt_ctf_field_type_set_native_byte_order(
664 stream_class
->event_header_type
, stream_class
->byte_order
);
665 bt_ctf_field_type_set_native_byte_order(
666 stream_class
->packet_context_type
, stream_class
->byte_order
);
667 bt_ctf_field_type_set_native_byte_order(
668 stream_class
->event_context_type
, stream_class
->byte_order
);
670 /* Set all events' native byte order */
671 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
672 bt_ctf_event_class_set_native_byte_order(
673 g_ptr_array_index(stream_class
->event_classes
, i
),
674 stream_class
->byte_order
);
675 bt_ctf_event_class_freeze(
676 g_ptr_array_index(stream_class
->event_classes
, i
));
683 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
684 struct metadata_context
*context
)
689 g_string_assign(context
->field_name
, "");
690 context
->current_indentation_level
= 1;
691 if (!stream_class
->id_set
) {
696 g_string_append_printf(context
->string
,
697 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
699 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
705 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
706 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
712 if (stream_class
->event_context_type
) {
713 g_string_append(context
->string
, ";\n\n\tevent.context := ");
714 ret
= bt_ctf_field_type_serialize(
715 stream_class
->event_context_type
, context
);
721 g_string_append(context
->string
, ";\n};\n\n");
722 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
723 struct bt_ctf_event_class
*event_class
=
724 stream_class
->event_classes
->pdata
[i
];
726 ret
= bt_ctf_event_class_serialize(event_class
, context
);
732 context
->current_indentation_level
= 0;
737 int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class
*stream_class
,
738 struct bt_ctf_trace
*trace
)
747 if (stream_class
->trace
&& trace
) {
748 /* Already attached to a trace */
753 stream_class
->trace
= trace
;
759 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
761 struct bt_ctf_stream_class
*stream_class
;
763 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
764 bt_put(stream_class
->clock
);
766 if (stream_class
->event_classes
) {
769 /* Unregister this stream class from the event classes */
770 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
771 struct bt_ctf_event_class
*event_class
=
772 g_ptr_array_index(stream_class
->event_classes
,
775 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
778 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
781 if (stream_class
->name
) {
782 g_string_free(stream_class
->name
, TRUE
);
785 bt_put(stream_class
->event_header_type
);
786 bt_put(stream_class
->packet_context_type
);
787 bt_put(stream_class
->event_context_type
);
788 g_free(stream_class
);
792 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
795 struct bt_ctf_field_type
*event_header_type
=
796 bt_ctf_field_type_structure_create();
797 struct bt_ctf_field_type
*_uint32_t
=
798 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
799 struct bt_ctf_field_type
*_uint64_t
=
800 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
802 if (!event_header_type
) {
807 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
813 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
814 _uint64_t
, "timestamp");
819 if (stream_class
->event_header_type
) {
820 bt_put(stream_class
->event_header_type
);
822 stream_class
->event_header_type
= event_header_type
;
825 bt_put(event_header_type
);
834 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
837 struct bt_ctf_field_type
*packet_context_type
=
838 bt_ctf_field_type_structure_create();
839 struct bt_ctf_field_type
*_uint64_t
=
840 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
842 if (!packet_context_type
) {
848 * We create a stream packet context as proposed in the CTF
851 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
852 _uint64_t
, "timestamp_begin");
857 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
858 _uint64_t
, "timestamp_end");
863 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
864 _uint64_t
, "content_size");
869 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
870 _uint64_t
, "packet_size");
875 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
876 _uint64_t
, "events_discarded");
881 bt_put(stream_class
->packet_context_type
);
882 stream_class
->packet_context_type
= packet_context_type
;
885 bt_put(packet_context_type
);