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-writer/clock-internal.h>
31 #include <babeltrace/ctf-ir/clock-class-internal.h>
32 #include <babeltrace/ctf-writer/event.h>
33 #include <babeltrace/ctf-ir/event-class-internal.h>
34 #include <babeltrace/ctf-ir/event-internal.h>
35 #include <babeltrace/ctf-ir/field-types-internal.h>
36 #include <babeltrace/ctf-ir/fields-internal.h>
37 #include <babeltrace/ctf-writer/stream.h>
38 #include <babeltrace/ctf-ir/stream-class-internal.h>
39 #include <babeltrace/ctf-ir/validation-internal.h>
40 #include <babeltrace/ctf-ir/visitor-internal.h>
41 #include <babeltrace/ctf-writer/functor-internal.h>
42 #include <babeltrace/ctf-ir/utils.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/align-internal.h>
46 #include <babeltrace/endian-internal.h>
50 void bt_ctf_stream_class_destroy(struct bt_object
*obj
);
52 int init_event_header(struct bt_ctf_stream_class
*stream_class
);
54 int init_packet_context(struct bt_ctf_stream_class
*stream_class
);
56 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
59 struct bt_ctf_stream_class
*stream_class
= NULL
;
61 if (name
&& bt_ctf_validate_identifier(name
)) {
65 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
70 stream_class
->name
= g_string_new(name
);
71 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
72 (GDestroyNotify
) bt_object_release
);
73 if (!stream_class
->event_classes
) {
77 stream_class
->event_classes_ht
= g_hash_table_new_full(g_int64_hash
,
78 g_int64_equal
, g_free
, NULL
);
80 ret
= init_event_header(stream_class
);
85 ret
= init_packet_context(stream_class
);
90 bt_object_init(stream_class
, bt_ctf_stream_class_destroy
);
98 struct bt_ctf_trace
*bt_ctf_stream_class_get_trace(
99 struct bt_ctf_stream_class
*stream_class
)
101 return stream_class
?
102 bt_get(bt_ctf_stream_class_borrow_trace(stream_class
)) :
106 const char *bt_ctf_stream_class_get_name(
107 struct bt_ctf_stream_class
*stream_class
)
109 const char *name
= NULL
;
115 name
= stream_class
->name
->str
;
120 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
125 if (!stream_class
|| stream_class
->frozen
) {
130 g_string_assign(stream_class
->name
, name
);
135 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
136 struct bt_ctf_stream_class
*stream_class
)
138 struct bt_ctf_clock
*clock
= NULL
;
140 if (!stream_class
|| !stream_class
->clock
) {
144 clock
= bt_get(stream_class
->clock
);
149 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
150 struct bt_ctf_clock
*clock
)
153 struct bt_ctf_field_type
*timestamp_field
= NULL
;
155 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
161 * Look for a "timestamp" integer field type in the stream
162 * class's event header field type and map the stream class's
163 * clock's class to that field type if there's no current
166 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
167 stream_class
->event_header_type
, "timestamp");
168 if (timestamp_field
) {
169 struct bt_ctf_clock_class
*mapped_clock_class
=
170 bt_ctf_field_type_integer_get_mapped_clock_class(
173 if (!mapped_clock_class
) {
174 ret
= bt_ctf_field_type_integer_set_mapped_clock_class(
175 timestamp_field
, clock
->clock_class
);
181 BT_PUT(mapped_clock_class
);
184 /* Replace the current clock of this stream class. */
185 bt_put(stream_class
->clock
);
186 stream_class
->clock
= bt_get(clock
);
189 bt_put(timestamp_field
);
193 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
197 if (!stream_class
|| !stream_class
->id_set
) {
202 ret
= (int64_t) stream_class
->id
;
208 int _bt_ctf_stream_class_set_id(
209 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
211 stream_class
->id
= id
;
212 stream_class
->id_set
= 1;
216 struct event_class_set_stream_id_data
{
222 void event_class_set_stream_id(gpointer event_class
, gpointer data
)
224 struct event_class_set_stream_id_data
*typed_data
= data
;
226 typed_data
->ret
|= bt_ctf_event_class_set_stream_id(event_class
,
227 typed_data
->stream_id
);
231 int bt_ctf_stream_class_set_id_no_check(
232 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
235 struct event_class_set_stream_id_data data
=
236 { .stream_id
= id
, .ret
= 0 };
239 * Make sure all event classes have their "stream_id" attribute
242 g_ptr_array_foreach(stream_class
->event_classes
,
243 event_class_set_stream_id
, &data
);
249 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
257 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
262 if (!stream_class
|| stream_class
->frozen
) {
267 ret
= bt_ctf_stream_class_set_id_no_check(stream_class
, id
);
273 void event_class_exists(gpointer element
, gpointer query
)
275 struct bt_ctf_event_class
*event_class_a
= element
;
276 struct search_query
*search_query
= query
;
277 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
280 if (search_query
->value
== element
) {
281 search_query
->found
= 1;
286 * Two event classes cannot share the same name in a given
289 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
290 bt_ctf_event_class_get_name(event_class_b
))) {
291 search_query
->found
= 1;
296 * Two event classes cannot share the same ID in a given
299 id_a
= bt_ctf_event_class_get_id(event_class_a
);
300 id_b
= bt_ctf_event_class_get_id(event_class_b
);
302 if (id_a
< 0 || id_b
< 0) {
303 /* at least one ID is not set: will be automatically set later */
308 search_query
->found
= 1;
316 int bt_ctf_stream_class_add_event_class(
317 struct bt_ctf_stream_class
*stream_class
,
318 struct bt_ctf_event_class
*event_class
)
321 int64_t *event_id
= NULL
;
322 struct bt_ctf_trace
*trace
= NULL
;
323 struct bt_ctf_stream_class
*old_stream_class
= NULL
;
324 struct bt_ctf_validation_output validation_output
= { 0 };
325 struct bt_ctf_field_type
*packet_header_type
= NULL
;
326 struct bt_ctf_field_type
*packet_context_type
= NULL
;
327 struct bt_ctf_field_type
*event_header_type
= NULL
;
328 struct bt_ctf_field_type
*stream_event_ctx_type
= NULL
;
329 struct bt_ctf_field_type
*event_context_type
= NULL
;
330 struct bt_ctf_field_type
*event_payload_type
= NULL
;
331 const enum bt_ctf_validation_flag validation_flags
=
332 BT_CTF_VALIDATION_FLAG_EVENT
;
334 if (!stream_class
|| !event_class
) {
339 trace
= bt_ctf_stream_class_get_trace(stream_class
);
340 if (trace
&& trace
->is_static
) {
345 event_id
= g_new(int64_t, 1);
351 /* Check for duplicate event classes */
352 struct search_query query
= { .value
= event_class
, .found
= 0 };
353 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
360 old_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
361 if (old_stream_class
) {
362 /* Event class is already associated to a stream class. */
369 * If the stream class is associated with a trace, then
370 * both those objects are frozen. Also, this event class
371 * is about to be frozen.
373 * Therefore the event class must be validated here.
374 * The trace and stream class should be valid at this
377 assert(trace
->valid
);
378 assert(stream_class
->valid
);
380 bt_ctf_trace_get_packet_header_type(trace
);
381 packet_context_type
=
382 bt_ctf_stream_class_get_packet_context_type(
385 bt_ctf_stream_class_get_event_header_type(stream_class
);
386 stream_event_ctx_type
=
387 bt_ctf_stream_class_get_event_context_type(
390 bt_ctf_event_class_get_context_type(event_class
);
392 bt_ctf_event_class_get_payload_type(event_class
);
393 ret
= bt_ctf_validate_class_types(
394 trace
->environment
, packet_header_type
,
395 packet_context_type
, event_header_type
,
396 stream_event_ctx_type
, event_context_type
,
397 event_payload_type
, trace
->valid
,
398 stream_class
->valid
, event_class
->valid
,
399 &validation_output
, validation_flags
);
400 BT_PUT(packet_header_type
);
401 BT_PUT(packet_context_type
);
402 BT_PUT(event_header_type
);
403 BT_PUT(stream_event_ctx_type
);
404 BT_PUT(event_context_type
);
405 BT_PUT(event_payload_type
);
409 * This means something went wrong during the
410 * validation process, not that the objects are
416 if ((validation_output
.valid_flags
& validation_flags
) !=
418 /* Invalid event class */
424 /* Only set an event ID if none was explicitly set before */
425 *event_id
= bt_ctf_event_class_get_id(event_class
);
427 if (bt_ctf_event_class_set_id(event_class
,
428 stream_class
->next_event_id
++)) {
432 *event_id
= stream_class
->next_event_id
;
435 ret
= bt_ctf_event_class_set_stream_id(event_class
, stream_class
->id
);
440 bt_object_set_parent(event_class
, stream_class
);
444 * At this point we know that the function will be
445 * successful. Therefore we can replace the event
446 * class's field types with what's in the validation
447 * output structure and mark this event class as valid.
449 bt_ctf_validation_replace_types(NULL
, NULL
, event_class
,
450 &validation_output
, validation_flags
);
451 event_class
->valid
= 1;
454 * Put what was not moved in
455 * bt_ctf_validation_replace_types().
457 bt_ctf_validation_output_put_types(&validation_output
);
460 /* Add to the event classes of the stream class */
461 g_ptr_array_add(stream_class
->event_classes
, event_class
);
462 g_hash_table_insert(stream_class
->event_classes_ht
, event_id
,
466 /* Freeze the event class */
467 bt_ctf_event_class_freeze(event_class
);
469 /* Notifiy listeners of the trace's schema modification. */
471 struct bt_ctf_object obj
= { .object
= event_class
,
472 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
474 (void) bt_ctf_trace_object_modification(&obj
, trace
);
478 BT_PUT(old_stream_class
);
479 bt_ctf_validation_output_put_types(&validation_output
);
480 assert(!packet_header_type
);
481 assert(!packet_context_type
);
482 assert(!event_header_type
);
483 assert(!stream_event_ctx_type
);
484 assert(!event_context_type
);
485 assert(!event_payload_type
);
491 int bt_ctf_stream_class_get_event_class_count(
492 struct bt_ctf_stream_class
*stream_class
)
501 ret
= (int) stream_class
->event_classes
->len
;
506 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
507 struct bt_ctf_stream_class
*stream_class
, int index
)
509 struct bt_ctf_event_class
*event_class
= NULL
;
511 if (!stream_class
|| index
< 0 ||
512 index
>= stream_class
->event_classes
->len
) {
516 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
522 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
523 struct bt_ctf_stream_class
*stream_class
, const char *name
)
526 struct bt_ctf_event_class
*event_class
= NULL
;
528 if (!stream_class
|| !name
) {
532 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
533 struct bt_ctf_event_class
*cur_event_class
=
534 g_ptr_array_index(stream_class
->event_classes
, i
);
535 const char *cur_event_class_name
=
536 bt_ctf_event_class_get_name(cur_event_class
);
538 if (!strcmp(name
, cur_event_class_name
)) {
539 event_class
= cur_event_class
;
548 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
549 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
552 struct bt_ctf_event_class
*event_class
= NULL
;
558 event_class
= g_hash_table_lookup(stream_class
->event_classes_ht
,
565 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
566 struct bt_ctf_stream_class
*stream_class
)
568 struct bt_ctf_field_type
*ret
= NULL
;
574 bt_get(stream_class
->packet_context_type
);
575 ret
= stream_class
->packet_context_type
;
580 int bt_ctf_stream_class_set_packet_context_type(
581 struct bt_ctf_stream_class
*stream_class
,
582 struct bt_ctf_field_type
*packet_context_type
)
586 if (!stream_class
|| stream_class
->frozen
) {
591 if (packet_context_type
&&
592 bt_ctf_field_type_get_type_id(packet_context_type
) !=
593 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
594 /* A packet context must be a structure. */
599 bt_put(stream_class
->packet_context_type
);
600 bt_get(packet_context_type
);
601 stream_class
->packet_context_type
= packet_context_type
;
606 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
607 struct bt_ctf_stream_class
*stream_class
)
609 struct bt_ctf_field_type
*ret
= NULL
;
611 if (!stream_class
|| !stream_class
->event_header_type
) {
615 bt_get(stream_class
->event_header_type
);
616 ret
= stream_class
->event_header_type
;
621 int bt_ctf_stream_class_set_event_header_type(
622 struct bt_ctf_stream_class
*stream_class
,
623 struct bt_ctf_field_type
*event_header_type
)
627 if (!stream_class
|| stream_class
->frozen
) {
632 if (event_header_type
&&
633 bt_ctf_field_type_get_type_id(event_header_type
) !=
634 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
635 /* An event header must be a structure. */
640 bt_put(stream_class
->event_header_type
);
641 stream_class
->event_header_type
= bt_get(event_header_type
);
646 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
647 struct bt_ctf_stream_class
*stream_class
)
649 struct bt_ctf_field_type
*ret
= NULL
;
651 if (!stream_class
|| !stream_class
->event_context_type
) {
655 bt_get(stream_class
->event_context_type
);
656 ret
= stream_class
->event_context_type
;
661 int bt_ctf_stream_class_set_event_context_type(
662 struct bt_ctf_stream_class
*stream_class
,
663 struct bt_ctf_field_type
*event_context_type
)
667 if (!stream_class
|| stream_class
->frozen
) {
672 if (event_context_type
&&
673 bt_ctf_field_type_get_type_id(event_context_type
) !=
674 BT_CTF_FIELD_TYPE_ID_STRUCT
) {
675 /* A packet context must be a structure. */
680 bt_put(stream_class
->event_context_type
);
681 stream_class
->event_context_type
= bt_get(event_context_type
);
686 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
688 bt_get(stream_class
);
691 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
693 bt_put(stream_class
);
697 int get_event_class_count(void *element
)
699 return bt_ctf_stream_class_get_event_class_count(
700 (struct bt_ctf_stream_class
*) element
);
704 void *get_event_class(void *element
, int i
)
706 return bt_ctf_stream_class_get_event_class(
707 (struct bt_ctf_stream_class
*) element
, i
);
711 int visit_event_class(void *object
, bt_ctf_visitor visitor
,void *data
)
713 struct bt_ctf_object obj
=
715 .type
= BT_CTF_OBJECT_TYPE_EVENT_CLASS
};
717 return visitor(&obj
, data
);
720 int bt_ctf_stream_class_visit(struct bt_ctf_stream_class
*stream_class
,
721 bt_ctf_visitor visitor
, void *data
)
724 struct bt_ctf_object obj
=
725 { .object
= stream_class
,
726 .type
= BT_CTF_OBJECT_TYPE_STREAM_CLASS
};
728 if (!stream_class
|| !visitor
) {
733 ret
= visitor_helper(&obj
, get_event_class_count
,
735 visit_event_class
, visitor
, data
);
741 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
747 stream_class
->frozen
= 1;
748 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
749 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
750 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
752 if (stream_class
->clock
) {
753 bt_ctf_clock_class_freeze(stream_class
->clock
->clock_class
);
758 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
759 struct metadata_context
*context
)
764 g_string_assign(context
->field_name
, "");
765 context
->current_indentation_level
= 1;
766 if (!stream_class
->id_set
) {
771 g_string_append_printf(context
->string
,
772 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
774 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
780 if (stream_class
->packet_context_type
) {
781 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
782 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
789 if (stream_class
->event_context_type
) {
790 g_string_append(context
->string
, ";\n\n\tevent.context := ");
791 ret
= bt_ctf_field_type_serialize(
792 stream_class
->event_context_type
, context
);
798 g_string_append(context
->string
, ";\n};\n\n");
799 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
800 struct bt_ctf_event_class
*event_class
=
801 stream_class
->event_classes
->pdata
[i
];
803 ret
= bt_ctf_event_class_serialize(event_class
, context
);
809 context
->current_indentation_level
= 0;
814 void bt_ctf_stream_class_destroy(struct bt_object
*obj
)
816 struct bt_ctf_stream_class
*stream_class
;
818 stream_class
= container_of(obj
, struct bt_ctf_stream_class
, base
);
819 bt_put(stream_class
->clock
);
821 if (stream_class
->event_classes_ht
) {
822 g_hash_table_destroy(stream_class
->event_classes_ht
);
824 if (stream_class
->event_classes
) {
825 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
828 if (stream_class
->name
) {
829 g_string_free(stream_class
->name
, TRUE
);
832 bt_put(stream_class
->event_header_type
);
833 bt_put(stream_class
->packet_context_type
);
834 bt_put(stream_class
->event_context_type
);
835 g_free(stream_class
);
839 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
842 struct bt_ctf_field_type
*event_header_type
=
843 bt_ctf_field_type_structure_create();
844 struct bt_ctf_field_type
*_uint32_t
=
845 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
846 struct bt_ctf_field_type
*_uint64_t
=
847 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
849 if (!event_header_type
) {
854 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
860 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
861 _uint64_t
, "timestamp");
866 if (stream_class
->event_header_type
) {
867 bt_put(stream_class
->event_header_type
);
869 stream_class
->event_header_type
= event_header_type
;
872 bt_put(event_header_type
);
881 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
884 struct bt_ctf_field_type
*packet_context_type
=
885 bt_ctf_field_type_structure_create();
886 struct bt_ctf_field_type
*_uint64_t
=
887 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
889 if (!packet_context_type
) {
895 * We create a stream packet context as proposed in the CTF
898 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
899 _uint64_t
, "timestamp_begin");
904 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
905 _uint64_t
, "timestamp_end");
910 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
911 _uint64_t
, "content_size");
916 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
917 _uint64_t
, "packet_size");
922 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
923 _uint64_t
, "events_discarded");
928 bt_put(stream_class
->packet_context_type
);
929 stream_class
->packet_context_type
= packet_context_type
;
932 bt_put(packet_context_type
);