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/compiler.h>
39 #include <babeltrace/align.h>
42 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
);
44 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
45 enum bt_ctf_byte_order byte_order
);
47 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
48 enum bt_ctf_byte_order byte_order
);
50 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
53 struct bt_ctf_stream_class
*stream_class
= NULL
;
55 if (!name
|| !strlen(name
)) {
59 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
64 stream_class
->name
= g_string_new(name
);
65 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
66 (GDestroyNotify
)bt_ctf_event_class_put
);
67 if (!stream_class
->event_classes
) {
71 ret
= init_packet_context(stream_class
, BT_CTF_BYTE_ORDER_NATIVE
);
76 bt_ctf_ref_init(&stream_class
->ref_count
);
80 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
86 const char *bt_ctf_stream_class_get_name(
87 struct bt_ctf_stream_class
*stream_class
)
89 const char *name
= NULL
;
95 name
= stream_class
->name
->str
;
100 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
101 struct bt_ctf_stream_class
*stream_class
)
103 struct bt_ctf_clock
*clock
= NULL
;
105 if (!stream_class
|| !stream_class
->clock
) {
109 clock
= stream_class
->clock
;
110 bt_ctf_clock_get(clock
);
115 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
116 struct bt_ctf_clock
*clock
)
120 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
125 if (stream_class
->clock
) {
126 bt_ctf_clock_put(stream_class
->clock
);
129 stream_class
->clock
= clock
;
130 bt_ctf_clock_get(clock
);
135 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
139 if (!stream_class
|| !stream_class
->id_set
) {
144 ret
= (int64_t) stream_class
->id
;
149 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
154 if (!stream_class
|| stream_class
->frozen
) {
159 stream_class
->id
= id
;
160 stream_class
->id_set
= 1;
165 int bt_ctf_stream_class_add_event_class(
166 struct bt_ctf_stream_class
*stream_class
,
167 struct bt_ctf_event_class
*event_class
)
172 if (!stream_class
|| !event_class
) {
177 /* Check for duplicate event classes */
178 struct search_query query
= { .value
= event_class
, .found
= 0 };
179 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
185 /* Only set an event id if none was explicitly set before */
186 event_id
= bt_ctf_event_class_get_id(event_class
);
188 if (bt_ctf_event_class_set_id(event_class
,
189 stream_class
->next_event_id
++)) {
195 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
200 bt_ctf_event_class_get(event_class
);
201 g_ptr_array_add(stream_class
->event_classes
, event_class
);
206 int bt_ctf_stream_class_get_event_class_count(
207 struct bt_ctf_stream_class
*stream_class
)
216 ret
= (int) stream_class
->event_classes
->len
;
221 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
222 struct bt_ctf_stream_class
*stream_class
, int index
)
224 struct bt_ctf_event_class
*event_class
= NULL
;
226 if (!stream_class
|| index
< 0 ||
227 index
>= stream_class
->event_classes
->len
) {
231 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
232 bt_ctf_event_class_get(event_class
);
237 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
238 struct bt_ctf_stream_class
*stream_class
, const char *name
)
242 struct bt_ctf_event_class
*event_class
= NULL
;
244 if (!stream_class
|| !name
) {
248 name_quark
= g_quark_try_string(name
);
253 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
254 struct bt_ctf_event_class
*current_event_class
=
255 g_ptr_array_index(stream_class
->event_classes
, i
);
257 if (name_quark
== current_event_class
->name
) {
258 event_class
= current_event_class
;
259 bt_ctf_event_class_get(event_class
);
267 struct bt_ctf_field_type
*bt_ctf_stream_class_get_packet_context_type(
268 struct bt_ctf_stream_class
*stream_class
)
270 struct bt_ctf_field_type
*ret
= NULL
;
276 assert(stream_class
->packet_context_type
);
277 bt_ctf_field_type_get(stream_class
->packet_context_type
);
278 ret
= stream_class
->packet_context_type
;
283 int bt_ctf_stream_class_set_packet_context_type(
284 struct bt_ctf_stream_class
*stream_class
,
285 struct bt_ctf_field_type
*packet_context_type
)
289 if (!stream_class
|| !packet_context_type
|| stream_class
->frozen
) {
294 assert(stream_class
->packet_context_type
);
295 if (bt_ctf_field_type_get_type_id(packet_context_type
) !=
297 /* A packet context must be a structure */
302 bt_ctf_field_type_put(stream_class
->packet_context_type
);
303 bt_ctf_field_type_get(packet_context_type
);
304 stream_class
->packet_context_type
= packet_context_type
;
309 struct bt_ctf_field_type
*bt_ctf_stream_class_get_event_context_type(
310 struct bt_ctf_stream_class
*stream_class
)
312 struct bt_ctf_field_type
*ret
= NULL
;
314 if (!stream_class
|| !stream_class
->event_context_type
) {
318 assert(stream_class
->event_context_type
);
319 bt_ctf_field_type_get(stream_class
->event_context_type
);
320 ret
= stream_class
->event_context_type
;
325 int bt_ctf_stream_class_set_event_context_type(
326 struct bt_ctf_stream_class
*stream_class
,
327 struct bt_ctf_field_type
*event_context_type
)
331 if (!stream_class
|| !event_context_type
|| stream_class
->frozen
) {
336 if (bt_ctf_field_type_get_type_id(event_context_type
) !=
338 /* A packet context must be a structure */
343 bt_ctf_field_type_put(stream_class
->event_context_type
);
344 bt_ctf_field_type_get(event_context_type
);
345 stream_class
->event_context_type
= event_context_type
;
350 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
356 bt_ctf_ref_get(&stream_class
->ref_count
);
359 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
365 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
369 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
375 stream_class
->frozen
= 1;
376 bt_ctf_field_type_freeze(stream_class
->packet_context_type
);
377 bt_ctf_field_type_freeze(stream_class
->event_context_type
);
378 bt_ctf_clock_freeze(stream_class
->clock
);
379 g_ptr_array_foreach(stream_class
->event_classes
,
380 (GFunc
)bt_ctf_event_class_freeze
, NULL
);
384 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
385 enum bt_ctf_byte_order byte_order
)
389 ret
= init_event_header(stream_class
, byte_order
);
398 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
399 struct metadata_context
*context
)
404 g_string_assign(context
->field_name
, "");
405 context
->current_indentation_level
= 1;
406 if (!stream_class
->id_set
) {
411 g_string_append_printf(context
->string
,
412 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
414 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
420 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
421 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
427 if (stream_class
->event_context_type
) {
428 g_string_append(context
->string
, ";\n\n\tevent.context := ");
429 ret
= bt_ctf_field_type_serialize(
430 stream_class
->event_context_type
, context
);
436 g_string_append(context
->string
, ";\n};\n\n");
437 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
438 struct bt_ctf_event_class
*event_class
=
439 stream_class
->event_classes
->pdata
[i
];
441 ret
= bt_ctf_event_class_serialize(event_class
, context
);
447 context
->current_indentation_level
= 0;
452 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
454 struct bt_ctf_stream_class
*stream_class
;
460 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
461 bt_ctf_clock_put(stream_class
->clock
);
463 if (stream_class
->event_classes
) {
466 /* Unregister this stream class from the event classes */
467 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
468 struct bt_ctf_event_class
*event_class
=
469 g_ptr_array_index(stream_class
->event_classes
,
472 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
475 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
478 if (stream_class
->name
) {
479 g_string_free(stream_class
->name
, TRUE
);
482 bt_ctf_field_type_put(stream_class
->event_header_type
);
483 bt_ctf_field_put(stream_class
->event_header
);
484 bt_ctf_field_type_put(stream_class
->packet_context_type
);
485 if (stream_class
->event_context_type
) {
486 bt_ctf_field_type_put(stream_class
->event_context_type
);
488 g_free(stream_class
);
492 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
493 enum bt_ctf_byte_order byte_order
)
496 struct bt_ctf_field_type
*event_header_type
=
497 bt_ctf_field_type_structure_create();
498 struct bt_ctf_field_type
*_uint32_t
=
499 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
500 struct bt_ctf_field_type
*_uint64_t
=
501 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
503 if (!event_header_type
) {
508 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
, byte_order
);
513 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
518 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
524 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
525 _uint64_t
, "timestamp");
530 stream_class
->event_header_type
= event_header_type
;
531 stream_class
->event_header
= bt_ctf_field_create(
532 stream_class
->event_header_type
);
533 if (!stream_class
->event_header
) {
538 bt_ctf_field_type_put(event_header_type
);
541 bt_ctf_field_type_put(_uint32_t
);
542 bt_ctf_field_type_put(_uint64_t
);
547 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
548 enum bt_ctf_byte_order byte_order
)
551 struct bt_ctf_field_type
*packet_context_type
=
552 bt_ctf_field_type_structure_create();
553 struct bt_ctf_field_type
*_uint64_t
=
554 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
556 if (!packet_context_type
) {
562 * We create a stream packet context as proposed in the CTF
565 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
570 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
571 _uint64_t
, "timestamp_begin");
576 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
577 _uint64_t
, "timestamp_end");
582 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
583 _uint64_t
, "content_size");
588 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
589 _uint64_t
, "packet_size");
594 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
595 _uint64_t
, "events_discarded");
600 stream_class
->packet_context_type
= packet_context_type
;
603 bt_ctf_field_type_put(packet_context_type
);
607 bt_ctf_field_type_put(_uint64_t
);