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/graph/component-internal.h>
42 #include <babeltrace/ref.h>
43 #include <babeltrace/ctf-writer/functor-internal.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/align-internal.h>
49 void bt_ctf_stream_destroy(struct bt_object
*obj
);
51 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
53 int set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
56 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
59 struct bt_ctf_field_type
*field_type
= NULL
;
66 field_type
= bt_ctf_field_get_type(field
);
69 if (bt_ctf_field_type_get_type_id(field_type
) !=
70 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
71 /* Not an integer and the value is unset, error. */
76 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
77 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
79 /* Value is out of range, error. */
83 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
85 /* Value is out of range, error. */
95 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
98 struct bt_ctf_field_type
*magic_field_type
= NULL
;
99 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
100 stream
->packet_header
, "magic");
103 /* No magic field found. Not an error, skip. */
107 if (bt_ctf_field_is_set(magic_field
)) {
108 /* Value already set. Not an error, skip. */
112 magic_field_type
= bt_ctf_field_get_type(magic_field
);
113 assert(magic_field_type
);
115 if (bt_ctf_field_type_get_type_id(magic_field_type
) !=
116 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
117 /* Magic field is not an integer. Not an error, skip. */
121 if (bt_ctf_field_type_integer_get_size(magic_field_type
) != 32) {
123 * Magic field is not of the expected size.
124 * Not an error, skip.
129 ret
= bt_ctf_field_type_integer_get_signed(magic_field_type
);
132 ret
= bt_ctf_field_signed_integer_set_value(magic_field
,
133 (int64_t) 0xC1FC1FC1);
135 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
136 (uint64_t) 0xC1FC1FC1);
140 bt_put(magic_field_type
);
145 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
148 struct bt_ctf_trace
*trace
= NULL
;
149 struct bt_ctf_field_type
*uuid_field_type
= NULL
;
150 struct bt_ctf_field_type
*element_field_type
= NULL
;
151 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
152 stream
->packet_header
, "uuid");
155 /* No uuid field found. Not an error, skip. */
159 if (bt_ctf_field_is_set(uuid_field
)) {
160 /* Value already set. Not an error, skip. */
164 uuid_field_type
= bt_ctf_field_get_type(uuid_field
);
165 assert(uuid_field_type
);
166 if (bt_ctf_field_type_get_type_id(uuid_field_type
) !=
167 BT_CTF_FIELD_TYPE_ID_ARRAY
) {
168 /* UUID field is not an array. Not an error, skip. */
172 if (bt_ctf_field_type_array_get_length(uuid_field_type
) != 16) {
174 * UUID field is not of the expected size.
175 * Not an error, skip.
180 element_field_type
= bt_ctf_field_type_array_get_element_type(
182 assert(element_field_type
);
183 if (bt_ctf_field_type_get_type_id(element_field_type
) !=
184 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
185 /* UUID array elements are not integers. Not an error, skip */
189 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
190 for (i
= 0; i
< 16; i
++) {
191 struct bt_ctf_field
*uuid_element
=
192 bt_ctf_field_array_get_field(uuid_field
, i
);
194 ret
= bt_ctf_field_type_integer_get_signed(element_field_type
);
198 ret
= bt_ctf_field_signed_integer_set_value(
199 uuid_element
, (int64_t) trace
->uuid
[i
]);
201 ret
= bt_ctf_field_unsigned_integer_set_value(
203 (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 stream
= g_new0(struct bt_ctf_stream
, 1);
375 bt_object_init(stream
, bt_ctf_stream_destroy
);
377 * Acquire reference to parent since stream will become publicly
378 * reachable; it needs its parent to remain valid.
380 bt_object_set_parent(stream
, trace
);
381 stream
->id
= stream_class
->next_stream_id
++;
382 stream
->stream_class
= stream_class
;
385 stream
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
386 sizeof(struct bt_ctf_stream_destroy_listener
));
387 if (!stream
->destroy_listeners
) {
392 stream
->name
= g_string_new(name
);
398 if (trace
->is_created_by_writer
) {
400 writer
= (struct bt_ctf_writer
*)
401 bt_object_get_parent(trace
);
404 if (stream_class
->packet_context_type
) {
405 stream
->packet_context
= bt_ctf_field_create(
406 stream_class
->packet_context_type
);
407 if (!stream
->packet_context
) {
411 /* Initialize events_discarded */
412 ret
= try_set_structure_field_integer(
413 stream
->packet_context
,
414 "events_discarded", 0);
420 stream
->events
= g_ptr_array_new_with_free_func(
421 (GDestroyNotify
) release_event
);
422 if (!stream
->events
) {
426 /* A trace is not allowed to have a NULL packet header */
427 assert(trace
->packet_header_type
);
428 stream
->packet_header
=
429 bt_ctf_field_create(trace
->packet_header_type
);
430 if (!stream
->packet_header
) {
435 * Attempt to populate the default trace packet header fields
436 * (magic, uuid and stream_id). This will _not_ fail shall the
437 * fields not be found or be of an incompatible type; they will
438 * simply not be populated automatically. The user will have to
439 * make sure to set the trace packet header fields himself
442 ret
= set_packet_header(stream
);
447 /* Create file associated with this stream */
448 fd
= create_stream_file(writer
, stream
);
453 ret
= set_stream_fd(stream
, fd
);
458 /* Freeze the writer */
459 bt_ctf_writer_freeze(writer
);
461 /* Non-writer stream indicated by a negative FD */
462 ret
= set_stream_fd(stream
, -1);
467 stream
->comp_cur_port
= g_hash_table_new(g_direct_hash
,
469 if (!stream
->comp_cur_port
) {
474 /* Add this stream to the trace's streams */
475 g_ptr_array_add(trace
->streams
, stream
);
487 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
488 struct bt_ctf_stream
*stream
)
490 struct bt_ctf_stream_class
*stream_class
= NULL
;
496 stream_class
= stream
->stream_class
;
497 bt_get(stream_class
);
502 int bt_ctf_stream_get_discarded_events_count(
503 struct bt_ctf_stream
*stream
, uint64_t *count
)
507 struct bt_ctf_field
*events_discarded_field
= NULL
;
508 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
510 if (!stream
|| !count
|| !stream
->packet_context
||
511 stream
->pos
.fd
< 0) {
516 events_discarded_field
= bt_ctf_field_structure_get_field(
517 stream
->packet_context
, "events_discarded");
518 if (!events_discarded_field
) {
523 events_discarded_field_type
= bt_ctf_field_get_type(
524 events_discarded_field
);
525 if (!events_discarded_field_type
) {
530 field_signed
= bt_ctf_field_type_integer_get_signed(
531 events_discarded_field_type
);
532 if (field_signed
< 0) {
538 int64_t signed_count
;
540 ret
= bt_ctf_field_signed_integer_get_value(
541 events_discarded_field
, &signed_count
);
545 if (signed_count
< 0) {
550 *count
= (uint64_t) signed_count
;
552 ret
= bt_ctf_field_unsigned_integer_get_value(
553 events_discarded_field
, count
);
559 bt_put(events_discarded_field
);
560 bt_put(events_discarded_field_type
);
564 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
565 uint64_t event_count
)
569 uint64_t previous_count
;
571 struct bt_ctf_field
*events_discarded_field
= NULL
;
572 struct bt_ctf_field_type
*events_discarded_field_type
= NULL
;
574 if (!stream
|| !stream
->packet_context
|| stream
->pos
.fd
< 0) {
578 ret
= bt_ctf_stream_get_discarded_events_count(stream
,
584 events_discarded_field
= bt_ctf_field_structure_get_field(
585 stream
->packet_context
, "events_discarded");
586 if (!events_discarded_field
) {
590 events_discarded_field_type
= bt_ctf_field_get_type(
591 events_discarded_field
);
592 if (!events_discarded_field_type
) {
596 field_signed
= bt_ctf_field_type_integer_get_signed(
597 events_discarded_field_type
);
598 if (field_signed
< 0) {
602 new_count
= previous_count
+ event_count
;
604 ret
= bt_ctf_field_signed_integer_set_value(
605 events_discarded_field
, (int64_t) new_count
);
610 ret
= bt_ctf_field_unsigned_integer_set_value(
611 events_discarded_field
, new_count
);
618 bt_put(events_discarded_field
);
619 bt_put(events_discarded_field_type
);
622 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
623 struct bt_ctf_event
*event
)
626 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
627 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
629 if (!event
|| event
->frozen
) {
635 * The condition to automatically set the ID are:
637 * 1. The event header field "id" exists and is an integer
639 * 2. The event header field "id" is NOT set.
641 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
642 if (id_field
&& !bt_ctf_field_is_set(id_field
)) {
643 ret
= set_integer_field_value(id_field
,
644 (uint64_t) bt_ctf_event_class_get_id(
645 event
->event_class
));
652 * The conditions to automatically set the timestamp are:
654 * 1. The event header field "timestamp" exists and is an
656 * 2. This stream's class has a registered clock (set with
657 * bt_ctf_stream_class_set_clock()).
658 * 3. The event header field "timestamp" has its type mapped to
659 * a clock class which is also the clock class of this
660 * stream's class's registered clock.
661 * 4. The event header field "timestamp" is NOT set.
663 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
665 if (timestamp_field
&& !bt_ctf_field_is_set(timestamp_field
) &&
666 stream
->stream_class
->clock
) {
667 struct bt_ctf_clock_class
*stream_class_clock_class
=
668 stream
->stream_class
->clock
->clock_class
;
669 struct bt_ctf_field_type
*timestamp_field_type
=
670 bt_ctf_field_get_type(timestamp_field
);
672 assert(timestamp_field_type
);
674 bt_ctf_field_type_integer_get_mapped_clock_class(
675 timestamp_field_type
);
676 BT_PUT(timestamp_field_type
);
677 if (mapped_clock_class
== stream_class_clock_class
) {
680 ret
= bt_ctf_clock_get_value(
681 stream
->stream_class
->clock
,
687 ret
= set_integer_field_value(timestamp_field
,
697 bt_put(timestamp_field
);
698 bt_put(mapped_clock_class
);
702 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
703 struct bt_ctf_event
*event
)
707 if (!stream
|| !event
|| stream
->pos
.fd
< 0) {
713 * The event is not supposed to have a parent stream at this
714 * point. The only other way an event can have a parent stream
715 * is if it was assigned when setting a packet to the event,
716 * in which case the packet's stream is not a writer stream,
717 * and thus the user is trying to append an event which belongs
720 if (event
->base
.parent
) {
725 bt_object_set_parent(event
, stream
);
726 ret
= auto_populate_event_header(stream
, event
);
731 /* Make sure the various scopes of the event are set */
732 ret
= bt_ctf_event_validate(event
);
737 /* Save the new event and freeze it */
738 bt_ctf_event_freeze(event
);
739 g_ptr_array_add(stream
->events
, event
);
742 * Event had to hold a reference to its event class as long as it wasn't
743 * part of the same trace hierarchy. From now on, the event and its
744 * class share the same lifetime guarantees and the reference is no
747 bt_put(event
->event_class
);
754 * Orphan the event; we were not successful in associating it to
757 bt_object_set_parent(event
, NULL
);
762 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
763 struct bt_ctf_stream
*stream
)
765 struct bt_ctf_field
*packet_context
= NULL
;
767 if (!stream
|| stream
->pos
.fd
< 0) {
771 packet_context
= stream
->packet_context
;
772 if (packet_context
) {
773 bt_get(packet_context
);
776 return packet_context
;
779 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
780 struct bt_ctf_field
*field
)
783 struct bt_ctf_field_type
*field_type
;
785 if (!stream
|| stream
->pos
.fd
< 0) {
790 field_type
= bt_ctf_field_get_type(field
);
791 if (bt_ctf_field_type_compare(field_type
,
792 stream
->stream_class
->packet_context_type
)) {
798 bt_put(stream
->packet_context
);
799 stream
->packet_context
= bt_get(field
);
804 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
805 struct bt_ctf_stream
*stream
)
807 struct bt_ctf_field
*packet_header
= NULL
;
809 if (!stream
|| stream
->pos
.fd
< 0) {
813 packet_header
= stream
->packet_header
;
815 bt_get(packet_header
);
818 return packet_header
;
821 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
822 struct bt_ctf_field
*field
)
825 struct bt_ctf_trace
*trace
= NULL
;
826 struct bt_ctf_field_type
*field_type
= NULL
;
828 if (!stream
|| stream
->pos
.fd
< 0) {
833 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
834 field_type
= bt_ctf_field_get_type(field
);
835 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
840 bt_put(stream
->packet_header
);
841 stream
->packet_header
= bt_get(field
);
849 int get_event_header_timestamp(struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
852 struct bt_ctf_field
*timestamp_field
= NULL
;
853 struct bt_ctf_field_type
*timestamp_field_type
= NULL
;
855 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
857 if (!timestamp_field
) {
862 timestamp_field_type
= bt_ctf_field_get_type(timestamp_field
);
863 assert(timestamp_field_type
);
864 if (bt_ctf_field_type_get_type_id(timestamp_field_type
) !=
865 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
870 if (bt_ctf_field_type_integer_get_signed(timestamp_field_type
)) {
873 ret
= bt_ctf_field_signed_integer_get_value(timestamp_field
,
878 *timestamp
= (uint64_t) val
;
880 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
887 bt_put(timestamp_field
);
888 bt_put(timestamp_field_type
);
893 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
895 struct bt_ctf_field
*member
;
897 member
= bt_ctf_field_structure_get_field(structure
, name
);
899 (void) bt_ctf_field_reset(member
);
903 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
907 uint64_t timestamp_begin
, timestamp_end
;
908 struct bt_ctf_field
*integer
= NULL
;
909 struct bt_ctf_stream_pos packet_context_pos
;
910 struct bt_ctf_trace
*trace
;
911 enum bt_ctf_byte_order native_byte_order
;
913 uint64_t packet_size_bits
;
915 bool timestamp_begin
;
919 } auto_set_fields
= { 0 };
921 if (!stream
|| stream
->pos
.fd
< 0) {
923 * Stream does not have an associated fd. It is,
924 * therefore, not a stream being used to write events.
930 if (!stream
->packet_context
&& stream
->flushed_packet_count
> 0) {
932 * A stream without a packet context, and thus without
933 * content and packet size members, can't have more than
940 trace
= bt_ctf_stream_class_borrow_trace(stream
->stream_class
);
942 native_byte_order
= bt_ctf_trace_get_byte_order(trace
);
943 empty_packet
= (stream
->events
->len
== 0);
945 /* mmap the next packet */
946 bt_ctf_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
947 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
,
953 if (stream
->packet_context
) {
954 /* Set the default context attributes if present and unset. */
955 if (!empty_packet
&& !get_event_header_timestamp(
956 ((struct bt_ctf_event
*) g_ptr_array_index(
957 stream
->events
, 0))->event_header
, ×tamp_begin
)) {
958 ret
= try_set_structure_field_integer(
959 stream
->packet_context
,
960 "timestamp_begin", timestamp_begin
);
964 auto_set_fields
.timestamp_begin
= ret
== 1;
967 if (!empty_packet
&& !get_event_header_timestamp(
968 ((struct bt_ctf_event
*) g_ptr_array_index(
969 stream
->events
, stream
->events
->len
- 1))->event_header
,
972 ret
= try_set_structure_field_integer(
973 stream
->packet_context
,
974 "timestamp_end", timestamp_end
);
978 auto_set_fields
.timestamp_end
= ret
== 1;
980 ret
= try_set_structure_field_integer(stream
->packet_context
,
981 "content_size", UINT64_MAX
);
985 auto_set_fields
.content_size
= ret
== 1;
987 ret
= try_set_structure_field_integer(stream
->packet_context
,
988 "packet_size", UINT64_MAX
);
992 auto_set_fields
.packet_size
= ret
== 1;
994 /* Write packet context */
995 memcpy(&packet_context_pos
, &stream
->pos
,
996 sizeof(packet_context_pos
));
997 ret
= bt_ctf_field_serialize(stream
->packet_context
,
998 &stream
->pos
, native_byte_order
);
1004 for (i
= 0; i
< stream
->events
->len
; i
++) {
1005 struct bt_ctf_event
*event
= g_ptr_array_index(
1008 /* Write event header */
1009 ret
= bt_ctf_field_serialize(event
->event_header
,
1010 &stream
->pos
, native_byte_order
);
1015 /* Write stream event context */
1016 if (event
->stream_event_context
) {
1017 ret
= bt_ctf_field_serialize(
1018 event
->stream_event_context
, &stream
->pos
,
1025 /* Write event content */
1026 ret
= bt_ctf_event_serialize(event
, &stream
->pos
,
1033 /* Rounded-up in case content_size is not byte-aligned. */
1034 packet_size_bits
= (stream
->pos
.offset
+ (CHAR_BIT
- 1)) &
1036 stream
->pos
.packet_size
= packet_size_bits
;
1038 if (stream
->packet_context
) {
1040 * Update the packet total size and content size and overwrite
1041 * the packet context.
1042 * Copy base_mma as the packet may have been remapped (e.g. when
1043 * a packet is resized).
1045 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1046 if (auto_set_fields
.content_size
) {
1047 ret
= set_structure_field_integer(
1048 stream
->packet_context
,
1049 "content_size", stream
->pos
.offset
);
1055 if (auto_set_fields
.packet_size
) {
1056 ret
= set_structure_field_integer(stream
->packet_context
,
1057 "packet_size", packet_size_bits
);
1063 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1064 &packet_context_pos
, native_byte_order
);
1070 g_ptr_array_set_size(stream
->events
, 0);
1071 stream
->flushed_packet_count
++;
1072 stream
->size
+= packet_size_bits
/ CHAR_BIT
;
1074 /* Reset automatically-set fields. */
1075 if (auto_set_fields
.timestamp_begin
) {
1076 reset_structure_field(stream
->packet_context
,
1079 if (auto_set_fields
.timestamp_end
) {
1080 reset_structure_field(stream
->packet_context
,
1083 if (auto_set_fields
.packet_size
) {
1084 reset_structure_field(stream
->packet_context
,
1087 if (auto_set_fields
.content_size
) {
1088 reset_structure_field(stream
->packet_context
,
1095 * We failed to write the packet. Its size is therefore set to 0
1096 * to ensure the next mapping is done in the same place rather
1097 * than advancing by "stream->pos.packet_size", which would
1098 * leave a corrupted packet in the trace.
1100 stream
->pos
.packet_size
= 0;
1105 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
1110 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
1116 void bt_ctf_stream_destroy(struct bt_object
*obj
)
1118 struct bt_ctf_stream
*stream
;
1121 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
1123 /* Call destroy listeners in reverse registration order */
1124 for (i
= stream
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
1125 struct bt_ctf_stream_destroy_listener
*listener
=
1126 &g_array_index(stream
->destroy_listeners
,
1127 struct bt_ctf_stream_destroy_listener
, i
);
1129 listener
->func(stream
, listener
->data
);
1132 (void) bt_ctf_stream_pos_fini(&stream
->pos
);
1133 if (stream
->pos
.fd
>= 0) {
1137 * Truncate the file's size to the minimum required to fit the
1138 * last packet as we might have grown it too much on the last
1142 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1143 } while (ret
== -1 && errno
== EINTR
);
1145 perror("ftruncate");
1148 if (close(stream
->pos
.fd
)) {
1153 if (stream
->events
) {
1154 g_ptr_array_free(stream
->events
, TRUE
);
1158 g_string_free(stream
->name
, TRUE
);
1161 if (stream
->comp_cur_port
) {
1162 GHashTableIter ht_iter
;
1163 gpointer comp_gptr
, port_gptr
;
1166 * Since we're destroying the stream, remove the destroy
1167 * listeners that it registered for each component in
1168 * its component-port mapping hash table. Otherwise they
1169 * would be called and the stream would be accessed once
1170 * it's freed or another stream would be accessed.
1172 g_hash_table_iter_init(&ht_iter
, stream
->comp_cur_port
);
1174 while (g_hash_table_iter_next(&ht_iter
, &comp_gptr
, &port_gptr
)) {
1176 bt_component_remove_destroy_listener((void *) comp_gptr
,
1177 component_destroy_listener
, stream
);
1180 g_hash_table_destroy(stream
->comp_cur_port
);
1183 if (stream
->destroy_listeners
) {
1184 g_array_free(stream
->destroy_listeners
, TRUE
);
1187 bt_put(stream
->packet_header
);
1188 bt_put(stream
->packet_context
);
1193 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1194 uint64_t value
, bool force
)
1197 struct bt_ctf_field_type
*field_type
= NULL
;
1198 struct bt_ctf_field
*integer
=
1199 bt_ctf_field_structure_get_field(structure
, name
);
1201 if (!structure
|| !name
) {
1207 /* Field not found, not an error. */
1211 /* Make sure the payload has not already been set. */
1212 if (!force
&& bt_ctf_field_is_set(integer
)) {
1213 /* Payload already set, not an error */
1217 field_type
= bt_ctf_field_get_type(integer
);
1219 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1221 * The user most likely meant for us to populate this field
1222 * automatically. However, we can only do this if the field
1223 * is an integer. Return an error.
1229 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
1230 ret
= bt_ctf_field_signed_integer_set_value(integer
,
1233 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
1235 ret
= !ret
? 1 : ret
;
1243 int set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1246 return _set_structure_field_integer(structure
, name
, value
, true);
1250 * Returns the following codes:
1251 * 1 if the field was found and set,
1252 * 0 if nothing was done (field not found, or was already set),
1253 * <0 if an error was encoutered
1256 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1259 return _set_structure_field_integer(structure
, name
, value
, false);
1262 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1264 const char *name
= NULL
;
1270 name
= stream
->name
? stream
->name
->str
: NULL
;
1276 int bt_ctf_stream_is_writer(struct bt_ctf_stream
*stream
)
1284 ret
= (stream
->pos
.fd
>= 0);
1291 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream
*stream
,
1292 struct bt_component
*comp
,
1293 struct bt_port
*port
)
1298 assert(stream
->comp_cur_port
);
1301 * Do not take a reference to the component here because we
1302 * don't want the component to exist as long as this stream
1303 * exists. Instead, keep a weak reference, but add a destroy
1304 * listener so that we remove this hash table entry when we know
1305 * the component is destroyed.
1307 bt_component_add_destroy_listener(comp
, component_destroy_listener
,
1309 g_hash_table_insert(stream
->comp_cur_port
, comp
, port
);
1313 struct bt_port
*bt_ctf_stream_port_for_component(struct bt_ctf_stream
*stream
,
1314 struct bt_component
*comp
)
1318 assert(stream
->comp_cur_port
);
1319 return g_hash_table_lookup(stream
->comp_cur_port
, comp
);
1323 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream
*stream
,
1324 bt_ctf_stream_destroy_listener_func func
, void *data
)
1326 struct bt_ctf_stream_destroy_listener listener
;
1330 listener
.func
= func
;
1331 listener
.data
= data
;
1332 g_array_append_val(stream
->destroy_listeners
, listener
);
1336 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream
*stream
,
1337 bt_ctf_stream_destroy_listener_func func
, void *data
)
1344 for (i
= 0; i
< stream
->destroy_listeners
->len
; i
++) {
1345 struct bt_ctf_stream_destroy_listener
*listener
=
1346 &g_array_index(stream
->destroy_listeners
,
1347 struct bt_ctf_stream_destroy_listener
, i
);
1349 if (listener
->func
== func
&& listener
->data
== data
) {
1350 g_array_remove_index(stream
->destroy_listeners
, i
);