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 struct bt_ctf_clock
*bt_ctf_stream_class_get_clock(
81 struct bt_ctf_stream_class
*stream_class
)
83 struct bt_ctf_clock
*clock
= NULL
;
85 if (!stream_class
|| !stream_class
->clock
) {
89 clock
= stream_class
->clock
;
90 bt_ctf_clock_get(clock
);
95 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
96 struct bt_ctf_clock
*clock
)
100 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
105 if (stream_class
->clock
) {
106 bt_ctf_clock_put(stream_class
->clock
);
109 stream_class
->clock
= clock
;
110 bt_ctf_clock_get(clock
);
115 int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class
*stream_class
)
119 if (!stream_class
|| !stream_class
->id_set
) {
124 ret
= (int64_t) stream_class
->id
;
129 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
139 stream_class
->id
= id
;
140 stream_class
->id_set
= 1;
145 int bt_ctf_stream_class_add_event_class(
146 struct bt_ctf_stream_class
*stream_class
,
147 struct bt_ctf_event_class
*event_class
)
152 if (!stream_class
|| !event_class
) {
157 /* Check for duplicate event classes */
158 struct search_query query
= { .value
= event_class
, .found
= 0 };
159 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
165 /* Only set an event id if none was explicitly set before */
166 event_id
= bt_ctf_event_class_get_id(event_class
);
168 if (bt_ctf_event_class_set_id(event_class
,
169 stream_class
->next_event_id
++)) {
175 ret
= bt_ctf_event_class_set_stream_class(event_class
, stream_class
);
180 bt_ctf_event_class_get(event_class
);
181 g_ptr_array_add(stream_class
->event_classes
, event_class
);
186 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
192 bt_ctf_ref_get(&stream_class
->ref_count
);
195 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
201 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
205 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
211 stream_class
->frozen
= 1;
212 bt_ctf_clock_freeze(stream_class
->clock
);
213 g_ptr_array_foreach(stream_class
->event_classes
,
214 (GFunc
)bt_ctf_event_class_freeze
, NULL
);
218 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
219 enum bt_ctf_byte_order byte_order
)
223 ret
= init_packet_context(stream_class
, byte_order
);
228 ret
= init_event_header(stream_class
, byte_order
);
237 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
238 struct metadata_context
*context
)
243 g_string_assign(context
->field_name
, "");
244 context
->current_indentation_level
= 1;
245 if (!stream_class
->id_set
) {
250 g_string_append_printf(context
->string
,
251 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
253 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
259 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
260 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
266 if (stream_class
->event_context_type
) {
267 g_string_append(context
->string
, ";\n\n\tevent.context := ");
268 ret
= bt_ctf_field_type_serialize(
269 stream_class
->event_context_type
, context
);
275 g_string_append(context
->string
, ";\n};\n\n");
276 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
277 struct bt_ctf_event_class
*event_class
=
278 stream_class
->event_classes
->pdata
[i
];
284 ret
= bt_ctf_event_class_serialize(event_class
, context
);
290 context
->current_indentation_level
= 0;
295 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
297 struct bt_ctf_stream_class
*stream_class
;
303 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
304 bt_ctf_clock_put(stream_class
->clock
);
306 if (stream_class
->event_classes
) {
309 /* Unregister this stream class from the event classes */
310 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
311 struct bt_ctf_event_class
*event_class
=
312 g_ptr_array_index(stream_class
->event_classes
,
315 bt_ctf_event_class_set_stream_class(event_class
, NULL
);
318 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
321 if (stream_class
->name
) {
322 g_string_free(stream_class
->name
, TRUE
);
325 bt_ctf_field_type_put(stream_class
->event_header_type
);
326 bt_ctf_field_put(stream_class
->event_header
);
327 bt_ctf_field_type_put(stream_class
->packet_context_type
);
328 bt_ctf_field_put(stream_class
->packet_context
);
329 bt_ctf_field_type_put(stream_class
->event_context_type
);
330 bt_ctf_field_put(stream_class
->event_context
);
331 g_free(stream_class
);
335 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
336 enum bt_ctf_byte_order byte_order
)
339 struct bt_ctf_field_type
*event_header_type
=
340 bt_ctf_field_type_structure_create();
341 struct bt_ctf_field_type
*_uint32_t
=
342 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
343 struct bt_ctf_field_type
*_uint64_t
=
344 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
346 if (!event_header_type
) {
351 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
, byte_order
);
356 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
361 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
367 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
368 _uint64_t
, "timestamp");
373 stream_class
->event_header_type
= event_header_type
;
374 stream_class
->event_header
= bt_ctf_field_create(
375 stream_class
->event_header_type
);
376 if (!stream_class
->event_header
) {
381 bt_ctf_field_type_put(event_header_type
);
384 bt_ctf_field_type_put(_uint32_t
);
385 bt_ctf_field_type_put(_uint64_t
);
390 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
391 enum bt_ctf_byte_order byte_order
)
394 struct bt_ctf_field_type
*packet_context_type
=
395 bt_ctf_field_type_structure_create();
396 struct bt_ctf_field_type
*_uint64_t
=
397 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
399 if (!packet_context_type
) {
405 * We create a stream packet context as proposed in the CTF
408 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
413 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
414 _uint64_t
, "timestamp_begin");
419 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
420 _uint64_t
, "timestamp_end");
425 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
426 _uint64_t
, "content_size");
431 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
432 _uint64_t
, "packet_size");
437 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
438 _uint64_t
, "events_discarded");
443 stream_class
->packet_context_type
= packet_context_type
;
444 stream_class
->packet_context
= bt_ctf_field_create(packet_context_type
);
445 if (!stream_class
->packet_context
) {
450 bt_ctf_field_type_put(packet_context_type
);
454 bt_ctf_field_type_put(_uint64_t
);