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-writer/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-writer/event-internal.h>
33 #include <babeltrace/ctf-writer/event-types-internal.h>
34 #include <babeltrace/ctf-writer/event-fields-internal.h>
35 #include <babeltrace/ctf-writer/stream.h>
36 #include <babeltrace/ctf-writer/stream-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_destroy(struct bt_ctf_ref
*ref
);
44 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
);
46 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
47 enum bt_ctf_byte_order byte_order
);
49 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
50 enum bt_ctf_byte_order byte_order
);
52 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
54 struct bt_ctf_stream_class
*bt_ctf_stream_class_create(const char *name
)
56 struct bt_ctf_stream_class
*stream_class
= NULL
;
58 if (!name
|| !strlen(name
)) {
62 stream_class
= g_new0(struct bt_ctf_stream_class
, 1);
67 stream_class
->name
= g_string_new(name
);
68 stream_class
->event_classes
= g_ptr_array_new_with_free_func(
69 (GDestroyNotify
)bt_ctf_event_class_put
);
70 if (!stream_class
->event_classes
) {
74 bt_ctf_ref_init(&stream_class
->ref_count
);
78 bt_ctf_stream_class_destroy(&stream_class
->ref_count
);
84 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class
*stream_class
,
85 struct bt_ctf_clock
*clock
)
89 if (!stream_class
|| !clock
|| stream_class
->frozen
) {
94 if (stream_class
->clock
) {
95 bt_ctf_clock_put(stream_class
->clock
);
98 stream_class
->clock
= clock
;
99 bt_ctf_clock_get(clock
);
104 int bt_ctf_stream_class_add_event_class(
105 struct bt_ctf_stream_class
*stream_class
,
106 struct bt_ctf_event_class
*event_class
)
110 if (!stream_class
|| !event_class
) {
115 /* Check for duplicate event classes */
116 struct search_query query
= { .value
= event_class
, .found
= 0 };
117 g_ptr_array_foreach(stream_class
->event_classes
, value_exists
, &query
);
123 if (bt_ctf_event_class_set_id(event_class
,
124 stream_class
->next_event_id
++)) {
125 /* The event is already associated to a stream class */
130 bt_ctf_event_class_get(event_class
);
131 g_ptr_array_add(stream_class
->event_classes
, event_class
);
136 void bt_ctf_stream_class_get(struct bt_ctf_stream_class
*stream_class
)
142 bt_ctf_ref_get(&stream_class
->ref_count
);
145 void bt_ctf_stream_class_put(struct bt_ctf_stream_class
*stream_class
)
151 bt_ctf_ref_put(&stream_class
->ref_count
, bt_ctf_stream_class_destroy
);
155 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class
*stream_class
)
161 stream_class
->frozen
= 1;
162 bt_ctf_clock_freeze(stream_class
->clock
);
163 g_ptr_array_foreach(stream_class
->event_classes
,
164 (GFunc
)bt_ctf_event_class_freeze
, NULL
);
168 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class
*stream_class
,
174 (stream_class
->id_set
&& (id
!= stream_class
->id
))) {
179 stream_class
->id
= id
;
180 stream_class
->id_set
= 1;
186 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class
*stream_class
,
187 enum bt_ctf_byte_order byte_order
)
191 ret
= init_packet_context(stream_class
, byte_order
);
196 ret
= init_event_header(stream_class
, byte_order
);
205 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class
*stream_class
,
206 struct metadata_context
*context
)
211 g_string_assign(context
->field_name
, "");
212 context
->current_indentation_level
= 1;
213 if (!stream_class
->id_set
) {
218 g_string_append_printf(context
->string
,
219 "stream {\n\tid = %" PRIu32
";\n\tevent.header := ",
221 ret
= bt_ctf_field_type_serialize(stream_class
->event_header_type
,
227 g_string_append(context
->string
, ";\n\n\tpacket.context := ");
228 ret
= bt_ctf_field_type_serialize(stream_class
->packet_context_type
,
234 if (stream_class
->event_context_type
) {
235 g_string_append(context
->string
, ";\n\n\tevent.context := ");
236 ret
= bt_ctf_field_type_serialize(
237 stream_class
->event_context_type
, context
);
243 g_string_append(context
->string
, ";\n};\n\n");
245 /* Assign this stream's ID to every event and serialize them */
246 g_ptr_array_foreach(stream_class
->event_classes
,
247 (GFunc
) bt_ctf_event_class_set_stream_id
,
248 GUINT_TO_POINTER(stream_class
->id
));
249 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
250 struct bt_ctf_event_class
*event_class
=
251 stream_class
->event_classes
->pdata
[i
];
253 ret
= bt_ctf_event_class_set_stream_id(event_class
,
259 ret
= bt_ctf_event_class_serialize(event_class
, context
);
265 context
->current_indentation_level
= 0;
270 struct bt_ctf_stream
*bt_ctf_stream_create(
271 struct bt_ctf_stream_class
*stream_class
)
273 struct bt_ctf_stream
*stream
= NULL
;
279 stream
= g_new0(struct bt_ctf_stream
, 1);
284 bt_ctf_ref_init(&stream
->ref_count
);
286 stream
->id
= stream_class
->next_stream_id
++;
287 stream
->stream_class
= stream_class
;
288 bt_ctf_stream_class_get(stream_class
);
289 bt_ctf_stream_class_freeze(stream_class
);
290 stream
->events
= g_ptr_array_new_with_free_func(
291 (GDestroyNotify
)bt_ctf_event_put
);
297 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream
*stream
,
298 flush_func callback
, void *data
)
300 int ret
= stream
? 0 : -1;
306 stream
->flush
.func
= callback
;
307 stream
->flush
.data
= data
;
313 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
317 if (stream
->pos
.fd
!= -1) {
322 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
328 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
329 uint64_t event_count
)
335 stream
->events_discarded
+= event_count
;
338 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
339 struct bt_ctf_event
*event
)
344 if (!stream
|| !event
) {
349 ret
= bt_ctf_event_validate(event
);
354 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
355 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
360 bt_ctf_event_get(event
);
361 g_ptr_array_add(stream
->events
, event
);
366 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
370 uint64_t timestamp_begin
, timestamp_end
;
371 struct bt_ctf_stream_class
*stream_class
;
372 struct bt_ctf_field
*integer
= NULL
;
373 struct ctf_stream_pos packet_context_pos
;
380 if (!stream
->events
->len
) {
384 if (stream
->flush
.func
) {
385 stream
->flush
.func(stream
, stream
->flush
.data
);
388 stream_class
= stream
->stream_class
;
389 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
390 stream
->events
, 0))->timestamp
;
391 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
392 stream
->events
, stream
->events
->len
- 1))->timestamp
;
393 ret
= set_structure_field_integer(stream_class
->packet_context
,
394 "timestamp_begin", timestamp_begin
);
399 ret
= set_structure_field_integer(stream_class
->packet_context
,
400 "timestamp_end", timestamp_end
);
405 ret
= set_structure_field_integer(stream_class
->packet_context
,
406 "events_discarded", stream
->events_discarded
);
411 ret
= set_structure_field_integer(stream_class
->packet_context
,
412 "content_size", UINT64_MAX
);
417 ret
= set_structure_field_integer(stream_class
->packet_context
,
418 "packet_size", UINT64_MAX
);
423 /* Write packet context */
424 memcpy(&packet_context_pos
, &stream
->pos
,
425 sizeof(struct ctf_stream_pos
));
426 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
432 for (i
= 0; i
< stream
->events
->len
; i
++) {
433 struct bt_ctf_event
*event
= g_ptr_array_index(
435 uint32_t event_id
= bt_ctf_event_class_get_id(
437 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
439 ret
= set_structure_field_integer(stream_class
->event_header
,
444 ret
= set_structure_field_integer(stream_class
->event_header
,
445 "timestamp", timestamp
);
450 /* Write event header */
451 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
457 /* Write event content */
458 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
465 * Update the packet total size and content size and overwrite the
468 ret
= set_structure_field_integer(stream_class
->packet_context
,
469 "content_size", stream
->pos
.offset
);
474 ret
= set_structure_field_integer(stream_class
->packet_context
,
475 "packet_size", stream
->pos
.packet_size
);
480 ret
= bt_ctf_field_serialize(stream_class
->packet_context
,
481 &packet_context_pos
);
486 g_ptr_array_set_size(stream
->events
, 0);
487 stream
->flushed_packet_count
++;
489 bt_ctf_field_put(integer
);
493 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
499 bt_ctf_ref_get(&stream
->ref_count
);
502 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
508 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
512 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
514 struct bt_ctf_stream
*stream
;
520 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
521 ctf_fini_pos(&stream
->pos
);
522 if (close(stream
->pos
.fd
)) {
525 bt_ctf_stream_class_put(stream
->stream_class
);
526 g_ptr_array_free(stream
->events
, TRUE
);
531 void bt_ctf_stream_class_destroy(struct bt_ctf_ref
*ref
)
533 struct bt_ctf_stream_class
*stream_class
;
539 stream_class
= container_of(ref
, struct bt_ctf_stream_class
, ref_count
);
540 bt_ctf_clock_put(stream_class
->clock
);
542 if (stream_class
->event_classes
) {
543 g_ptr_array_free(stream_class
->event_classes
, TRUE
);
546 if (stream_class
->name
) {
547 g_string_free(stream_class
->name
, TRUE
);
550 bt_ctf_field_type_put(stream_class
->event_header_type
);
551 bt_ctf_field_put(stream_class
->event_header
);
552 bt_ctf_field_type_put(stream_class
->packet_context_type
);
553 bt_ctf_field_put(stream_class
->packet_context
);
554 bt_ctf_field_type_put(stream_class
->event_context_type
);
555 bt_ctf_field_put(stream_class
->event_context
);
556 g_free(stream_class
);
560 int init_event_header(struct bt_ctf_stream_class
*stream_class
,
561 enum bt_ctf_byte_order byte_order
)
564 struct bt_ctf_field_type
*event_header_type
=
565 bt_ctf_field_type_structure_create();
566 struct bt_ctf_field_type
*_uint32_t
=
567 get_field_type(FIELD_TYPE_ALIAS_UINT32_T
);
568 struct bt_ctf_field_type
*_uint64_t
=
569 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
571 if (!event_header_type
) {
576 ret
= bt_ctf_field_type_set_byte_order(_uint32_t
, byte_order
);
581 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
586 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
592 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
593 _uint64_t
, "timestamp");
598 stream_class
->event_header_type
= event_header_type
;
599 stream_class
->event_header
= bt_ctf_field_create(
600 stream_class
->event_header_type
);
601 if (!stream_class
->event_header
) {
606 bt_ctf_field_type_put(event_header_type
);
609 bt_ctf_field_type_put(_uint32_t
);
610 bt_ctf_field_type_put(_uint64_t
);
615 int init_packet_context(struct bt_ctf_stream_class
*stream_class
,
616 enum bt_ctf_byte_order byte_order
)
619 struct bt_ctf_field_type
*packet_context_type
=
620 bt_ctf_field_type_structure_create();
621 struct bt_ctf_field_type
*_uint64_t
=
622 get_field_type(FIELD_TYPE_ALIAS_UINT64_T
);
624 if (!packet_context_type
) {
630 * We create a stream packet context as proposed in the CTF
633 ret
= bt_ctf_field_type_set_byte_order(_uint64_t
, byte_order
);
638 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
639 _uint64_t
, "timestamp_begin");
644 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
645 _uint64_t
, "timestamp_end");
650 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
651 _uint64_t
, "content_size");
656 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
657 _uint64_t
, "packet_size");
662 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
663 _uint64_t
, "events_discarded");
668 stream_class
->packet_context_type
= packet_context_type
;
669 stream_class
->packet_context
= bt_ctf_field_create(packet_context_type
);
670 if (!stream_class
->packet_context
) {
675 bt_ctf_field_type_put(packet_context_type
);
679 bt_ctf_field_type_put(_uint64_t
);
684 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
689 struct bt_ctf_field
*integer
=
690 bt_ctf_field_structure_get_field(structure
, name
);
696 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
698 bt_ctf_field_put(integer
);