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
,
46 enum bt_ctf_byte_order byte_order
);
48 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
49 enum bt_ctf_byte_order byte_order
);
51 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
54 struct bt_ctf_stream_class
*stream_class
= NULL
;
56 if (!name
|| !strlen(name
) || bt_ctf_validate_identifier(name
)) {
60 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
65 stream_class
->name
= g_string_new(name
);
66 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
67 (GDestroyNotify
)bt_ctf_event_class_put
);
68 if (!stream_class
->event_classes
) {
72 ret
= init_packet_context(stream_class
, BT_CTF_BYTE_ORDER_NATIVE
);
77 bt_ctf_ref_init(&stream_class
->ref_count
);
81 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
87 const char *bt_ctf_stream_class_get_name(
88 struct bt_ctf_stream_class
*stream_class
)
90 const char *name
= NULL
;
96 name
= stream_class
->name
->str
;
101 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
102 struct bt_ctf_stream_class
*stream_class
)
104 struct bt_ctf_clock
*clock
= NULL
;
106 if (!stream_class
|| !stream_class
->clock
) {
110 clock
= stream_class
->clock
;
111 bt_ctf_clock_get(clock
);
116 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
117 struct bt_ctf_clock
*clock
)
121 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
126 if (stream_class
->clock
) {
127 bt_ctf_clock_put(stream_class
->clock
);
130 stream_class
->clock
= clock
;
131 bt_ctf_clock_get(clock
);
136 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
140 if (!stream_class
|| !stream_class
->id_set
) {
145 ret
= (int64_t) stream_class
->id
;
150 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
155 if (!stream_class
|| stream_class
->frozen
) {
160 stream_class
->id
= id
;
161 stream_class
->id_set
= 1;
166 int bt_ctf_stream_class_add_event_class(
167 struct bt_ctf_stream_class
*stream_class
,
168 struct bt_ctf_event_class
*event_class
)
173 if (!stream_class
|| !event_class
) {
178 /* Check for duplicate event classes */
179 struct search_query query
= { .value
= event_class
, .found
= 0 };
180 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
186 /* Only set an event id if none was explicitly set before */
187 event_id
= bt_ctf_event_class_get_id(event_class
);
189 if (bt_ctf_event_class_set_id(event_class
,
190 stream_class
->next_event_id
++)) {
196 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
201 bt_ctf_event_class_get(event_class
);
202 g_ptr_array_add(stream_class
->event_classes
, event_class
);
207 int bt_ctf_stream_class_get_event_class_count(
208 struct bt_ctf_stream_class
*stream_class
)
217 ret
= (int) stream_class
->event_classes
->len
;
222 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
223 struct bt_ctf_stream_class
*stream_class
, int index
)
225 struct bt_ctf_event_class
*event_class
= NULL
;
227 if (!stream_class
|| index
< 0 ||
228 index
>= stream_class
->event_classes
->len
) {
232 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
233 bt_ctf_event_class_get(event_class
);
238 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
239 struct bt_ctf_stream_class
*stream_class
, const char *name
)
243 struct bt_ctf_event_class
*event_class
= NULL
;
245 if (!stream_class
|| !name
) {
249 name_quark
= g_quark_try_string(name
);
254 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
255 struct bt_ctf_event_class
*current_event_class
=
256 g_ptr_array_index(stream_class
->event_classes
, i
);
258 if (name_quark
== current_event_class
->name
) {
259 event_class
= current_event_class
;
260 bt_ctf_event_class_get(event_class
);
268 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
269 struct bt_ctf_stream_class
*stream_class
)
271 struct bt_ctf_field_type
*ret
= NULL
;
277 assert(stream_class
->packet_context_type
);
278 bt_ctf_field_type_get(stream_class
->packet_context_type
);
279 ret
= stream_class
->packet_context_type
;
284 int bt_ctf_stream_class_set_packet_context_type(
285 struct bt_ctf_stream_class
*stream_class
,
286 struct bt_ctf_field_type
*packet_context_type
)
290 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
295 assert(stream_class
->packet_context_type
);
296 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
298 /* A packet context must be a structure */
303 bt_ctf_field_type_put(stream_class
->packet_context_type
);
304 bt_ctf_field_type_get(packet_context_type
);
305 stream_class
->packet_context_type
= packet_context_type
;
310 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
311 struct bt_ctf_stream_class
*stream_class
)
313 struct bt_ctf_field_type
*ret
= NULL
;
315 if (!stream_class
|| !stream_class
->event_context_type
) {
319 assert(stream_class
->event_context_type
);
320 bt_ctf_field_type_get(stream_class
->event_context_type
);
321 ret
= stream_class
->event_context_type
;
326 int bt_ctf_stream_class_set_event_context_type(
327 struct bt_ctf_stream_class
*stream_class
,
328 struct bt_ctf_field_type
*event_context_type
)
332 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
337 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
339 /* A packet context must be a structure */
344 bt_ctf_field_type_put(stream_class
->event_context_type
);
345 bt_ctf_field_type_get(event_context_type
);
346 stream_class
->event_context_type
= event_context_type
;
351 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
357 bt_ctf_ref_get(&stream_class
->ref_count
);
360 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
366 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
370 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
378 stream_class
->frozen
= 1;
379 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
380 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
381 bt_ctf_clock_freeze(stream_class
->clock
);
383 bt_ctf_field_type_set_native_byte_order(
384 stream_class
->event_header_type
, stream_class
->byte_order
);
385 bt_ctf_field_type_set_native_byte_order(
386 stream_class
->packet_context_type
, stream_class
->byte_order
);
387 bt_ctf_field_type_set_native_byte_order(
388 stream_class
->event_context_type
, stream_class
->byte_order
);
389 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
390 bt_ctf_event_class_set_native_byte_order(
391 g_ptr_array_index(stream_class
->event_classes
, i
),
392 stream_class
->byte_order
);
393 bt_ctf_event_class_freeze(
394 g_ptr_array_index(stream_class
->event_classes
, i
));
399 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
400 enum bt_ctf_byte_order byte_order
)
403 int internal_byte_order
;
405 /* Note that "NATIVE" means the trace's endianness, not the host's. */
406 if (!stream_class
|| byte_order
<= BT_CTF_BYTE_ORDER_UNKNOWN
||
407 byte_order
> BT_CTF_BYTE_ORDER_NETWORK
||
408 stream_class
->frozen
) {
413 switch (byte_order
) {
414 case BT_CTF_BYTE_ORDER_NETWORK
:
415 case BT_CTF_BYTE_ORDER_BIG_ENDIAN
:
416 internal_byte_order
= BIG_ENDIAN
;
418 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
:
419 internal_byte_order
= LITTLE_ENDIAN
;
426 stream_class
->byte_order
= internal_byte_order
;
432 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
433 struct metadata_context
*context
)
438 g_string_assign(context
->field_name
, "");
439 context
->current_indentation_level
= 1;
440 if (!stream_class
->id_set
) {
445 g_string_append_printf(context
->string
,
446 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
448 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
454 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
455 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
461 if (stream_class
->event_context_type
) {
462 g_string_append(context
->string
, ";\n\n\tevent.context := ");
463 ret
= bt_ctf_field_type_serialize(
464 stream_class
->event_context_type
, context
);
470 g_string_append(context
->string
, ";\n};\n\n");
471 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
472 struct bt_ctf_event_class
*event_class
=
473 stream_class
->event_classes
->pdata
[i
];
475 ret
= bt_ctf_event_class_serialize(event_class
, context
);
481 context
->current_indentation_level
= 0;
486 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
488 struct bt_ctf_stream_class
*stream_class
;
494 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
495 bt_ctf_clock_put(stream_class
->clock
);
497 if (stream_class
->event_classes
) {
500 /* Unregister this stream class from the event classes */
501 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
502 struct bt_ctf_event_class
*event_class
=
503 g_ptr_array_index(stream_class
->event_classes
,
506 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
509 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
512 if (stream_class
->name
) {
513 g_string_free(stream_class
->name
, TRUE
);
516 bt_ctf_field_type_put(stream_class
->event_header_type
);
517 bt_ctf_field_put(stream_class
->event_header
);
518 bt_ctf_field_type_put(stream_class
->packet_context_type
);
519 if (stream_class
->event_context_type
) {
520 bt_ctf_field_type_put(stream_class
->event_context_type
);
522 g_free(stream_class
);
526 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
527 enum bt_ctf_byte_order byte_order
)
530 struct bt_ctf_field_type
*event_header_type
=
531 bt_ctf_field_type_structure_create();
532 struct bt_ctf_field_type
*_uint32_t
=
533 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
534 struct bt_ctf_field_type
*_uint64_t
=
535 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
537 if (!event_header_type
) {
542 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
548 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
549 _uint64_t
, "timestamp");
554 stream_class
->event_header_type
= event_header_type
;
555 stream_class
->event_header
= bt_ctf_field_create(
556 stream_class
->event_header_type
);
557 if (!stream_class
->event_header
) {
562 bt_ctf_field_type_put(event_header_type
);
565 bt_ctf_field_type_put(_uint32_t
);
566 bt_ctf_field_type_put(_uint64_t
);
571 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
572 enum bt_ctf_byte_order byte_order
)
575 struct bt_ctf_field_type
*packet_context_type
=
576 bt_ctf_field_type_structure_create();
577 struct bt_ctf_field_type
*_uint64_t
=
578 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
580 if (!packet_context_type
) {
586 * We create a stream packet context as proposed in the CTF
589 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
590 _uint64_t
, "timestamp_begin");
595 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
596 _uint64_t
, "timestamp_end");
601 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
602 _uint64_t
, "content_size");
607 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
608 _uint64_t
, "packet_size");
613 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
614 _uint64_t
, "events_discarded");
619 stream_class
->packet_context_type
= packet_context_type
;
622 bt_ctf_field_type_put(packet_context_type
);
626 bt_ctf_field_type_put(_uint64_t
);