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
);
223 int bt_ctf_stream_class_add_event_class(
224 struct bt_ctf_stream_class
*stream_class
,
225 struct bt_ctf_event_class
*event_class
)
230 if (!stream_class
|| !event_class
) {
235 /* Check for duplicate event classes */
236 struct search_query query
= { .value
= event_class
, .found
= 0 };
237 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
243 /* Only set an event id if none was explicitly set before */
244 event_id
= bt_ctf_event_class_get_id(event_class
);
246 if (bt_ctf_event_class_set_id(event_class
,
247 stream_class
->next_event_id
++)) {
253 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
258 bt_ctf_event_class_get(event_class
);
259 g_ptr_array_add(stream_class
->event_classes
, event_class
);
260 bt_ctf_event_class_freeze(event_class
);
262 if (stream_class
->byte_order
) {
264 * Only set native byte order if it has been initialized
265 * when the stream class was added to a trace.
267 * If not set here, this will be set when the stream
268 * classe will be added to a trace.
270 bt_ctf_event_class_set_native_byte_order(event_class
,
271 stream_class
->byte_order
);
277 int bt_ctf_stream_class_get_event_class_count(
278 struct bt_ctf_stream_class
*stream_class
)
287 ret
= (int) stream_class
->event_classes
->len
;
292 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
293 struct bt_ctf_stream_class
*stream_class
, int index
)
295 struct bt_ctf_event_class
*event_class
= NULL
;
297 if (!stream_class
|| index
< 0 ||
298 index
>= stream_class
->event_classes
->len
) {
302 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
303 bt_ctf_event_class_get(event_class
);
308 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
309 struct bt_ctf_stream_class
*stream_class
, const char *name
)
313 struct bt_ctf_event_class
*event_class
= NULL
;
315 if (!stream_class
|| !name
) {
319 name_quark
= g_quark_try_string(name
);
324 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
325 struct bt_ctf_event_class
*current_event_class
=
326 g_ptr_array_index(stream_class
->event_classes
, i
);
328 if (name_quark
== current_event_class
->name
) {
329 event_class
= current_event_class
;
330 bt_ctf_event_class_get(event_class
);
338 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
339 struct bt_ctf_stream_class
*stream_class
)
341 struct bt_ctf_field_type
*ret
= NULL
;
347 assert(stream_class
->packet_context_type
);
348 bt_ctf_field_type_get(stream_class
->packet_context_type
);
349 ret
= stream_class
->packet_context_type
;
354 int bt_ctf_stream_class_set_packet_context_type(
355 struct bt_ctf_stream_class
*stream_class
,
356 struct bt_ctf_field_type
*packet_context_type
)
360 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
365 assert(stream_class
->packet_context_type
);
366 if (stream_class
->packet_context_type
== packet_context_type
) {
369 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
371 /* A packet context must be a structure */
376 bt_ctf_field_type_put(stream_class
->packet_context_type
);
377 bt_ctf_field_type_get(packet_context_type
);
378 stream_class
->packet_context_type
= packet_context_type
;
383 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
384 struct bt_ctf_stream_class
*stream_class
)
386 struct bt_ctf_field_type
*ret
= NULL
;
388 if (!stream_class
|| !stream_class
->event_header_type
) {
392 assert(stream_class
->event_header_type
);
393 bt_ctf_field_type_get(stream_class
->event_header_type
);
394 ret
= stream_class
->event_header_type
;
399 int bt_ctf_stream_class_set_event_header_type(
400 struct bt_ctf_stream_class
*stream_class
,
401 struct bt_ctf_field_type
*event_header_type
)
405 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
410 assert(stream_class
->event_header_type
);
411 if (stream_class
->event_header_type
== event_header_type
) {
414 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
416 /* An event header must be a structure */
421 bt_ctf_field_type_put(stream_class
->event_header_type
);
422 bt_ctf_field_type_get(event_header_type
);
423 stream_class
->event_header_type
= event_header_type
;
428 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
429 struct bt_ctf_stream_class
*stream_class
)
431 struct bt_ctf_field_type
*ret
= NULL
;
433 if (!stream_class
|| !stream_class
->event_context_type
) {
437 assert(stream_class
->event_context_type
);
438 bt_ctf_field_type_get(stream_class
->event_context_type
);
439 ret
= stream_class
->event_context_type
;
444 int bt_ctf_stream_class_set_event_context_type(
445 struct bt_ctf_stream_class
*stream_class
,
446 struct bt_ctf_field_type
*event_context_type
)
450 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
455 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
457 /* A packet context must be a structure */
462 bt_ctf_field_type_put(stream_class
->event_context_type
);
463 bt_ctf_field_type_get(event_context_type
);
464 stream_class
->event_context_type
= event_context_type
;
469 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
475 bt_ctf_ref_get(&stream_class
->ref_count
);
478 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
484 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
488 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
494 stream_class
->frozen
= 1;
495 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
496 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
497 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
498 bt_ctf_clock_freeze(stream_class
->clock
);
502 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
503 enum bt_ctf_byte_order byte_order
)
506 int internal_byte_order
;
508 /* Note that "NATIVE" means the trace's endianness, not the host's. */
509 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
510 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
) {
515 switch (byte_order
) {
516 case BT_CTF_BYTE_ORDER_NETWORK
:
517 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
518 internal_byte_order
= BIG_ENDIAN
;
520 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
521 internal_byte_order
= LITTLE_ENDIAN
;
528 stream_class
->byte_order
= internal_byte_order
;
530 /* Set native byte order to little or big endian */
531 bt_ctf_field_type_set_native_byte_order(
532 stream_class
->event_header_type
, stream_class
->byte_order
);
533 bt_ctf_field_type_set_native_byte_order(
534 stream_class
->packet_context_type
, stream_class
->byte_order
);
535 bt_ctf_field_type_set_native_byte_order(
536 stream_class
->event_context_type
, stream_class
->byte_order
);
538 /* Set all events' native byte order */
539 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
540 bt_ctf_event_class_set_native_byte_order(
541 g_ptr_array_index(stream_class
->event_classes
, i
),
542 stream_class
->byte_order
);
543 bt_ctf_event_class_freeze(
544 g_ptr_array_index(stream_class
->event_classes
, i
));
551 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
552 struct metadata_context
*context
)
557 g_string_assign(context
->field_name
, "");
558 context
->current_indentation_level
= 1;
559 if (!stream_class
->id_set
) {
564 g_string_append_printf(context
->string
,
565 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
567 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
573 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
574 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
580 if (stream_class
->event_context_type
) {
581 g_string_append(context
->string
, ";\n\n\tevent.context := ");
582 ret
= bt_ctf_field_type_serialize(
583 stream_class
->event_context_type
, context
);
589 g_string_append(context
->string
, ";\n};\n\n");
590 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
591 struct bt_ctf_event_class
*event_class
=
592 stream_class
->event_classes
->pdata
[i
];
594 ret
= bt_ctf_event_class_serialize(event_class
, context
);
600 context
->current_indentation_level
= 0;
605 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
607 struct bt_ctf_stream_class
*stream_class
;
613 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
614 bt_ctf_clock_put(stream_class
->clock
);
616 if (stream_class
->event_classes
) {
619 /* Unregister this stream class from the event classes */
620 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
621 struct bt_ctf_event_class
*event_class
=
622 g_ptr_array_index(stream_class
->event_classes
,
625 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
628 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
631 if (stream_class
->name
) {
632 g_string_free(stream_class
->name
, TRUE
);
635 bt_ctf_field_type_put(stream_class
->event_header_type
);
636 bt_ctf_field_type_put(stream_class
->packet_context_type
);
637 if (stream_class
->event_context_type
) {
638 bt_ctf_field_type_put(stream_class
->event_context_type
);
640 g_free(stream_class
);
644 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
647 struct bt_ctf_field_type
*event_header_type
=
648 bt_ctf_field_type_structure_create();
649 struct bt_ctf_field_type
*_uint32_t
=
650 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
651 struct bt_ctf_field_type
*_uint64_t
=
652 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
654 if (!event_header_type
) {
659 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
665 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
666 _uint64_t
, "timestamp");
671 if (stream_class
->event_header_type
) {
672 bt_ctf_field_type_put(stream_class
->event_header_type
);
674 stream_class
->event_header_type
= event_header_type
;
677 bt_ctf_field_type_put(event_header_type
);
680 bt_ctf_field_type_put(_uint32_t
);
681 bt_ctf_field_type_put(_uint64_t
);
686 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
689 struct bt_ctf_field_type
*packet_context_type
=
690 bt_ctf_field_type_structure_create();
691 struct bt_ctf_field_type
*_uint64_t
=
692 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
694 if (!packet_context_type
) {
700 * We create a stream packet context as proposed in the CTF
703 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
704 _uint64_t
, "timestamp_begin");
709 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
710 _uint64_t
, "timestamp_end");
715 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
716 _uint64_t
, "content_size");
721 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
722 _uint64_t
, "packet_size");
727 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
728 _uint64_t
, "events_discarded");
733 if (stream_class
->packet_context_type
) {
734 bt_ctf_field_type_put(stream_class
->packet_context_type
);
736 stream_class
->packet_context_type
= packet_context_type
;
739 bt_ctf_field_type_put(packet_context_type
);
743 bt_ctf_field_type_put(_uint64_t
);