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-writer/functor-internal.h>
38 #include <babeltrace/ctf-ir/utils.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/align.h>
43 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
);
45 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
47 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
49 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
52 struct bt_ctf_stream_class
*stream_class
= NULL
;
54 if (name
&& bt_ctf_validate_identifier(name
)) {
58 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
63 stream_class
->name
= g_string_new(name
);
64 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
65 (GDestroyNotify
)bt_ctf_event_class_put
);
66 if (!stream_class
->event_classes
) {
70 ret
= init_event_header(stream_class
);
75 ret
= init_packet_context(stream_class
);
80 bt_ctf_ref_init(&stream_class
->ref_count
);
84 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
90 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
91 struct bt_ctf_stream_class
*stream_class
)
93 struct bt_ctf_trace
*trace
= NULL
;
99 trace
= stream_class
->trace
;
101 bt_ctf_trace_get(trace
);
107 const char *bt_ctf_stream_class_get_name(
108 struct bt_ctf_stream_class
*stream_class
)
110 const char *name
= NULL
;
116 name
= stream_class
->name
->str
;
121 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
126 if (!stream_class
|| stream_class
->frozen
) {
131 g_string_assign(stream_class
->name
, name
);
136 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
137 struct bt_ctf_stream_class
*stream_class
)
139 struct bt_ctf_clock
*clock
= NULL
;
141 if (!stream_class
|| !stream_class
->clock
) {
145 clock
= stream_class
->clock
;
146 bt_ctf_clock_get(clock
);
151 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
152 struct bt_ctf_clock
*clock
)
155 struct bt_ctf_field_type
*timestamp_field
= NULL
;
157 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
163 * Look for a "timestamp" field in the stream class' event header type
164 * and map the stream's clock to that field if no current mapping is
167 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
168 stream_class
->event_header_type
, "timestamp");
169 if (timestamp_field
) {
170 struct bt_ctf_clock
*mapped_clock
;
172 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
175 bt_ctf_clock_put(mapped_clock
);
179 ret
= bt_ctf_field_type_integer_set_mapped_clock(
180 timestamp_field
, clock
);
186 if (stream_class
->clock
) {
187 bt_ctf_clock_put(stream_class
->clock
);
190 stream_class
->clock
= clock
;
191 bt_ctf_clock_get(clock
);
193 if (timestamp_field
) {
194 bt_ctf_field_type_put(timestamp_field
);
199 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
203 if (!stream_class
|| !stream_class
->id_set
) {
208 ret
= (int64_t) stream_class
->id
;
214 int _bt_ctf_stream_class_set_id(
215 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
217 stream_class
->id
= id
;
218 stream_class
->id_set
= 1;
222 struct event_class_set_stream_id_data
{
228 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
230 struct event_class_set_stream_id_data
*typed_data
= data
;
232 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
233 typed_data
->stream_id
);
237 int bt_ctf_stream_class_set_id_no_check(
238 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
241 struct event_class_set_stream_id_data data
=
242 { .stream_id
= id
, .ret
= 0 };
245 * Make sure all event classes have their "stream_id" attribute
248 g_ptr_array_foreach(stream_class
->event_classes
,
249 event_class_set_stream_id
, &data
);
255 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
263 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
268 if (!stream_class
|| stream_class
->frozen
) {
273 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
279 void event_class_exists(gpointer element
, gpointer query
)
281 struct bt_ctf_event_class
*event_class_a
= element
;
282 struct search_query
*search_query
= query
;
283 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
286 if (search_query
->value
== element
) {
287 search_query
->found
= 1;
292 * Two event classes cannot share the same name in a given
295 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
296 bt_ctf_event_class_get_name(event_class_b
))) {
297 search_query
->found
= 1;
302 * Two event classes cannot share the same ID in a given
305 id_a
= bt_ctf_event_class_get_id(event_class_a
);
306 id_b
= bt_ctf_event_class_get_id(event_class_b
);
308 if (id_a
< 0 || id_b
< 0) {
309 /* at least one ID is not set: will be automatically set later */
314 search_query
->found
= 1;
322 int bt_ctf_stream_class_add_event_class(
323 struct bt_ctf_stream_class
*stream_class
,
324 struct bt_ctf_event_class
*event_class
)
329 if (!stream_class
|| !event_class
) {
334 /* Check for duplicate event classes */
335 struct search_query query
= { .value
= event_class
, .found
= 0 };
336 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
343 /* Only set an event id if none was explicitly set before */
344 event_id
= bt_ctf_event_class_get_id(event_class
);
346 if (bt_ctf_event_class_set_id(event_class
,
347 stream_class
->next_event_id
++)) {
353 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
358 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
363 bt_ctf_event_class_get(event_class
);
364 g_ptr_array_add(stream_class
->event_classes
, event_class
);
365 bt_ctf_event_class_freeze(event_class
);
367 if (stream_class
->byte_order
) {
369 * Only set native byte order if it has been initialized
370 * when the stream class was added to a trace.
372 * If not set here, this will be set when the stream
373 * classe will be added to a trace.
375 bt_ctf_event_class_set_native_byte_order(event_class
,
376 stream_class
->byte_order
);
382 int bt_ctf_stream_class_get_event_class_count(
383 struct bt_ctf_stream_class
*stream_class
)
392 ret
= (int) stream_class
->event_classes
->len
;
397 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
398 struct bt_ctf_stream_class
*stream_class
, int index
)
400 struct bt_ctf_event_class
*event_class
= NULL
;
402 if (!stream_class
|| index
< 0 ||
403 index
>= stream_class
->event_classes
->len
) {
407 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
408 bt_ctf_event_class_get(event_class
);
413 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
414 struct bt_ctf_stream_class
*stream_class
, const char *name
)
417 struct bt_ctf_event_class
*event_class
= NULL
;
419 if (!stream_class
|| !name
) {
423 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
424 struct bt_ctf_event_class
*cur_event_class
=
425 g_ptr_array_index(stream_class
->event_classes
, i
);
426 const char *cur_event_class_name
=
427 bt_ctf_event_class_get_name(cur_event_class
);
429 if (!strcmp(name
, cur_event_class_name
)) {
430 event_class
= cur_event_class
;
431 bt_ctf_event_class_get(event_class
);
439 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
440 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
443 struct bt_ctf_event_class
*event_class
= NULL
;
449 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
450 struct bt_ctf_event_class
*current_event_class
=
451 g_ptr_array_index(stream_class
->event_classes
, i
);
453 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
454 event_class
= current_event_class
;
455 bt_ctf_event_class_get(event_class
);
463 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
464 struct bt_ctf_stream_class
*stream_class
)
466 struct bt_ctf_field_type
*ret
= NULL
;
472 assert(stream_class
->packet_context_type
);
473 bt_ctf_field_type_get(stream_class
->packet_context_type
);
474 ret
= stream_class
->packet_context_type
;
479 int bt_ctf_stream_class_set_packet_context_type(
480 struct bt_ctf_stream_class
*stream_class
,
481 struct bt_ctf_field_type
*packet_context_type
)
485 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
490 assert(stream_class
->packet_context_type
);
491 if (stream_class
->packet_context_type
== packet_context_type
) {
494 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
496 /* A packet context must be a structure */
501 bt_ctf_field_type_put(stream_class
->packet_context_type
);
502 bt_ctf_field_type_get(packet_context_type
);
503 stream_class
->packet_context_type
= packet_context_type
;
508 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
509 struct bt_ctf_stream_class
*stream_class
)
511 struct bt_ctf_field_type
*ret
= NULL
;
513 if (!stream_class
|| !stream_class
->event_header_type
) {
517 assert(stream_class
->event_header_type
);
518 bt_ctf_field_type_get(stream_class
->event_header_type
);
519 ret
= stream_class
->event_header_type
;
524 int bt_ctf_stream_class_set_event_header_type(
525 struct bt_ctf_stream_class
*stream_class
,
526 struct bt_ctf_field_type
*event_header_type
)
530 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
535 assert(stream_class
->event_header_type
);
536 if (stream_class
->event_header_type
== event_header_type
) {
539 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
541 /* An event header must be a structure */
546 bt_ctf_field_type_put(stream_class
->event_header_type
);
547 bt_ctf_field_type_get(event_header_type
);
548 stream_class
->event_header_type
= event_header_type
;
553 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
554 struct bt_ctf_stream_class
*stream_class
)
556 struct bt_ctf_field_type
*ret
= NULL
;
558 if (!stream_class
|| !stream_class
->event_context_type
) {
562 assert(stream_class
->event_context_type
);
563 bt_ctf_field_type_get(stream_class
->event_context_type
);
564 ret
= stream_class
->event_context_type
;
569 int bt_ctf_stream_class_set_event_context_type(
570 struct bt_ctf_stream_class
*stream_class
,
571 struct bt_ctf_field_type
*event_context_type
)
575 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
580 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
582 /* A packet context must be a structure */
587 bt_ctf_field_type_put(stream_class
->event_context_type
);
588 bt_ctf_field_type_get(event_context_type
);
589 stream_class
->event_context_type
= event_context_type
;
594 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
600 bt_ctf_ref_get(&stream_class
->ref_count
);
603 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
609 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
613 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
619 stream_class
->frozen
= 1;
620 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
621 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
622 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
623 bt_ctf_clock_freeze(stream_class
->clock
);
627 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
628 enum bt_ctf_byte_order byte_order
)
631 int internal_byte_order
;
633 /* Note that "NATIVE" means the trace's endianness, not the host's. */
634 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
635 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
640 switch (byte_order
) {
641 case BT_CTF_BYTE_ORDER_NETWORK
:
642 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
643 internal_byte_order
= BIG_ENDIAN
;
645 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
646 internal_byte_order
= LITTLE_ENDIAN
;
653 stream_class
->byte_order
= internal_byte_order
;
655 /* Set native byte order to little or big endian */
656 bt_ctf_field_type_set_native_byte_order(
657 stream_class
->event_header_type
, stream_class
->byte_order
);
658 bt_ctf_field_type_set_native_byte_order(
659 stream_class
->packet_context_type
, stream_class
->byte_order
);
660 bt_ctf_field_type_set_native_byte_order(
661 stream_class
->event_context_type
, stream_class
->byte_order
);
663 /* Set all events' native byte order */
664 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
665 bt_ctf_event_class_set_native_byte_order(
666 g_ptr_array_index(stream_class
->event_classes
, i
),
667 stream_class
->byte_order
);
668 bt_ctf_event_class_freeze(
669 g_ptr_array_index(stream_class
->event_classes
, i
));
676 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
677 struct metadata_context
*context
)
682 g_string_assign(context
->field_name
, "");
683 context
->current_indentation_level
= 1;
684 if (!stream_class
->id_set
) {
689 g_string_append_printf(context
->string
,
690 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
692 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
698 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
699 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
705 if (stream_class
->event_context_type
) {
706 g_string_append(context
->string
, ";\n\n\tevent.context := ");
707 ret
= bt_ctf_field_type_serialize(
708 stream_class
->event_context_type
, context
);
714 g_string_append(context
->string
, ";\n};\n\n");
715 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
716 struct bt_ctf_event_class
*event_class
=
717 stream_class
->event_classes
->pdata
[i
];
719 ret
= bt_ctf_event_class_serialize(event_class
, context
);
725 context
->current_indentation_level
= 0;
730 int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class
*stream_class
,
731 struct bt_ctf_trace
*trace
)
740 if (stream_class
->trace
&& trace
) {
741 /* Already attached to a trace */
746 stream_class
->trace
= trace
;
752 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
754 struct bt_ctf_stream_class
*stream_class
;
760 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
761 bt_ctf_clock_put(stream_class
->clock
);
763 if (stream_class
->event_classes
) {
766 /* Unregister this stream class from the event classes */
767 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
768 struct bt_ctf_event_class
*event_class
=
769 g_ptr_array_index(stream_class
->event_classes
,
772 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
775 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
778 if (stream_class
->name
) {
779 g_string_free(stream_class
->name
, TRUE
);
782 bt_ctf_field_type_put(stream_class
->event_header_type
);
783 bt_ctf_field_type_put(stream_class
->packet_context_type
);
784 if (stream_class
->event_context_type
) {
785 bt_ctf_field_type_put(stream_class
->event_context_type
);
787 g_free(stream_class
);
791 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
794 struct bt_ctf_field_type
*event_header_type
=
795 bt_ctf_field_type_structure_create();
796 struct bt_ctf_field_type
*_uint32_t
=
797 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
798 struct bt_ctf_field_type
*_uint64_t
=
799 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
801 if (!event_header_type
) {
806 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
812 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
813 _uint64_t
, "timestamp");
818 if (stream_class
->event_header_type
) {
819 bt_ctf_field_type_put(stream_class
->event_header_type
);
821 stream_class
->event_header_type
= event_header_type
;
824 bt_ctf_field_type_put(event_header_type
);
827 bt_ctf_field_type_put(_uint32_t
);
828 bt_ctf_field_type_put(_uint64_t
);
833 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
836 struct bt_ctf_field_type
*packet_context_type
=
837 bt_ctf_field_type_structure_create();
838 struct bt_ctf_field_type
*_uint64_t
=
839 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
841 if (!packet_context_type
) {
847 * We create a stream packet context as proposed in the CTF
850 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
851 _uint64_t
, "timestamp_begin");
856 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
857 _uint64_t
, "timestamp_end");
862 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
863 _uint64_t
, "content_size");
868 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
869 _uint64_t
, "packet_size");
874 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
875 _uint64_t
, "events_discarded");
880 if (stream_class
->packet_context_type
) {
881 bt_ctf_field_type_put(stream_class
->packet_context_type
);
883 stream_class
->packet_context_type
= packet_context_type
;
886 bt_ctf_field_type_put(packet_context_type
);
890 bt_ctf_field_type_put(_uint64_t
);