4 * Babeltrace CTF Writer
6 * Copyright 2013 EfficiOS Inc.
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-writer/stream-internal.h>
37 #include <babeltrace/ctf-ir/stream-class-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/align.h>
43 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
);
45 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
);
47 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
48 enum bt_ctf_byte_order byte_order
);
50 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
51 enum bt_ctf_byte_order byte_order
);
53 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
55 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
57 struct bt_ctf_stream_class
*stream_class
= NULL
;
59 if (!name
|| !strlen(name
)) {
63 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
68 stream_class
->name
= g_string_new(name
);
69 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
70 (GDestroyNotify
)bt_ctf_event_class_put
);
71 if (!stream_class
->event_classes
) {
75 bt_ctf_ref_init(&stream_class
->ref_count
);
79 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
85 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
86 struct bt_ctf_clock
*clock
)
90 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
95 if (stream_class
->clock
) {
96 bt_ctf_clock_put(stream_class
->clock
);
99 stream_class
->clock
= clock
;
100 bt_ctf_clock_get(clock
);
105 int bt_ctf_stream_class_add_event_class(
106 struct bt_ctf_stream_class
*stream_class
,
107 struct bt_ctf_event_class
*event_class
)
111 if (!stream_class
|| !event_class
) {
116 /* Check for duplicate event classes */
117 struct search_query query
= { .value
= event_class
, .found
= 0 };
118 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
124 if (bt_ctf_event_class_set_id(event_class
,
125 stream_class
->next_event_id
++)) {
126 /* The event is already associated to a stream class */
131 bt_ctf_event_class_get(event_class
);
132 g_ptr_array_add(stream_class
->event_classes
, event_class
);
137 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
143 bt_ctf_ref_get(&stream_class
->ref_count
);
146 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
152 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
156 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
162 stream_class
->frozen
= 1;
163 bt_ctf_clock_freeze(stream_class
->clock
);
164 g_ptr_array_foreach(stream_class
->event_classes
,
165 (GFunc
)bt_ctf_event_class_freeze
, NULL
);
169 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
175 (stream_class
->id_set
&& (id
!= stream_class
->id
))) {
180 stream_class
->id
= id
;
181 stream_class
->id_set
= 1;
187 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
188 enum bt_ctf_byte_order byte_order
)
192 ret
= init_packet_context(stream_class
, byte_order
);
197 ret
= init_event_header(stream_class
, byte_order
);
206 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
207 struct metadata_context
*context
)
212 g_string_assign(context
->field_name
, "");
213 context
->current_indentation_level
= 1;
214 if (!stream_class
->id_set
) {
219 g_string_append_printf(context
->string
,
220 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
222 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
228 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
229 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
235 if (stream_class
->event_context_type
) {
236 g_string_append(context
->string
, ";\n\n\tevent.context := ");
237 ret
= bt_ctf_field_type_serialize(
238 stream_class
->event_context_type
, context
);
244 g_string_append(context
->string
, ";\n};\n\n");
246 /* Assign this stream's ID to every event and serialize them */
247 g_ptr_array_foreach(stream_class
->event_classes
,
248 (GFunc
) bt_ctf_event_class_set_stream_id
,
249 GUINT_TO_POINTER(stream_class
->id
));
250 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
251 struct bt_ctf_event_class
*event_class
=
252 stream_class
->event_classes
->pdata
[i
];
254 ret
= bt_ctf_event_class_set_stream_id(event_class
,
260 ret
= bt_ctf_event_class_serialize(event_class
, context
);
266 context
->current_indentation_level
= 0;
271 struct bt_ctf_stream
*bt_ctf_stream_create(
272 struct bt_ctf_stream_class
*stream_class
)
274 struct bt_ctf_stream
*stream
= NULL
;
280 stream
= g_new0(struct bt_ctf_stream
, 1);
285 bt_ctf_ref_init(&stream
->ref_count
);
287 stream
->id
= stream_class
->next_stream_id
++;
288 stream
->stream_class
= stream_class
;
289 bt_ctf_stream_class_get(stream_class
);
290 bt_ctf_stream_class_freeze(stream_class
);
291 stream
->events
= g_ptr_array_new_with_free_func(
292 (GDestroyNotify
)bt_ctf_event_put
);
298 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
299 flush_func callback
, void *data
)
301 int ret
= stream
? 0 : -1;
307 stream
->flush
.func
= callback
;
308 stream
->flush
.data
= data
;
314 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
318 if (stream
->pos
.fd
!= -1) {
323 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
329 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
330 uint64_t event_count
)
336 stream
->events_discarded
+= event_count
;
339 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
340 struct bt_ctf_event
*event
)
345 if (!stream
|| !event
) {
350 ret
= bt_ctf_event_validate(event
);
355 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
356 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
361 bt_ctf_event_get(event
);
362 g_ptr_array_add(stream
->events
, event
);
367 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
371 uint64_t timestamp_begin
, timestamp_end
;
372 struct bt_ctf_stream_class
*stream_class
;
373 struct bt_ctf_field
*integer
= NULL
;
374 struct ctf_stream_pos packet_context_pos
;
381 if (!stream
->events
->len
) {
385 if (stream
->flush
.func
) {
386 stream
->flush
.func(stream
, stream
->flush
.data
);
389 stream_class
= stream
->stream_class
;
390 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
391 stream
->events
, 0))->timestamp
;
392 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
393 stream
->events
, stream
->events
->len
- 1))->timestamp
;
394 ret
= set_structure_field_integer(stream_class
->packet_context
,
395 "timestamp_begin", timestamp_begin
);
400 ret
= set_structure_field_integer(stream_class
->packet_context
,
401 "timestamp_end", timestamp_end
);
406 ret
= set_structure_field_integer(stream_class
->packet_context
,
407 "events_discarded", stream
->events_discarded
);
412 ret
= set_structure_field_integer(stream_class
->packet_context
,
413 "content_size", UINT64_MAX
);
418 ret
= set_structure_field_integer(stream_class
->packet_context
,
419 "packet_size", UINT64_MAX
);
424 /* Write packet context */
425 memcpy(&packet_context_pos
, &stream
->pos
,
426 sizeof(struct ctf_stream_pos
));
427 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
433 for (i
= 0; i
< stream
->events
->len
; i
++) {
434 struct bt_ctf_event
*event
= g_ptr_array_index(
436 uint32_t event_id
= bt_ctf_event_class_get_id(
438 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
440 ret
= set_structure_field_integer(stream_class
->event_header
,
445 ret
= set_structure_field_integer(stream_class
->event_header
,
446 "timestamp", timestamp
);
451 /* Write event header */
452 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
458 /* Write event content */
459 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
466 * Update the packet total size and content size and overwrite the
468 * Copy base_mma as the packet may have been remapped (e.g. when a
469 * packet is resized).
471 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
472 ret
= set_structure_field_integer(stream_class
->packet_context
,
473 "content_size", stream
->pos
.offset
);
478 ret
= set_structure_field_integer(stream_class
->packet_context
,
479 "packet_size", stream
->pos
.packet_size
);
484 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
485 &packet_context_pos
);
490 g_ptr_array_set_size(stream
->events
, 0);
491 stream
->flushed_packet_count
++;
493 bt_ctf_field_put(integer
);
497 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
503 bt_ctf_ref_get(&stream
->ref_count
);
506 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
512 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
516 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
518 struct bt_ctf_stream
*stream
;
524 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
525 ctf_fini_pos(&stream
->pos
);
526 if (close(stream
->pos
.fd
)) {
529 bt_ctf_stream_class_put(stream
->stream_class
);
530 g_ptr_array_free(stream
->events
, TRUE
);
535 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
537 struct bt_ctf_stream_class
*stream_class
;
543 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
544 bt_ctf_clock_put(stream_class
->clock
);
546 if (stream_class
->event_classes
) {
547 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
550 if (stream_class
->name
) {
551 g_string_free(stream_class
->name
, TRUE
);
554 bt_ctf_field_type_put(stream_class
->event_header_type
);
555 bt_ctf_field_put(stream_class
->event_header
);
556 bt_ctf_field_type_put(stream_class
->packet_context_type
);
557 bt_ctf_field_put(stream_class
->packet_context
);
558 bt_ctf_field_type_put(stream_class
->event_context_type
);
559 bt_ctf_field_put(stream_class
->event_context
);
560 g_free(stream_class
);
564 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
565 enum bt_ctf_byte_order byte_order
)
568 struct bt_ctf_field_type
*event_header_type
=
569 bt_ctf_field_type_structure_create();
570 struct bt_ctf_field_type
*_uint32_t
=
571 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
572 struct bt_ctf_field_type
*_uint64_t
=
573 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
575 if (!event_header_type
) {
580 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
, byte_order
);
585 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
590 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
596 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
597 _uint64_t
, "timestamp");
602 stream_class
->event_header_type
= event_header_type
;
603 stream_class
->event_header
= bt_ctf_field_create(
604 stream_class
->event_header_type
);
605 if (!stream_class
->event_header
) {
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
,
620 enum bt_ctf_byte_order byte_order
)
623 struct bt_ctf_field_type
*packet_context_type
=
624 bt_ctf_field_type_structure_create();
625 struct bt_ctf_field_type
*_uint64_t
=
626 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
628 if (!packet_context_type
) {
634 * We create a stream packet context as proposed in the CTF
637 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
642 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
643 _uint64_t
, "timestamp_begin");
648 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
649 _uint64_t
, "timestamp_end");
654 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
655 _uint64_t
, "content_size");
660 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
661 _uint64_t
, "packet_size");
666 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
667 _uint64_t
, "events_discarded");
672 stream_class
->packet_context_type
= packet_context_type
;
673 stream_class
->packet_context
= bt_ctf_field_create(packet_context_type
);
674 if (!stream_class
->packet_context
) {
679 bt_ctf_field_type_put(packet_context_type
);
683 bt_ctf_field_type_put(_uint64_t
);
688 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
693 struct bt_ctf_field
*integer
=
694 bt_ctf_field_structure_get_field(structure
, name
);
700 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
702 bt_ctf_field_put(integer
);