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-class.h>
30 #include <babeltrace/ctf-writer/clock.h>
31 #include <babeltrace/ctf-writer/clock-internal.h>
32 #include <babeltrace/ctf-writer/event.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/ctf-ir/fields-internal.h>
36 #include <babeltrace/ctf-ir/stream.h>
37 #include <babeltrace/ctf-ir/stream-internal.h>
38 #include <babeltrace/ctf-ir/stream-class-internal.h>
39 #include <babeltrace/ctf-ir/trace-internal.h>
40 #include <babeltrace/ctf-writer/writer-internal.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/ctf-writer/functor-internal.h>
43 #include <babeltrace/compiler.h>
44 #include <babeltrace/align.h>
45 #include <babeltrace/ctf/ctf-index.h>
48 void bt_ctf_stream_destroy(struct bt_object
*obj
);
50 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
53 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
56 struct bt_ctf_field_type
*field_type
= NULL
;
63 field_type
= bt_ctf_field_get_type(field
);
66 if (bt_ctf_field_type_get_type_id(field_type
) !=
67 BT_CTF_TYPE_ID_INTEGER
) {
68 /* Not an integer and the value is unset, error. */
73 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
74 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
76 /* Value is out of range, error. */
80 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
82 /* Value is out of range, error. */
92 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
95 struct bt_ctf_field_type
*magic_field_type
= NULL
;
96 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
97 stream
->packet_header
, "magic");
100 /* No magic field found. Not an error, skip. */
104 if (bt_ctf_field_is_set(magic_field
)) {
105 /* Value already set. Not an error, skip. */
109 magic_field_type
= bt_ctf_field_get_type(magic_field
);
110 assert(magic_field_type
);
112 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
113 BT_CTF_TYPE_ID_INTEGER
) {
114 /* Magic field is not an integer. Not an error, skip. */
118 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
120 * Magic field is not of the expected size.
121 * Not an error, skip.
126 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
129 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
130 (int64_t) 0xC1FC1FC1);
132 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
133 (uint64_t) 0xC1FC1FC1);
137 bt_put(magic_field_type
);
142 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
145 struct bt_ctf_trace
*trace
= NULL
;
146 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
147 struct bt_ctf_field_type
*element_field_type
= NULL
;
148 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
149 stream
->packet_header
, "uuid");
152 /* No uuid field found. Not an error, skip. */
156 if (bt_ctf_field_is_set(uuid_field
)) {
157 /* Value already set. Not an error, skip. */
161 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
162 assert(uuid_field_type
);
163 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
164 BT_CTF_TYPE_ID_ARRAY
) {
165 /* UUID field is not an array. Not an error, skip. */
169 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
171 * UUID field is not of the expected size.
172 * Not an error, skip.
177 element_field_type
= bt_ctf_field_type_array_get_element_type(
179 assert(element_field_type
);
180 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
181 BT_CTF_TYPE_ID_INTEGER
) {
182 /* UUID array elements are not integers. Not an error, skip */
186 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
187 for (i
= 0; i
< 16; i
++) {
188 struct bt_ctf_field
*uuid_element
=
189 bt_ctf_field_array_get_field(uuid_field
, i
);
191 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
195 ret
= bt_ctf_field_signed_integer_set_value(
196 uuid_element
, (int64_t) trace
->uuid
[i
]);
198 ret
= bt_ctf_field_unsigned_integer_set_value(
200 (uint64_t) trace
->uuid
[i
]);
202 bt_put(uuid_element
);
210 bt_put(uuid_field_type
);
211 bt_put(element_field_type
);
216 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
220 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
221 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
222 stream
->packet_header
, "stream_id");
224 if (!stream_id_field
) {
225 /* No stream_id field found. Not an error, skip. */
229 if (bt_ctf_field_is_set(stream_id_field
)) {
230 /* Value already set. Not an error, skip. */
234 stream_id_field_type
= bt_ctf_field_get_type(stream_id_field
);
235 assert(stream_id_field_type
);
236 if (bt_ctf_field_type_get_type_id(stream_id_field_type
) !=
237 BT_CTF_TYPE_ID_INTEGER
) {
238 /* stream_id field is not an integer. Not an error, skip. */
242 stream_id
= stream
->stream_class
->id
;
243 ret
= bt_ctf_field_type_integer_get_signed(stream_id_field_type
);
246 ret
= bt_ctf_field_signed_integer_set_value(stream_id_field
,
247 (int64_t) stream_id
);
249 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
250 (uint64_t) stream_id
);
253 bt_put(stream_id_field
);
254 bt_put(stream_id_field_type
);
259 int set_packet_header(struct bt_ctf_stream
*stream
)
263 ret
= set_packet_header_magic(stream
);
268 ret
= set_packet_header_uuid(stream
);
273 ret
= set_packet_header_stream_id(stream
);
282 void release_event(struct bt_ctf_event
*event
)
284 if (bt_object_get_ref_count(event
)) {
286 * The event is being orphaned, but it must guarantee the
287 * existence of its event class for the duration of its
290 bt_get(event
->event_class
);
291 BT_PUT(event
->base
.parent
);
293 bt_object_release(event
);
298 int create_stream_file(struct bt_ctf_writer
*writer
,
299 struct bt_ctf_stream
*stream
)
302 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
304 if (stream
->stream_class
->name
->len
== 0) {
307 ret
= bt_ctf_stream_class_get_id(stream
->stream_class
);
313 g_string_printf(filename
, "stream_%" PRId64
, ret
);
316 g_string_append_printf(filename
, "_%" PRIu32
, stream
->id
);
317 fd
= openat(writer
->trace_dir_fd
, filename
->str
,
318 O_RDWR
| O_CREAT
| O_TRUNC
,
319 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
321 g_string_free(filename
, TRUE
);
326 int set_stream_fd(struct bt_ctf_stream
*stream
, int fd
)
330 if (stream
->pos
.fd
!= -1) {
335 ctf_init_pos(&stream
->pos
, NULL
, fd
, O_RDWR
);
341 struct bt_ctf_stream
*bt_ctf_stream_create(
342 struct bt_ctf_stream_class
*stream_class
,
346 struct bt_ctf_stream
*stream
= NULL
;
347 struct bt_ctf_trace
*trace
= NULL
;
348 struct bt_ctf_writer
*writer
= NULL
;
354 trace
= bt_ctf_stream_class_get_trace(stream_class
);
359 stream
= g_new0(struct bt_ctf_stream
, 1);
364 bt_object_init(stream
, bt_ctf_stream_destroy
);
366 * Acquire reference to parent since stream will become publicly
367 * reachable; it needs its parent to remain valid.
369 bt_object_set_parent(stream
, trace
);
370 stream
->id
= stream_class
->next_stream_id
++;
371 stream
->stream_class
= stream_class
;
375 stream
->name
= g_string_new(name
);
381 if (trace
->is_created_by_writer
) {
383 writer
= (struct bt_ctf_writer
*)
384 bt_object_get_parent(trace
);
387 if (stream_class
->packet_context_type
) {
388 stream
->packet_context
= bt_ctf_field_create(
389 stream_class
->packet_context_type
);
390 if (!stream
->packet_context
) {
394 /* Initialize events_discarded */
395 ret
= set_structure_field_integer(stream
->packet_context
,
396 "events_discarded", 0);
402 stream
->events
= g_ptr_array_new_with_free_func(
403 (GDestroyNotify
) release_event
);
404 if (!stream
->events
) {
408 /* A trace is not allowed to have a NULL packet header */
409 assert(trace
->packet_header_type
);
410 stream
->packet_header
=
411 bt_ctf_field_create(trace
->packet_header_type
);
412 if (!stream
->packet_header
) {
417 * Attempt to populate the default trace packet header fields
418 * (magic, uuid and stream_id). This will _not_ fail shall the
419 * fields not be found or be of an incompatible type; they will
420 * simply not be populated automatically. The user will have to
421 * make sure to set the trace packet header fields himself
424 ret
= set_packet_header(stream
);
429 /* Create file associated with this stream */
430 fd
= create_stream_file(writer
, stream
);
435 ret
= set_stream_fd(stream
, fd
);
440 /* Freeze the writer */
441 bt_ctf_writer_freeze(writer
);
443 /* Non-writer stream indicated by a negative FD */
444 ret
= set_stream_fd(stream
, -1);
450 /* Add this stream to the trace's streams */
451 g_ptr_array_add(trace
->streams
, stream
);
463 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
464 struct bt_ctf_stream
*stream
)
466 struct bt_ctf_stream_class
*stream_class
= NULL
;
472 stream_class
= stream
->stream_class
;
473 bt_get(stream_class
);
478 int bt_ctf_stream_get_discarded_events_count(
479 struct bt_ctf_stream
*stream
, uint64_t *count
)
483 struct bt_ctf_field
*events_discarded_field
= NULL
;
484 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
486 if (!stream
|| !count
|| !stream
->packet_context
||
487 stream
->pos
.fd
< 0) {
492 events_discarded_field
= bt_ctf_field_structure_get_field(
493 stream
->packet_context
, "events_discarded");
494 if (!events_discarded_field
) {
499 events_discarded_field_type
= bt_ctf_field_get_type(
500 events_discarded_field
);
501 if (!events_discarded_field_type
) {
506 field_signed
= bt_ctf_field_type_integer_get_signed(
507 events_discarded_field_type
);
508 if (field_signed
< 0) {
514 int64_t signed_count
;
516 ret
= bt_ctf_field_signed_integer_get_value(
517 events_discarded_field
, &signed_count
);
521 if (signed_count
< 0) {
526 *count
= (uint64_t) signed_count
;
528 ret
= bt_ctf_field_unsigned_integer_get_value(
529 events_discarded_field
, count
);
535 bt_put(events_discarded_field
);
536 bt_put(events_discarded_field_type
);
540 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
541 uint64_t event_count
)
545 uint64_t previous_count
;
547 struct bt_ctf_field
*events_discarded_field
= NULL
;
548 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
550 if (!stream
|| !stream
->packet_context
|| stream
->pos
.fd
< 0) {
554 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
560 events_discarded_field
= bt_ctf_field_structure_get_field(
561 stream
->packet_context
, "events_discarded");
562 if (!events_discarded_field
) {
566 events_discarded_field_type
= bt_ctf_field_get_type(
567 events_discarded_field
);
568 if (!events_discarded_field_type
) {
572 field_signed
= bt_ctf_field_type_integer_get_signed(
573 events_discarded_field_type
);
574 if (field_signed
< 0) {
578 new_count
= previous_count
+ event_count
;
580 ret
= bt_ctf_field_signed_integer_set_value(
581 events_discarded_field
, (int64_t) new_count
);
586 ret
= bt_ctf_field_unsigned_integer_set_value(
587 events_discarded_field
, new_count
);
594 bt_put(events_discarded_field
);
595 bt_put(events_discarded_field_type
);
598 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
599 struct bt_ctf_event
*event
)
602 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
603 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
605 if (!event
|| event
->frozen
) {
611 * The condition to automatically set the ID are:
613 * 1. The event header field "id" exists and is an integer
615 * 2. The event header field "id" is NOT set.
617 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
618 if (id_field
&& !bt_ctf_field_is_set(id_field
)) {
619 ret
= set_integer_field_value(id_field
,
620 (uint64_t) bt_ctf_event_class_get_id(
621 event
->event_class
));
628 * The conditions to automatically set the timestamp are:
630 * 1. The event header field "timestamp" exists and is an
632 * 2. This stream's class has a registered clock (set with
633 * bt_ctf_stream_class_set_clock()).
634 * 3. The event header field "timestamp" has its type mapped to
635 * a clock class which is also the clock class of this
636 * stream's class's registered clock.
637 * 4. The event header field "timestamp" is NOT set.
639 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
641 if (timestamp_field
&& !bt_ctf_field_is_set(timestamp_field
) &&
642 stream
->stream_class
->clock
) {
643 struct bt_ctf_clock_class
*stream_class_clock_class
=
644 stream
->stream_class
->clock
->clock_class
;
645 struct bt_ctf_field_type
*timestamp_field_type
=
646 bt_ctf_field_get_type(timestamp_field
);
648 assert(timestamp_field_type
);
650 bt_ctf_field_type_integer_get_mapped_clock_class(
651 timestamp_field_type
);
652 BT_PUT(timestamp_field_type
);
653 if (mapped_clock_class
== stream_class_clock_class
) {
656 ret
= bt_ctf_clock_get_value(
657 stream
->stream_class
->clock
,
663 ret
= set_integer_field_value(timestamp_field
,
673 bt_put(timestamp_field
);
674 bt_put(mapped_clock_class
);
678 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
679 struct bt_ctf_event
*event
)
683 if (!stream
|| !event
|| stream
->pos
.fd
< 0) {
689 * The event is not supposed to have a parent stream at this
690 * point. The only other way an event can have a parent stream
691 * is if it was assigned when setting a packet to the event,
692 * in which case the packet's stream is not a writer stream,
693 * and thus the user is trying to append an event which belongs
696 if (event
->base
.parent
) {
701 bt_object_set_parent(event
, stream
);
702 ret
= auto_populate_event_header(stream
, event
);
707 /* Make sure the various scopes of the event are set */
708 ret
= bt_ctf_event_validate(event
);
713 /* Save the new event and freeze it */
714 bt_ctf_event_freeze(event
);
715 g_ptr_array_add(stream
->events
, event
);
718 * Event had to hold a reference to its event class as long as it wasn't
719 * part of the same trace hierarchy. From now on, the event and its
720 * class share the same lifetime guarantees and the reference is no
723 bt_put(event
->event_class
);
730 * Orphan the event; we were not successful in associating it to
733 bt_object_set_parent(event
, NULL
);
738 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
739 struct bt_ctf_stream
*stream
)
741 struct bt_ctf_field
*packet_context
= NULL
;
743 if (!stream
|| stream
->pos
.fd
< 0) {
747 packet_context
= stream
->packet_context
;
748 if (packet_context
) {
749 bt_get(packet_context
);
752 return packet_context
;
755 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
756 struct bt_ctf_field
*field
)
759 struct bt_ctf_field_type
*field_type
;
761 if (!stream
|| stream
->pos
.fd
< 0) {
766 field_type
= bt_ctf_field_get_type(field
);
767 if (bt_ctf_field_type_compare(field_type
,
768 stream
->stream_class
->packet_context_type
)) {
774 bt_put(stream
->packet_context
);
775 stream
->packet_context
= bt_get(field
);
780 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
781 struct bt_ctf_stream
*stream
)
783 struct bt_ctf_field
*packet_header
= NULL
;
785 if (!stream
|| stream
->pos
.fd
< 0) {
789 packet_header
= stream
->packet_header
;
791 bt_get(packet_header
);
794 return packet_header
;
797 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
798 struct bt_ctf_field
*field
)
801 struct bt_ctf_trace
*trace
= NULL
;
802 struct bt_ctf_field_type
*field_type
= NULL
;
804 if (!stream
|| stream
->pos
.fd
< 0) {
809 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
810 field_type
= bt_ctf_field_get_type(field
);
811 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
816 bt_put(stream
->packet_header
);
817 stream
->packet_header
= bt_get(field
);
825 int get_event_header_timestamp(struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
828 struct bt_ctf_field
*timestamp_field
= NULL
;
829 struct bt_ctf_field_type
*timestamp_field_type
= NULL
;
831 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
833 if (!timestamp_field
) {
838 timestamp_field_type
= bt_ctf_field_get_type(timestamp_field
);
839 assert(timestamp_field_type
);
840 if (bt_ctf_field_type_get_type_id(timestamp_field_type
) !=
841 BT_CTF_TYPE_ID_INTEGER
) {
846 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type
)) {
849 ret
= bt_ctf_field_signed_integer_get_value(timestamp_field
,
854 *timestamp
= (uint64_t) val
;
856 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
863 bt_put(timestamp_field
);
864 bt_put(timestamp_field_type
);
868 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
872 uint64_t timestamp_begin
, timestamp_end
, events_discarded
;
873 struct bt_ctf_field
*integer
= NULL
;
874 struct ctf_stream_pos packet_context_pos
;
876 uint64_t packet_size_bits
;
878 if (!stream
|| stream
->pos
.fd
< 0) {
880 * Stream does not have an associated fd. It is,
881 * therefore, not a stream being used to write events.
887 if (!stream
->packet_context
&& stream
->flushed_packet_count
> 0) {
889 * A stream without a packet context, and thus without
890 * content and packet size members, can't have more than
897 empty_packet
= (stream
->events
->len
== 0);
899 /* mmap the next packet */
900 ctf_packet_seek(&stream
->pos
.parent
, 0, SEEK_CUR
);
902 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
);
907 if (stream
->packet_context
) {
908 /* Set the default context attributes if present and unset. */
909 if (!empty_packet
&& !get_event_header_timestamp(
910 ((struct bt_ctf_event
*) g_ptr_array_index(
911 stream
->events
, 0))->event_header
, ×tamp_begin
)) {
912 ret
= set_structure_field_integer(stream
->packet_context
,
913 "timestamp_begin", timestamp_begin
);
919 if (!empty_packet
&& !get_event_header_timestamp(
920 ((struct bt_ctf_event
*) g_ptr_array_index(
921 stream
->events
, stream
->events
->len
- 1))->event_header
,
924 ret
= set_structure_field_integer(stream
->packet_context
,
925 "timestamp_end", timestamp_end
);
930 ret
= set_structure_field_integer(stream
->packet_context
,
931 "content_size", UINT64_MAX
);
936 ret
= set_structure_field_integer(stream
->packet_context
,
937 "packet_size", UINT64_MAX
);
942 /* Write packet context */
943 memcpy(&packet_context_pos
, &stream
->pos
,
944 sizeof(struct ctf_stream_pos
));
945 ret
= bt_ctf_field_serialize(stream
->packet_context
,
951 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
957 /* Unset the packet context's fields. */
958 ret
= bt_ctf_field_reset(stream
->packet_context
);
963 /* Set the previous number of discarded events. */
964 ret
= set_structure_field_integer(stream
->packet_context
,
965 "events_discarded", events_discarded
);
971 for (i
= 0; i
< stream
->events
->len
; i
++) {
972 struct bt_ctf_event
*event
= g_ptr_array_index(
975 ret
= bt_ctf_field_reset(event
->event_header
);
980 /* Write event header */
981 ret
= bt_ctf_field_serialize(event
->event_header
,
987 /* Write stream event context */
988 if (event
->stream_event_context
) {
989 ret
= bt_ctf_field_serialize(
990 event
->stream_event_context
, &stream
->pos
);
996 /* Write event content */
997 ret
= bt_ctf_event_serialize(event
, &stream
->pos
);
1003 /* Rounded-up in case content_size is not byte-aligned. */
1004 packet_size_bits
= (stream
->pos
.offset
+ (CHAR_BIT
- 1)) &
1006 stream
->pos
.packet_size
= packet_size_bits
;
1008 if (stream
->packet_context
) {
1010 * Update the packet total size and content size and overwrite
1011 * the packet context.
1012 * Copy base_mma as the packet may have been remapped (e.g. when
1013 * a packet is resized).
1015 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1016 ret
= set_structure_field_integer(stream
->packet_context
,
1017 "content_size", stream
->pos
.offset
);
1022 ret
= set_structure_field_integer(stream
->packet_context
,
1023 "packet_size", packet_size_bits
);
1028 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1029 &packet_context_pos
);
1035 g_ptr_array_set_size(stream
->events
, 0);
1036 stream
->flushed_packet_count
++;
1037 stream
->size
+= packet_size_bits
/ CHAR_BIT
;
1043 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
1048 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
1054 void bt_ctf_stream_destroy(struct bt_object
*obj
)
1056 struct bt_ctf_stream
*stream
;
1058 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
1059 ctf_fini_pos(&stream
->pos
);
1060 if (stream
->pos
.fd
>= 0) {
1064 * Truncate the file's size to the minimum required to fit the
1065 * last packet as we might have grown it too much on the last
1069 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1070 } while (ret
== -1 && errno
== EINTR
);
1072 perror("ftruncate");
1075 if (close(stream
->pos
.fd
)) {
1080 if (stream
->events
) {
1081 g_ptr_array_free(stream
->events
, TRUE
);
1085 g_string_free(stream
->name
, TRUE
);
1088 bt_put(stream
->packet_header
);
1089 bt_put(stream
->packet_context
);
1094 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1098 struct bt_ctf_field_type
*field_type
= NULL
;
1099 struct bt_ctf_field
*integer
=
1100 bt_ctf_field_structure_get_field(structure
, name
);
1102 if (!structure
|| !name
) {
1108 /* Field not found, not an error. */
1112 /* Make sure the payload has not already been set. */
1113 if (bt_ctf_field_is_set(integer
)) {
1114 /* Payload already set, not an error */
1118 field_type
= bt_ctf_field_get_type(integer
);
1119 /* Something is serioulsly wrong */
1121 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_TYPE_ID_INTEGER
) {
1123 * The user most likely meant for us to populate this field
1124 * automatically. However, we can only do this if the field
1125 * is an integer. Return an error.
1131 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
1132 ret
= bt_ctf_field_signed_integer_set_value(integer
,
1135 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
1143 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1145 const char *name
= NULL
;
1151 name
= stream
->name
? stream
->name
->str
: NULL
;
1157 int bt_ctf_stream_is_writer(struct bt_ctf_stream
*stream
)
1165 ret
= (stream
->pos
.fd
>= 0);