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_object_release
);
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 return (struct bt_ctf_trace
*) bt_object_get_parent(
98 const char *bt_ctf_stream_class_get_name(
99 struct bt_ctf_stream_class
*stream_class
)
101 const char *name
= NULL
;
107 name
= stream_class
->name
->str
;
112 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
117 if (!stream_class
|| stream_class
->frozen
) {
122 g_string_assign(stream_class
->name
, name
);
127 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
128 struct bt_ctf_stream_class
*stream_class
)
130 struct bt_ctf_clock
*clock
= NULL
;
132 if (!stream_class
|| !stream_class
->clock
) {
136 clock
= stream_class
->clock
;
142 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
143 struct bt_ctf_clock
*clock
)
146 struct bt_ctf_field_type
*timestamp_field
= NULL
;
148 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
154 * Look for a "timestamp" field in the stream class' event header type
155 * and map the stream's clock to that field if no current mapping is
158 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
159 stream_class
->event_header_type
, "timestamp");
160 if (timestamp_field
) {
161 struct bt_ctf_clock
*mapped_clock
;
163 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
166 bt_put(mapped_clock
);
170 ret
= bt_ctf_field_type_integer_set_mapped_clock(
171 timestamp_field
, clock
);
177 if (stream_class
->clock
) {
178 bt_put(stream_class
->clock
);
181 stream_class
->clock
= clock
;
184 if (timestamp_field
) {
185 bt_put(timestamp_field
);
190 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
194 if (!stream_class
|| !stream_class
->id_set
) {
199 ret
= (int64_t) stream_class
->id
;
205 int _bt_ctf_stream_class_set_id(
206 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
208 stream_class
->id
= id
;
209 stream_class
->id_set
= 1;
213 struct event_class_set_stream_id_data
{
219 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
221 struct event_class_set_stream_id_data
*typed_data
= data
;
223 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
224 typed_data
->stream_id
);
228 int bt_ctf_stream_class_set_id_no_check(
229 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
232 struct event_class_set_stream_id_data data
=
233 { .stream_id
= id
, .ret
= 0 };
236 * Make sure all event classes have their "stream_id" attribute
239 g_ptr_array_foreach(stream_class
->event_classes
,
240 event_class_set_stream_id
, &data
);
246 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
254 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
259 if (!stream_class
|| stream_class
->frozen
) {
264 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
270 void event_class_exists(gpointer element
, gpointer query
)
272 struct bt_ctf_event_class
*event_class_a
= element
;
273 struct search_query
*search_query
= query
;
274 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
277 if (search_query
->value
== element
) {
278 search_query
->found
= 1;
283 * Two event classes cannot share the same name in a given
286 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
287 bt_ctf_event_class_get_name(event_class_b
))) {
288 search_query
->found
= 1;
293 * Two event classes cannot share the same ID in a given
296 id_a
= bt_ctf_event_class_get_id(event_class_a
);
297 id_b
= bt_ctf_event_class_get_id(event_class_b
);
299 if (id_a
< 0 || id_b
< 0) {
300 /* at least one ID is not set: will be automatically set later */
305 search_query
->found
= 1;
313 int bt_ctf_stream_class_add_event_class(
314 struct bt_ctf_stream_class
*stream_class
,
315 struct bt_ctf_event_class
*event_class
)
319 struct bt_ctf_trace
*trace
= NULL
;
320 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
322 if (!stream_class
|| !event_class
) {
327 /* Check for duplicate event classes */
328 struct search_query query
= { .value
= event_class
, .found
= 0 };
329 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
336 old_stream_class
= (struct bt_ctf_stream_class
*) bt_object_get_parent(
338 if (old_stream_class
) {
339 /* Event class is already associated to a stream class. */
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 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(
353 ret
= bt_ctf_event_class_resolve_types(event_class
,
354 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_id(event_class
, stream_class
->id
);
375 bt_object_set_parent(event_class
, stream_class
);
376 g_ptr_array_add(stream_class
->event_classes
, event_class
);
377 bt_ctf_event_class_freeze(event_class
);
379 if (stream_class
->byte_order
) {
381 * Only set native byte order if it has been initialized
382 * when the stream class was added to a trace.
384 * If not set here, this will be set when the stream
385 * classe will be added to a trace.
387 bt_ctf_event_class_set_native_byte_order(event_class
,
388 stream_class
->byte_order
);
392 BT_PUT(old_stream_class
);
396 int bt_ctf_stream_class_get_event_class_count(
397 struct bt_ctf_stream_class
*stream_class
)
406 ret
= (int) stream_class
->event_classes
->len
;
411 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
412 struct bt_ctf_stream_class
*stream_class
, int index
)
414 struct bt_ctf_event_class
*event_class
= NULL
;
416 if (!stream_class
|| index
< 0 ||
417 index
>= stream_class
->event_classes
->len
) {
421 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
427 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
428 struct bt_ctf_stream_class
*stream_class
, const char *name
)
431 struct bt_ctf_event_class
*event_class
= NULL
;
433 if (!stream_class
|| !name
) {
437 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
438 struct bt_ctf_event_class
*cur_event_class
=
439 g_ptr_array_index(stream_class
->event_classes
, i
);
440 const char *cur_event_class_name
=
441 bt_ctf_event_class_get_name(cur_event_class
);
443 if (!strcmp(name
, cur_event_class_name
)) {
444 event_class
= cur_event_class
;
453 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
454 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
457 struct bt_ctf_event_class
*event_class
= NULL
;
463 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
464 struct bt_ctf_event_class
*current_event_class
=
465 g_ptr_array_index(stream_class
->event_classes
, i
);
467 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
468 event_class
= current_event_class
;
477 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
478 struct bt_ctf_stream_class
*stream_class
)
480 struct bt_ctf_field_type
*ret
= NULL
;
486 assert(stream_class
->packet_context_type
);
487 bt_get(stream_class
->packet_context_type
);
488 ret
= stream_class
->packet_context_type
;
493 int bt_ctf_stream_class_set_packet_context_type(
494 struct bt_ctf_stream_class
*stream_class
,
495 struct bt_ctf_field_type
*packet_context_type
)
499 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
504 assert(stream_class
->packet_context_type
);
505 if (stream_class
->packet_context_type
== packet_context_type
) {
508 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
510 /* A packet context must be a structure */
515 bt_put(stream_class
->packet_context_type
);
516 bt_get(packet_context_type
);
517 stream_class
->packet_context_type
= packet_context_type
;
522 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
523 struct bt_ctf_stream_class
*stream_class
)
525 struct bt_ctf_field_type
*ret
= NULL
;
527 if (!stream_class
|| !stream_class
->event_header_type
) {
531 assert(stream_class
->event_header_type
);
532 bt_get(stream_class
->event_header_type
);
533 ret
= stream_class
->event_header_type
;
538 int bt_ctf_stream_class_set_event_header_type(
539 struct bt_ctf_stream_class
*stream_class
,
540 struct bt_ctf_field_type
*event_header_type
)
544 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
549 assert(stream_class
->event_header_type
);
550 if (stream_class
->event_header_type
== event_header_type
) {
553 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
555 /* An event header must be a structure */
560 bt_put(stream_class
->event_header_type
);
561 bt_get(event_header_type
);
562 stream_class
->event_header_type
= event_header_type
;
567 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
568 struct bt_ctf_stream_class
*stream_class
)
570 struct bt_ctf_field_type
*ret
= NULL
;
572 if (!stream_class
|| !stream_class
->event_context_type
) {
576 assert(stream_class
->event_context_type
);
577 bt_get(stream_class
->event_context_type
);
578 ret
= stream_class
->event_context_type
;
583 int bt_ctf_stream_class_set_event_context_type(
584 struct bt_ctf_stream_class
*stream_class
,
585 struct bt_ctf_field_type
*event_context_type
)
589 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
594 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
596 /* A packet context must be a structure */
601 bt_put(stream_class
->event_context_type
);
602 bt_get(event_context_type
);
603 stream_class
->event_context_type
= event_context_type
;
608 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
610 bt_get(stream_class
);
613 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
615 bt_put(stream_class
);
619 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
625 stream_class
->frozen
= 1;
626 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
627 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
628 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
629 bt_ctf_clock_freeze(stream_class
->clock
);
633 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
634 enum bt_ctf_byte_order byte_order
)
637 int internal_byte_order
;
639 /* Note that "NATIVE" means the trace's endianness, not the host's. */
640 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
641 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
646 switch (byte_order
) {
647 case BT_CTF_BYTE_ORDER_NETWORK
:
648 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
649 internal_byte_order
= BIG_ENDIAN
;
651 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
652 internal_byte_order
= LITTLE_ENDIAN
;
659 stream_class
->byte_order
= internal_byte_order
;
661 /* Set native byte order to little or big endian */
662 bt_ctf_field_type_set_native_byte_order(
663 stream_class
->event_header_type
, stream_class
->byte_order
);
664 bt_ctf_field_type_set_native_byte_order(
665 stream_class
->packet_context_type
, stream_class
->byte_order
);
666 bt_ctf_field_type_set_native_byte_order(
667 stream_class
->event_context_type
, stream_class
->byte_order
);
669 /* Set all events' native byte order */
670 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
671 struct bt_ctf_event_class
*event_class
;
673 event_class
= g_ptr_array_index(stream_class
->event_classes
, i
);
674 bt_ctf_event_class_set_native_byte_order(event_class
,
675 stream_class
->byte_order
);
682 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
683 struct metadata_context
*context
)
688 g_string_assign(context
->field_name
, "");
689 context
->current_indentation_level
= 1;
690 if (!stream_class
->id_set
) {
695 g_string_append_printf(context
->string
,
696 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
698 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
704 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
705 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
711 if (stream_class
->event_context_type
) {
712 g_string_append(context
->string
, ";\n\n\tevent.context := ");
713 ret
= bt_ctf_field_type_serialize(
714 stream_class
->event_context_type
, context
);
720 g_string_append(context
->string
, ";\n};\n\n");
721 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
722 struct bt_ctf_event_class
*event_class
=
723 stream_class
->event_classes
->pdata
[i
];
725 ret
= bt_ctf_event_class_serialize(event_class
, context
);
731 context
->current_indentation_level
= 0;
736 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
738 struct bt_ctf_stream_class
*stream_class
;
740 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
741 bt_put(stream_class
->clock
);
743 if (stream_class
->event_classes
) {
744 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
747 if (stream_class
->name
) {
748 g_string_free(stream_class
->name
, TRUE
);
751 bt_put(stream_class
->event_header_type
);
752 bt_put(stream_class
->packet_context_type
);
753 bt_put(stream_class
->event_context_type
);
754 g_free(stream_class
);
758 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
761 struct bt_ctf_field_type
*event_header_type
=
762 bt_ctf_field_type_structure_create();
763 struct bt_ctf_field_type
*_uint32_t
=
764 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
765 struct bt_ctf_field_type
*_uint64_t
=
766 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
768 if (!event_header_type
) {
773 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
779 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
780 _uint64_t
, "timestamp");
785 if (stream_class
->event_header_type
) {
786 bt_put(stream_class
->event_header_type
);
788 stream_class
->event_header_type
= event_header_type
;
791 bt_put(event_header_type
);
800 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
803 struct bt_ctf_field_type
*packet_context_type
=
804 bt_ctf_field_type_structure_create();
805 struct bt_ctf_field_type
*_uint64_t
=
806 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
808 if (!packet_context_type
) {
814 * We create a stream packet context as proposed in the CTF
817 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
818 _uint64_t
, "timestamp_begin");
823 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
824 _uint64_t
, "timestamp_end");
829 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
830 _uint64_t
, "content_size");
835 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
836 _uint64_t
, "packet_size");
841 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
842 _uint64_t
, "events_discarded");
847 bt_put(stream_class
->packet_context_type
);
848 stream_class
->packet_context_type
= packet_context_type
;
851 bt_put(packet_context_type
);