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.h>
40 #include <babeltrace/ctf-ir/trace-internal.h>
41 #include <babeltrace/ctf-writer/writer-internal.h>
42 #include <babeltrace/graph/component-internal.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/ctf-writer/functor-internal.h>
45 #include <babeltrace/compiler-internal.h>
46 #include <babeltrace/align-internal.h>
50 void bt_ctf_stream_destroy(struct bt_object
*obj
);
52 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
54 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
57 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
60 struct bt_ctf_field_type
*field_type
= NULL
;
67 field_type
= bt_ctf_field_get_type(field
);
70 if (bt_ctf_field_type_get_type_id(field_type
) !=
71 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
72 /* Not an integer and the value is unset, error. */
77 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
78 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
80 /* Value is out of range, error. */
84 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
86 /* Value is out of range, error. */
96 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
99 struct bt_ctf_field_type
*magic_field_type
= NULL
;
100 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
101 stream
->packet_header
, "magic");
104 /* No magic field found. Not an error, skip. */
108 if (bt_ctf_field_is_set(magic_field
)) {
109 /* Value already set. Not an error, skip. */
113 magic_field_type
= bt_ctf_field_get_type(magic_field
);
114 assert(magic_field_type
);
116 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
117 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
118 /* Magic field is not an integer. Not an error, skip. */
122 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
124 * Magic field is not of the expected size.
125 * Not an error, skip.
130 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
133 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
134 (int64_t) 0xC1FC1FC1);
136 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
137 (uint64_t) 0xC1FC1FC1);
141 bt_put(magic_field_type
);
146 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
149 struct bt_ctf_trace
*trace
= NULL
;
150 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
151 struct bt_ctf_field_type
*element_field_type
= NULL
;
152 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
153 stream
->packet_header
, "uuid");
156 /* No uuid field found. Not an error, skip. */
160 if (bt_ctf_field_is_set(uuid_field
)) {
161 /* Value already set. Not an error, skip. */
165 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
166 assert(uuid_field_type
);
167 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
168 BT_CTF_FIELD_TYPE_ID_ARRAY
) {
169 /* UUID field is not an array. Not an error, skip. */
173 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
175 * UUID field is not of the expected size.
176 * Not an error, skip.
181 element_field_type
= bt_ctf_field_type_array_get_element_type(
183 assert(element_field_type
);
184 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
185 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
186 /* UUID array elements are not integers. Not an error, skip */
190 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
191 for (i
= 0; i
< 16; i
++) {
192 struct bt_ctf_field
*uuid_element
=
193 bt_ctf_field_array_get_field(uuid_field
, i
);
195 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
199 ret
= bt_ctf_field_signed_integer_set_value(
200 uuid_element
, (int64_t) trace
->uuid
[i
]);
202 ret
= bt_ctf_field_unsigned_integer_set_value(
203 uuid_element
, (uint64_t) trace
->uuid
[i
]);
205 bt_put(uuid_element
);
213 bt_put(uuid_field_type
);
214 bt_put(element_field_type
);
219 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
223 struct bt_ctf_field_type
*stream_id_field_type
= NULL
;
224 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
225 stream
->packet_header
, "stream_id");
227 if (!stream_id_field
) {
228 /* No stream_id field found. Not an error, skip. */
232 if (bt_ctf_field_is_set(stream_id_field
)) {
233 /* Value already set. Not an error, skip. */
237 stream_id_field_type
= bt_ctf_field_get_type(stream_id_field
);
238 assert(stream_id_field_type
);
239 if (bt_ctf_field_type_get_type_id(stream_id_field_type
) !=
240 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
241 /* stream_id field is not an integer. Not an error, skip. */
245 stream_id
= stream
->stream_class
->id
;
246 ret
= bt_ctf_field_type_integer_get_signed(stream_id_field_type
);
249 ret
= bt_ctf_field_signed_integer_set_value(stream_id_field
,
250 (int64_t) stream_id
);
252 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
253 (uint64_t) stream_id
);
256 bt_put(stream_id_field
);
257 bt_put(stream_id_field_type
);
262 int set_packet_header(struct bt_ctf_stream
*stream
)
266 ret
= set_packet_header_magic(stream
);
271 ret
= set_packet_header_uuid(stream
);
276 ret
= set_packet_header_stream_id(stream
);
285 void release_event(struct bt_ctf_event
*event
)
287 if (bt_object_get_ref_count(event
)) {
289 * The event is being orphaned, but it must guarantee the
290 * existence of its event class for the duration of its
293 bt_get(event
->event_class
);
294 BT_PUT(event
->base
.parent
);
296 bt_object_release(event
);
301 int create_stream_file(struct bt_ctf_writer
*writer
,
302 struct bt_ctf_stream
*stream
)
305 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
307 if (stream
->stream_class
->name
->len
== 0) {
310 ret
= bt_ctf_stream_class_get_id(stream
->stream_class
);
316 g_string_printf(filename
, "stream_%" PRId64
, ret
);
319 g_string_append_printf(filename
, "_%" PRIu32
, stream
->id
);
320 fd
= openat(writer
->trace_dir_fd
, filename
->str
,
321 O_RDWR
| O_CREAT
| O_TRUNC
,
322 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
324 g_string_free(filename
, TRUE
);
329 int set_stream_fd(struct bt_ctf_stream
*stream
, int fd
)
333 if (stream
->pos
.fd
!= -1) {
338 (void) bt_ctf_stream_pos_init(&stream
->pos
, fd
, O_RDWR
);
345 void component_destroy_listener(struct bt_component
*component
, void *data
)
347 struct bt_ctf_stream
*stream
= data
;
349 g_hash_table_remove(stream
->comp_cur_port
, component
);
352 struct bt_ctf_stream
*bt_ctf_stream_create(
353 struct bt_ctf_stream_class
*stream_class
,
357 struct bt_ctf_stream
*stream
= NULL
;
358 struct bt_ctf_trace
*trace
= NULL
;
359 struct bt_ctf_writer
*writer
= NULL
;
365 trace
= bt_ctf_stream_class_get_trace(stream_class
);
370 if (bt_ctf_trace_is_static(trace
)) {
372 * A static trace has the property that all its stream
373 * classes, clock classes, and streams are definitive:
374 * no more can be added, and each object is also frozen.
379 stream
= g_new0(struct bt_ctf_stream
, 1);
384 bt_object_init(stream
, bt_ctf_stream_destroy
);
386 * Acquire reference to parent since stream will become publicly
387 * reachable; it needs its parent to remain valid.
389 bt_object_set_parent(stream
, trace
);
390 stream
->id
= stream_class
->next_stream_id
++;
391 stream
->stream_class
= stream_class
;
394 stream
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
395 sizeof(struct bt_ctf_stream_destroy_listener
));
396 if (!stream
->destroy_listeners
) {
401 stream
->name
= g_string_new(name
);
407 if (trace
->is_created_by_writer
) {
409 writer
= (struct bt_ctf_writer
*)
410 bt_object_get_parent(trace
);
413 if (stream_class
->packet_context_type
) {
414 stream
->packet_context
= bt_ctf_field_create(
415 stream_class
->packet_context_type
);
416 if (!stream
->packet_context
) {
420 /* Initialize events_discarded */
421 ret
= try_set_structure_field_integer(
422 stream
->packet_context
,
423 "events_discarded", 0);
429 stream
->events
= g_ptr_array_new_with_free_func(
430 (GDestroyNotify
) release_event
);
431 if (!stream
->events
) {
435 /* A trace is not allowed to have a NULL packet header */
436 assert(trace
->packet_header_type
);
437 stream
->packet_header
=
438 bt_ctf_field_create(trace
->packet_header_type
);
439 if (!stream
->packet_header
) {
444 * Attempt to populate the default trace packet header fields
445 * (magic, uuid and stream_id). This will _not_ fail shall the
446 * fields not be found or be of an incompatible type; they will
447 * simply not be populated automatically. The user will have to
448 * make sure to set the trace packet header fields himself
451 ret
= set_packet_header(stream
);
456 /* Create file associated with this stream */
457 fd
= create_stream_file(writer
, stream
);
462 ret
= set_stream_fd(stream
, fd
);
467 /* Freeze the writer */
468 bt_ctf_writer_freeze(writer
);
470 /* Non-writer stream indicated by a negative FD */
471 ret
= set_stream_fd(stream
, -1);
476 stream
->comp_cur_port
= g_hash_table_new(g_direct_hash
,
478 if (!stream
->comp_cur_port
) {
483 /* Add this stream to the trace's streams */
484 g_ptr_array_add(trace
->streams
, stream
);
496 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
497 struct bt_ctf_stream
*stream
)
499 struct bt_ctf_stream_class
*stream_class
= NULL
;
505 stream_class
= stream
->stream_class
;
506 bt_get(stream_class
);
511 int bt_ctf_stream_get_discarded_events_count(
512 struct bt_ctf_stream
*stream
, uint64_t *count
)
516 struct bt_ctf_field
*events_discarded_field
= NULL
;
517 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
519 if (!stream
|| !count
|| !stream
->packet_context
||
520 stream
->pos
.fd
< 0) {
525 events_discarded_field
= bt_ctf_field_structure_get_field(
526 stream
->packet_context
, "events_discarded");
527 if (!events_discarded_field
) {
532 events_discarded_field_type
= bt_ctf_field_get_type(
533 events_discarded_field
);
534 if (!events_discarded_field_type
) {
539 field_signed
= bt_ctf_field_type_integer_get_signed(
540 events_discarded_field_type
);
541 if (field_signed
< 0) {
547 int64_t signed_count
;
549 ret
= bt_ctf_field_signed_integer_get_value(
550 events_discarded_field
, &signed_count
);
554 if (signed_count
< 0) {
559 *count
= (uint64_t) signed_count
;
561 ret
= bt_ctf_field_unsigned_integer_get_value(
562 events_discarded_field
, count
);
568 bt_put(events_discarded_field
);
569 bt_put(events_discarded_field_type
);
573 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
574 uint64_t event_count
)
578 uint64_t previous_count
;
580 struct bt_ctf_field
*events_discarded_field
= NULL
;
581 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
583 if (!stream
|| !stream
->packet_context
|| stream
->pos
.fd
< 0) {
587 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
593 events_discarded_field
= bt_ctf_field_structure_get_field(
594 stream
->packet_context
, "events_discarded");
595 if (!events_discarded_field
) {
599 events_discarded_field_type
= bt_ctf_field_get_type(
600 events_discarded_field
);
601 if (!events_discarded_field_type
) {
605 field_signed
= bt_ctf_field_type_integer_get_signed(
606 events_discarded_field_type
);
607 if (field_signed
< 0) {
611 new_count
= previous_count
+ event_count
;
612 assert(new_count
>= previous_count
);
614 ret
= bt_ctf_field_signed_integer_set_value(
615 events_discarded_field
, (int64_t) new_count
);
620 ret
= bt_ctf_field_unsigned_integer_set_value(
621 events_discarded_field
, new_count
);
628 bt_put(events_discarded_field
);
629 bt_put(events_discarded_field_type
);
632 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
633 struct bt_ctf_event
*event
)
636 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
637 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
639 if (!event
|| event
->frozen
) {
645 * The condition to automatically set the ID are:
647 * 1. The event header field "id" exists and is an integer
649 * 2. The event header field "id" is NOT set.
651 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
652 if (id_field
&& !bt_ctf_field_is_set(id_field
)) {
653 ret
= set_integer_field_value(id_field
,
654 (uint64_t) bt_ctf_event_class_get_id(
655 event
->event_class
));
662 * The conditions to automatically set the timestamp are:
664 * 1. The event header field "timestamp" exists and is an
666 * 2. This stream's class has a registered clock (set with
667 * bt_ctf_stream_class_set_clock()).
668 * 3. The event header field "timestamp" has its type mapped to
669 * a clock class which is also the clock class of this
670 * stream's class's registered clock.
671 * 4. The event header field "timestamp" is NOT set.
673 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
675 if (timestamp_field
&& !bt_ctf_field_is_set(timestamp_field
) &&
676 stream
->stream_class
->clock
) {
677 struct bt_ctf_clock_class
*stream_class_clock_class
=
678 stream
->stream_class
->clock
->clock_class
;
679 struct bt_ctf_field_type
*timestamp_field_type
=
680 bt_ctf_field_get_type(timestamp_field
);
682 assert(timestamp_field_type
);
684 bt_ctf_field_type_integer_get_mapped_clock_class(
685 timestamp_field_type
);
686 BT_PUT(timestamp_field_type
);
687 if (mapped_clock_class
== stream_class_clock_class
) {
690 ret
= bt_ctf_clock_get_value(
691 stream
->stream_class
->clock
,
697 ret
= set_integer_field_value(timestamp_field
,
707 bt_put(timestamp_field
);
708 bt_put(mapped_clock_class
);
712 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
713 struct bt_ctf_event
*event
)
717 if (!stream
|| !event
|| stream
->pos
.fd
< 0) {
723 * The event is not supposed to have a parent stream at this
724 * point. The only other way an event can have a parent stream
725 * is if it was assigned when setting a packet to the event,
726 * in which case the packet's stream is not a writer stream,
727 * and thus the user is trying to append an event which belongs
730 if (event
->base
.parent
) {
735 bt_object_set_parent(event
, stream
);
736 ret
= auto_populate_event_header(stream
, event
);
741 /* Make sure the various scopes of the event are set */
742 ret
= bt_ctf_event_validate(event
);
747 /* Save the new event and freeze it */
748 bt_ctf_event_freeze(event
);
749 g_ptr_array_add(stream
->events
, event
);
752 * Event had to hold a reference to its event class as long as it wasn't
753 * part of the same trace hierarchy. From now on, the event and its
754 * class share the same lifetime guarantees and the reference is no
757 bt_put(event
->event_class
);
764 * Orphan the event; we were not successful in associating it to
767 bt_object_set_parent(event
, NULL
);
772 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
773 struct bt_ctf_stream
*stream
)
775 struct bt_ctf_field
*packet_context
= NULL
;
777 if (!stream
|| stream
->pos
.fd
< 0) {
781 packet_context
= stream
->packet_context
;
782 if (packet_context
) {
783 bt_get(packet_context
);
786 return packet_context
;
789 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
790 struct bt_ctf_field
*field
)
793 struct bt_ctf_field_type
*field_type
;
795 if (!stream
|| stream
->pos
.fd
< 0) {
800 field_type
= bt_ctf_field_get_type(field
);
801 if (bt_ctf_field_type_compare(field_type
,
802 stream
->stream_class
->packet_context_type
)) {
808 bt_put(stream
->packet_context
);
809 stream
->packet_context
= bt_get(field
);
814 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
815 struct bt_ctf_stream
*stream
)
817 struct bt_ctf_field
*packet_header
= NULL
;
819 if (!stream
|| stream
->pos
.fd
< 0) {
823 packet_header
= stream
->packet_header
;
825 bt_get(packet_header
);
828 return packet_header
;
831 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
832 struct bt_ctf_field
*field
)
835 struct bt_ctf_trace
*trace
= NULL
;
836 struct bt_ctf_field_type
*field_type
= NULL
;
838 if (!stream
|| stream
->pos
.fd
< 0) {
843 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
844 field_type
= bt_ctf_field_get_type(field
);
845 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
850 bt_put(stream
->packet_header
);
851 stream
->packet_header
= bt_get(field
);
859 int get_event_header_timestamp(struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
862 struct bt_ctf_field
*timestamp_field
= NULL
;
863 struct bt_ctf_field_type
*timestamp_field_type
= NULL
;
865 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
867 if (!timestamp_field
) {
872 timestamp_field_type
= bt_ctf_field_get_type(timestamp_field
);
873 assert(timestamp_field_type
);
874 if (bt_ctf_field_type_get_type_id(timestamp_field_type
) !=
875 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
880 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type
)) {
883 ret
= bt_ctf_field_signed_integer_get_value(timestamp_field
,
888 *timestamp
= (uint64_t) val
;
890 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
897 bt_put(timestamp_field
);
898 bt_put(timestamp_field_type
);
903 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
905 struct bt_ctf_field
*member
;
907 member
= bt_ctf_field_structure_get_field(structure
, name
);
909 (void) bt_ctf_field_reset(member
);
913 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
917 uint64_t timestamp_begin
, timestamp_end
;
918 struct bt_ctf_field
*integer
= NULL
;
919 struct bt_ctf_stream_pos packet_context_pos
;
920 struct bt_ctf_trace
*trace
;
921 enum bt_ctf_byte_order native_byte_order
;
923 uint64_t packet_size_bits
;
925 bool timestamp_begin
;
929 } auto_set_fields
= { 0 };
931 if (!stream
|| stream
->pos
.fd
< 0) {
933 * Stream does not have an associated fd. It is,
934 * therefore, not a stream being used to write events.
940 if (!stream
->packet_context
&& stream
->flushed_packet_count
> 0) {
942 * A stream without a packet context, and thus without
943 * content and packet size members, can't have more than
950 trace
= bt_ctf_stream_class_borrow_trace(stream
->stream_class
);
952 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
953 empty_packet
= (stream
->events
->len
== 0);
955 /* mmap the next packet */
956 bt_ctf_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
957 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
,
963 if (stream
->packet_context
) {
964 /* Set the default context attributes if present and unset. */
965 if (!empty_packet
&& !get_event_header_timestamp(
966 ((struct bt_ctf_event
*) g_ptr_array_index(
967 stream
->events
, 0))->event_header
, ×tamp_begin
)) {
968 ret
= try_set_structure_field_integer(
969 stream
->packet_context
,
970 "timestamp_begin", timestamp_begin
);
974 auto_set_fields
.timestamp_begin
= ret
== 1;
977 if (!empty_packet
&& !get_event_header_timestamp(
978 ((struct bt_ctf_event
*) g_ptr_array_index(
979 stream
->events
, stream
->events
->len
- 1))->event_header
,
982 ret
= try_set_structure_field_integer(
983 stream
->packet_context
,
984 "timestamp_end", timestamp_end
);
988 auto_set_fields
.timestamp_end
= ret
== 1;
990 ret
= try_set_structure_field_integer(stream
->packet_context
,
991 "content_size", UINT64_MAX
);
995 auto_set_fields
.content_size
= ret
== 1;
997 ret
= try_set_structure_field_integer(stream
->packet_context
,
998 "packet_size", UINT64_MAX
);
1002 auto_set_fields
.packet_size
= ret
== 1;
1004 /* Write packet context */
1005 memcpy(&packet_context_pos
, &stream
->pos
,
1006 sizeof(packet_context_pos
));
1007 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1008 &stream
->pos
, native_byte_order
);
1014 for (i
= 0; i
< stream
->events
->len
; i
++) {
1015 struct bt_ctf_event
*event
= g_ptr_array_index(
1018 /* Write event header */
1019 ret
= bt_ctf_field_serialize(event
->event_header
,
1020 &stream
->pos
, native_byte_order
);
1025 /* Write stream event context */
1026 if (event
->stream_event_context
) {
1027 ret
= bt_ctf_field_serialize(
1028 event
->stream_event_context
, &stream
->pos
,
1035 /* Write event content */
1036 ret
= bt_ctf_event_serialize(event
, &stream
->pos
,
1043 /* Rounded-up in case content_size is not byte-aligned. */
1044 packet_size_bits
= (stream
->pos
.offset
+ (CHAR_BIT
- 1)) &
1046 stream
->pos
.packet_size
= packet_size_bits
;
1048 if (stream
->packet_context
) {
1050 * Update the packet total size and content size and overwrite
1051 * the packet context.
1052 * Copy base_mma as the packet may have been remapped (e.g. when
1053 * a packet is resized).
1055 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1056 if (auto_set_fields
.content_size
) {
1057 ret
= set_structure_field_integer(
1058 stream
->packet_context
,
1059 "content_size", stream
->pos
.offset
);
1065 if (auto_set_fields
.packet_size
) {
1066 ret
= set_structure_field_integer(stream
->packet_context
,
1067 "packet_size", packet_size_bits
);
1073 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1074 &packet_context_pos
, native_byte_order
);
1080 g_ptr_array_set_size(stream
->events
, 0);
1081 stream
->flushed_packet_count
++;
1082 stream
->size
+= packet_size_bits
/ CHAR_BIT
;
1084 /* Reset automatically-set fields. */
1085 if (auto_set_fields
.timestamp_begin
) {
1086 reset_structure_field(stream
->packet_context
,
1089 if (auto_set_fields
.timestamp_end
) {
1090 reset_structure_field(stream
->packet_context
,
1093 if (auto_set_fields
.packet_size
) {
1094 reset_structure_field(stream
->packet_context
,
1097 if (auto_set_fields
.content_size
) {
1098 reset_structure_field(stream
->packet_context
,
1105 * We failed to write the packet. Its size is therefore set to 0
1106 * to ensure the next mapping is done in the same place rather
1107 * than advancing by "stream->pos.packet_size", which would
1108 * leave a corrupted packet in the trace.
1110 stream
->pos
.packet_size
= 0;
1115 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
1120 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
1126 void bt_ctf_stream_destroy(struct bt_object
*obj
)
1128 struct bt_ctf_stream
*stream
;
1131 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
1133 /* Call destroy listeners in reverse registration order */
1134 for (i
= stream
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
1135 struct bt_ctf_stream_destroy_listener
*listener
=
1136 &g_array_index(stream
->destroy_listeners
,
1137 struct bt_ctf_stream_destroy_listener
, i
);
1139 listener
->func(stream
, listener
->data
);
1142 (void) bt_ctf_stream_pos_fini(&stream
->pos
);
1143 if (stream
->pos
.fd
>= 0) {
1147 * Truncate the file's size to the minimum required to fit the
1148 * last packet as we might have grown it too much on the last
1152 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1153 } while (ret
== -1 && errno
== EINTR
);
1155 perror("ftruncate");
1158 if (close(stream
->pos
.fd
)) {
1163 if (stream
->events
) {
1164 g_ptr_array_free(stream
->events
, TRUE
);
1168 g_string_free(stream
->name
, TRUE
);
1171 if (stream
->comp_cur_port
) {
1172 GHashTableIter ht_iter
;
1173 gpointer comp_gptr
, port_gptr
;
1176 * Since we're destroying the stream, remove the destroy
1177 * listeners that it registered for each component in
1178 * its component-port mapping hash table. Otherwise they
1179 * would be called and the stream would be accessed once
1180 * it's freed or another stream would be accessed.
1182 g_hash_table_iter_init(&ht_iter
, stream
->comp_cur_port
);
1184 while (g_hash_table_iter_next(&ht_iter
, &comp_gptr
, &port_gptr
)) {
1186 bt_component_remove_destroy_listener((void *) comp_gptr
,
1187 component_destroy_listener
, stream
);
1190 g_hash_table_destroy(stream
->comp_cur_port
);
1193 if (stream
->destroy_listeners
) {
1194 g_array_free(stream
->destroy_listeners
, TRUE
);
1197 bt_put(stream
->packet_header
);
1198 bt_put(stream
->packet_context
);
1203 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1204 uint64_t value
, bool force
)
1207 struct bt_ctf_field_type
*field_type
= NULL
;
1208 struct bt_ctf_field
*integer
=
1209 bt_ctf_field_structure_get_field(structure
, name
);
1211 if (!structure
|| !name
) {
1217 /* Field not found, not an error. */
1221 /* Make sure the payload has not already been set. */
1222 if (!force
&& bt_ctf_field_is_set(integer
)) {
1223 /* Payload already set, not an error */
1227 field_type
= bt_ctf_field_get_type(integer
);
1229 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1231 * The user most likely meant for us to populate this field
1232 * automatically. However, we can only do this if the field
1233 * is an integer. Return an error.
1239 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
1240 ret
= bt_ctf_field_signed_integer_set_value(integer
,
1243 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
1245 ret
= !ret
? 1 : ret
;
1253 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1256 return _set_structure_field_integer(structure
, name
, value
, true);
1260 * Returns the following codes:
1261 * 1 if the field was found and set,
1262 * 0 if nothing was done (field not found, or was already set),
1263 * <0 if an error was encoutered
1266 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1269 return _set_structure_field_integer(structure
, name
, value
, false);
1272 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1274 const char *name
= NULL
;
1280 name
= stream
->name
? stream
->name
->str
: NULL
;
1286 int bt_ctf_stream_is_writer(struct bt_ctf_stream
*stream
)
1294 ret
= (stream
->pos
.fd
>= 0);
1301 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream
*stream
,
1302 struct bt_component
*comp
,
1303 struct bt_port
*port
)
1308 assert(stream
->comp_cur_port
);
1311 * Do not take a reference to the component here because we
1312 * don't want the component to exist as long as this stream
1313 * exists. Instead, keep a weak reference, but add a destroy
1314 * listener so that we remove this hash table entry when we know
1315 * the component is destroyed.
1317 bt_component_add_destroy_listener(comp
, component_destroy_listener
,
1319 g_hash_table_insert(stream
->comp_cur_port
, comp
, port
);
1323 struct bt_port
*bt_ctf_stream_port_for_component(struct bt_ctf_stream
*stream
,
1324 struct bt_component
*comp
)
1328 assert(stream
->comp_cur_port
);
1329 return g_hash_table_lookup(stream
->comp_cur_port
, comp
);
1333 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream
*stream
,
1334 bt_ctf_stream_destroy_listener_func func
, void *data
)
1336 struct bt_ctf_stream_destroy_listener listener
;
1340 listener
.func
= func
;
1341 listener
.data
= data
;
1342 g_array_append_val(stream
->destroy_listeners
, listener
);
1346 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream
*stream
,
1347 bt_ctf_stream_destroy_listener_func func
, void *data
)
1354 for (i
= 0; i
< stream
->destroy_listeners
->len
; i
++) {
1355 struct bt_ctf_stream_destroy_listener
*listener
=
1356 &g_array_index(stream
->destroy_listeners
,
1357 struct bt_ctf_stream_destroy_listener
, i
);
1359 if (listener
->func
== func
&& listener
->data
== data
) {
1360 g_array_remove_index(stream
->destroy_listeners
, i
);