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 const char *bt_ctf_stream_class_get_name(
91 struct bt_ctf_stream_class
*stream_class
)
93 const char *name
= NULL
;
99 name
= stream_class
->name
->str
;
104 int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class
*stream_class
,
109 if (!stream_class
|| stream_class
->frozen
) {
114 g_string_assign(stream_class
->name
, name
);
119 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
120 struct bt_ctf_stream_class
*stream_class
)
122 struct bt_ctf_clock
*clock
= NULL
;
124 if (!stream_class
|| !stream_class
->clock
) {
128 clock
= stream_class
->clock
;
129 bt_ctf_clock_get(clock
);
134 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
135 struct bt_ctf_clock
*clock
)
138 struct bt_ctf_field_type
*timestamp_field
= NULL
;
140 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
146 * Look for a "timestamp" field in the stream class' event header type
147 * and map the stream's clock to that field if no current mapping is
150 timestamp_field
= bt_ctf_field_type_structure_get_field_type_by_name(
151 stream_class
->event_header_type
, "timestamp");
152 if (timestamp_field
) {
153 struct bt_ctf_clock
*mapped_clock
;
155 mapped_clock
= bt_ctf_field_type_integer_get_mapped_clock(
158 bt_ctf_clock_put(mapped_clock
);
162 ret
= bt_ctf_field_type_integer_set_mapped_clock(
163 timestamp_field
, clock
);
169 if (stream_class
->clock
) {
170 bt_ctf_clock_put(stream_class
->clock
);
173 stream_class
->clock
= clock
;
174 bt_ctf_clock_get(clock
);
176 if (timestamp_field
) {
177 bt_ctf_field_type_put(timestamp_field
);
182 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
186 if (!stream_class
|| !stream_class
->id_set
) {
191 ret
= (int64_t) stream_class
->id
;
197 int _bt_ctf_stream_class_set_id(
198 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
200 stream_class
->id
= id
;
201 stream_class
->id_set
= 1;
205 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
210 if (!stream_class
|| stream_class
->frozen
) {
215 ret
= _bt_ctf_stream_class_set_id(stream_class
, id
);
224 void event_class_exists(gpointer element
, gpointer query
)
226 struct bt_ctf_event_class
*event_class_a
= element
;
227 struct search_query
*search_query
= query
;
228 struct bt_ctf_event_class
*event_class_b
= search_query
->value
;
231 if (search_query
->value
== element
) {
232 search_query
->found
= 1;
237 * Two event classes cannot share the same name in a given
240 if (!strcmp(bt_ctf_event_class_get_name(event_class_a
),
241 bt_ctf_event_class_get_name(event_class_b
))) {
242 search_query
->found
= 1;
247 * Two event classes cannot share the same ID in a given
250 id_a
= bt_ctf_event_class_get_id(event_class_a
);
251 id_b
= bt_ctf_event_class_get_id(event_class_b
);
253 if (id_a
< 0 || id_b
< 0) {
254 /* at least one ID is not set: will be automatically set later */
259 search_query
->found
= 1;
267 int bt_ctf_stream_class_add_event_class(
268 struct bt_ctf_stream_class
*stream_class
,
269 struct bt_ctf_event_class
*event_class
)
274 if (!stream_class
|| !event_class
) {
279 /* Check for duplicate event classes */
280 struct search_query query
= { .value
= event_class
, .found
= 0 };
281 g_ptr_array_foreach(stream_class
->event_classes
, event_class_exists
,
288 /* Only set an event id if none was explicitly set before */
289 event_id
= bt_ctf_event_class_get_id(event_class
);
291 if (bt_ctf_event_class_set_id(event_class
,
292 stream_class
->next_event_id
++)) {
298 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
303 bt_ctf_event_class_get(event_class
);
304 g_ptr_array_add(stream_class
->event_classes
, event_class
);
305 bt_ctf_event_class_freeze(event_class
);
307 if (stream_class
->byte_order
) {
309 * Only set native byte order if it has been initialized
310 * when the stream class was added to a trace.
312 * If not set here, this will be set when the stream
313 * classe will be added to a trace.
315 bt_ctf_event_class_set_native_byte_order(event_class
,
316 stream_class
->byte_order
);
322 int bt_ctf_stream_class_get_event_class_count(
323 struct bt_ctf_stream_class
*stream_class
)
332 ret
= (int) stream_class
->event_classes
->len
;
337 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
338 struct bt_ctf_stream_class
*stream_class
, int index
)
340 struct bt_ctf_event_class
*event_class
= NULL
;
342 if (!stream_class
|| index
< 0 ||
343 index
>= stream_class
->event_classes
->len
) {
347 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
348 bt_ctf_event_class_get(event_class
);
353 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
354 struct bt_ctf_stream_class
*stream_class
, const char *name
)
357 struct bt_ctf_event_class
*event_class
= NULL
;
359 if (!stream_class
|| !name
) {
363 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
364 struct bt_ctf_event_class
*cur_event_class
=
365 g_ptr_array_index(stream_class
->event_classes
, i
);
366 const char *cur_event_class_name
=
367 bt_ctf_event_class_get_name(cur_event_class
);
369 if (!strcmp(name
, cur_event_class_name
)) {
370 event_class
= cur_event_class
;
371 bt_ctf_event_class_get(event_class
);
379 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_id(
380 struct bt_ctf_stream_class
*stream_class
, uint32_t id
)
383 struct bt_ctf_event_class
*event_class
= NULL
;
389 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
390 struct bt_ctf_event_class
*current_event_class
=
391 g_ptr_array_index(stream_class
->event_classes
, i
);
393 if (bt_ctf_event_class_get_id(current_event_class
) == id
) {
394 event_class
= current_event_class
;
395 bt_ctf_event_class_get(event_class
);
403 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
404 struct bt_ctf_stream_class
*stream_class
)
406 struct bt_ctf_field_type
*ret
= NULL
;
412 assert(stream_class
->packet_context_type
);
413 bt_ctf_field_type_get(stream_class
->packet_context_type
);
414 ret
= stream_class
->packet_context_type
;
419 int bt_ctf_stream_class_set_packet_context_type(
420 struct bt_ctf_stream_class
*stream_class
,
421 struct bt_ctf_field_type
*packet_context_type
)
425 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
430 assert(stream_class
->packet_context_type
);
431 if (stream_class
->packet_context_type
== packet_context_type
) {
434 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
436 /* A packet context must be a structure */
441 bt_ctf_field_type_put(stream_class
->packet_context_type
);
442 bt_ctf_field_type_get(packet_context_type
);
443 stream_class
->packet_context_type
= packet_context_type
;
448 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
449 struct bt_ctf_stream_class
*stream_class
)
451 struct bt_ctf_field_type
*ret
= NULL
;
453 if (!stream_class
|| !stream_class
->event_header_type
) {
457 assert(stream_class
->event_header_type
);
458 bt_ctf_field_type_get(stream_class
->event_header_type
);
459 ret
= stream_class
->event_header_type
;
464 int bt_ctf_stream_class_set_event_header_type(
465 struct bt_ctf_stream_class
*stream_class
,
466 struct bt_ctf_field_type
*event_header_type
)
470 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
475 assert(stream_class
->event_header_type
);
476 if (stream_class
->event_header_type
== event_header_type
) {
479 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
481 /* An event header must be a structure */
486 bt_ctf_field_type_put(stream_class
->event_header_type
);
487 bt_ctf_field_type_get(event_header_type
);
488 stream_class
->event_header_type
= event_header_type
;
493 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
494 struct bt_ctf_stream_class
*stream_class
)
496 struct bt_ctf_field_type
*ret
= NULL
;
498 if (!stream_class
|| !stream_class
->event_context_type
) {
502 assert(stream_class
->event_context_type
);
503 bt_ctf_field_type_get(stream_class
->event_context_type
);
504 ret
= stream_class
->event_context_type
;
509 int bt_ctf_stream_class_set_event_context_type(
510 struct bt_ctf_stream_class
*stream_class
,
511 struct bt_ctf_field_type
*event_context_type
)
515 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
520 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
522 /* A packet context must be a structure */
527 bt_ctf_field_type_put(stream_class
->event_context_type
);
528 bt_ctf_field_type_get(event_context_type
);
529 stream_class
->event_context_type
= event_context_type
;
534 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
540 bt_ctf_ref_get(&stream_class
->ref_count
);
543 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
549 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
553 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
559 stream_class
->frozen
= 1;
560 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
561 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
562 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
563 bt_ctf_clock_freeze(stream_class
->clock
);
567 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
568 enum bt_ctf_byte_order byte_order
)
571 int internal_byte_order
;
573 /* Note that "NATIVE" means the trace's endianness, not the host's. */
574 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
575 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
580 switch (byte_order
) {
581 case BT_CTF_BYTE_ORDER_NETWORK
:
582 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
583 internal_byte_order
= BIG_ENDIAN
;
585 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
586 internal_byte_order
= LITTLE_ENDIAN
;
593 stream_class
->byte_order
= internal_byte_order
;
595 /* Set native byte order to little or big endian */
596 bt_ctf_field_type_set_native_byte_order(
597 stream_class
->event_header_type
, stream_class
->byte_order
);
598 bt_ctf_field_type_set_native_byte_order(
599 stream_class
->packet_context_type
, stream_class
->byte_order
);
600 bt_ctf_field_type_set_native_byte_order(
601 stream_class
->event_context_type
, stream_class
->byte_order
);
603 /* Set all events' native byte order */
604 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
605 bt_ctf_event_class_set_native_byte_order(
606 g_ptr_array_index(stream_class
->event_classes
, i
),
607 stream_class
->byte_order
);
608 bt_ctf_event_class_freeze(
609 g_ptr_array_index(stream_class
->event_classes
, i
));
616 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
617 struct metadata_context
*context
)
622 g_string_assign(context
->field_name
, "");
623 context
->current_indentation_level
= 1;
624 if (!stream_class
->id_set
) {
629 g_string_append_printf(context
->string
,
630 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
632 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
638 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
639 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
645 if (stream_class
->event_context_type
) {
646 g_string_append(context
->string
, ";\n\n\tevent.context := ");
647 ret
= bt_ctf_field_type_serialize(
648 stream_class
->event_context_type
, context
);
654 g_string_append(context
->string
, ";\n};\n\n");
655 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
656 struct bt_ctf_event_class
*event_class
=
657 stream_class
->event_classes
->pdata
[i
];
659 ret
= bt_ctf_event_class_serialize(event_class
, context
);
665 context
->current_indentation_level
= 0;
670 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
672 struct bt_ctf_stream_class
*stream_class
;
678 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
679 bt_ctf_clock_put(stream_class
->clock
);
681 if (stream_class
->event_classes
) {
684 /* Unregister this stream class from the event classes */
685 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
686 struct bt_ctf_event_class
*event_class
=
687 g_ptr_array_index(stream_class
->event_classes
,
690 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
693 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
696 if (stream_class
->name
) {
697 g_string_free(stream_class
->name
, TRUE
);
700 bt_ctf_field_type_put(stream_class
->event_header_type
);
701 bt_ctf_field_type_put(stream_class
->packet_context_type
);
702 if (stream_class
->event_context_type
) {
703 bt_ctf_field_type_put(stream_class
->event_context_type
);
705 g_free(stream_class
);
709 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
712 struct bt_ctf_field_type
*event_header_type
=
713 bt_ctf_field_type_structure_create();
714 struct bt_ctf_field_type
*_uint32_t
=
715 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
716 struct bt_ctf_field_type
*_uint64_t
=
717 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
719 if (!event_header_type
) {
724 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
730 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
731 _uint64_t
, "timestamp");
736 if (stream_class
->event_header_type
) {
737 bt_ctf_field_type_put(stream_class
->event_header_type
);
739 stream_class
->event_header_type
= event_header_type
;
742 bt_ctf_field_type_put(event_header_type
);
745 bt_ctf_field_type_put(_uint32_t
);
746 bt_ctf_field_type_put(_uint64_t
);
751 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
754 struct bt_ctf_field_type
*packet_context_type
=
755 bt_ctf_field_type_structure_create();
756 struct bt_ctf_field_type
*_uint64_t
=
757 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
759 if (!packet_context_type
) {
765 * We create a stream packet context as proposed in the CTF
768 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
769 _uint64_t
, "timestamp_begin");
774 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
775 _uint64_t
, "timestamp_end");
780 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
781 _uint64_t
, "content_size");
786 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
787 _uint64_t
, "packet_size");
792 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
793 _uint64_t
, "events_discarded");
798 if (stream_class
->packet_context_type
) {
799 bt_ctf_field_type_put(stream_class
->packet_context_type
);
801 stream_class
->packet_context_type
= packet_context_type
;
804 bt_ctf_field_type_put(packet_context_type
);
808 bt_ctf_field_type_put(_uint64_t
);