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 #define BT_LOG_TAG "STREAM"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/clock-class.h>
33 #include <babeltrace/ctf-writer/clock.h>
34 #include <babeltrace/ctf-writer/clock-internal.h>
35 #include <babeltrace/ctf-writer/event.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/fields-internal.h>
39 #include <babeltrace/ctf-ir/stream.h>
40 #include <babeltrace/ctf-ir/stream-internal.h>
41 #include <babeltrace/ctf-ir/stream-class-internal.h>
42 #include <babeltrace/ctf-ir/trace.h>
43 #include <babeltrace/ctf-ir/trace-internal.h>
44 #include <babeltrace/ctf-writer/writer-internal.h>
45 #include <babeltrace/graph/component-internal.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/ctf-writer/functor-internal.h>
48 #include <babeltrace/compiler-internal.h>
49 #include <babeltrace/align-internal.h>
50 #include <babeltrace/assert-internal.h>
55 void bt_stream_destroy(struct bt_object
*obj
);
57 int try_set_structure_field_integer(struct bt_field
*, char *, uint64_t);
60 int set_integer_field_value(struct bt_field
* field
, uint64_t value
)
63 struct bt_field_type
*field_type
= NULL
;
66 BT_LOGW_STR("Invalid parameter: field is NULL.");
71 field_type
= bt_field_get_type(field
);
72 BT_ASSERT(field_type
);
74 if (bt_field_type_get_type_id(field_type
) !=
75 BT_FIELD_TYPE_ID_INTEGER
) {
76 /* Not an integer and the value is unset, error. */
77 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
78 "field-addr=%p, ft-addr=%p, ft-id=%s",
80 bt_field_type_id_string(field_type
->id
));
85 if (bt_field_type_integer_is_signed(field_type
)) {
86 ret
= bt_field_signed_integer_set_value(field
, (int64_t) value
);
88 /* Value is out of range, error. */
89 BT_LOGW("Cannot set signed integer field's value: "
90 "addr=%p, value=%" PRId64
,
91 field
, (int64_t) value
);
95 ret
= bt_field_unsigned_integer_set_value(field
, value
);
97 /* Value is out of range, error. */
98 BT_LOGW("Cannot set unsigned integer field's value: "
99 "addr=%p, value=%" PRIu64
,
110 int set_packet_header_magic(struct bt_stream
*stream
)
113 struct bt_field
*magic_field
= bt_field_structure_get_field_by_name(
114 stream
->packet_header
, "magic");
115 const uint32_t magic_value
= 0xc1fc1fc1;
120 /* No magic field found. Not an error, skip. */
121 BT_LOGV("No field named `magic` in packet header: skipping: "
122 "stream-addr=%p, stream-name=\"%s\"",
123 stream
, bt_stream_get_name(stream
));
127 ret
= bt_field_unsigned_integer_set_value(magic_field
,
128 (uint64_t) magic_value
);
131 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
132 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
133 stream
, bt_stream_get_name(stream
),
134 magic_field
, (uint64_t) magic_value
);
136 BT_LOGV("Set packet header field's `magic` field's value: "
137 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
138 stream
, bt_stream_get_name(stream
),
139 magic_field
, (uint64_t) magic_value
);
147 int set_packet_header_uuid(struct bt_stream
*stream
)
151 struct bt_trace
*trace
= NULL
;
152 struct bt_field
*uuid_field
= bt_field_structure_get_field_by_name(
153 stream
->packet_header
, "uuid");
158 /* No uuid field found. Not an error, skip. */
159 BT_LOGV("No field named `uuid` in packet header: skipping: "
160 "stream-addr=%p, stream-name=\"%s\"",
161 stream
, bt_stream_get_name(stream
));
165 trace
= (struct bt_trace
*) bt_object_get_parent(stream
);
166 for (i
= 0; i
< 16; i
++) {
167 struct bt_field
*uuid_element
=
168 bt_field_array_get_field(uuid_field
, i
);
170 ret
= bt_field_unsigned_integer_set_value(
171 uuid_element
, (uint64_t) trace
->uuid
[i
]);
172 bt_put(uuid_element
);
174 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
175 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
176 "value=%" PRIu64
", index=%" PRId64
,
177 stream
, bt_stream_get_name(stream
),
178 uuid_element
, (uint64_t) trace
->uuid
[i
], i
);
183 BT_LOGV("Set packet header field's `uuid` field's value: "
184 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
185 stream
, bt_stream_get_name(stream
), uuid_field
);
193 int set_packet_header_stream_id(struct bt_stream
*stream
)
197 struct bt_field
*stream_id_field
= bt_field_structure_get_field_by_name(
198 stream
->packet_header
, "stream_id");
200 if (!stream_id_field
) {
201 /* No stream_id field found. Not an error, skip. */
202 BT_LOGV("No field named `stream_id` in packet header: skipping: "
203 "stream-addr=%p, stream-name=\"%s\"",
204 stream
, bt_stream_get_name(stream
));
208 stream_id
= stream
->stream_class
->id
;
209 ret
= bt_field_unsigned_integer_set_value(stream_id_field
,
210 (uint64_t) stream_id
);
212 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
213 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
214 stream
, bt_stream_get_name(stream
),
215 stream_id_field
, (uint64_t) stream_id
);
217 BT_LOGV("Set packet header field's `stream_id` field's value: "
218 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
219 stream
, bt_stream_get_name(stream
),
220 stream_id_field
, (uint64_t) stream_id
);
224 bt_put(stream_id_field
);
229 int auto_populate_packet_header(struct bt_stream
*stream
)
233 if (!stream
->packet_header
) {
237 ret
= set_packet_header_magic(stream
);
239 BT_LOGW("Cannot set packet header's magic number field: "
240 "stream-addr=%p, stream-name=\"%s\"",
241 stream
, bt_stream_get_name(stream
));
245 ret
= set_packet_header_uuid(stream
);
247 BT_LOGW("Cannot set packet header's UUID field: "
248 "stream-addr=%p, stream-name=\"%s\"",
249 stream
, bt_stream_get_name(stream
));
253 ret
= set_packet_header_stream_id(stream
);
255 BT_LOGW("Cannot set packet header's stream class ID field: "
256 "stream-addr=%p, stream-name=\"%s\"",
257 stream
, bt_stream_get_name(stream
));
261 BT_LOGV("Automatically populated stream's packet header's known fields: "
262 "stream-addr=%p, stream-name=\"%s\"",
263 stream
, bt_stream_get_name(stream
));
270 int set_packet_context_packet_size(struct bt_stream
*stream
)
273 struct bt_field
*field
= bt_field_structure_get_field_by_name(
274 stream
->packet_context
, "packet_size");
279 /* No packet size field found. Not an error, skip. */
280 BT_LOGV("No field named `packet_size` in packet context: skipping: "
281 "stream-addr=%p, stream-name=\"%s\"",
282 stream
, bt_stream_get_name(stream
));
286 ret
= bt_field_unsigned_integer_set_value(field
,
287 stream
->pos
.packet_size
);
289 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
290 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
291 stream
, bt_stream_get_name(stream
),
292 field
, stream
->pos
.packet_size
);
294 BT_LOGV("Set packet context field's `packet_size` field's value: "
295 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
296 stream
, bt_stream_get_name(stream
),
297 field
, stream
->pos
.packet_size
);
306 int set_packet_context_content_size(struct bt_stream
*stream
)
309 struct bt_field
*field
= bt_field_structure_get_field_by_name(
310 stream
->packet_context
, "content_size");
315 /* No content size field found. Not an error, skip. */
316 BT_LOGV("No field named `content_size` in packet context: skipping: "
317 "stream-addr=%p, stream-name=\"%s\"",
318 stream
, bt_stream_get_name(stream
));
322 ret
= bt_field_unsigned_integer_set_value(field
,
325 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
326 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
327 stream
, bt_stream_get_name(stream
),
328 field
, stream
->pos
.offset
);
330 BT_LOGV("Set packet context field's `content_size` field's value: "
331 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
332 stream
, bt_stream_get_name(stream
),
333 field
, stream
->pos
.offset
);
342 int set_packet_context_events_discarded(struct bt_stream
*stream
)
345 struct bt_field
*field
= bt_field_structure_get_field_by_name(
346 stream
->packet_context
, "events_discarded");
351 /* No discarded events count field found. Not an error, skip. */
352 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
353 "stream-addr=%p, stream-name=\"%s\"",
354 stream
, bt_stream_get_name(stream
));
359 * If the field is set by the user, make sure that the value is
360 * greater than or equal to the stream's current count of
361 * discarded events. We do not allow wrapping here. If it's
362 * valid, update the stream's current count.
364 if (bt_field_is_set_recursive(field
)) {
367 ret
= bt_field_unsigned_integer_get_value(field
,
370 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
371 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
372 stream
, bt_stream_get_name(stream
), field
);
376 if (user_val
< stream
->discarded_events
) {
377 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
378 "value is lesser than the stream's current discarded events count: "
379 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
380 "value=%" PRIu64
", "
381 "stream-discarded-events-count=%" PRIu64
,
382 stream
, bt_stream_get_name(stream
), field
,
383 user_val
, stream
->discarded_events
);
387 stream
->discarded_events
= user_val
;
389 ret
= bt_field_unsigned_integer_set_value(field
,
390 stream
->discarded_events
);
392 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
393 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
394 stream
, bt_stream_get_name(stream
),
395 field
, stream
->discarded_events
);
397 BT_LOGV("Set packet context field's `events_discarded` field's value: "
398 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
399 stream
, bt_stream_get_name(stream
),
400 field
, stream
->discarded_events
);
410 void update_clock_value(uint64_t *val
, uint64_t new_val
,
411 unsigned int new_val_size
)
413 const uint64_t pow2
= 1ULL << new_val_size
;
414 const uint64_t mask
= pow2
- 1;
417 #ifdef BT_LOG_ENABLED_VERBOSE
418 uint64_t old_val
= *val
;
421 if (new_val_size
== 64) {
426 val_masked
= *val
& mask
;
428 if (new_val
< val_masked
) {
437 BT_LOGV("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
443 int visit_field_update_clock_value(struct bt_field
*field
, uint64_t *val
)
451 switch (bt_field_get_type_id(field
)) {
452 case BT_FIELD_TYPE_ID_INTEGER
:
454 struct bt_clock_class
*cc
=
455 bt_field_type_integer_get_mapped_clock_class(
465 val_size
= bt_field_type_integer_get_size(field
->type
);
466 BT_ASSERT(val_size
>= 1);
468 if (bt_field_type_integer_is_signed(field
->type
)) {
471 ret
= bt_field_signed_integer_get_value(field
, &ival
);
472 uval
= (uint64_t) ival
;
474 ret
= bt_field_unsigned_integer_get_value(field
, &uval
);
482 update_clock_value(val
, uval
, val_size
);
485 case BT_FIELD_TYPE_ID_ENUM
:
487 struct bt_field
*int_field
=
488 bt_field_enumeration_get_container(field
);
490 BT_ASSERT(int_field
);
491 ret
= visit_field_update_clock_value(int_field
, val
);
495 case BT_FIELD_TYPE_ID_ARRAY
:
498 int64_t len
= bt_field_type_array_get_length(field
->type
);
502 for (i
= 0; i
< len
; i
++) {
503 struct bt_field
*elem_field
=
504 bt_field_array_get_field(field
, i
);
506 BT_ASSERT(elem_field
);
507 ret
= visit_field_update_clock_value(elem_field
, val
);
515 case BT_FIELD_TYPE_ID_SEQUENCE
:
518 int64_t len
= bt_field_sequence_get_int_length(field
);
525 for (i
= 0; i
< len
; i
++) {
526 struct bt_field
*elem_field
=
527 bt_field_sequence_get_field(field
, i
);
529 BT_ASSERT(elem_field
);
530 ret
= visit_field_update_clock_value(elem_field
, val
);
538 case BT_FIELD_TYPE_ID_STRUCT
:
541 int64_t len
= bt_field_type_structure_get_field_count(
546 for (i
= 0; i
< len
; i
++) {
547 struct bt_field
*member_field
=
548 bt_field_structure_get_field_by_index(field
, i
);
550 BT_ASSERT(member_field
);
551 ret
= visit_field_update_clock_value(member_field
, val
);
552 bt_put(member_field
);
559 case BT_FIELD_TYPE_ID_VARIANT
:
561 struct bt_field
*cur_field
=
562 bt_field_variant_get_current_field(field
);
569 ret
= visit_field_update_clock_value(cur_field
, val
);
581 int visit_event_update_clock_value(struct bt_event
*event
, uint64_t *val
)
584 struct bt_field
*field
;
586 field
= bt_event_get_header(event
);
587 ret
= visit_field_update_clock_value(field
, val
);
590 BT_LOGW_STR("Cannot automatically update clock value in "
595 field
= bt_event_get_stream_event_context(event
);
596 ret
= visit_field_update_clock_value(field
, val
);
599 BT_LOGW_STR("Cannot automatically update clock value in "
600 "event's stream event context.");
604 field
= bt_event_get_event_context(event
);
605 ret
= visit_field_update_clock_value(field
, val
);
608 BT_LOGW_STR("Cannot automatically update clock value in "
613 field
= bt_event_get_event_payload(event
);
614 ret
= visit_field_update_clock_value(field
, val
);
617 BT_LOGW_STR("Cannot automatically update clock value in "
627 int set_packet_context_timestamps(struct bt_stream
*stream
)
631 uint64_t cur_clock_value
;
632 uint64_t init_clock_value
= 0;
633 struct bt_field
*ts_begin_field
= bt_field_structure_get_field_by_name(
634 stream
->packet_context
, "timestamp_begin");
635 struct bt_field
*ts_end_field
= bt_field_structure_get_field_by_name(
636 stream
->packet_context
, "timestamp_end");
640 if (ts_begin_field
&& bt_field_is_set_recursive(ts_begin_field
)) {
641 /* Use provided `timestamp_begin` value as starting value */
642 ret
= bt_field_unsigned_integer_get_value(ts_begin_field
, &val
);
644 init_clock_value
= val
;
645 } else if (stream
->last_ts_end
!= -1ULL) {
646 /* Use last packet's ending timestamp as starting value */
647 init_clock_value
= stream
->last_ts_end
;
650 cur_clock_value
= init_clock_value
;
652 if (stream
->last_ts_end
!= -1ULL &&
653 cur_clock_value
< stream
->last_ts_end
) {
654 BT_LOGW("Packet's initial timestamp is less than previous "
655 "packet's final timestamp: "
656 "stream-addr=%p, stream-name=\"%s\", "
657 "cur-packet-ts-begin=%" PRIu64
", "
658 "prev-packet-ts-end=%" PRIu64
,
659 stream
, bt_stream_get_name(stream
),
660 cur_clock_value
, stream
->last_ts_end
);
666 * Visit all the packet context fields, followed by all the
667 * fields of all the events, in order, updating our current
668 * clock value as we visit.
670 * While visiting the packet context fields, do not consider
671 * `timestamp_begin` and `timestamp_end` because this function's
672 * purpose is to set them anyway. Also do not consider
673 * `packet_size`, `content_size`, `events_discarded`, and
674 * `packet_seq_num` if they are not set because those are
675 * autopopulating fields.
677 len
= bt_field_type_structure_get_field_count(
678 stream
->packet_context
->type
);
681 for (i
= 0; i
< len
; i
++) {
682 const char *member_name
;
683 struct bt_field
*member_field
;
685 ret
= bt_field_type_structure_get_field_by_index(
686 stream
->packet_context
->type
, &member_name
, NULL
, i
);
689 if (strcmp(member_name
, "timestamp_begin") == 0 ||
690 strcmp(member_name
, "timestamp_end") == 0) {
694 member_field
= bt_field_structure_get_field_by_index(
695 stream
->packet_context
, i
);
696 BT_ASSERT(member_field
);
698 if (strcmp(member_name
, "packet_size") == 0 &&
699 !bt_field_is_set_recursive(member_field
)) {
700 bt_put(member_field
);
704 if (strcmp(member_name
, "content_size") == 0 &&
705 !bt_field_is_set_recursive(member_field
)) {
706 bt_put(member_field
);
710 if (strcmp(member_name
, "events_discarded") == 0 &&
711 !bt_field_is_set_recursive(member_field
)) {
712 bt_put(member_field
);
716 if (strcmp(member_name
, "packet_seq_num") == 0 &&
717 !bt_field_is_set_recursive(member_field
)) {
718 bt_put(member_field
);
722 ret
= visit_field_update_clock_value(member_field
,
724 bt_put(member_field
);
726 BT_LOGW("Cannot automatically update clock value "
727 "in stream's packet context: "
728 "stream-addr=%p, stream-name=\"%s\", "
730 stream
, bt_stream_get_name(stream
),
736 for (i
= 0; i
< stream
->events
->len
; i
++) {
737 struct bt_event
*event
= g_ptr_array_index(stream
->events
, i
);
740 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
742 BT_LOGW("Cannot automatically update clock value "
743 "in stream's packet context: "
744 "stream-addr=%p, stream-name=\"%s\", "
745 "index=%" PRIu64
", event-addr=%p, "
746 "event-class-id=%" PRId64
", "
747 "event-class-name=\"%s\"",
748 stream
, bt_stream_get_name(stream
),
750 bt_event_class_get_id(event
->event_class
),
751 bt_event_class_get_name(event
->event_class
));
757 * Everything is visited, thus the current clock value
758 * corresponds to the ending timestamp. Validate this value
759 * against the provided value of `timestamp_end`, if any,
762 if (ts_end_field
&& bt_field_is_set_recursive(ts_end_field
)) {
763 ret
= bt_field_unsigned_integer_get_value(ts_end_field
, &val
);
766 if (val
< cur_clock_value
) {
767 BT_LOGW("Packet's final timestamp is less than "
768 "computed packet's final timestamp: "
769 "stream-addr=%p, stream-name=\"%s\", "
770 "cur-packet-ts-end=%" PRIu64
", "
771 "computed-packet-ts-end=%" PRIu64
,
772 stream
, bt_stream_get_name(stream
),
773 val
, cur_clock_value
);
778 stream
->last_ts_end
= val
;
781 if (ts_end_field
&& !bt_field_is_set_recursive(ts_end_field
)) {
782 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
784 stream
->last_ts_end
= cur_clock_value
;
788 stream
->last_ts_end
= cur_clock_value
;
791 /* Set `timestamp_begin` field to initial clock value */
792 if (ts_begin_field
&& !bt_field_is_set_recursive(ts_begin_field
)) {
793 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
798 bt_put(ts_begin_field
);
799 bt_put(ts_end_field
);
804 int auto_populate_packet_context(struct bt_stream
*stream
, bool set_ts
)
808 if (!stream
->packet_context
) {
812 ret
= set_packet_context_packet_size(stream
);
814 BT_LOGW("Cannot set packet context's packet size field: "
815 "stream-addr=%p, stream-name=\"%s\"",
816 stream
, bt_stream_get_name(stream
));
820 ret
= set_packet_context_content_size(stream
);
822 BT_LOGW("Cannot set packet context's content size field: "
823 "stream-addr=%p, stream-name=\"%s\"",
824 stream
, bt_stream_get_name(stream
));
829 ret
= set_packet_context_timestamps(stream
);
831 BT_LOGW("Cannot set packet context's timestamp fields: "
832 "stream-addr=%p, stream-name=\"%s\"",
833 stream
, bt_stream_get_name(stream
));
838 ret
= set_packet_context_events_discarded(stream
);
840 BT_LOGW("Cannot set packet context's discarded events count field: "
841 "stream-addr=%p, stream-name=\"%s\"",
842 stream
, bt_stream_get_name(stream
));
846 BT_LOGV("Automatically populated stream's packet context's known fields: "
847 "stream-addr=%p, stream-name=\"%s\"",
848 stream
, bt_stream_get_name(stream
));
855 void release_event(struct bt_event
*event
)
857 if (bt_object_get_ref_count(event
)) {
859 * The event is being orphaned, but it must guarantee the
860 * existence of its event class for the duration of its
863 bt_get(event
->event_class
);
864 BT_PUT(event
->base
.parent
);
866 bt_object_release(event
);
871 int create_stream_file(struct bt_ctf_writer
*writer
,
872 struct bt_stream
*stream
)
875 GString
*filename
= g_string_new(NULL
);
876 int64_t stream_class_id
;
877 char *file_path
= NULL
;
879 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
880 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
881 writer
, stream
, bt_stream_get_name(stream
),
882 stream
->stream_class
, stream
->stream_class
->name
->str
);
884 if (stream
->name
&& stream
->name
->len
> 0) {
885 /* Use stream name's base name as prefix */
886 gchar
*basename
= g_path_get_basename(stream
->name
->str
);
890 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
891 g_string_assign(filename
, "stream");
893 g_string_assign(filename
, basename
);
900 if (stream
->stream_class
->name
&&
901 stream
->stream_class
->name
->len
> 0) {
902 /* Use stream class name's base name as prefix */
904 g_path_get_basename(stream
->stream_class
->name
->str
);
908 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
909 g_string_assign(filename
, "stream");
911 g_string_assign(filename
, basename
);
918 /* Default to using `stream-` as prefix */
919 g_string_assign(filename
, "stream");
922 stream_class_id
= bt_stream_class_get_id(stream
->stream_class
);
923 BT_ASSERT(stream_class_id
>= 0);
924 BT_ASSERT(stream
->id
>= 0);
925 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
926 stream_class_id
, stream
->id
);
928 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
929 if (file_path
== NULL
) {
935 O_RDWR
| O_CREAT
| O_TRUNC
,
936 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
939 BT_LOGW_ERRNO("Failed to open stream file for writing",
940 ": file_path=\"%s\", filename=\"%s\", ret=%d",
941 file_path
, filename
->str
, fd
);
945 BT_LOGD("Created stream file for writing: "
946 "stream-addr=%p, stream-name=\"%s\", "
947 "filename=\"%s\", fd=%d", stream
, bt_stream_get_name(stream
),
951 g_string_free(filename
, TRUE
);
956 void set_stream_fd(struct bt_stream
*stream
, int fd
)
958 (void) bt_stream_pos_init(&stream
->pos
, fd
, O_RDWR
);
963 void component_destroy_listener(struct bt_component
*component
, void *data
)
965 struct bt_stream
*stream
= data
;
967 BT_LOGD("Component is being destroyed, stream is notified: "
968 "comp-addr=%p, stream-addr=%p", component
, stream
);
969 g_hash_table_remove(stream
->comp_cur_port
, component
);
973 struct bt_stream
*bt_stream_create_with_id_no_check(
974 struct bt_stream_class
*stream_class
,
975 const char *name
, uint64_t id
)
978 struct bt_stream
*stream
= NULL
;
979 struct bt_trace
*trace
= NULL
;
980 struct bt_ctf_writer
*writer
= NULL
;
983 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
987 BT_LOGD("Creating stream object: stream-class-addr=%p, "
988 "stream-class-name=\"%s\", stream-name=\"%s\", "
989 "stream-id=%" PRIu64
,
990 stream_class
, bt_stream_class_get_name(stream_class
),
992 trace
= bt_stream_class_borrow_trace(stream_class
);
994 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
995 "stream-class-addr=%p, stream-class-name=\"%s\", "
996 "stream-name=\"%s\"",
997 stream_class
, bt_stream_class_get_name(stream_class
),
1002 if (bt_trace_is_static(trace
)) {
1004 * A static trace has the property that all its stream
1005 * classes, clock classes, and streams are definitive:
1006 * no more can be added, and each object is also frozen.
1008 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
1009 "stream-class-addr=%p, stream-class-name=\"%s\", "
1010 "stream-name=\"%s\", trace-addr=%p",
1011 stream_class
, bt_stream_class_get_name(stream_class
),
1018 * Validate that the given ID is unique amongst all the
1019 * existing trace's streams created from the same stream
1024 for (i
= 0; i
< trace
->streams
->len
; i
++) {
1025 struct bt_stream
*trace_stream
=
1026 g_ptr_array_index(trace
->streams
, i
);
1028 if (trace_stream
->stream_class
!= stream_class
) {
1032 if (trace_stream
->id
== id
) {
1033 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
1039 stream
= g_new0(struct bt_stream
, 1);
1041 BT_LOGE_STR("Failed to allocate one stream.");
1045 bt_object_init(stream
, bt_stream_destroy
);
1047 * Acquire reference to parent since stream will become publicly
1048 * reachable; it needs its parent to remain valid.
1050 bt_object_set_parent(stream
, trace
);
1051 stream
->stream_class
= stream_class
;
1052 stream
->pos
.fd
= -1;
1053 stream
->id
= (int64_t) id
;
1055 stream
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
1056 sizeof(struct bt_stream_destroy_listener
));
1057 if (!stream
->destroy_listeners
) {
1058 BT_LOGE_STR("Failed to allocate a GArray.");
1063 stream
->name
= g_string_new(name
);
1064 if (!stream
->name
) {
1065 BT_LOGE_STR("Failed to allocate a GString.");
1070 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace
);
1072 if (trace
->is_created_by_writer
) {
1075 writer
= (struct bt_ctf_writer
*) bt_object_get_parent(trace
);
1076 stream
->id
= (int64_t) stream_class
->next_stream_id
++;
1077 stream
->last_ts_end
= -1ULL;
1079 BT_LOGD("Stream object belongs to a writer's trace: "
1080 "writer-addr=%p", writer
);
1083 if (stream_class
->packet_context_type
) {
1084 BT_LOGD("Creating stream's packet context field: "
1085 "ft-addr=%p", stream_class
->packet_context_type
);
1086 stream
->packet_context
= bt_field_create(
1087 stream_class
->packet_context_type
);
1088 if (!stream
->packet_context
) {
1089 BT_LOGW_STR("Cannot create stream's packet context field.");
1093 /* Initialize events_discarded */
1094 ret
= try_set_structure_field_integer(
1095 stream
->packet_context
, "events_discarded", 0);
1097 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1098 "ret=%d, packet-context-field-addr=%p",
1099 ret
, stream
->packet_context
);
1104 stream
->events
= g_ptr_array_new_with_free_func(
1105 (GDestroyNotify
) release_event
);
1106 if (!stream
->events
) {
1107 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1111 if (trace
->packet_header_type
) {
1112 BT_LOGD("Creating stream's packet header field: "
1113 "ft-addr=%p", trace
->packet_header_type
);
1114 stream
->packet_header
=
1115 bt_field_create(trace
->packet_header_type
);
1116 if (!stream
->packet_header
) {
1117 BT_LOGW_STR("Cannot create stream's packet header field.");
1123 * Attempt to populate the default trace packet header fields
1124 * (magic, uuid and stream_id). This will _not_ fail shall the
1125 * fields not be found or be of an incompatible type; they will
1126 * simply not be populated automatically. The user will have to
1127 * make sure to set the trace packet header fields himself
1130 ret
= auto_populate_packet_header(stream
);
1132 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1136 /* Create file associated with this stream */
1137 fd
= create_stream_file(writer
, stream
);
1139 BT_LOGW_STR("Cannot create stream file.");
1143 set_stream_fd(stream
, fd
);
1145 /* Freeze the writer */
1146 BT_LOGD_STR("Freezing stream's CTF writer.");
1147 bt_ctf_writer_freeze(writer
);
1149 /* Non-writer stream indicated by a negative FD */
1150 set_stream_fd(stream
, -1);
1151 stream
->comp_cur_port
= g_hash_table_new(g_direct_hash
,
1153 if (!stream
->comp_cur_port
) {
1154 BT_LOGE_STR("Failed to allocate a GHashTable.");
1159 /* Add this stream to the trace's streams */
1160 g_ptr_array_add(trace
->streams
, stream
);
1161 BT_LOGD("Created stream object: addr=%p", stream
);
1172 struct bt_stream
*bt_stream_create_with_id(
1173 struct bt_stream_class
*stream_class
,
1174 const char *name
, uint64_t id_param
)
1176 struct bt_trace
*trace
;
1177 struct bt_stream
*stream
= NULL
;
1178 int64_t id
= (int64_t) id_param
;
1180 if (!stream_class
) {
1181 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
1186 BT_LOGW("Invalid parameter: invalid stream's ID: "
1187 "name=\"%s\", id=%" PRIu64
,
1192 trace
= bt_stream_class_borrow_trace(stream_class
);
1194 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1195 "stream-class-addr=%p, stream-class-name=\"%s\", "
1196 "stream-name=\"%s\", stream-id=%" PRIu64
,
1197 stream_class
, bt_stream_class_get_name(stream_class
),
1202 if (trace
->is_created_by_writer
) {
1203 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_stream_create(): "
1204 "stream-class-addr=%p, stream-class-name=\"%s\", "
1205 "stream-name=\"%s\", stream-id=%" PRIu64
,
1206 stream_class
, bt_stream_class_get_name(stream_class
),
1211 stream
= bt_stream_create_with_id_no_check(stream_class
,
1218 struct bt_stream
*bt_stream_create(
1219 struct bt_stream_class
*stream_class
,
1222 return bt_stream_create_with_id_no_check(stream_class
,
1226 struct bt_stream_class
*bt_stream_get_class(
1227 struct bt_stream
*stream
)
1229 struct bt_stream_class
*stream_class
= NULL
;
1232 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1236 stream_class
= stream
->stream_class
;
1237 bt_get(stream_class
);
1239 return stream_class
;
1242 int bt_stream_get_discarded_events_count(
1243 struct bt_stream
*stream
, uint64_t *count
)
1248 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1254 BT_LOGW_STR("Invalid parameter: count is NULL.");
1259 if (stream
->pos
.fd
< 0) {
1260 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1261 "stream-addr=%p, stream-name=\"%s\"",
1262 stream
, bt_stream_get_name(stream
));
1267 *count
= (uint64_t) stream
->discarded_events
;
1274 int set_packet_context_events_discarded_field(struct bt_stream
*stream
,
1278 struct bt_field
*events_discarded_field
= NULL
;
1280 if (!stream
->packet_context
) {
1284 events_discarded_field
= bt_field_structure_get_field_by_name(
1285 stream
->packet_context
, "events_discarded");
1286 if (!events_discarded_field
) {
1290 ret
= bt_field_unsigned_integer_set_value(
1291 events_discarded_field
, count
);
1293 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1294 "field-addr=%p, value=%" PRIu64
,
1295 events_discarded_field
, count
);
1300 bt_put(events_discarded_field
);
1304 void bt_stream_append_discarded_events(struct bt_stream
*stream
,
1305 uint64_t event_count
)
1309 struct bt_field
*events_discarded_field
= NULL
;
1312 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1316 BT_LOGV("Appending discarded events to stream: "
1317 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1318 stream
, bt_stream_get_name(stream
), event_count
);
1320 if (!stream
->packet_context
) {
1321 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1325 if (stream
->pos
.fd
< 0) {
1326 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1330 events_discarded_field
= bt_field_structure_get_field_by_name(
1331 stream
->packet_context
, "events_discarded");
1332 if (!events_discarded_field
) {
1333 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1337 new_count
= stream
->discarded_events
+ event_count
;
1338 if (new_count
< stream
->discarded_events
) {
1339 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1340 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1341 stream
->discarded_events
, new_count
);
1345 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1347 /* set_packet_context_events_discarded_field() logs errors */
1351 stream
->discarded_events
= new_count
;
1352 BT_LOGV("Appended discarded events to stream: "
1353 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1354 stream
, bt_stream_get_name(stream
), event_count
);
1357 bt_put(events_discarded_field
);
1360 static int auto_populate_event_header(struct bt_stream
*stream
,
1361 struct bt_event
*event
)
1364 struct bt_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1365 struct bt_clock_class
*mapped_clock_class
= NULL
;
1366 int64_t event_class_id
;
1370 if (!event
->event_header
) {
1374 if (event
->frozen
) {
1375 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1380 BT_LOGV("Automatically populating event's header field: "
1381 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1382 stream
, bt_stream_get_name(stream
), event
);
1384 id_field
= bt_field_structure_get_field_by_name(event
->event_header
, "id");
1385 event_class_id
= bt_event_class_get_id(event
->event_class
);
1386 BT_ASSERT(event_class_id
>= 0);
1388 if (id_field
&& bt_field_type_is_integer(id_field
->type
)) {
1389 ret
= set_integer_field_value(id_field
, event_class_id
);
1391 BT_LOGW("Cannot set event header's `id` field's value: "
1392 "addr=%p, value=%" PRIu64
, id_field
,
1399 * The conditions to automatically set the timestamp are:
1401 * 1. The event header field "timestamp" exists and is an
1403 * 2. This stream's class has a registered clock (set with
1404 * bt_stream_class_set_clock()).
1405 * 3. The "timestamp" field is not set.
1407 timestamp_field
= bt_field_structure_get_field_by_name(
1408 event
->event_header
, "timestamp");
1409 if (timestamp_field
&& stream
->stream_class
->clock
&&
1410 bt_field_type_is_integer(timestamp_field
->type
) &&
1411 !bt_field_is_set_recursive(timestamp_field
)) {
1412 mapped_clock_class
=
1413 bt_field_type_integer_get_mapped_clock_class(
1414 timestamp_field
->type
);
1415 if (mapped_clock_class
) {
1418 BT_ASSERT(mapped_clock_class
==
1419 stream
->stream_class
->clock
->clock_class
);
1420 ret
= bt_ctf_clock_get_value(
1421 stream
->stream_class
->clock
,
1423 BT_ASSERT(ret
== 0);
1424 ret
= set_integer_field_value(timestamp_field
,
1427 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1428 "addr=%p, value=%" PRIu64
,
1429 timestamp_field
, timestamp
);
1435 BT_LOGV("Automatically populated event's header field: "
1436 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1437 stream
, bt_stream_get_name(stream
), event
);
1441 bt_put(timestamp_field
);
1442 bt_put(mapped_clock_class
);
1446 int bt_stream_append_event(struct bt_stream
*stream
,
1447 struct bt_event
*event
)
1452 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1458 BT_LOGW_STR("Invalid parameter: event is NULL.");
1463 if (stream
->pos
.fd
< 0) {
1464 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1469 BT_LOGV("Appending event to stream: "
1470 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1471 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1472 stream
, bt_stream_get_name(stream
), event
,
1473 bt_event_class_get_name(bt_event_borrow_event_class(event
)),
1474 bt_event_class_get_id(bt_event_borrow_event_class(event
)));
1477 * The event is not supposed to have a parent stream at this
1478 * point. The only other way an event can have a parent stream
1479 * is if it was assigned when setting a packet to the event,
1480 * in which case the packet's stream is not a writer stream,
1481 * and thus the user is trying to append an event which belongs
1482 * to another stream.
1484 if (event
->base
.parent
) {
1489 bt_object_set_parent(event
, stream
);
1490 BT_LOGV_STR("Automatically populating the header of the event to append.");
1491 ret
= auto_populate_event_header(stream
, event
);
1493 /* auto_populate_event_header() reports errors */
1497 /* Make sure the various scopes of the event are set */
1498 BT_LOGV_STR("Validating event to append.");
1499 ret
= bt_event_validate(event
);
1504 /* Save the new event and freeze it */
1505 BT_LOGV_STR("Freezing the event to append.");
1506 bt_event_freeze(event
);
1507 g_ptr_array_add(stream
->events
, event
);
1510 * Event had to hold a reference to its event class as long as it wasn't
1511 * part of the same trace hierarchy. From now on, the event and its
1512 * class share the same lifetime guarantees and the reference is no
1515 BT_LOGV_STR("Putting the event's class.");
1516 bt_put(event
->event_class
);
1517 BT_LOGV("Appended event to stream: "
1518 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1519 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1520 stream
, bt_stream_get_name(stream
), event
,
1521 bt_event_class_get_name(bt_event_borrow_event_class(event
)),
1522 bt_event_class_get_id(bt_event_borrow_event_class(event
)));
1529 * Orphan the event; we were not successful in associating it to
1532 bt_object_set_parent(event
, NULL
);
1537 struct bt_field
*bt_stream_get_packet_context(struct bt_stream
*stream
)
1539 struct bt_field
*packet_context
= NULL
;
1542 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1546 if (stream
->pos
.fd
< 0) {
1547 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1548 "stream-addr=%p, stream-name=\"%s\"", stream
,
1549 bt_stream_get_name(stream
));
1553 packet_context
= stream
->packet_context
;
1554 if (packet_context
) {
1555 bt_get(packet_context
);
1558 return packet_context
;
1561 int bt_stream_set_packet_context(struct bt_stream
*stream
,
1562 struct bt_field
*field
)
1565 struct bt_field_type
*field_type
;
1568 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1573 if (stream
->pos
.fd
< 0) {
1574 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1579 field_type
= bt_field_get_type(field
);
1580 if (bt_field_type_compare(field_type
,
1581 stream
->stream_class
->packet_context_type
)) {
1582 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1583 "stream-addr=%p, stream-name=\"%s\", "
1584 "packet-context-field-addr=%p, "
1585 "packet-context-ft-addr=%p",
1586 stream
, bt_stream_get_name(stream
),
1593 bt_put(stream
->packet_context
);
1594 stream
->packet_context
= bt_get(field
);
1595 BT_LOGV("Set stream's packet context field: "
1596 "stream-addr=%p, stream-name=\"%s\", "
1597 "packet-context-field-addr=%p",
1598 stream
, bt_stream_get_name(stream
), field
);
1603 struct bt_field
*bt_stream_get_packet_header(struct bt_stream
*stream
)
1605 struct bt_field
*packet_header
= NULL
;
1608 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1612 if (stream
->pos
.fd
< 0) {
1613 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1614 "stream-addr=%p, stream-name=\"%s\"", stream
,
1615 bt_stream_get_name(stream
));
1619 packet_header
= stream
->packet_header
;
1620 if (packet_header
) {
1621 bt_get(packet_header
);
1624 return packet_header
;
1627 int bt_stream_set_packet_header(struct bt_stream
*stream
,
1628 struct bt_field
*field
)
1631 struct bt_trace
*trace
= NULL
;
1632 struct bt_field_type
*field_type
= NULL
;
1635 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1640 if (stream
->pos
.fd
< 0) {
1641 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1646 trace
= (struct bt_trace
*) bt_object_get_parent(stream
);
1649 if (trace
->packet_header_type
) {
1650 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1651 "stream-addr=%p, stream-name=\"%s\", "
1652 "packet-header-field-addr=%p, "
1653 "expected-ft-addr=%p",
1654 stream
, bt_stream_get_name(stream
),
1655 field
, trace
->packet_header_type
);
1660 goto skip_validation
;
1663 field_type
= bt_field_get_type(field
);
1664 BT_ASSERT(field_type
);
1666 if (bt_field_type_compare(field_type
, trace
->packet_header_type
)) {
1667 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1668 "stream-addr=%p, stream-name=\"%s\", "
1669 "packet-header-field-addr=%p, "
1670 "packet-header-ft-addr=%p",
1671 stream
, bt_stream_get_name(stream
),
1678 bt_put(stream
->packet_header
);
1679 stream
->packet_header
= bt_get(field
);
1680 BT_LOGV("Set stream's packet header field: "
1681 "stream-addr=%p, stream-name=\"%s\", "
1682 "packet-header-field-addr=%p",
1683 stream
, bt_stream_get_name(stream
), field
);
1691 void reset_structure_field(struct bt_field
*structure
, const char *name
)
1693 struct bt_field
*member
;
1695 member
= bt_field_structure_get_field_by_name(structure
, name
);
1697 (void) bt_field_reset_recursive(member
);
1702 int bt_stream_flush(struct bt_stream
*stream
)
1706 struct bt_stream_pos packet_context_pos
;
1707 struct bt_trace
*trace
;
1708 enum bt_byte_order native_byte_order
;
1709 bool has_packet_size
= false;
1712 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1717 if (stream
->pos
.fd
< 0) {
1718 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1723 if (stream
->packet_context
) {
1724 struct bt_field
*packet_size_field
;
1726 packet_size_field
= bt_field_structure_get_field_by_name(
1727 stream
->packet_context
, "packet_size");
1728 has_packet_size
= (packet_size_field
!= NULL
);
1729 bt_put(packet_size_field
);
1732 if (stream
->flushed_packet_count
== 1) {
1733 if (!stream
->packet_context
) {
1734 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1739 if (!has_packet_size
) {
1740 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1746 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1747 "stream-name=\"%s\", packet-index=%u", stream
,
1748 bt_stream_get_name(stream
), stream
->flushed_packet_count
);
1749 trace
= bt_stream_class_borrow_trace(stream
->stream_class
);
1751 native_byte_order
= bt_trace_get_native_byte_order(trace
);
1753 ret
= auto_populate_packet_header(stream
);
1755 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1760 ret
= auto_populate_packet_context(stream
, true);
1762 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1767 /* mmap the next packet */
1768 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64
,
1769 stream
->pos
.offset
);
1770 bt_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
1771 BT_ASSERT(stream
->pos
.packet_size
% 8 == 0);
1773 if (stream
->packet_header
) {
1774 BT_LOGV_STR("Serializing packet header field.");
1775 ret
= bt_field_serialize_recursive(stream
->packet_header
,
1776 &stream
->pos
, native_byte_order
);
1778 BT_LOGW("Cannot serialize stream's packet header field: "
1779 "field-addr=%p", stream
->packet_header
);
1784 if (stream
->packet_context
) {
1785 /* Write packet context */
1786 memcpy(&packet_context_pos
, &stream
->pos
,
1787 sizeof(packet_context_pos
));
1788 BT_LOGV_STR("Serializing packet context field.");
1789 ret
= bt_field_serialize_recursive(stream
->packet_context
,
1790 &stream
->pos
, native_byte_order
);
1792 BT_LOGW("Cannot serialize stream's packet context field: "
1793 "field-addr=%p", stream
->packet_context
);
1798 BT_LOGV("Serializing events: count=%u", stream
->events
->len
);
1800 for (i
= 0; i
< stream
->events
->len
; i
++) {
1801 struct bt_event
*event
= g_ptr_array_index(
1803 struct bt_event_class
*event_class
=
1804 bt_event_borrow_event_class(event
);
1806 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1807 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1808 "pos-offset=%" PRId64
", packet-size=%" PRIu64
,
1809 i
, event
, bt_event_class_get_name(event_class
),
1810 bt_event_class_get_id(event_class
),
1811 stream
->pos
.offset
, stream
->pos
.packet_size
);
1813 /* Write event header */
1814 if (event
->event_header
) {
1815 BT_LOGV_STR("Serializing event's header field.");
1816 ret
= bt_field_serialize_recursive(event
->event_header
,
1817 &stream
->pos
, native_byte_order
);
1819 BT_LOGW("Cannot serialize event's header field: "
1820 "field-addr=%p", event
->event_header
);
1825 /* Write stream event context */
1826 if (event
->stream_event_context
) {
1827 BT_LOGV_STR("Serializing event's stream event context field.");
1828 ret
= bt_field_serialize_recursive(
1829 event
->stream_event_context
, &stream
->pos
,
1832 BT_LOGW("Cannot serialize event's stream event context field: "
1833 "field-addr=%p", event
->stream_event_context
);
1838 /* Write event content */
1839 ret
= bt_event_serialize(event
, &stream
->pos
,
1842 /* bt_event_serialize() logs errors */
1847 if (!has_packet_size
&& stream
->pos
.offset
% 8 != 0) {
1848 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1849 "but current content size is not a multiple of 8 bits: "
1850 "content-size=%" PRId64
", "
1851 "packet-size=%" PRIu64
,
1853 stream
->pos
.packet_size
);
1858 BT_ASSERT(stream
->pos
.packet_size
% 8 == 0);
1861 * Remove extra padding bytes.
1863 stream
->pos
.packet_size
= (stream
->pos
.offset
+ 7) & ~7;
1865 if (stream
->packet_context
) {
1867 * The whole packet is serialized at this point. Make sure that,
1868 * if `packet_size` is missing, the current content size is
1869 * equal to the current packet size.
1871 struct bt_field
*field
= bt_field_structure_get_field_by_name(
1872 stream
->packet_context
, "content_size");
1876 if (stream
->pos
.offset
!= stream
->pos
.packet_size
) {
1877 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1878 "but current packet's content size is not equal to its packet size: "
1879 "content-size=%" PRId64
", "
1880 "packet-size=%" PRIu64
,
1882 stream
->pos
.packet_size
);
1889 * Overwrite the packet context now that the stream
1890 * position's packet and content sizes have the correct
1893 * Copy base_mma as the packet may have been remapped
1894 * (e.g. when a packet is resized).
1896 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1897 ret
= auto_populate_packet_context(stream
, false);
1899 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1904 BT_LOGV("Rewriting (serializing) packet context field.");
1905 ret
= bt_field_serialize_recursive(stream
->packet_context
,
1906 &packet_context_pos
, native_byte_order
);
1908 BT_LOGW("Cannot serialize stream's packet context field: "
1909 "field-addr=%p", stream
->packet_context
);
1914 g_ptr_array_set_size(stream
->events
, 0);
1915 stream
->flushed_packet_count
++;
1916 stream
->size
+= stream
->pos
.packet_size
/ CHAR_BIT
;
1919 /* Reset automatically-set fields. */
1920 if (stream
->packet_context
) {
1921 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1922 reset_structure_field(stream
->packet_context
, "timestamp_end");
1923 reset_structure_field(stream
->packet_context
, "packet_size");
1924 reset_structure_field(stream
->packet_context
, "content_size");
1925 reset_structure_field(stream
->packet_context
, "events_discarded");
1930 * We failed to write the packet. Its size is therefore set to 0
1931 * to ensure the next mapping is done in the same place rather
1932 * than advancing by "stream->pos.packet_size", which would
1933 * leave a corrupted packet in the trace.
1935 stream
->pos
.packet_size
= 0;
1937 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64
", "
1938 "packet-size=%" PRIu64
,
1939 stream
->pos
.offset
, stream
->pos
.packet_size
);
1946 /* Pre-2.0 CTF writer backward compatibility */
1947 void bt_ctf_stream_get(struct bt_stream
*stream
)
1952 /* Pre-2.0 CTF writer backward compatibility */
1953 void bt_ctf_stream_put(struct bt_stream
*stream
)
1959 void bt_stream_destroy(struct bt_object
*obj
)
1961 struct bt_stream
*stream
;
1964 stream
= container_of(obj
, struct bt_stream
, base
);
1965 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1966 stream
, bt_stream_get_name(stream
));
1968 /* Call destroy listeners in reverse registration order */
1969 for (i
= stream
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
1970 struct bt_stream_destroy_listener
*listener
=
1971 &g_array_index(stream
->destroy_listeners
,
1972 struct bt_stream_destroy_listener
, i
);
1974 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1975 listener
->func
, listener
->data
, i
);
1976 listener
->func(stream
, listener
->data
);
1979 (void) bt_stream_pos_fini(&stream
->pos
);
1980 if (stream
->pos
.fd
>= 0) {
1984 * Truncate the file's size to the minimum required to fit the
1985 * last packet as we might have grown it too much on the last
1989 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1990 } while (ret
== -1 && errno
== EINTR
);
1992 BT_LOGE_ERRNO("Failed to truncate stream file",
1993 ": ret=%d, size=%" PRIu64
,
1994 ret
, (uint64_t) stream
->size
);
1997 if (close(stream
->pos
.fd
)) {
1998 BT_LOGE_ERRNO("Failed to close stream file",
2003 if (stream
->events
) {
2004 BT_LOGD_STR("Putting events.");
2005 g_ptr_array_free(stream
->events
, TRUE
);
2009 g_string_free(stream
->name
, TRUE
);
2012 if (stream
->comp_cur_port
) {
2013 GHashTableIter ht_iter
;
2014 gpointer comp_gptr
, port_gptr
;
2017 * Since we're destroying the stream, remove the destroy
2018 * listeners that it registered for each component in
2019 * its component-port mapping hash table. Otherwise they
2020 * would be called and the stream would be accessed once
2021 * it's freed or another stream would be accessed.
2023 g_hash_table_iter_init(&ht_iter
, stream
->comp_cur_port
);
2025 while (g_hash_table_iter_next(&ht_iter
, &comp_gptr
, &port_gptr
)) {
2026 BT_ASSERT(comp_gptr
);
2027 bt_component_remove_destroy_listener((void *) comp_gptr
,
2028 component_destroy_listener
, stream
);
2031 g_hash_table_destroy(stream
->comp_cur_port
);
2034 if (stream
->destroy_listeners
) {
2035 g_array_free(stream
->destroy_listeners
, TRUE
);
2038 BT_LOGD_STR("Putting packet header field.");
2039 bt_put(stream
->packet_header
);
2040 BT_LOGD_STR("Putting packet context field.");
2041 bt_put(stream
->packet_context
);
2046 int _set_structure_field_integer(struct bt_field
*structure
, char *name
,
2047 uint64_t value
, bt_bool force
)
2050 struct bt_field_type
*field_type
= NULL
;
2051 struct bt_field
*integer
;
2053 BT_ASSERT(structure
);
2056 integer
= bt_field_structure_get_field_by_name(structure
, name
);
2058 /* Field not found, not an error. */
2059 BT_LOGV("Field not found: struct-field-addr=%p, "
2060 "name=\"%s\", force=%d", structure
, name
, force
);
2064 /* Make sure the payload has not already been set. */
2065 if (!force
&& bt_field_is_set_recursive(integer
)) {
2066 /* Payload already set, not an error */
2067 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
2068 "name=\"%s\", force=%d", structure
, name
, force
);
2072 field_type
= bt_field_get_type(integer
);
2073 BT_ASSERT(field_type
);
2074 if (bt_field_type_get_type_id(field_type
) != BT_FIELD_TYPE_ID_INTEGER
) {
2076 * The user most likely meant for us to populate this field
2077 * automatically. However, we can only do this if the field
2078 * is an integer. Return an error.
2080 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
2081 "field-addr=%p, ft-addr=%p, ft-id=%s",
2082 integer
, field_type
,
2083 bt_field_type_id_string(field_type
->id
));
2088 if (bt_field_type_integer_is_signed(field_type
)) {
2089 ret
= bt_field_signed_integer_set_value(integer
,
2092 ret
= bt_field_unsigned_integer_set_value(integer
, value
);
2094 ret
= !ret
? 1 : ret
;
2102 * Returns the following codes:
2103 * 1 if the field was found and set,
2104 * 0 if nothing was done (field not found, or was already set),
2105 * <0 if an error was encoutered
2108 int try_set_structure_field_integer(struct bt_field
*structure
, char *name
,
2111 return _set_structure_field_integer(structure
, name
, value
, BT_FALSE
);
2114 const char *bt_stream_get_name(struct bt_stream
*stream
)
2116 const char *name
= NULL
;
2119 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2123 name
= stream
->name
? stream
->name
->str
: NULL
;
2129 int bt_stream_is_writer(struct bt_stream
*stream
)
2134 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2138 ret
= (stream
->pos
.fd
>= 0);
2145 void bt_stream_map_component_to_port(struct bt_stream
*stream
,
2146 struct bt_component
*comp
,
2147 struct bt_port
*port
)
2152 BT_ASSERT(stream
->comp_cur_port
);
2155 * Do not take a reference to the component here because we
2156 * don't want the component to exist as long as this stream
2157 * exists. Instead, keep a weak reference, but add a destroy
2158 * listener so that we remove this hash table entry when we know
2159 * the component is destroyed.
2161 BT_LOGV("Adding component's destroy listener for stream: "
2162 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
2163 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2164 stream
, bt_stream_get_name(stream
),
2165 comp
, bt_component_get_name(comp
), port
,
2166 bt_port_get_name(port
));
2167 bt_component_add_destroy_listener(comp
, component_destroy_listener
,
2169 g_hash_table_insert(stream
->comp_cur_port
, comp
, port
);
2170 BT_LOGV_STR("Mapped component to port for stream.");
2174 struct bt_port
*bt_stream_port_for_component(struct bt_stream
*stream
,
2175 struct bt_component
*comp
)
2179 BT_ASSERT(stream
->comp_cur_port
);
2180 return g_hash_table_lookup(stream
->comp_cur_port
, comp
);
2184 void bt_stream_add_destroy_listener(struct bt_stream
*stream
,
2185 bt_stream_destroy_listener_func func
, void *data
)
2187 struct bt_stream_destroy_listener listener
;
2191 listener
.func
= func
;
2192 listener
.data
= data
;
2193 g_array_append_val(stream
->destroy_listeners
, listener
);
2194 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
2195 "stream-name=\"%s\", func=%p, data=%p",
2196 stream
, bt_stream_get_name(stream
), func
, data
);
2200 void bt_stream_remove_destroy_listener(struct bt_stream
*stream
,
2201 bt_stream_destroy_listener_func func
, void *data
)
2208 for (i
= 0; i
< stream
->destroy_listeners
->len
; i
++) {
2209 struct bt_stream_destroy_listener
*listener
=
2210 &g_array_index(stream
->destroy_listeners
,
2211 struct bt_stream_destroy_listener
, i
);
2213 if (listener
->func
== func
&& listener
->data
== data
) {
2214 g_array_remove_index(stream
->destroy_listeners
, i
);
2216 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
2217 "stream-name=\"%s\", func=%p, data=%p",
2218 stream
, bt_stream_get_name(stream
),
2224 int64_t bt_stream_get_id(struct bt_stream
*stream
)
2229 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2236 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
2237 stream
, bt_stream_get_name(stream
));