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/validation-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
;
321 struct bt_ctf_validation_output validation_output
= { 0 };
322 struct bt_ctf_field_type
*packet_header_type
= NULL
;
323 struct bt_ctf_field_type
*packet_context_type
= NULL
;
324 struct bt_ctf_field_type
*event_header_type
= NULL
;
325 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
326 struct bt_ctf_field_type
*event_context_type
= NULL
;
327 struct bt_ctf_field_type
*event_payload_type
= NULL
;
328 const enum bt_ctf_validation_flag validation_flags
=
329 BT_CTF_VALIDATION_FLAG_EVENT
;
331 if (!stream_class
|| !event_class
) {
336 /* Check for duplicate event classes */
337 struct search_query query
= { .value
= event_class
, .found
= 0 };
338 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
345 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
346 if (old_stream_class
) {
347 /* Event class is already associated to a stream class. */
352 trace
= bt_ctf_stream_class_get_trace(stream_class
);
355 * If the stream class is associated with a trace, then
356 * both those objects are frozen. Also, this event class
357 * is about to be frozen.
359 * Therefore the event class must be validated here.
360 * The trace and stream class should be valid at this
363 assert(trace
->valid
);
364 assert(stream_class
->valid
);
366 bt_ctf_trace_get_packet_header_type(trace
);
367 packet_context_type
=
368 bt_ctf_stream_class_get_packet_context_type(
371 bt_ctf_stream_class_get_event_header_type(stream_class
);
372 stream_event_ctx_type
=
373 bt_ctf_stream_class_get_event_context_type(
376 bt_ctf_event_class_get_context_type(event_class
);
378 bt_ctf_event_class_get_payload_type(event_class
);
379 ret
= bt_ctf_validate_class_types(
380 trace
->environment
, packet_header_type
,
381 packet_context_type
, event_header_type
,
382 stream_event_ctx_type
, event_context_type
,
383 event_payload_type
, trace
->valid
,
384 stream_class
->valid
, event_class
->valid
,
385 &validation_output
, validation_flags
);
386 BT_PUT(packet_header_type
);
387 BT_PUT(packet_context_type
);
388 BT_PUT(event_header_type
);
389 BT_PUT(stream_event_ctx_type
);
390 BT_PUT(event_context_type
);
391 BT_PUT(event_payload_type
);
395 * This means something went wrong during the
396 * validation process, not that the objects are
402 if ((validation_output
.valid_flags
& validation_flags
) !=
404 /* Invalid event class */
410 /* Only set an event ID if none was explicitly set before */
411 event_id
= bt_ctf_event_class_get_id(event_class
);
413 if (bt_ctf_event_class_set_id(event_class
,
414 stream_class
->next_event_id
++)) {
420 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
425 bt_object_set_parent(event_class
, stream_class
);
429 * At this point we know that the function will be
430 * successful. Therefore we can replace the event
431 * class's field types with what's in the validation
432 * output structure and mark this event class as valid.
434 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
435 &validation_output
, validation_flags
);
436 event_class
->valid
= 1;
439 * Put what was not moved in
440 * bt_ctf_validation_replace_types().
442 bt_ctf_validation_output_put_types(&validation_output
);
445 /* Add to the event classes of the stream class */
446 g_ptr_array_add(stream_class
->event_classes
, event_class
);
448 /* Freeze the event class */
449 bt_ctf_event_class_freeze(event_class
);
451 if (stream_class
->byte_order
) {
453 * Only set native byte order if it has been initialized
454 * when the stream class was added to a trace.
456 * If not set here, this will be set when the stream
457 * class is added to a trace.
459 bt_ctf_event_class_set_native_byte_order(event_class
,
460 stream_class
->byte_order
);
465 BT_PUT(old_stream_class
);
466 bt_ctf_validation_output_put_types(&validation_output
);
467 assert(!packet_header_type
);
468 assert(!packet_context_type
);
469 assert(!event_header_type
);
470 assert(!stream_event_ctx_type
);
471 assert(!event_context_type
);
472 assert(!event_payload_type
);
477 int bt_ctf_stream_class_get_event_class_count(
478 struct bt_ctf_stream_class
*stream_class
)
487 ret
= (int) stream_class
->event_classes
->len
;
492 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
493 struct bt_ctf_stream_class
*stream_class
, int index
)
495 struct bt_ctf_event_class
*event_class
= NULL
;
497 if (!stream_class
|| index
< 0 ||
498 index
>= stream_class
->event_classes
->len
) {
502 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
508 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
509 struct bt_ctf_stream_class
*stream_class
, const char *name
)
512 struct bt_ctf_event_class
*event_class
= NULL
;
514 if (!stream_class
|| !name
) {
518 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
519 struct bt_ctf_event_class
*cur_event_class
=
520 g_ptr_array_index(stream_class
->event_classes
, i
);
521 const char *cur_event_class_name
=
522 bt_ctf_event_class_get_name(cur_event_class
);
524 if (!strcmp(name
, cur_event_class_name
)) {
525 event_class
= cur_event_class
;
534 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
535 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
538 struct bt_ctf_event_class
*event_class
= NULL
;
544 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
545 struct bt_ctf_event_class
*current_event_class
=
546 g_ptr_array_index(stream_class
->event_classes
, i
);
548 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
549 event_class
= current_event_class
;
558 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
559 struct bt_ctf_stream_class
*stream_class
)
561 struct bt_ctf_field_type
*ret
= NULL
;
567 assert(stream_class
->packet_context_type
);
568 bt_get(stream_class
->packet_context_type
);
569 ret
= stream_class
->packet_context_type
;
574 int bt_ctf_stream_class_set_packet_context_type(
575 struct bt_ctf_stream_class
*stream_class
,
576 struct bt_ctf_field_type
*packet_context_type
)
580 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
585 assert(stream_class
->packet_context_type
);
586 if (stream_class
->packet_context_type
== packet_context_type
) {
589 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
590 BT_CTF_TYPE_ID_STRUCT
) {
591 /* A packet context must be a structure */
596 bt_put(stream_class
->packet_context_type
);
597 bt_get(packet_context_type
);
598 stream_class
->packet_context_type
= packet_context_type
;
603 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
604 struct bt_ctf_stream_class
*stream_class
)
606 struct bt_ctf_field_type
*ret
= NULL
;
608 if (!stream_class
|| !stream_class
->event_header_type
) {
612 assert(stream_class
->event_header_type
);
613 bt_get(stream_class
->event_header_type
);
614 ret
= stream_class
->event_header_type
;
619 int bt_ctf_stream_class_set_event_header_type(
620 struct bt_ctf_stream_class
*stream_class
,
621 struct bt_ctf_field_type
*event_header_type
)
625 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
630 assert(stream_class
->event_header_type
);
631 if (stream_class
->event_header_type
== event_header_type
) {
634 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
635 BT_CTF_TYPE_ID_STRUCT
) {
636 /* An event header must be a structure */
641 bt_put(stream_class
->event_header_type
);
642 bt_get(event_header_type
);
643 stream_class
->event_header_type
= event_header_type
;
648 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
649 struct bt_ctf_stream_class
*stream_class
)
651 struct bt_ctf_field_type
*ret
= NULL
;
653 if (!stream_class
|| !stream_class
->event_context_type
) {
657 assert(stream_class
->event_context_type
);
658 bt_get(stream_class
->event_context_type
);
659 ret
= stream_class
->event_context_type
;
664 int bt_ctf_stream_class_set_event_context_type(
665 struct bt_ctf_stream_class
*stream_class
,
666 struct bt_ctf_field_type
*event_context_type
)
670 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
675 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
676 BT_CTF_TYPE_ID_STRUCT
) {
677 /* A packet context must be a structure */
682 bt_put(stream_class
->event_context_type
);
683 bt_get(event_context_type
);
684 stream_class
->event_context_type
= event_context_type
;
689 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
691 bt_get(stream_class
);
694 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
696 bt_put(stream_class
);
700 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
706 stream_class
->frozen
= 1;
707 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
708 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
709 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
710 bt_ctf_clock_freeze(stream_class
->clock
);
714 void bt_ctf_stream_class_set_byte_order(
715 struct bt_ctf_stream_class
*stream_class
, int byte_order
)
719 assert(stream_class
);
720 assert(byte_order
== LITTLE_ENDIAN
|| byte_order
== BIG_ENDIAN
);
721 stream_class
->byte_order
= byte_order
;
723 /* Set native byte order to little or big endian */
724 bt_ctf_field_type_set_native_byte_order(
725 stream_class
->event_header_type
, byte_order
);
726 bt_ctf_field_type_set_native_byte_order(
727 stream_class
->packet_context_type
, byte_order
);
728 bt_ctf_field_type_set_native_byte_order(
729 stream_class
->event_context_type
, byte_order
);
731 /* Set all events' native byte order */
732 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
733 struct bt_ctf_event_class
*event_class
=
734 g_ptr_array_index(stream_class
->event_classes
, i
);
736 bt_ctf_event_class_set_native_byte_order(event_class
,
742 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
743 struct metadata_context
*context
)
748 g_string_assign(context
->field_name
, "");
749 context
->current_indentation_level
= 1;
750 if (!stream_class
->id_set
) {
755 g_string_append_printf(context
->string
,
756 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
758 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
764 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
765 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
771 if (stream_class
->event_context_type
) {
772 g_string_append(context
->string
, ";\n\n\tevent.context := ");
773 ret
= bt_ctf_field_type_serialize(
774 stream_class
->event_context_type
, context
);
780 g_string_append(context
->string
, ";\n};\n\n");
781 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
782 struct bt_ctf_event_class
*event_class
=
783 stream_class
->event_classes
->pdata
[i
];
785 ret
= bt_ctf_event_class_serialize(event_class
, context
);
791 context
->current_indentation_level
= 0;
796 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
798 struct bt_ctf_stream_class
*stream_class
;
800 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
801 bt_put(stream_class
->clock
);
803 if (stream_class
->event_classes
) {
804 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
807 if (stream_class
->name
) {
808 g_string_free(stream_class
->name
, TRUE
);
811 bt_put(stream_class
->event_header_type
);
812 bt_put(stream_class
->packet_context_type
);
813 bt_put(stream_class
->event_context_type
);
814 g_free(stream_class
);
818 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
821 struct bt_ctf_field_type
*event_header_type
=
822 bt_ctf_field_type_structure_create();
823 struct bt_ctf_field_type
*_uint32_t
=
824 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
825 struct bt_ctf_field_type
*_uint64_t
=
826 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
828 if (!event_header_type
) {
833 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
839 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
840 _uint64_t
, "timestamp");
845 if (stream_class
->event_header_type
) {
846 bt_put(stream_class
->event_header_type
);
848 stream_class
->event_header_type
= event_header_type
;
851 bt_put(event_header_type
);
860 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
863 struct bt_ctf_field_type
*packet_context_type
=
864 bt_ctf_field_type_structure_create();
865 struct bt_ctf_field_type
*_uint64_t
=
866 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
868 if (!packet_context_type
) {
874 * We create a stream packet context as proposed in the CTF
877 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
878 _uint64_t
, "timestamp_begin");
883 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
884 _uint64_t
, "timestamp_end");
889 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
890 _uint64_t
, "content_size");
895 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
896 _uint64_t
, "packet_size");
901 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
902 _uint64_t
, "events_discarded");
907 bt_put(stream_class
->packet_context_type
);
908 stream_class
->packet_context_type
= packet_context_type
;
911 bt_put(packet_context_type
);