4 * Babeltrace CTF IR - Stream
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-ir/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-ir/stream.h>
36 #include <babeltrace/ctf-ir/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>
41 #include <babeltrace/ctf/ctf-index.h>
44 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
);
46 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
49 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
52 struct bt_ctf_field_type
*magic_field_type
= NULL
;
53 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
54 stream
->packet_header
, "magic");
57 /* No magic field found. Not an error, skip. */
61 if (!bt_ctf_field_validate(magic_field
)) {
62 /* Value already set. Not an error, skip. */
66 magic_field_type
= bt_ctf_field_get_type(magic_field
);
67 assert(magic_field_type
);
69 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
71 /* Magic field is not an integer. Not an error, skip. */
75 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
77 * Magic field is not of the expected size.
83 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
86 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
87 (int64_t) 0xC1FC1FC1);
89 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
90 (uint64_t) 0xC1FC1FC1);
94 bt_ctf_field_put(magic_field
);
96 if (magic_field_type
) {
97 bt_ctf_field_type_put(magic_field_type
);
103 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
106 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
107 struct bt_ctf_field_type
*element_field_type
= NULL
;
108 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
109 stream
->packet_header
, "uuid");
112 /* No uuid field found. Not an error, skip. */
116 if (!bt_ctf_field_validate(uuid_field
)) {
117 /* Value already set. Not an error, skip. */
121 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
122 assert(uuid_field_type
);
123 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
125 /* UUID field is not an array. Not an error, skip. */
129 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
131 * UUID field is not of the expected size.
132 * Not an error, skip.
137 element_field_type
= bt_ctf_field_type_array_get_element_type(
139 assert(element_field_type
);
140 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
142 /* UUID array elements are not integers. Not an error, skip */
146 for (i
= 0; i
< 16; i
++) {
147 struct bt_ctf_field
*uuid_element
=
148 bt_ctf_field_array_get_field(uuid_field
, i
);
150 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
154 ret
= bt_ctf_field_signed_integer_set_value(
155 uuid_element
, (int64_t) stream
->trace
->uuid
[i
]);
157 ret
= bt_ctf_field_unsigned_integer_set_value(
159 (uint64_t) stream
->trace
->uuid
[i
]);
161 bt_ctf_field_put(uuid_element
);
169 bt_ctf_field_put(uuid_field
);
171 if (uuid_field_type
) {
172 bt_ctf_field_type_put(uuid_field_type
);
174 if (element_field_type
) {
175 bt_ctf_field_type_put(element_field_type
);
180 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
184 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
185 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
186 stream
->packet_header
, "stream_id");
188 if (!stream_id_field
) {
189 /* No stream_id field found. Not an error, skip. */
193 if (!bt_ctf_field_validate(stream_id_field
)) {
194 /* Value already set. Not an error, skip. */
198 stream_id_field_type
= bt_ctf_field_get_type(stream_id_field
);
199 assert(stream_id_field_type
);
200 if (bt_ctf_field_type_get_type_id(stream_id_field_type
) !=
202 /* stream_id field is not an integer. Not an error, skip. */
206 stream_id
= stream
->stream_class
->id
;
207 ret
= bt_ctf_field_type_integer_get_signed(stream_id_field_type
);
210 ret
= bt_ctf_field_signed_integer_set_value(stream_id_field
,
211 (int64_t) stream_id
);
213 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
214 (uint64_t) stream_id
);
217 if (stream_id_field
) {
218 bt_ctf_field_put(stream_id_field
);
220 if (stream_id_field_type
) {
221 bt_ctf_field_type_put(stream_id_field_type
);
227 int set_packet_header(struct bt_ctf_stream
*stream
)
231 ret
= set_packet_header_magic(stream
);
236 ret
= set_packet_header_uuid(stream
);
241 ret
= set_packet_header_stream_id(stream
);
250 struct bt_ctf_stream
*bt_ctf_stream_create(
251 struct bt_ctf_stream_class
*stream_class
,
252 struct bt_ctf_trace
*trace
)
255 struct bt_ctf_stream
*stream
= NULL
;
257 if (!stream_class
|| !trace
) {
261 stream
= g_new0(struct bt_ctf_stream
, 1);
266 stream
->trace
= trace
;
267 bt_ctf_ref_init(&stream
->ref_count
);
268 stream
->packet_context
= bt_ctf_field_create(
269 stream_class
->packet_context_type
);
270 if (!stream
->packet_context
) {
275 * A stream class may not have a stream event context defined
276 * in which case this stream will never have a stream_event_context
277 * member since, after a stream's creation, the parent stream class
278 * is "frozen" (immutable).
280 if (stream_class
->event_context_type
) {
281 stream
->event_context
= bt_ctf_field_create(
282 stream_class
->event_context_type
);
283 if (!stream
->packet_context
) {
288 /* Initialize events_discarded */
289 ret
= set_structure_field_integer(stream
->packet_context
,
290 "events_discarded", 0);
296 stream
->id
= stream_class
->next_stream_id
++;
297 stream
->stream_class
= stream_class
;
298 bt_ctf_stream_class_get(stream_class
);
299 stream
->events
= g_ptr_array_new_with_free_func(
300 (GDestroyNotify
) bt_ctf_event_put
);
301 if (!stream
->events
) {
304 if (stream_class
->event_context_type
) {
305 stream
->event_contexts
= g_ptr_array_new_with_free_func(
306 (GDestroyNotify
) bt_ctf_field_put
);
307 if (!stream
->event_contexts
) {
312 /* A trace is not allowed to have a NULL packet header */
313 assert(trace
->packet_header_type
);
314 stream
->packet_header
= bt_ctf_field_create(trace
->packet_header_type
);
316 * Attempt to populate the default trace packet header fields
317 * (magic, uuid and stream_id). This will _not_ fail shall the
318 * fields not be found or be of an incompatible type; they will
319 * simply not be populated automatically. The user will have to
320 * make sure to set the trace packet header fields himself before
323 ret
= set_packet_header(stream
);
330 bt_ctf_stream_destroy(&stream
->ref_count
);
335 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
339 if (stream
->pos
.fd
!= -1) {
344 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
351 void bt_ctf_stream_set_trace(struct bt_ctf_stream
*stream
,
352 struct bt_ctf_trace
*trace
)
354 stream
->trace
= trace
;
357 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
358 struct bt_ctf_stream
*stream
)
360 struct bt_ctf_stream_class
*stream_class
= NULL
;
366 stream_class
= stream
->stream_class
;
367 bt_ctf_stream_class_get(stream_class
);
372 int bt_ctf_stream_get_discarded_events_count(
373 struct bt_ctf_stream
*stream
, uint64_t *count
)
377 struct bt_ctf_field
*events_discarded_field
= NULL
;
378 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
380 if (!stream
|| !count
|| !stream
->packet_context
) {
385 events_discarded_field
= bt_ctf_field_structure_get_field(
386 stream
->packet_context
, "events_discarded");
387 if (!events_discarded_field
) {
392 events_discarded_field_type
= bt_ctf_field_get_type(
393 events_discarded_field
);
394 if (!events_discarded_field_type
) {
399 field_signed
= bt_ctf_field_type_integer_get_signed(
400 events_discarded_field_type
);
401 if (field_signed
< 0) {
407 int64_t signed_count
;
409 ret
= bt_ctf_field_signed_integer_get_value(
410 events_discarded_field
, &signed_count
);
414 if (signed_count
< 0) {
419 *count
= (uint64_t) signed_count
;
421 ret
= bt_ctf_field_unsigned_integer_get_value(
422 events_discarded_field
, count
);
428 if (events_discarded_field
) {
429 bt_ctf_field_put(events_discarded_field
);
431 if (events_discarded_field_type
) {
432 bt_ctf_field_type_put(events_discarded_field_type
);
437 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
438 uint64_t event_count
)
442 uint64_t previous_count
;
444 struct bt_ctf_field
*events_discarded_field
= NULL
;
445 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
447 if (!stream
|| !stream
->packet_context
) {
451 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
457 events_discarded_field
= bt_ctf_field_structure_get_field(
458 stream
->packet_context
, "events_discarded");
459 if (!events_discarded_field
) {
463 events_discarded_field_type
= bt_ctf_field_get_type(
464 events_discarded_field
);
465 if (!events_discarded_field_type
) {
469 field_signed
= bt_ctf_field_type_integer_get_signed(
470 events_discarded_field_type
);
471 if (field_signed
< 0) {
475 new_count
= previous_count
+ event_count
;
477 ret
= bt_ctf_field_signed_integer_set_value(
478 events_discarded_field
, (int64_t) new_count
);
483 ret
= bt_ctf_field_unsigned_integer_set_value(
484 events_discarded_field
, new_count
);
491 if (events_discarded_field
) {
492 bt_ctf_field_put(events_discarded_field
);
494 if (events_discarded_field_type
) {
495 bt_ctf_field_type_put(events_discarded_field_type
);
499 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
500 struct bt_ctf_event
*event
)
504 struct bt_ctf_field
*event_context_copy
= NULL
;
506 if (!stream
|| !event
) {
511 /* Make sure the event's payload is set */
512 ret
= bt_ctf_event_validate(event
);
517 /* Sample the current stream event context by copying it */
518 if (stream
->event_context
) {
519 /* Make sure the event context's payload is set */
520 ret
= bt_ctf_field_validate(stream
->event_context
);
525 event_context_copy
= bt_ctf_field_copy(stream
->event_context
);
526 if (!event_context_copy
) {
532 timestamp
= bt_ctf_clock_get_time(stream
->stream_class
->clock
);
533 ret
= bt_ctf_event_set_timestamp(event
, timestamp
);
538 bt_ctf_event_get(event
);
539 /* Save the new event along with its associated stream event context */
540 g_ptr_array_add(stream
->events
, event
);
541 if (event_context_copy
) {
542 g_ptr_array_add(stream
->event_contexts
, event_context_copy
);
548 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
549 struct bt_ctf_stream
*stream
)
551 struct bt_ctf_field
*packet_context
= NULL
;
557 packet_context
= stream
->packet_context
;
558 if (packet_context
) {
559 bt_ctf_field_get(packet_context
);
562 return packet_context
;
565 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
566 struct bt_ctf_field
*field
)
569 struct bt_ctf_field_type
*field_type
;
571 if (!stream
|| !field
) {
576 field_type
= bt_ctf_field_get_type(field
);
577 if (field_type
!= stream
->stream_class
->packet_context_type
) {
582 bt_ctf_field_type_put(field_type
);
583 bt_ctf_field_get(field
);
584 bt_ctf_field_put(stream
->packet_context
);
585 stream
->packet_context
= field
;
590 struct bt_ctf_field
*bt_ctf_stream_get_event_context(
591 struct bt_ctf_stream
*stream
)
593 struct bt_ctf_field
*event_context
= NULL
;
599 event_context
= stream
->event_context
;
601 bt_ctf_field_get(event_context
);
604 return event_context
;
607 int bt_ctf_stream_set_event_context(struct bt_ctf_stream
*stream
,
608 struct bt_ctf_field
*field
)
611 struct bt_ctf_field_type
*field_type
= NULL
;
613 if (!stream
|| !field
) {
618 field_type
= bt_ctf_field_get_type(field
);
619 if (field_type
!= stream
->stream_class
->event_context_type
) {
624 bt_ctf_field_get(field
);
625 bt_ctf_field_put(stream
->event_context
);
626 stream
->event_context
= field
;
629 bt_ctf_field_type_put(field_type
);
634 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
635 struct bt_ctf_stream
*stream
)
637 struct bt_ctf_field
*packet_header
= NULL
;
643 packet_header
= stream
->packet_header
;
645 bt_ctf_field_get(packet_header
);
648 return packet_header
;
651 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
652 struct bt_ctf_field
*field
)
655 struct bt_ctf_field_type
*field_type
= NULL
;
657 if (!stream
|| !field
) {
662 field_type
= bt_ctf_field_get_type(field
);
663 if (field_type
!= stream
->trace
->packet_header_type
) {
668 bt_ctf_field_get(field
);
669 bt_ctf_field_put(stream
->packet_header
);
670 stream
->packet_header
= field
;
673 bt_ctf_field_type_put(field_type
);
678 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
682 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
683 struct bt_ctf_stream_class
*stream_class
;
684 struct bt_ctf_field
*integer
= NULL
;
685 struct ctf_stream_pos packet_context_pos
;
687 if (!stream
|| stream
->pos
.fd
< 0) {
689 * Stream does not have an associated fd. It is,
690 * therefore, not a stream being used to write events.
696 if (!stream
->events
->len
) {
700 ret
= bt_ctf_field_validate(stream
->packet_header
);
705 if (stream
->flushed_packet_count
) {
706 /* ctf_init_pos has already initialized the first packet */
707 ctf_packet_seek(&stream
->pos
.parent
, 0, SEEK_CUR
);
710 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
);
715 stream_class
= stream
->stream_class
;
716 timestamp_begin
= ((struct bt_ctf_event
*) g_ptr_array_index(
717 stream
->events
, 0))->timestamp
;
718 timestamp_end
= ((struct bt_ctf_event
*) g_ptr_array_index(
719 stream
->events
, stream
->events
->len
- 1))->timestamp
;
721 /* Set the default context attributes if present and unset. */
722 ret
= set_structure_field_integer(stream
->packet_context
,
723 "timestamp_begin", timestamp_begin
);
728 ret
= set_structure_field_integer(stream
->packet_context
,
729 "timestamp_end", timestamp_end
);
734 ret
= set_structure_field_integer(stream
->packet_context
,
735 "content_size", UINT64_MAX
);
740 ret
= set_structure_field_integer(stream
->packet_context
,
741 "packet_size", UINT64_MAX
);
746 /* Write packet context */
747 memcpy(&packet_context_pos
, &stream
->pos
,
748 sizeof(struct ctf_stream_pos
));
749 ret
= bt_ctf_field_serialize(stream
->packet_context
,
755 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
761 /* Unset the packet context's fields. */
762 ret
= bt_ctf_field_reset(stream
->packet_context
);
767 /* Set the previous number of discarded events. */
768 ret
= set_structure_field_integer(stream
->packet_context
,
769 "events_discarded", events_discarded
);
774 for (i
= 0; i
< stream
->events
->len
; i
++) {
775 struct bt_ctf_event
*event
= g_ptr_array_index(
777 uint32_t event_id
= bt_ctf_event_class_get_id(
779 uint64_t timestamp
= bt_ctf_event_get_timestamp(event
);
781 ret
= bt_ctf_field_reset(stream_class
->event_header
);
786 ret
= set_structure_field_integer(stream_class
->event_header
,
791 ret
= set_structure_field_integer(stream_class
->event_header
,
792 "timestamp", timestamp
);
797 /* Write event header */
798 ret
= bt_ctf_field_serialize(stream_class
->event_header
,
804 /* Write stream event context */
805 if (stream
->event_contexts
) {
806 ret
= bt_ctf_field_serialize(
807 g_ptr_array_index(stream
->event_contexts
, i
),
814 /* Write event content */
815 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
822 * Update the packet total size and content size and overwrite the
824 * Copy base_mma as the packet may have been remapped (e.g. when a
825 * packet is resized).
827 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
828 ret
= set_structure_field_integer(stream
->packet_context
,
829 "content_size", stream
->pos
.offset
);
834 ret
= set_structure_field_integer(stream
->packet_context
,
835 "packet_size", stream
->pos
.packet_size
);
840 ret
= bt_ctf_field_serialize(stream
->packet_context
,
841 &packet_context_pos
);
846 g_ptr_array_set_size(stream
->events
, 0);
847 if (stream
->event_contexts
) {
848 g_ptr_array_set_size(stream
->event_contexts
, 0);
850 stream
->flushed_packet_count
++;
852 bt_ctf_field_put(integer
);
856 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
862 bt_ctf_ref_get(&stream
->ref_count
);
865 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
871 bt_ctf_ref_put(&stream
->ref_count
, bt_ctf_stream_destroy
);
875 void bt_ctf_stream_destroy(struct bt_ctf_ref
*ref
)
877 struct bt_ctf_stream
*stream
;
883 stream
= container_of(ref
, struct bt_ctf_stream
, ref_count
);
884 ctf_fini_pos(&stream
->pos
);
885 if (close(stream
->pos
.fd
)) {
889 if (stream
->stream_class
) {
890 bt_ctf_stream_class_put(stream
->stream_class
);
892 if (stream
->events
) {
893 g_ptr_array_free(stream
->events
, TRUE
);
895 if (stream
->event_contexts
) {
896 g_ptr_array_free(stream
->event_contexts
, TRUE
);
898 if (stream
->packet_header
) {
899 bt_ctf_field_put(stream
->packet_header
);
901 if (stream
->packet_context
) {
902 bt_ctf_field_put(stream
->packet_context
);
904 if (stream
->event_context
) {
905 bt_ctf_field_put(stream
->event_context
);
911 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
915 struct bt_ctf_field_type
*field_type
= NULL
;
916 struct bt_ctf_field
*integer
=
917 bt_ctf_field_structure_get_field(structure
, name
);
919 if (!structure
|| !name
) {
925 /* Field not found, not an error. */
929 /* Make sure the payload has not already been set. */
930 if (!bt_ctf_field_validate(integer
)) {
931 /* Payload already set, not an error */
935 field_type
= bt_ctf_field_get_type(integer
);
936 /* Something is serioulsly wrong */
938 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
940 * The user most likely meant for us to populate this field
941 * automatically. However, we can only do this if the field
942 * is an integer. Return an error.
948 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
949 ret
= bt_ctf_field_signed_integer_set_value(integer
,
952 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
956 bt_ctf_field_put(integer
);
959 bt_ctf_field_type_put(field_type
);