4 * Babeltrace CTF Writer
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
)
52 struct bt_ctf_stream_class
*stream_class
= NULL
;
54 if (!name
|| !strlen(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 bt_ctf_ref_init(&stream_class
->ref_count
);
74 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
80 const char *bt_ctf_stream_class_get_name(
81 struct bt_ctf_stream_class
*stream_class
)
83 const char *name
= NULL
;
89 name
= stream_class
->name
->str
;
94 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
95 struct bt_ctf_stream_class
*stream_class
)
97 struct bt_ctf_clock
*clock
= NULL
;
99 if (!stream_class
|| !stream_class
->clock
) {
103 clock
= stream_class
->clock
;
104 bt_ctf_clock_get(clock
);
109 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
110 struct bt_ctf_clock
*clock
)
114 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
119 if (stream_class
->clock
) {
120 bt_ctf_clock_put(stream_class
->clock
);
123 stream_class
->clock
= clock
;
124 bt_ctf_clock_get(clock
);
129 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
133 if (!stream_class
|| !stream_class
->id_set
) {
138 ret
= (int64_t) stream_class
->id
;
143 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
153 stream_class
->id
= id
;
154 stream_class
->id_set
= 1;
159 int bt_ctf_stream_class_add_event_class(
160 struct bt_ctf_stream_class
*stream_class
,
161 struct bt_ctf_event_class
*event_class
)
166 if (!stream_class
|| !event_class
) {
171 /* Check for duplicate event classes */
172 struct search_query query
= { .value
= event_class
, .found
= 0 };
173 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
179 /* Only set an event id if none was explicitly set before */
180 event_id
= bt_ctf_event_class_get_id(event_class
);
182 if (bt_ctf_event_class_set_id(event_class
,
183 stream_class
->next_event_id
++)) {
189 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
194 bt_ctf_event_class_get(event_class
);
195 g_ptr_array_add(stream_class
->event_classes
, event_class
);
200 int64_t bt_ctf_stream_class_get_event_class_count(
201 struct bt_ctf_stream_class
*stream_class
)
210 ret
= (int64_t) stream_class
->event_classes
->len
;
215 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class(
216 struct bt_ctf_stream_class
*stream_class
, size_t index
)
218 struct bt_ctf_event_class
*event_class
= NULL
;
220 if (!stream_class
|| index
>= stream_class
->event_classes
->len
) {
224 event_class
= g_ptr_array_index(stream_class
->event_classes
, index
);
225 bt_ctf_event_class_get(event_class
);
230 struct bt_ctf_event_class
*bt_ctf_stream_class_get_event_class_by_name(
231 struct bt_ctf_stream_class
*stream_class
, const char *name
)
235 struct bt_ctf_event_class
*event_class
= NULL
;
237 if (!stream_class
|| !name
) {
241 name_quark
= g_quark_try_string(name
);
246 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
247 struct bt_ctf_event_class
*current_event_class
=
248 g_ptr_array_index(stream_class
->event_classes
, i
);
250 if (name_quark
== current_event_class
->name
) {
251 event_class
= current_event_class
;
252 bt_ctf_event_class_get(event_class
);
260 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
266 bt_ctf_ref_get(&stream_class
->ref_count
);
269 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
275 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
279 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
285 stream_class
->frozen
= 1;
286 bt_ctf_clock_freeze(stream_class
->clock
);
287 g_ptr_array_foreach(stream_class
->event_classes
,
288 (GFunc
)bt_ctf_event_class_freeze
, NULL
);
292 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
293 enum bt_ctf_byte_order byte_order
)
297 ret
= init_packet_context(stream_class
, byte_order
);
302 ret
= init_event_header(stream_class
, byte_order
);
311 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
312 struct metadata_context
*context
)
317 g_string_assign(context
->field_name
, "");
318 context
->current_indentation_level
= 1;
319 if (!stream_class
->id_set
) {
324 g_string_append_printf(context
->string
,
325 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
327 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
333 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
334 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
340 if (stream_class
->event_context_type
) {
341 g_string_append(context
->string
, ";\n\n\tevent.context := ");
342 ret
= bt_ctf_field_type_serialize(
343 stream_class
->event_context_type
, context
);
349 g_string_append(context
->string
, ";\n};\n\n");
350 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
351 struct bt_ctf_event_class
*event_class
=
352 stream_class
->event_classes
->pdata
[i
];
358 ret
= bt_ctf_event_class_serialize(event_class
, context
);
364 context
->current_indentation_level
= 0;
369 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
371 struct bt_ctf_stream_class
*stream_class
;
377 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
378 bt_ctf_clock_put(stream_class
->clock
);
380 if (stream_class
->event_classes
) {
383 /* Unregister this stream class from the event classes */
384 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
385 struct bt_ctf_event_class
*event_class
=
386 g_ptr_array_index(stream_class
->event_classes
,
389 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
392 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
395 if (stream_class
->name
) {
396 g_string_free(stream_class
->name
, TRUE
);
399 bt_ctf_field_type_put(stream_class
->event_header_type
);
400 bt_ctf_field_put(stream_class
->event_header
);
401 bt_ctf_field_type_put(stream_class
->packet_context_type
);
402 bt_ctf_field_put(stream_class
->packet_context
);
403 bt_ctf_field_type_put(stream_class
->event_context_type
);
404 bt_ctf_field_put(stream_class
->event_context
);
405 g_free(stream_class
);
409 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
410 enum bt_ctf_byte_order byte_order
)
413 struct bt_ctf_field_type
*event_header_type
=
414 bt_ctf_field_type_structure_create();
415 struct bt_ctf_field_type
*_uint32_t
=
416 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
417 struct bt_ctf_field_type
*_uint64_t
=
418 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
420 if (!event_header_type
) {
425 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
, byte_order
);
430 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
435 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
441 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
442 _uint64_t
, "timestamp");
447 stream_class
->event_header_type
= event_header_type
;
448 stream_class
->event_header
= bt_ctf_field_create(
449 stream_class
->event_header_type
);
450 if (!stream_class
->event_header
) {
455 bt_ctf_field_type_put(event_header_type
);
458 bt_ctf_field_type_put(_uint32_t
);
459 bt_ctf_field_type_put(_uint64_t
);
464 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
465 enum bt_ctf_byte_order byte_order
)
468 struct bt_ctf_field_type
*packet_context_type
=
469 bt_ctf_field_type_structure_create();
470 struct bt_ctf_field_type
*_uint64_t
=
471 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
473 if (!packet_context_type
) {
479 * We create a stream packet context as proposed in the CTF
482 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
487 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
488 _uint64_t
, "timestamp_begin");
493 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
494 _uint64_t
, "timestamp_end");
499 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
500 _uint64_t
, "content_size");
505 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
506 _uint64_t
, "packet_size");
511 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
512 _uint64_t
, "events_discarded");
517 stream_class
->packet_context_type
= packet_context_type
;
518 stream_class
->packet_context
= bt_ctf_field_create(packet_context_type
);
519 if (!stream_class
->packet_context
) {
524 bt_ctf_field_type_put(packet_context_type
);
528 bt_ctf_field_type_put(_uint64_t
);