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-ir/ref.h>
39 #include <babeltrace/ctf-ir/common-internal.h>
40 #include <babeltrace/ctf-writer/functor-internal.h>
41 #include <babeltrace/compiler.h>
42 #include <babeltrace/align.h>
43 #include <babeltrace/ctf/ctf-index.h>
46 void bt_ctf_stream_destroy(struct bt_ref
*ref
);
48 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
51 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
54 struct bt_ctf_field_type
*magic_field_type
= NULL
;
55 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
56 stream
->packet_header
, "magic");
59 /* No magic field found. Not an error, skip. */
63 if (!bt_ctf_field_validate(magic_field
)) {
64 /* Value already set. Not an error, skip. */
68 magic_field_type
= bt_ctf_field_get_type(magic_field
);
69 assert(magic_field_type
);
71 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
73 /* Magic field is not an integer. Not an error, skip. */
77 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
79 * Magic field is not of the expected size.
85 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
88 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
89 (int64_t) 0xC1FC1FC1);
91 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
92 (uint64_t) 0xC1FC1FC1);
96 bt_ctf_field_put(magic_field
);
98 if (magic_field_type
) {
99 bt_ctf_field_type_put(magic_field_type
);
105 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
108 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
109 struct bt_ctf_field_type
*element_field_type
= NULL
;
110 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
111 stream
->packet_header
, "uuid");
114 /* No uuid field found. Not an error, skip. */
118 if (!bt_ctf_field_validate(uuid_field
)) {
119 /* Value already set. Not an error, skip. */
123 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
124 assert(uuid_field_type
);
125 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
127 /* UUID field is not an array. Not an error, skip. */
131 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
133 * UUID field is not of the expected size.
134 * Not an error, skip.
139 element_field_type
= bt_ctf_field_type_array_get_element_type(
141 assert(element_field_type
);
142 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
144 /* UUID array elements are not integers. Not an error, skip */
148 for (i
= 0; i
< 16; i
++) {
149 struct bt_ctf_field
*uuid_element
=
150 bt_ctf_field_array_get_field(uuid_field
, i
);
152 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
156 ret
= bt_ctf_field_signed_integer_set_value(
157 uuid_element
, (int64_t) stream
->trace
->uuid
[i
]);
159 ret
= bt_ctf_field_unsigned_integer_set_value(
161 (uint64_t) stream
->trace
->uuid
[i
]);
163 bt_ctf_field_put(uuid_element
);
171 bt_ctf_field_put(uuid_field
);
173 if (uuid_field_type
) {
174 bt_ctf_field_type_put(uuid_field_type
);
176 if (element_field_type
) {
177 bt_ctf_field_type_put(element_field_type
);
182 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
186 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
187 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
188 stream
->packet_header
, "stream_id");
190 if (!stream_id_field
) {
191 /* No stream_id field found. Not an error, skip. */
195 if (!bt_ctf_field_validate(stream_id_field
)) {
196 /* Value already set. Not an error, skip. */
200 stream_id_field_type
= bt_ctf_field_get_type(stream_id_field
);
201 assert(stream_id_field_type
);
202 if (bt_ctf_field_type_get_type_id(stream_id_field_type
) !=
204 /* stream_id field is not an integer. Not an error, skip. */
208 stream_id
= stream
->stream_class
->id
;
209 ret
= bt_ctf_field_type_integer_get_signed(stream_id_field_type
);
212 ret
= bt_ctf_field_signed_integer_set_value(stream_id_field
,
213 (int64_t) stream_id
);
215 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
216 (uint64_t) stream_id
);
219 if (stream_id_field
) {
220 bt_ctf_field_put(stream_id_field
);
222 if (stream_id_field_type
) {
223 bt_ctf_field_type_put(stream_id_field_type
);
229 int set_packet_header(struct bt_ctf_stream
*stream
)
233 ret
= set_packet_header_magic(stream
);
238 ret
= set_packet_header_uuid(stream
);
243 ret
= set_packet_header_stream_id(stream
);
252 void put_event(struct bt_ctf_event
*event
)
254 bt_ctf_event_set_stream(event
, NULL
);
255 bt_ctf_event_put(event
);
259 struct bt_ctf_stream
*bt_ctf_stream_create(
260 struct bt_ctf_stream_class
*stream_class
,
261 struct bt_ctf_trace
*trace
)
264 struct bt_ctf_stream
*stream
= NULL
;
266 if (!stream_class
|| !trace
) {
270 stream
= g_new0(struct bt_ctf_stream
, 1);
275 /* A stream has no ownership of its trace (weak ptr) */
276 stream
->trace
= trace
;
277 bt_ctf_base_init(stream
, bt_ctf_stream_destroy
);
278 stream
->packet_context
= bt_ctf_field_create(
279 stream_class
->packet_context_type
);
280 if (!stream
->packet_context
) {
285 * A stream class may not have a stream event context defined
286 * in which case this stream will never have a stream_event_context
287 * member since, after a stream's creation, the parent stream class
288 * is "frozen" (immutable).
290 if (stream_class
->event_context_type
) {
291 stream
->event_context
= bt_ctf_field_create(
292 stream_class
->event_context_type
);
293 if (!stream
->packet_context
) {
298 /* Initialize events_discarded */
299 ret
= set_structure_field_integer(stream
->packet_context
,
300 "events_discarded", 0);
306 stream
->id
= stream_class
->next_stream_id
++;
307 stream
->stream_class
= stream_class
;
308 bt_ctf_stream_class_get(stream_class
);
309 stream
->events
= g_ptr_array_new_with_free_func(
310 (GDestroyNotify
) put_event
);
311 if (!stream
->events
) {
314 if (stream_class
->event_context_type
) {
315 stream
->event_contexts
= g_ptr_array_new_with_free_func(
316 (GDestroyNotify
) bt_ctf_field_put
);
317 if (!stream
->event_contexts
) {
322 /* A trace is not allowed to have a NULL packet header */
323 assert(trace
->packet_header_type
);
324 stream
->packet_header
= bt_ctf_field_create(trace
->packet_header_type
);
325 if (!stream
->packet_header
) {
329 * Attempt to populate the default trace packet header fields
330 * (magic, uuid and stream_id). This will _not_ fail shall the
331 * fields not be found or be of an incompatible type; they will
332 * simply not be populated automatically. The user will have to
333 * make sure to set the trace packet header fields himself before
336 ret
= set_packet_header(stream
);
343 bt_ctf_stream_destroy(&stream
->base
.ref_count
);
348 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
352 if (stream
->pos
.fd
!= -1) {
357 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
363 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
364 struct bt_ctf_stream
*stream
)
366 struct bt_ctf_stream_class
*stream_class
= NULL
;
372 stream_class
= stream
->stream_class
;
373 bt_ctf_stream_class_get(stream_class
);
378 int bt_ctf_stream_get_discarded_events_count(
379 struct bt_ctf_stream
*stream
, uint64_t *count
)
383 struct bt_ctf_field
*events_discarded_field
= NULL
;
384 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
386 if (!stream
|| !count
|| !stream
->packet_context
) {
391 events_discarded_field
= bt_ctf_field_structure_get_field(
392 stream
->packet_context
, "events_discarded");
393 if (!events_discarded_field
) {
398 events_discarded_field_type
= bt_ctf_field_get_type(
399 events_discarded_field
);
400 if (!events_discarded_field_type
) {
405 field_signed
= bt_ctf_field_type_integer_get_signed(
406 events_discarded_field_type
);
407 if (field_signed
< 0) {
413 int64_t signed_count
;
415 ret
= bt_ctf_field_signed_integer_get_value(
416 events_discarded_field
, &signed_count
);
420 if (signed_count
< 0) {
425 *count
= (uint64_t) signed_count
;
427 ret
= bt_ctf_field_unsigned_integer_get_value(
428 events_discarded_field
, count
);
434 if (events_discarded_field
) {
435 bt_ctf_field_put(events_discarded_field
);
437 if (events_discarded_field_type
) {
438 bt_ctf_field_type_put(events_discarded_field_type
);
443 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
444 uint64_t event_count
)
448 uint64_t previous_count
;
450 struct bt_ctf_field
*events_discarded_field
= NULL
;
451 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
453 if (!stream
|| !stream
->packet_context
) {
457 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
463 events_discarded_field
= bt_ctf_field_structure_get_field(
464 stream
->packet_context
, "events_discarded");
465 if (!events_discarded_field
) {
469 events_discarded_field_type
= bt_ctf_field_get_type(
470 events_discarded_field
);
471 if (!events_discarded_field_type
) {
475 field_signed
= bt_ctf_field_type_integer_get_signed(
476 events_discarded_field_type
);
477 if (field_signed
< 0) {
481 new_count
= previous_count
+ event_count
;
483 ret
= bt_ctf_field_signed_integer_set_value(
484 events_discarded_field
, (int64_t) new_count
);
489 ret
= bt_ctf_field_unsigned_integer_set_value(
490 events_discarded_field
, new_count
);
497 if (events_discarded_field
) {
498 bt_ctf_field_put(events_discarded_field
);
500 if (events_discarded_field_type
) {
501 bt_ctf_field_type_put(events_discarded_field_type
);
505 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
506 struct bt_ctf_event
*event
)
509 struct bt_ctf_field
*event_context_copy
= NULL
;
511 if (!stream
|| !event
) {
516 ret
= bt_ctf_event_set_stream(event
, stream
);
518 /* Event was already associated to a stream */
523 ret
= bt_ctf_event_populate_event_header(event
);
528 /* Make sure the event's payload is set */
529 ret
= bt_ctf_event_validate(event
);
534 /* Sample the current stream event context by copying it */
535 if (stream
->event_context
) {
536 /* Make sure the event context's payload is set */
537 ret
= bt_ctf_field_validate(stream
->event_context
);
542 event_context_copy
= bt_ctf_field_copy(stream
->event_context
);
543 if (!event_context_copy
) {
549 bt_ctf_event_get(event
);
550 /* Save the new event along with its associated stream event context */
551 g_ptr_array_add(stream
->events
, event
);
552 if (event_context_copy
) {
553 g_ptr_array_add(stream
->event_contexts
, event_context_copy
);
557 (void) bt_ctf_event_set_stream(event
, NULL
);
562 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
563 struct bt_ctf_stream
*stream
)
565 struct bt_ctf_field
*packet_context
= NULL
;
571 packet_context
= stream
->packet_context
;
572 if (packet_context
) {
573 bt_ctf_field_get(packet_context
);
576 return packet_context
;
579 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
580 struct bt_ctf_field
*field
)
583 struct bt_ctf_field_type
*field_type
;
585 if (!stream
|| !field
) {
590 field_type
= bt_ctf_field_get_type(field
);
591 if (field_type
!= stream
->stream_class
->packet_context_type
) {
596 bt_ctf_field_type_put(field_type
);
597 bt_ctf_field_get(field
);
598 bt_ctf_field_put(stream
->packet_context
);
599 stream
->packet_context
= field
;
604 struct bt_ctf_field
*bt_ctf_stream_get_event_context(
605 struct bt_ctf_stream
*stream
)
607 struct bt_ctf_field
*event_context
= NULL
;
613 event_context
= stream
->event_context
;
615 bt_ctf_field_get(event_context
);
618 return event_context
;
621 int bt_ctf_stream_set_event_context(struct bt_ctf_stream
*stream
,
622 struct bt_ctf_field
*field
)
625 struct bt_ctf_field_type
*field_type
= NULL
;
627 if (!stream
|| !field
) {
632 field_type
= bt_ctf_field_get_type(field
);
633 if (field_type
!= stream
->stream_class
->event_context_type
) {
638 bt_ctf_field_get(field
);
639 bt_ctf_field_put(stream
->event_context
);
640 stream
->event_context
= field
;
643 bt_ctf_field_type_put(field_type
);
648 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
649 struct bt_ctf_stream
*stream
)
651 struct bt_ctf_field
*packet_header
= NULL
;
657 packet_header
= stream
->packet_header
;
659 bt_ctf_field_get(packet_header
);
662 return packet_header
;
665 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
666 struct bt_ctf_field
*field
)
669 struct bt_ctf_field_type
*field_type
= NULL
;
671 if (!stream
|| !field
) {
676 field_type
= bt_ctf_field_get_type(field
);
677 if (field_type
!= stream
->trace
->packet_header_type
) {
682 bt_ctf_field_get(field
);
683 bt_ctf_field_put(stream
->packet_header
);
684 stream
->packet_header
= field
;
687 bt_ctf_field_type_put(field_type
);
693 int get_event_header_timestamp(struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
696 struct bt_ctf_field
*timestamp_field
= NULL
;
697 struct bt_ctf_field_type
*timestamp_field_type
= NULL
;
699 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
701 if (!timestamp_field
) {
706 timestamp_field_type
= bt_ctf_field_get_type(timestamp_field
);
707 assert(timestamp_field_type
);
708 if (bt_ctf_field_type_get_type_id(timestamp_field_type
) !=
714 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type
)) {
717 ret
= bt_ctf_field_signed_integer_get_value(timestamp_field
,
722 *timestamp
= (uint64_t) val
;
724 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
731 bt_ctf_field_put(timestamp_field
);
732 bt_ctf_field_type_put(timestamp_field_type
);
736 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
740 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
741 struct bt_ctf_field
*integer
= NULL
;
742 struct ctf_stream_pos packet_context_pos
;
744 if (!stream
|| stream
->pos
.fd
< 0) {
746 * Stream does not have an associated fd. It is,
747 * therefore, not a stream being used to write events.
753 if (!stream
->events
->len
) {
757 ret
= bt_ctf_field_validate(stream
->packet_header
);
762 /* mmap the next packet */
763 ctf_packet_seek(&stream
->pos
.parent
, 0, SEEK_CUR
);
765 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
);
770 /* Set the default context attributes if present and unset. */
771 if (!get_event_header_timestamp(
772 ((struct bt_ctf_event
*) g_ptr_array_index(
773 stream
->events
, 0))->event_header
, ×tamp_begin
)) {
774 ret
= set_structure_field_integer(stream
->packet_context
,
775 "timestamp_begin", timestamp_begin
);
781 if (!get_event_header_timestamp(
782 ((struct bt_ctf_event
*) g_ptr_array_index(
783 stream
->events
, stream
->events
->len
- 1))->event_header
,
786 ret
= set_structure_field_integer(stream
->packet_context
,
787 "timestamp_end", timestamp_end
);
792 ret
= set_structure_field_integer(stream
->packet_context
,
793 "content_size", UINT64_MAX
);
798 ret
= set_structure_field_integer(stream
->packet_context
,
799 "packet_size", UINT64_MAX
);
804 /* Write packet context */
805 memcpy(&packet_context_pos
, &stream
->pos
,
806 sizeof(struct ctf_stream_pos
));
807 ret
= bt_ctf_field_serialize(stream
->packet_context
,
813 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
819 /* Unset the packet context's fields. */
820 ret
= bt_ctf_field_reset(stream
->packet_context
);
825 /* Set the previous number of discarded events. */
826 ret
= set_structure_field_integer(stream
->packet_context
,
827 "events_discarded", events_discarded
);
832 for (i
= 0; i
< stream
->events
->len
; i
++) {
833 struct bt_ctf_event
*event
= g_ptr_array_index(
836 ret
= bt_ctf_field_reset(event
->event_header
);
841 /* Write event header */
842 ret
= bt_ctf_field_serialize(event
->event_header
,
848 /* Write stream event context */
849 if (stream
->event_contexts
) {
850 ret
= bt_ctf_field_serialize(
851 g_ptr_array_index(stream
->event_contexts
, i
),
858 /* Write event content */
859 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
866 * Update the packet total size and content size and overwrite the
868 * Copy base_mma as the packet may have been remapped (e.g. when a
869 * packet is resized).
871 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
872 ret
= set_structure_field_integer(stream
->packet_context
,
873 "content_size", stream
->pos
.offset
);
878 ret
= set_structure_field_integer(stream
->packet_context
,
879 "packet_size", stream
->pos
.packet_size
);
884 ret
= bt_ctf_field_serialize(stream
->packet_context
,
885 &packet_context_pos
);
890 g_ptr_array_set_size(stream
->events
, 0);
891 if (stream
->event_contexts
) {
892 g_ptr_array_set_size(stream
->event_contexts
, 0);
894 stream
->flushed_packet_count
++;
896 bt_ctf_field_put(integer
);
900 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
905 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
911 void bt_ctf_stream_destroy(struct bt_ref
*ref
)
913 struct bt_ctf_stream
*stream
;
914 struct bt_ctf_base
*base
;
920 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
921 stream
= container_of(base
, struct bt_ctf_stream
, base
);
922 ctf_fini_pos(&stream
->pos
);
923 if (stream
->pos
.fd
>= 0 && close(stream
->pos
.fd
)) {
927 if (stream
->stream_class
) {
928 bt_ctf_stream_class_put(stream
->stream_class
);
930 if (stream
->events
) {
931 g_ptr_array_free(stream
->events
, TRUE
);
933 if (stream
->event_contexts
) {
934 g_ptr_array_free(stream
->event_contexts
, TRUE
);
936 if (stream
->packet_header
) {
937 bt_ctf_field_put(stream
->packet_header
);
939 if (stream
->packet_context
) {
940 bt_ctf_field_put(stream
->packet_context
);
942 if (stream
->event_context
) {
943 bt_ctf_field_put(stream
->event_context
);
949 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
953 struct bt_ctf_field_type
*field_type
= NULL
;
954 struct bt_ctf_field
*integer
=
955 bt_ctf_field_structure_get_field(structure
, name
);
957 if (!structure
|| !name
) {
963 /* Field not found, not an error. */
967 /* Make sure the payload has not already been set. */
968 if (!bt_ctf_field_validate(integer
)) {
969 /* Payload already set, not an error */
973 field_type
= bt_ctf_field_get_type(integer
);
974 /* Something is serioulsly wrong */
976 if (bt_ctf_field_type_get_type_id(field_type
) != CTF_TYPE_INTEGER
) {
978 * The user most likely meant for us to populate this field
979 * automatically. However, we can only do this if the field
980 * is an integer. Return an error.
986 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
987 ret
= bt_ctf_field_signed_integer_set_value(integer
,
990 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
994 bt_ctf_field_put(integer
);
997 bt_ctf_field_type_put(field_type
);