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
|| !strlen(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 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
105 struct bt_ctf_stream_class
*stream_class
)
107 struct bt_ctf_clock
*clock
= NULL
;
109 if (!stream_class
|| !stream_class
->clock
) {
113 clock
= stream_class
->clock
;
114 bt_ctf_clock_get(clock
);
119 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
120 struct bt_ctf_clock
*clock
)
124 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
129 if (stream_class
->clock
) {
130 bt_ctf_clock_put(stream_class
->clock
);
133 stream_class
->clock
= clock
;
134 bt_ctf_clock_get(clock
);
139 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
143 if (!stream_class
|| !stream_class
->id_set
) {
148 ret
= (int64_t) stream_class
->id
;
153 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
158 if (!stream_class
|| stream_class
->frozen
) {
163 stream_class
->id
= id
;
164 stream_class
->id_set
= 1;
169 int bt_ctf_stream_class_add_event_class(
170 struct bt_ctf_stream_class
*stream_class
,
171 struct bt_ctf_event_class
*event_class
)
176 if (!stream_class
|| !event_class
) {
181 /* Check for duplicate event classes */
182 struct search_query query
= { .value
= event_class
, .found
= 0 };
183 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
189 /* Only set an event id if none was explicitly set before */
190 event_id
= bt_ctf_event_class_get_id(event_class
);
192 if (bt_ctf_event_class_set_id(event_class
,
193 stream_class
->next_event_id
++)) {
199 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
204 bt_ctf_event_class_get(event_class
);
205 g_ptr_array_add(stream_class
->event_classes
, event_class
);
210 int bt_ctf_stream_class_get_event_class_count(
211 struct bt_ctf_stream_class
*stream_class
)
220 ret
= (int) stream_class
->event_classes
->len
;
225 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
226 struct bt_ctf_stream_class
*stream_class
, int index
)
228 struct bt_ctf_event_class
*event_class
= NULL
;
230 if (!stream_class
|| index
< 0 ||
231 index
>= stream_class
->event_classes
->len
) {
235 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
236 bt_ctf_event_class_get(event_class
);
241 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
242 struct bt_ctf_stream_class
*stream_class
, const char *name
)
246 struct bt_ctf_event_class
*event_class
= NULL
;
248 if (!stream_class
|| !name
) {
252 name_quark
= g_quark_try_string(name
);
257 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
258 struct bt_ctf_event_class
*current_event_class
=
259 g_ptr_array_index(stream_class
->event_classes
, i
);
261 if (name_quark
== current_event_class
->name
) {
262 event_class
= current_event_class
;
263 bt_ctf_event_class_get(event_class
);
271 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
272 struct bt_ctf_stream_class
*stream_class
)
274 struct bt_ctf_field_type
*ret
= NULL
;
280 assert(stream_class
->packet_context_type
);
281 bt_ctf_field_type_get(stream_class
->packet_context_type
);
282 ret
= stream_class
->packet_context_type
;
287 int bt_ctf_stream_class_set_packet_context_type(
288 struct bt_ctf_stream_class
*stream_class
,
289 struct bt_ctf_field_type
*packet_context_type
)
293 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
298 assert(stream_class
->packet_context_type
);
299 if (stream_class
->packet_context_type
== packet_context_type
) {
302 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
304 /* A packet context must be a structure */
309 bt_ctf_field_type_put(stream_class
->packet_context_type
);
310 bt_ctf_field_type_get(packet_context_type
);
311 stream_class
->packet_context_type
= packet_context_type
;
316 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_header_type(
317 struct bt_ctf_stream_class
*stream_class
)
319 struct bt_ctf_field_type
*ret
= NULL
;
321 if (!stream_class
|| !stream_class
->event_header_type
) {
325 assert(stream_class
->event_header_type
);
326 bt_ctf_field_type_get(stream_class
->event_header_type
);
327 ret
= stream_class
->event_header_type
;
332 int bt_ctf_stream_class_set_event_header_type(
333 struct bt_ctf_stream_class
*stream_class
,
334 struct bt_ctf_field_type
*event_header_type
)
338 if (!stream_class
|| !event_header_type
|| stream_class
->frozen
) {
343 assert(stream_class
->event_header_type
);
344 if (stream_class
->event_header_type
== event_header_type
) {
347 if (bt_ctf_field_type_get_type_id(event_header_type
) !=
349 /* An event header must be a structure */
354 bt_ctf_field_type_put(stream_class
->event_header_type
);
355 bt_ctf_field_type_get(event_header_type
);
356 stream_class
->event_header_type
= event_header_type
;
361 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
362 struct bt_ctf_stream_class
*stream_class
)
364 struct bt_ctf_field_type
*ret
= NULL
;
366 if (!stream_class
|| !stream_class
->event_context_type
) {
370 assert(stream_class
->event_context_type
);
371 bt_ctf_field_type_get(stream_class
->event_context_type
);
372 ret
= stream_class
->event_context_type
;
377 int bt_ctf_stream_class_set_event_context_type(
378 struct bt_ctf_stream_class
*stream_class
,
379 struct bt_ctf_field_type
*event_context_type
)
383 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
388 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
390 /* A packet context must be a structure */
395 bt_ctf_field_type_put(stream_class
->event_context_type
);
396 bt_ctf_field_type_get(event_context_type
);
397 stream_class
->event_context_type
= event_context_type
;
402 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
408 bt_ctf_ref_get(&stream_class
->ref_count
);
411 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
417 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
421 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
429 stream_class
->frozen
= 1;
430 bt_ctf_field_type_freeze(stream_class
->event_header_type
);
431 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
432 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
433 bt_ctf_clock_freeze(stream_class
->clock
);
435 bt_ctf_field_type_set_native_byte_order(
436 stream_class
->event_header_type
, stream_class
->byte_order
);
437 bt_ctf_field_type_set_native_byte_order(
438 stream_class
->packet_context_type
, stream_class
->byte_order
);
439 bt_ctf_field_type_set_native_byte_order(
440 stream_class
->event_context_type
, stream_class
->byte_order
);
441 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
442 bt_ctf_event_class_set_native_byte_order(
443 g_ptr_array_index(stream_class
->event_classes
, i
),
444 stream_class
->byte_order
);
445 bt_ctf_event_class_freeze(
446 g_ptr_array_index(stream_class
->event_classes
, i
));
451 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
452 enum bt_ctf_byte_order byte_order
)
455 int internal_byte_order
;
457 /* Note that "NATIVE" means the trace's endianness, not the host's. */
458 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
459 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
||
460 stream_class
->frozen
) {
465 switch (byte_order
) {
466 case BT_CTF_BYTE_ORDER_NETWORK
:
467 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
468 internal_byte_order
= BIG_ENDIAN
;
470 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
471 internal_byte_order
= LITTLE_ENDIAN
;
478 stream_class
->byte_order
= internal_byte_order
;
484 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
485 struct metadata_context
*context
)
490 g_string_assign(context
->field_name
, "");
491 context
->current_indentation_level
= 1;
492 if (!stream_class
->id_set
) {
497 g_string_append_printf(context
->string
,
498 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
500 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
506 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
507 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
513 if (stream_class
->event_context_type
) {
514 g_string_append(context
->string
, ";\n\n\tevent.context := ");
515 ret
= bt_ctf_field_type_serialize(
516 stream_class
->event_context_type
, context
);
522 g_string_append(context
->string
, ";\n};\n\n");
523 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
524 struct bt_ctf_event_class
*event_class
=
525 stream_class
->event_classes
->pdata
[i
];
527 ret
= bt_ctf_event_class_serialize(event_class
, context
);
533 context
->current_indentation_level
= 0;
538 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
540 struct bt_ctf_stream_class
*stream_class
;
546 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
547 bt_ctf_clock_put(stream_class
->clock
);
549 if (stream_class
->event_classes
) {
552 /* Unregister this stream class from the event classes */
553 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
554 struct bt_ctf_event_class
*event_class
=
555 g_ptr_array_index(stream_class
->event_classes
,
558 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
561 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
564 if (stream_class
->name
) {
565 g_string_free(stream_class
->name
, TRUE
);
568 bt_ctf_field_type_put(stream_class
->event_header_type
);
569 bt_ctf_field_type_put(stream_class
->packet_context_type
);
570 if (stream_class
->event_context_type
) {
571 bt_ctf_field_type_put(stream_class
->event_context_type
);
573 g_free(stream_class
);
577 int init_event_header(struct bt_ctf_stream_class
*stream_class
)
580 struct bt_ctf_field_type
*event_header_type
=
581 bt_ctf_field_type_structure_create();
582 struct bt_ctf_field_type
*_uint32_t
=
583 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
584 struct bt_ctf_field_type
*_uint64_t
=
585 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
587 if (!event_header_type
) {
592 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
598 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
599 _uint64_t
, "timestamp");
604 if (stream_class
->event_header_type
) {
605 bt_ctf_field_type_put(stream_class
->event_header_type
);
607 stream_class
->event_header_type
= event_header_type
;
610 bt_ctf_field_type_put(event_header_type
);
613 bt_ctf_field_type_put(_uint32_t
);
614 bt_ctf_field_type_put(_uint64_t
);
619 int init_packet_context(struct bt_ctf_stream_class
*stream_class
)
622 struct bt_ctf_field_type
*packet_context_type
=
623 bt_ctf_field_type_structure_create();
624 struct bt_ctf_field_type
*_uint64_t
=
625 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
627 if (!packet_context_type
) {
633 * We create a stream packet context as proposed in the CTF
636 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
637 _uint64_t
, "timestamp_begin");
642 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
643 _uint64_t
, "timestamp_end");
648 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
649 _uint64_t
, "content_size");
654 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
655 _uint64_t
, "packet_size");
660 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
661 _uint64_t
, "events_discarded");
666 if (stream_class
->packet_context_type
) {
667 bt_ctf_field_type_put(stream_class
->packet_context_type
);
669 stream_class
->packet_context_type
= packet_context_type
;
672 bt_ctf_field_type_put(packet_context_type
);
676 bt_ctf_field_type_put(_uint64_t
);