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/field-types-internal.h>
34 #include <babeltrace/ctf-ir/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/ref.h>
39 #include <babeltrace/ctf-writer/functor-internal.h>
40 #include <babeltrace/compiler.h>
41 #include <babeltrace/align.h>
42 #include <babeltrace/ctf/ctf-index.h>
45 void bt_ctf_stream_destroy(struct bt_object
*obj
);
47 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
50 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
53 struct bt_ctf_field_type
*magic_field_type
= NULL
;
54 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
55 stream
->packet_header
, "magic");
58 /* No magic field found. Not an error, skip. */
62 if (!bt_ctf_field_validate(magic_field
)) {
63 /* Value already set. Not an error, skip. */
67 magic_field_type
= bt_ctf_field_get_type(magic_field
);
68 assert(magic_field_type
);
70 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
71 BT_CTF_TYPE_ID_INTEGER
) {
72 /* Magic field is not an integer. Not an error, skip. */
76 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
78 * Magic field is not of the expected size.
84 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
87 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
88 (int64_t) 0xC1FC1FC1);
90 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
91 (uint64_t) 0xC1FC1FC1);
95 bt_put(magic_field_type
);
100 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
103 struct bt_ctf_trace
*trace
= NULL
;
104 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
105 struct bt_ctf_field_type
*element_field_type
= NULL
;
106 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
107 stream
->packet_header
, "uuid");
110 /* No uuid field found. Not an error, skip. */
114 if (!bt_ctf_field_validate(uuid_field
)) {
115 /* Value already set. Not an error, skip. */
119 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
120 assert(uuid_field_type
);
121 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
122 BT_CTF_TYPE_ID_ARRAY
) {
123 /* UUID field is not an array. Not an error, skip. */
127 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
129 * UUID field is not of the expected size.
130 * Not an error, skip.
135 element_field_type
= bt_ctf_field_type_array_get_element_type(
137 assert(element_field_type
);
138 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
139 BT_CTF_TYPE_ID_INTEGER
) {
140 /* UUID array elements are not integers. Not an error, skip */
144 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
145 for (i
= 0; i
< 16; i
++) {
146 struct bt_ctf_field
*uuid_element
=
147 bt_ctf_field_array_get_field(uuid_field
, i
);
149 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
153 ret
= bt_ctf_field_signed_integer_set_value(
154 uuid_element
, (int64_t) trace
->uuid
[i
]);
156 ret
= bt_ctf_field_unsigned_integer_set_value(
158 (uint64_t) trace
->uuid
[i
]);
160 bt_put(uuid_element
);
168 bt_put(uuid_field_type
);
169 bt_put(element_field_type
);
174 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
178 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
179 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
180 stream
->packet_header
, "stream_id");
182 if (!stream_id_field
) {
183 /* No stream_id field found. Not an error, skip. */
187 if (!bt_ctf_field_validate(stream_id_field
)) {
188 /* Value already set. Not an error, skip. */
192 stream_id_field_type
= bt_ctf_field_get_type(stream_id_field
);
193 assert(stream_id_field_type
);
194 if (bt_ctf_field_type_get_type_id(stream_id_field_type
) !=
195 BT_CTF_TYPE_ID_INTEGER
) {
196 /* stream_id field is not an integer. Not an error, skip. */
200 stream_id
= stream
->stream_class
->id
;
201 ret
= bt_ctf_field_type_integer_get_signed(stream_id_field_type
);
204 ret
= bt_ctf_field_signed_integer_set_value(stream_id_field
,
205 (int64_t) stream_id
);
207 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
208 (uint64_t) stream_id
);
211 bt_put(stream_id_field
);
212 bt_put(stream_id_field_type
);
217 int set_packet_header(struct bt_ctf_stream
*stream
)
221 ret
= set_packet_header_magic(stream
);
226 ret
= set_packet_header_uuid(stream
);
231 ret
= set_packet_header_stream_id(stream
);
240 void release_event(struct bt_ctf_event
*event
)
242 if (bt_object_get_ref_count(event
)) {
244 * The event is being orphaned, but it must guarantee the
245 * existence of its event class for the duration of its
248 bt_get(event
->event_class
);
249 BT_PUT(event
->base
.parent
);
251 bt_object_release(event
);
256 struct bt_ctf_stream
*bt_ctf_stream_create(
257 struct bt_ctf_stream_class
*stream_class
,
258 struct bt_ctf_trace
*trace
)
261 struct bt_ctf_stream
*stream
= NULL
;
263 if (!stream_class
|| !trace
) {
267 stream
= g_new0(struct bt_ctf_stream
, 1);
272 bt_object_init(stream
, bt_ctf_stream_destroy
);
274 * Acquire reference to parent since stream will become publicly
275 * reachable; it needs its parent to remain valid.
277 bt_object_set_parent(stream
, trace
);
278 stream
->packet_context
= bt_ctf_field_create(
279 stream_class
->packet_context_type
);
280 if (!stream
->packet_context
) {
284 /* Initialize events_discarded */
285 ret
= set_structure_field_integer(stream
->packet_context
,
286 "events_discarded", 0);
292 stream
->id
= stream_class
->next_stream_id
++;
293 stream
->stream_class
= stream_class
;
294 stream
->events
= g_ptr_array_new_with_free_func(
295 (GDestroyNotify
) release_event
);
296 if (!stream
->events
) {
300 /* A trace is not allowed to have a NULL packet header */
301 assert(trace
->packet_header_type
);
302 stream
->packet_header
= bt_ctf_field_create(trace
->packet_header_type
);
303 if (!stream
->packet_header
) {
307 * Attempt to populate the default trace packet header fields
308 * (magic, uuid and stream_id). This will _not_ fail shall the
309 * fields not be found or be of an incompatible type; they will
310 * simply not be populated automatically. The user will have to
311 * make sure to set the trace packet header fields himself before
314 ret
= set_packet_header(stream
);
327 int bt_ctf_stream_set_fd(struct bt_ctf_stream
*stream
, int fd
)
331 if (stream
->pos
.fd
!= -1) {
336 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
342 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
343 struct bt_ctf_stream
*stream
)
345 struct bt_ctf_stream_class
*stream_class
= NULL
;
351 stream_class
= stream
->stream_class
;
352 bt_get(stream_class
);
357 int bt_ctf_stream_get_discarded_events_count(
358 struct bt_ctf_stream
*stream
, uint64_t *count
)
362 struct bt_ctf_field
*events_discarded_field
= NULL
;
363 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
365 if (!stream
|| !count
|| !stream
->packet_context
||
366 stream
->pos
.fd
< 0) {
371 events_discarded_field
= bt_ctf_field_structure_get_field(
372 stream
->packet_context
, "events_discarded");
373 if (!events_discarded_field
) {
378 events_discarded_field_type
= bt_ctf_field_get_type(
379 events_discarded_field
);
380 if (!events_discarded_field_type
) {
385 field_signed
= bt_ctf_field_type_integer_get_signed(
386 events_discarded_field_type
);
387 if (field_signed
< 0) {
393 int64_t signed_count
;
395 ret
= bt_ctf_field_signed_integer_get_value(
396 events_discarded_field
, &signed_count
);
400 if (signed_count
< 0) {
405 *count
= (uint64_t) signed_count
;
407 ret
= bt_ctf_field_unsigned_integer_get_value(
408 events_discarded_field
, count
);
414 bt_put(events_discarded_field
);
415 bt_put(events_discarded_field_type
);
419 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
420 uint64_t event_count
)
424 uint64_t previous_count
;
426 struct bt_ctf_field
*events_discarded_field
= NULL
;
427 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
429 if (!stream
|| !stream
->packet_context
|| stream
->pos
.fd
< 0) {
433 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
439 events_discarded_field
= bt_ctf_field_structure_get_field(
440 stream
->packet_context
, "events_discarded");
441 if (!events_discarded_field
) {
445 events_discarded_field_type
= bt_ctf_field_get_type(
446 events_discarded_field
);
447 if (!events_discarded_field_type
) {
451 field_signed
= bt_ctf_field_type_integer_get_signed(
452 events_discarded_field_type
);
453 if (field_signed
< 0) {
457 new_count
= previous_count
+ event_count
;
459 ret
= bt_ctf_field_signed_integer_set_value(
460 events_discarded_field
, (int64_t) new_count
);
465 ret
= bt_ctf_field_unsigned_integer_set_value(
466 events_discarded_field
, new_count
);
473 bt_put(events_discarded_field
);
474 bt_put(events_discarded_field_type
);
477 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
478 struct bt_ctf_event
*event
)
482 if (!stream
|| !event
|| stream
->pos
.fd
< 0) {
487 bt_object_set_parent(event
, stream
);
488 ret
= bt_ctf_event_populate_event_header(event
);
493 /* Make sure the various scopes of the event are set */
494 ret
= bt_ctf_event_validate(event
);
499 /* Save the new event and freeze it */
500 bt_ctf_event_freeze(event
);
501 g_ptr_array_add(stream
->events
, event
);
504 * Event had to hold a reference to its event class as long as it wasn't
505 * part of the same trace hierarchy. From now on, the event and its
506 * class share the same lifetime guarantees and the reference is no
509 bt_put(event
->event_class
);
513 * Orphan the event; we were not succesful in associating it to
516 bt_object_set_parent(event
, NULL
);
521 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
522 struct bt_ctf_stream
*stream
)
524 struct bt_ctf_field
*packet_context
= NULL
;
526 if (!stream
|| stream
->pos
.fd
< 0) {
530 packet_context
= stream
->packet_context
;
531 if (packet_context
) {
532 bt_get(packet_context
);
535 return packet_context
;
538 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
539 struct bt_ctf_field
*field
)
542 struct bt_ctf_field_type
*field_type
;
544 if (!stream
|| !field
|| stream
->pos
.fd
< 0) {
549 field_type
= bt_ctf_field_get_type(field
);
550 if (bt_ctf_field_type_compare(field_type
,
551 stream
->stream_class
->packet_context_type
)) {
558 bt_put(stream
->packet_context
);
559 stream
->packet_context
= field
;
564 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
565 struct bt_ctf_stream
*stream
)
567 struct bt_ctf_field
*packet_header
= NULL
;
569 if (!stream
|| stream
->pos
.fd
< 0) {
573 packet_header
= stream
->packet_header
;
575 bt_get(packet_header
);
578 return packet_header
;
581 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
582 struct bt_ctf_field
*field
)
585 struct bt_ctf_trace
*trace
= NULL
;
586 struct bt_ctf_field_type
*field_type
= NULL
;
588 if (!stream
|| !field
|| stream
->pos
.fd
< 0) {
593 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
594 field_type
= bt_ctf_field_get_type(field
);
595 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
601 bt_put(stream
->packet_header
);
602 stream
->packet_header
= field
;
610 int get_event_header_timestamp(struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
613 struct bt_ctf_field
*timestamp_field
= NULL
;
614 struct bt_ctf_field_type
*timestamp_field_type
= NULL
;
616 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
618 if (!timestamp_field
) {
623 timestamp_field_type
= bt_ctf_field_get_type(timestamp_field
);
624 assert(timestamp_field_type
);
625 if (bt_ctf_field_type_get_type_id(timestamp_field_type
) !=
626 BT_CTF_TYPE_ID_INTEGER
) {
631 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type
)) {
634 ret
= bt_ctf_field_signed_integer_get_value(timestamp_field
,
639 *timestamp
= (uint64_t) val
;
641 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
648 bt_put(timestamp_field
);
649 bt_put(timestamp_field_type
);
653 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
657 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
658 struct bt_ctf_field
*integer
= NULL
;
659 struct ctf_stream_pos packet_context_pos
;
661 if (!stream
|| stream
->pos
.fd
< 0) {
663 * Stream does not have an associated fd. It is,
664 * therefore, not a stream being used to write events.
670 if (!stream
->events
->len
) {
674 ret
= bt_ctf_field_validate(stream
->packet_header
);
679 /* mmap the next packet */
680 ctf_packet_seek(&stream
->pos
.parent
, 0, SEEK_CUR
);
682 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
);
687 /* Set the default context attributes if present and unset. */
688 if (!get_event_header_timestamp(
689 ((struct bt_ctf_event
*) g_ptr_array_index(
690 stream
->events
, 0))->event_header
, ×tamp_begin
)) {
691 ret
= set_structure_field_integer(stream
->packet_context
,
692 "timestamp_begin", timestamp_begin
);
698 if (!get_event_header_timestamp(
699 ((struct bt_ctf_event
*) g_ptr_array_index(
700 stream
->events
, stream
->events
->len
- 1))->event_header
,
703 ret
= set_structure_field_integer(stream
->packet_context
,
704 "timestamp_end", timestamp_end
);
709 ret
= set_structure_field_integer(stream
->packet_context
,
710 "content_size", UINT64_MAX
);
715 ret
= set_structure_field_integer(stream
->packet_context
,
716 "packet_size", UINT64_MAX
);
721 /* Write packet context */
722 memcpy(&packet_context_pos
, &stream
->pos
,
723 sizeof(struct ctf_stream_pos
));
724 ret
= bt_ctf_field_serialize(stream
->packet_context
,
730 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
736 /* Unset the packet context's fields. */
737 ret
= bt_ctf_field_reset(stream
->packet_context
);
742 /* Set the previous number of discarded events. */
743 ret
= set_structure_field_integer(stream
->packet_context
,
744 "events_discarded", events_discarded
);
749 for (i
= 0; i
< stream
->events
->len
; i
++) {
750 struct bt_ctf_event
*event
= g_ptr_array_index(
753 ret
= bt_ctf_field_reset(event
->event_header
);
758 /* Write event header */
759 ret
= bt_ctf_field_serialize(event
->event_header
,
765 /* Write stream event context */
766 if (event
->stream_event_context
) {
767 ret
= bt_ctf_field_serialize(
768 event
->stream_event_context
, &stream
->pos
);
774 /* Write event content */
775 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
782 * Update the packet total size and content size and overwrite the
784 * Copy base_mma as the packet may have been remapped (e.g. when a
785 * packet is resized).
787 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
788 ret
= set_structure_field_integer(stream
->packet_context
,
789 "content_size", stream
->pos
.offset
);
794 ret
= set_structure_field_integer(stream
->packet_context
,
795 "packet_size", stream
->pos
.packet_size
);
800 ret
= bt_ctf_field_serialize(stream
->packet_context
,
801 &packet_context_pos
);
806 g_ptr_array_set_size(stream
->events
, 0);
807 stream
->flushed_packet_count
++;
813 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
818 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
824 void bt_ctf_stream_destroy(struct bt_object
*obj
)
826 struct bt_ctf_stream
*stream
;
828 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
829 ctf_fini_pos(&stream
->pos
);
830 if (stream
->pos
.fd
>= 0 && close(stream
->pos
.fd
)) {
834 if (stream
->events
) {
835 g_ptr_array_free(stream
->events
, TRUE
);
837 bt_put(stream
->packet_header
);
838 bt_put(stream
->packet_context
);
839 bt_put(stream
->event_context
);
844 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
848 struct bt_ctf_field_type
*field_type
= NULL
;
849 struct bt_ctf_field
*integer
=
850 bt_ctf_field_structure_get_field(structure
, name
);
852 if (!structure
|| !name
) {
858 /* Field not found, not an error. */
862 /* Make sure the payload has not already been set. */
863 if (!bt_ctf_field_validate(integer
)) {
864 /* Payload already set, not an error */
868 field_type
= bt_ctf_field_get_type(integer
);
869 /* Something is serioulsly wrong */
871 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
873 * The user most likely meant for us to populate this field
874 * automatically. However, we can only do this if the field
875 * is an integer. Return an error.
881 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
882 ret
= bt_ctf_field_signed_integer_set_value(integer
,
885 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);