2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
8 #define BT_LOG_TAG "CTF-WRITER/STREAM"
16 #include <babeltrace2-ctf-writer/field-types.h>
17 #include <babeltrace2-ctf-writer/object.h>
18 #include <babeltrace2-ctf-writer/stream-class.h>
19 #include <babeltrace2-ctf-writer/stream.h>
20 #include <babeltrace2-ctf-writer/trace.h>
22 #include "common/align.h"
23 #include "common/assert.h"
24 #include "compat/compiler.h"
25 #include "ctfser/ctfser.h"
27 #include "assert-pre.h"
28 #include "event-class.h"
31 #include "stream-class.h"
36 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common
*stream
)
38 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
39 stream
, bt_ctf_stream_common_get_name(stream
));
42 g_string_free(stream
->name
, TRUE
);
46 int bt_ctf_stream_common_initialize(
47 struct bt_ctf_stream_common
*stream
,
48 struct bt_ctf_stream_class_common
*stream_class
, const char *name
,
49 uint64_t id
, bt_ctf_object_release_func release_func
)
52 struct bt_ctf_trace_common
*trace
= NULL
;
54 bt_ctf_object_init_shared_with_parent(&stream
->base
, release_func
);
57 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
61 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
62 "stream-class-name=\"%s\", stream-name=\"%s\", "
64 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
66 trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
68 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
69 "stream-class-addr=%p, stream-class-name=\"%s\", "
72 bt_ctf_stream_class_common_get_name(stream_class
), name
);
78 * Validate that the given ID is unique amongst all the
79 * existing trace's streams created from the same stream
84 for (i
= 0; i
< trace
->streams
->len
; i
++) {
85 struct bt_ctf_stream_common
*trace_stream
=
86 g_ptr_array_index(trace
->streams
, i
);
88 if (trace_stream
->stream_class
!= (void *) stream_class
) {
92 if (trace_stream
->id
== id
) {
93 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
100 * Acquire reference to parent since stream will become publicly
101 * reachable; it needs its parent to remain valid.
103 bt_ctf_object_set_parent(&stream
->base
, &trace
->base
);
104 stream
->stream_class
= stream_class
;
105 stream
->id
= (int64_t) id
;
108 stream
->name
= g_string_new(name
);
110 BT_LOGE_STR("Failed to allocate a GString.");
115 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace
);
117 /* Add this stream to the trace's streams */
118 BT_LOGD("Created common stream object: addr=%p", stream
);
129 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
);
132 int try_set_structure_field_integer(struct bt_ctf_field
*, const char *, uint64_t);
135 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
138 struct bt_ctf_field_type
*field_type
= NULL
;
141 BT_LOGW_STR("Invalid parameter: field is NULL.");
146 field_type
= bt_ctf_field_get_type(field
);
147 BT_ASSERT_DBG(field_type
);
149 if (bt_ctf_field_type_get_type_id(field_type
) !=
150 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
151 /* Not an integer and the value is unset, error. */
152 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
153 "field-addr=%p, ft-addr=%p, ft-id=%s",
155 bt_ctf_field_type_id_string((int)
156 bt_ctf_field_type_get_type_id(field_type
)));
161 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
162 ret
= bt_ctf_field_integer_signed_set_value(field
, (int64_t) value
);
164 /* Value is out of range, error. */
165 BT_LOGW("Cannot set signed integer field's value: "
166 "addr=%p, value=%" PRId64
,
167 field
, (int64_t) value
);
171 ret
= bt_ctf_field_integer_unsigned_set_value(field
, value
);
173 /* Value is out of range, error. */
174 BT_LOGW("Cannot set unsigned integer field's value: "
175 "addr=%p, value=%" PRIu64
,
181 bt_ctf_object_put_ref(field_type
);
186 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
189 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field_by_name(
190 stream
->packet_header
, "magic");
191 const uint32_t magic_value
= 0xc1fc1fc1;
193 BT_ASSERT_DBG(stream
);
196 /* No magic field found. Not an error, skip. */
197 BT_LOGT("No field named `magic` in packet header: skipping: "
198 "stream-addr=%p, stream-name=\"%s\"",
199 stream
, bt_ctf_stream_get_name(stream
));
203 ret
= bt_ctf_field_integer_unsigned_set_value(magic_field
,
204 (uint64_t) magic_value
);
207 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
208 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
209 stream
, bt_ctf_stream_get_name(stream
),
210 magic_field
, (uint64_t) magic_value
);
212 BT_LOGT("Set packet header field's `magic` field's value: "
213 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
214 stream
, bt_ctf_stream_get_name(stream
),
215 magic_field
, (uint64_t) magic_value
);
218 bt_ctf_object_put_ref(magic_field
);
223 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
227 struct bt_ctf_trace
*trace
= NULL
;
228 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field_by_name(
229 stream
->packet_header
, "uuid");
231 BT_ASSERT_DBG(stream
);
234 /* No uuid field found. Not an error, skip. */
235 BT_LOGT("No field named `uuid` in packet header: skipping: "
236 "stream-addr=%p, stream-name=\"%s\"",
237 stream
, bt_ctf_stream_get_name(stream
));
241 trace
= (struct bt_ctf_trace
*)
242 bt_ctf_object_get_parent(&stream
->common
.base
);
244 for (i
= 0; i
< 16; i
++) {
245 struct bt_ctf_field
*uuid_element
=
246 bt_ctf_field_array_get_field(uuid_field
, i
);
248 ret
= bt_ctf_field_integer_unsigned_set_value(
249 uuid_element
, (uint64_t) trace
->common
.uuid
[i
]);
250 bt_ctf_object_put_ref(uuid_element
);
252 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
253 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
254 "value=%" PRIu64
", index=%" PRId64
,
255 stream
, bt_ctf_stream_get_name(stream
),
256 uuid_element
, (uint64_t) trace
->common
.uuid
[i
], i
);
261 BT_LOGT("Set packet header field's `uuid` field's value: "
262 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
263 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
266 bt_ctf_object_put_ref(uuid_field
);
267 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
271 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
275 struct bt_ctf_field
*stream_id_field
=
276 bt_ctf_field_structure_get_field_by_name(
277 stream
->packet_header
, "stream_id");
279 if (!stream_id_field
) {
280 /* No stream_id field found. Not an error, skip. */
281 BT_LOGT("No field named `stream_id` in packet header: skipping: "
282 "stream-addr=%p, stream-name=\"%s\"",
283 stream
, bt_ctf_stream_get_name(stream
));
287 stream_id
= stream
->common
.stream_class
->id
;
288 ret
= bt_ctf_field_integer_unsigned_set_value(stream_id_field
,
289 (uint64_t) stream_id
);
291 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
292 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
293 stream
, bt_ctf_stream_get_name(stream
),
294 stream_id_field
, (uint64_t) stream_id
);
296 BT_LOGT("Set packet header field's `stream_id` field's value: "
297 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
298 stream
, bt_ctf_stream_get_name(stream
),
299 stream_id_field
, (uint64_t) stream_id
);
303 bt_ctf_object_put_ref(stream_id_field
);
308 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
312 if (!stream
->packet_header
) {
316 ret
= set_packet_header_magic(stream
);
318 BT_LOGW("Cannot set packet header's magic number field: "
319 "stream-addr=%p, stream-name=\"%s\"",
320 stream
, bt_ctf_stream_get_name(stream
));
324 ret
= set_packet_header_uuid(stream
);
326 BT_LOGW("Cannot set packet header's UUID field: "
327 "stream-addr=%p, stream-name=\"%s\"",
328 stream
, bt_ctf_stream_get_name(stream
));
332 ret
= set_packet_header_stream_id(stream
);
334 BT_LOGW("Cannot set packet header's stream class ID field: "
335 "stream-addr=%p, stream-name=\"%s\"",
336 stream
, bt_ctf_stream_get_name(stream
));
340 BT_LOGT("Automatically populated stream's packet header's known fields: "
341 "stream-addr=%p, stream-name=\"%s\"",
342 stream
, bt_ctf_stream_get_name(stream
));
349 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
,
350 uint64_t packet_size_bits
)
353 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
354 stream
->packet_context
, "packet_size");
356 ret
= bt_ctf_field_integer_unsigned_set_value(field
, packet_size_bits
);
358 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
359 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
360 stream
, bt_ctf_stream_get_name(stream
),
361 field
, packet_size_bits
);
363 BT_LOGT("Set packet context field's `packet_size` field's value: "
364 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
365 stream
, bt_ctf_stream_get_name(stream
),
366 field
, packet_size_bits
);
369 bt_ctf_object_put_ref(field
);
374 int set_packet_context_content_size(struct bt_ctf_stream
*stream
,
375 uint64_t content_size_bits
)
378 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
379 stream
->packet_context
, "content_size");
381 BT_ASSERT_DBG(stream
);
384 /* No content size field found. Not an error, skip. */
385 BT_LOGT("No field named `content_size` in packet context: skipping: "
386 "stream-addr=%p, stream-name=\"%s\"",
387 stream
, bt_ctf_stream_get_name(stream
));
391 ret
= bt_ctf_field_integer_unsigned_set_value(field
, content_size_bits
);
393 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
394 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
395 stream
, bt_ctf_stream_get_name(stream
),
396 field
, content_size_bits
);
398 BT_LOGT("Set packet context field's `content_size` field's value: "
399 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
400 stream
, bt_ctf_stream_get_name(stream
),
401 field
, content_size_bits
);
405 bt_ctf_object_put_ref(field
);
410 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
413 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
414 stream
->packet_context
, "events_discarded");
416 BT_ASSERT_DBG(stream
);
419 /* No discarded events count field found. Not an error, skip. */
420 BT_LOGT("No field named `events_discarded` in packet context: skipping: "
421 "stream-addr=%p, stream-name=\"%s\"",
422 stream
, bt_ctf_stream_get_name(stream
));
427 * If the field is set by the user, make sure that the value is
428 * greater than or equal to the stream's current count of
429 * discarded events. We do not allow wrapping here. If it's
430 * valid, update the stream's current count.
432 if (bt_ctf_field_is_set_recursive(field
)) {
435 ret
= bt_ctf_field_integer_unsigned_get_value(field
,
438 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
439 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
440 stream
, bt_ctf_stream_get_name(stream
), field
);
444 if (user_val
< stream
->discarded_events
) {
445 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
446 "value is lesser than the stream's current discarded events count: "
447 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
448 "value=%" PRIu64
", "
449 "stream-discarded-events-count=%" PRIu64
,
450 stream
, bt_ctf_stream_get_name(stream
), field
,
451 user_val
, stream
->discarded_events
);
455 stream
->discarded_events
= user_val
;
457 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
458 stream
->discarded_events
);
460 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
461 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
462 stream
, bt_ctf_stream_get_name(stream
),
463 field
, stream
->discarded_events
);
465 BT_LOGT("Set packet context field's `events_discarded` field's value: "
466 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
467 stream
, bt_ctf_stream_get_name(stream
),
468 field
, stream
->discarded_events
);
473 bt_ctf_object_put_ref(field
);
478 void update_clock_value(uint64_t *val
, uint64_t new_val
,
479 unsigned int new_val_size
)
481 const uint64_t pow2
= new_val_size
== 64 ? 0 : 1ULL << new_val_size
;
482 const uint64_t mask
= pow2
- 1;
485 #ifdef BT_LOG_ENABLED_TRACE
486 uint64_t old_val
= *val
;
489 if (new_val_size
== 64) {
494 val_masked
= *val
& mask
;
496 if (new_val
< val_masked
) {
505 BT_LOGT("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
511 int visit_field_update_clock_value(struct bt_ctf_field
*field
, uint64_t *val
)
514 struct bt_ctf_field_common
*field_common
= (void *) field
;
520 switch (bt_ctf_field_get_type_id(field
)) {
521 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
523 struct bt_ctf_clock_class
*cc
=
524 bt_ctf_field_type_integer_get_mapped_clock_class(
525 (void *) field_common
->type
);
533 bt_ctf_object_put_ref(cc
);
534 val_size
= bt_ctf_field_type_integer_get_size(
535 (void *) field_common
->type
);
536 BT_ASSERT_DBG(val_size
>= 1);
538 if (bt_ctf_field_type_integer_is_signed(
539 (void *) field_common
->type
)) {
542 ret
= bt_ctf_field_integer_signed_get_value(field
, &ival
);
543 uval
= (uint64_t) ival
;
545 ret
= bt_ctf_field_integer_unsigned_get_value(field
, &uval
);
553 update_clock_value(val
, uval
, val_size
);
556 case BT_CTF_FIELD_TYPE_ID_ENUM
:
558 struct bt_ctf_field
*int_field
=
559 bt_ctf_field_enumeration_get_container(field
);
561 BT_ASSERT_DBG(int_field
);
562 ret
= visit_field_update_clock_value(int_field
, val
);
563 bt_ctf_object_put_ref(int_field
);
566 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
569 int64_t len
= bt_ctf_field_type_array_get_length(
570 (void *) field_common
->type
);
572 BT_ASSERT_DBG(len
>= 0);
574 for (i
= 0; i
< len
; i
++) {
575 struct bt_ctf_field
*elem_field
=
576 bt_ctf_field_array_get_field(field
, i
);
578 BT_ASSERT_DBG(elem_field
);
579 ret
= visit_field_update_clock_value(elem_field
, val
);
580 bt_ctf_object_put_ref(elem_field
);
587 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
590 int64_t len
= bt_ctf_field_common_sequence_get_length(
598 for (i
= 0; i
< len
; i
++) {
599 struct bt_ctf_field
*elem_field
=
600 bt_ctf_field_sequence_get_field(field
, i
);
602 BT_ASSERT_DBG(elem_field
);
603 ret
= visit_field_update_clock_value(elem_field
, val
);
604 bt_ctf_object_put_ref(elem_field
);
611 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
614 int64_t len
= bt_ctf_field_type_structure_get_field_count(
615 (void *) field_common
->type
);
617 BT_ASSERT_DBG(len
>= 0);
619 for (i
= 0; i
< len
; i
++) {
620 struct bt_ctf_field
*member_field
=
621 bt_ctf_field_structure_get_field_by_index(field
, i
);
623 BT_ASSERT_DBG(member_field
);
624 ret
= visit_field_update_clock_value(member_field
, val
);
625 bt_ctf_object_put_ref(member_field
);
632 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
634 struct bt_ctf_field
*cur_field
=
635 bt_ctf_field_variant_get_current_field(field
);
642 ret
= visit_field_update_clock_value(cur_field
, val
);
643 bt_ctf_object_put_ref(cur_field
);
655 int visit_event_update_clock_value(struct bt_ctf_event
*event
, uint64_t *val
)
658 struct bt_ctf_field
*field
;
660 field
= bt_ctf_event_get_header(event
);
661 ret
= visit_field_update_clock_value(field
, val
);
662 bt_ctf_object_put_ref(field
);
664 BT_LOGW_STR("Cannot automatically update clock value in "
669 field
= bt_ctf_event_get_stream_event_context(event
);
670 ret
= visit_field_update_clock_value(field
, val
);
671 bt_ctf_object_put_ref(field
);
673 BT_LOGW_STR("Cannot automatically update clock value in "
674 "event's stream event context.");
678 field
= bt_ctf_event_get_context(event
);
679 ret
= visit_field_update_clock_value(field
, val
);
680 bt_ctf_object_put_ref(field
);
682 BT_LOGW_STR("Cannot automatically update clock value in "
687 field
= bt_ctf_event_get_payload_field(event
);
688 ret
= visit_field_update_clock_value(field
, val
);
689 bt_ctf_object_put_ref(field
);
691 BT_LOGW_STR("Cannot automatically update clock value in "
701 int set_packet_context_timestamps(struct bt_ctf_stream
*stream
)
705 uint64_t cur_clock_value
;
706 uint64_t init_clock_value
= 0;
707 struct bt_ctf_field
*ts_begin_field
= bt_ctf_field_structure_get_field_by_name(
708 stream
->packet_context
, "timestamp_begin");
709 struct bt_ctf_field
*ts_end_field
= bt_ctf_field_structure_get_field_by_name(
710 stream
->packet_context
, "timestamp_end");
711 struct bt_ctf_field_common
*packet_context
=
712 (void *) stream
->packet_context
;
716 if (ts_begin_field
&& bt_ctf_field_is_set_recursive(ts_begin_field
)) {
717 /* Use provided `timestamp_begin` value as starting value */
718 ret
= bt_ctf_field_integer_unsigned_get_value(ts_begin_field
, &val
);
719 BT_ASSERT_DBG(ret
== 0);
720 init_clock_value
= val
;
721 } else if (stream
->last_ts_end
!= -1ULL) {
722 /* Use last packet's ending timestamp as starting value */
723 init_clock_value
= stream
->last_ts_end
;
726 cur_clock_value
= init_clock_value
;
728 if (stream
->last_ts_end
!= -1ULL &&
729 cur_clock_value
< stream
->last_ts_end
) {
730 BT_LOGW("Packet's initial timestamp is less than previous "
731 "packet's final timestamp: "
732 "stream-addr=%p, stream-name=\"%s\", "
733 "cur-packet-ts-begin=%" PRIu64
", "
734 "prev-packet-ts-end=%" PRIu64
,
735 stream
, bt_ctf_stream_get_name(stream
),
736 cur_clock_value
, stream
->last_ts_end
);
742 * Visit all the packet context fields, followed by all the
743 * fields of all the events, in order, updating our current
744 * clock value as we visit.
746 * While visiting the packet context fields, do not consider
747 * `timestamp_begin` and `timestamp_end` because this function's
748 * purpose is to set them anyway. Also do not consider
749 * `packet_size`, `content_size`, `events_discarded`, and
750 * `packet_seq_num` if they are not set because those are
751 * autopopulating fields.
753 len
= bt_ctf_field_type_structure_get_field_count(
754 (void *) packet_context
->type
);
755 BT_ASSERT_DBG(len
>= 0);
757 for (i
= 0; i
< len
; i
++) {
758 const char *member_name
;
759 struct bt_ctf_field
*member_field
;
761 ret
= bt_ctf_field_type_structure_get_field_by_index(
762 (void *) packet_context
->type
, &member_name
, NULL
, i
);
763 BT_ASSERT_DBG(ret
== 0);
765 if (strcmp(member_name
, "timestamp_begin") == 0 ||
766 strcmp(member_name
, "timestamp_end") == 0) {
770 member_field
= bt_ctf_field_structure_get_field_by_index(
771 stream
->packet_context
, i
);
772 BT_ASSERT_DBG(member_field
);
774 if (strcmp(member_name
, "packet_size") == 0 &&
775 !bt_ctf_field_is_set_recursive(member_field
)) {
776 bt_ctf_object_put_ref(member_field
);
780 if (strcmp(member_name
, "content_size") == 0 &&
781 !bt_ctf_field_is_set_recursive(member_field
)) {
782 bt_ctf_object_put_ref(member_field
);
786 if (strcmp(member_name
, "events_discarded") == 0 &&
787 !bt_ctf_field_is_set_recursive(member_field
)) {
788 bt_ctf_object_put_ref(member_field
);
792 if (strcmp(member_name
, "packet_seq_num") == 0 &&
793 !bt_ctf_field_is_set_recursive(member_field
)) {
794 bt_ctf_object_put_ref(member_field
);
798 ret
= visit_field_update_clock_value(member_field
,
800 bt_ctf_object_put_ref(member_field
);
802 BT_LOGW("Cannot automatically update clock value "
803 "in stream's packet context: "
804 "stream-addr=%p, stream-name=\"%s\", "
806 stream
, bt_ctf_stream_get_name(stream
),
812 for (i
= 0; i
< stream
->events
->len
; i
++) {
813 struct bt_ctf_event
*event
= g_ptr_array_index(stream
->events
, i
);
815 BT_ASSERT_DBG(event
);
816 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
818 BT_LOGW("Cannot automatically update clock value "
819 "in stream's packet context: "
820 "stream-addr=%p, stream-name=\"%s\", "
821 "index=%" PRIu64
", event-addr=%p, "
822 "event-class-id=%" PRId64
", "
823 "event-class-name=\"%s\"",
824 stream
, bt_ctf_stream_get_name(stream
),
826 bt_ctf_event_class_common_get_id(event
->common
.class),
827 bt_ctf_event_class_common_get_name(event
->common
.class));
833 * Everything is visited, thus the current clock value
834 * corresponds to the ending timestamp. Validate this value
835 * against the provided value of `timestamp_end`, if any,
838 if (ts_end_field
&& bt_ctf_field_is_set_recursive(ts_end_field
)) {
839 ret
= bt_ctf_field_integer_unsigned_get_value(ts_end_field
, &val
);
840 BT_ASSERT_DBG(ret
== 0);
842 if (val
< cur_clock_value
) {
843 BT_LOGW("Packet's final timestamp is less than "
844 "computed packet's final timestamp: "
845 "stream-addr=%p, stream-name=\"%s\", "
846 "cur-packet-ts-end=%" PRIu64
", "
847 "computed-packet-ts-end=%" PRIu64
,
848 stream
, bt_ctf_stream_get_name(stream
),
849 val
, cur_clock_value
);
854 stream
->last_ts_end
= val
;
857 if (ts_end_field
&& !bt_ctf_field_is_set_recursive(ts_end_field
)) {
858 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
859 BT_ASSERT_DBG(ret
== 0);
860 stream
->last_ts_end
= cur_clock_value
;
864 stream
->last_ts_end
= cur_clock_value
;
867 /* Set `timestamp_begin` field to initial clock value */
868 if (ts_begin_field
&& !bt_ctf_field_is_set_recursive(ts_begin_field
)) {
869 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
870 BT_ASSERT_DBG(ret
== 0);
874 bt_ctf_object_put_ref(ts_begin_field
);
875 bt_ctf_object_put_ref(ts_end_field
);
880 int auto_populate_packet_context(struct bt_ctf_stream
*stream
, bool set_ts
,
881 uint64_t packet_size_bits
, uint64_t content_size_bits
)
885 if (!stream
->packet_context
) {
889 ret
= set_packet_context_packet_size(stream
, packet_size_bits
);
891 BT_LOGW("Cannot set packet context's packet size field: "
892 "stream-addr=%p, stream-name=\"%s\"",
893 stream
, bt_ctf_stream_get_name(stream
));
897 ret
= set_packet_context_content_size(stream
, content_size_bits
);
899 BT_LOGW("Cannot set packet context's content size field: "
900 "stream-addr=%p, stream-name=\"%s\"",
901 stream
, bt_ctf_stream_get_name(stream
));
906 ret
= set_packet_context_timestamps(stream
);
908 BT_LOGW("Cannot set packet context's timestamp fields: "
909 "stream-addr=%p, stream-name=\"%s\"",
910 stream
, bt_ctf_stream_get_name(stream
));
915 ret
= set_packet_context_events_discarded(stream
);
917 BT_LOGW("Cannot set packet context's discarded events count field: "
918 "stream-addr=%p, stream-name=\"%s\"",
919 stream
, bt_ctf_stream_get_name(stream
));
923 BT_LOGT("Automatically populated stream's packet context's known fields: "
924 "stream-addr=%p, stream-name=\"%s\"",
925 stream
, bt_ctf_stream_get_name(stream
));
932 void release_event(struct bt_ctf_event
*event
)
934 if (bt_ctf_object_get_ref_count(&event
->common
.base
)) {
936 * The event is being orphaned, but it must guarantee the
937 * existence of its event class for the duration of its
940 bt_ctf_object_get_ref(event
->common
.class);
941 BT_CTF_OBJECT_PUT_REF_AND_RESET(event
->common
.base
.parent
);
943 bt_ctf_object_try_spec_release(&event
->common
.base
);
948 int create_stream_file(struct bt_ctf_writer
*writer
,
949 struct bt_ctf_stream
*stream
)
952 GString
*filename
= g_string_new(NULL
);
953 int64_t stream_class_id
;
954 char *file_path
= NULL
;
956 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
957 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
958 writer
, stream
, bt_ctf_stream_get_name(stream
),
959 stream
->common
.stream_class
,
960 stream
->common
.stream_class
->name
->str
);
962 if (stream
->common
.name
&& stream
->common
.name
->len
> 0) {
963 /* Use stream name's base name as prefix */
964 gchar
*basename
= g_path_get_basename(stream
->common
.name
->str
);
966 BT_ASSERT_DBG(basename
);
968 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
969 g_string_assign(filename
, "stream");
971 g_string_assign(filename
, basename
);
978 if (stream
->common
.stream_class
->name
&&
979 stream
->common
.stream_class
->name
->len
> 0) {
980 /* Use stream class name's base name as prefix */
983 stream
->common
.stream_class
->name
->str
);
985 BT_ASSERT_DBG(basename
);
987 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
988 g_string_assign(filename
, "stream");
990 g_string_assign(filename
, basename
);
997 /* Default to using `stream-` as prefix */
998 g_string_assign(filename
, "stream");
1001 stream_class_id
= bt_ctf_stream_class_common_get_id(stream
->common
.stream_class
);
1002 BT_ASSERT_DBG(stream_class_id
>= 0);
1003 BT_ASSERT_DBG(stream
->common
.id
>= 0);
1004 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
1005 stream_class_id
, stream
->common
.id
);
1007 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
1013 ret
= bt_ctfser_init(&stream
->ctfser
, file_path
,
1014 BT_LOG_OUTPUT_LEVEL
);
1017 /* bt_ctfser_init() logs errors */
1021 BT_LOGD("Created stream file for writing: "
1022 "stream-addr=%p, stream-name=\"%s\", "
1023 "filename=\"%s\"", stream
, bt_ctf_stream_get_name(stream
),
1027 g_string_free(filename
, TRUE
);
1031 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
1032 struct bt_ctf_stream_class
*stream_class
,
1033 const char *name
, uint64_t id
)
1037 struct bt_ctf_stream
*stream
= NULL
;
1038 struct bt_ctf_trace
*trace
= NULL
;
1039 struct bt_ctf_writer
*writer
= NULL
;
1041 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1042 "stream-class-name=\"%s\", stream-name=\"%s\", "
1043 "stream-id=%" PRIu64
,
1044 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1046 stream
= g_new0(struct bt_ctf_stream
, 1);
1048 BT_LOGE_STR("Failed to allocate one stream.");
1053 id
= stream_class
->next_stream_id
;
1056 ret
= bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream
),
1057 BT_CTF_TO_COMMON(stream_class
), name
, id
, bt_ctf_stream_destroy
);
1059 /* bt_ctf_stream_common_initialize() logs errors */
1063 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1064 BT_CTF_TO_COMMON(stream_class
)));
1066 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1067 "stream-class-addr=%p, stream-class-name=\"%s\", "
1068 "stream-name=\"%s\"",
1069 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1074 writer
= (struct bt_ctf_writer
*)
1075 bt_ctf_object_get_parent(&trace
->common
.base
);
1076 stream
->last_ts_end
= -1ULL;
1077 BT_LOGD("CTF writer stream object belongs writer's trace: "
1078 "writer-addr=%p", writer
);
1079 BT_ASSERT_DBG(writer
);
1081 if (stream_class
->common
.packet_context_field_type
) {
1082 BT_LOGD("Creating stream's packet context field: "
1084 stream_class
->common
.packet_context_field_type
);
1085 stream
->packet_context
= bt_ctf_field_create(
1086 (void *) stream_class
->common
.packet_context_field_type
);
1087 if (!stream
->packet_context
) {
1088 BT_LOGW_STR("Cannot create stream's packet context field.");
1092 /* Initialize events_discarded */
1093 ret
= try_set_structure_field_integer(
1094 stream
->packet_context
, "events_discarded", 0);
1096 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1097 "ret=%d, packet-context-field-addr=%p",
1098 ret
, stream
->packet_context
);
1103 stream
->events
= g_ptr_array_new_with_free_func(
1104 (GDestroyNotify
) release_event
);
1105 if (!stream
->events
) {
1106 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1110 if (trace
->common
.packet_header_field_type
) {
1111 BT_LOGD("Creating stream's packet header field: "
1112 "ft-addr=%p", trace
->common
.packet_header_field_type
);
1113 stream
->packet_header
=
1114 bt_ctf_field_create(
1115 (void *) trace
->common
.packet_header_field_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 /* Freeze the writer */
1144 BT_LOGD_STR("Freezing stream's CTF writer.");
1145 bt_ctf_writer_freeze(writer
);
1147 /* Add this stream to the trace's streams */
1148 g_ptr_array_add(trace
->common
.streams
, stream
);
1149 stream_class
->next_stream_id
++;
1150 BT_LOGD("Created stream object: addr=%p", stream
);
1154 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
1157 bt_ctf_object_put_ref(writer
);
1162 struct bt_ctf_stream
*bt_ctf_stream_create(
1163 struct bt_ctf_stream_class
*stream_class
,
1164 const char *name
, uint64_t id_param
)
1166 return bt_ctf_stream_create_with_id(stream_class
,
1171 int bt_ctf_stream_get_discarded_events_count(
1172 struct bt_ctf_stream
*stream
, uint64_t *count
)
1177 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1183 BT_LOGW_STR("Invalid parameter: count is NULL.");
1188 *count
= (uint64_t) stream
->discarded_events
;
1195 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1199 struct bt_ctf_field
*events_discarded_field
= NULL
;
1201 if (!stream
->packet_context
) {
1205 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1206 stream
->packet_context
, "events_discarded");
1207 if (!events_discarded_field
) {
1211 ret
= bt_ctf_field_integer_unsigned_set_value(
1212 events_discarded_field
, count
);
1214 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1215 "field-addr=%p, value=%" PRIu64
,
1216 events_discarded_field
, count
);
1221 bt_ctf_object_put_ref(events_discarded_field
);
1226 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1227 uint64_t event_count
)
1231 struct bt_ctf_field
*events_discarded_field
= NULL
;
1234 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1238 BT_LOGT("Appending discarded events to stream: "
1239 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1240 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1242 if (!stream
->packet_context
) {
1243 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1247 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1248 stream
->packet_context
, "events_discarded");
1249 if (!events_discarded_field
) {
1250 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1254 new_count
= stream
->discarded_events
+ event_count
;
1255 if (new_count
< stream
->discarded_events
) {
1256 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1257 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1258 stream
->discarded_events
, new_count
);
1262 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1264 /* set_packet_context_events_discarded_field() logs errors */
1268 stream
->discarded_events
= new_count
;
1269 BT_LOGT("Appended discarded events to stream: "
1270 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1271 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1274 bt_ctf_object_put_ref(events_discarded_field
);
1277 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1278 struct bt_ctf_event
*event
)
1281 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1282 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1283 struct bt_ctf_stream_class
*stream_class
=
1284 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1285 BT_CTF_TO_COMMON(stream
)));
1286 int64_t event_class_id
;
1288 BT_ASSERT_DBG(event
);
1290 if (!event
->common
.header_field
) {
1294 if (event
->common
.frozen
) {
1295 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1300 BT_LOGT("Automatically populating event's header field: "
1301 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1302 stream
, bt_ctf_stream_get_name(stream
), event
);
1304 id_field
= bt_ctf_field_structure_get_field_by_name(
1305 (void *) event
->common
.header_field
->field
, "id");
1306 event_class_id
= bt_ctf_event_class_common_get_id(event
->common
.class);
1307 BT_ASSERT_DBG(event_class_id
>= 0);
1309 if (id_field
&& bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1310 ret
= set_integer_field_value(id_field
, event_class_id
);
1312 BT_LOGW("Cannot set event header's `id` field's value: "
1313 "addr=%p, value=%" PRIu64
, id_field
,
1320 * The conditions to automatically set the timestamp are:
1322 * 1. The event header field "timestamp" exists and is an
1324 * 2. This stream's class has a registered clock (set with
1325 * bt_ctf_stream_class_set_clock()).
1326 * 3. The "timestamp" field is not set.
1328 timestamp_field
= bt_ctf_field_structure_get_field_by_name(
1329 (void *) event
->common
.header_field
->field
, "timestamp");
1330 if (timestamp_field
&& stream_class
->clock
&&
1331 bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
&&
1332 !bt_ctf_field_is_set_recursive(timestamp_field
)) {
1333 mapped_clock_class
=
1334 bt_ctf_field_type_integer_get_mapped_clock_class(
1335 (void *) ((struct bt_ctf_field_common
*) timestamp_field
)->type
);
1336 if (mapped_clock_class
) {
1339 BT_ASSERT_DBG(mapped_clock_class
==
1340 stream_class
->clock
->clock_class
);
1341 ret
= bt_ctf_clock_get_value(
1342 stream_class
->clock
,
1344 BT_ASSERT_DBG(ret
== 0);
1345 ret
= set_integer_field_value(timestamp_field
,
1348 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1349 "addr=%p, value=%" PRIu64
,
1350 timestamp_field
, timestamp
);
1356 BT_LOGT("Automatically populated event's header field: "
1357 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1358 stream
, bt_ctf_stream_get_name(stream
), event
);
1361 bt_ctf_object_put_ref(id_field
);
1362 bt_ctf_object_put_ref(timestamp_field
);
1363 bt_ctf_object_put_ref(mapped_clock_class
);
1368 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1369 struct bt_ctf_event
*event
)
1374 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1380 BT_LOGW_STR("Invalid parameter: event is NULL.");
1385 BT_LOGT("Appending event to stream: "
1386 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1387 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1388 stream
, bt_ctf_stream_get_name(stream
), event
,
1389 bt_ctf_event_class_common_get_name(
1390 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1391 bt_ctf_event_class_common_get_id(
1392 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1395 * The event is not supposed to have a parent stream at this
1396 * point. The only other way an event can have a parent stream
1397 * is if it was assigned when setting a packet to the event,
1398 * in which case the packet's stream is not a writer stream,
1399 * and thus the user is trying to append an event which belongs
1400 * to another stream.
1402 if (event
->common
.base
.parent
) {
1407 bt_ctf_object_set_parent(&event
->common
.base
, &stream
->common
.base
);
1408 BT_LOGT_STR("Automatically populating the header of the event to append.");
1409 ret
= auto_populate_event_header(stream
, event
);
1411 /* auto_populate_event_header() reports errors */
1415 /* Make sure the various scopes of the event are set */
1416 BT_LOGT_STR("Validating event to append.");
1417 BT_CTF_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event
)) == 0,
1418 "Invalid event: event-addr=%p", event
);
1420 /* Save the new event and freeze it */
1421 BT_LOGT_STR("Freezing the event to append.");
1422 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event
), true);
1423 g_ptr_array_add(stream
->events
, event
);
1426 * Event had to hold a reference to its event class as long as it wasn't
1427 * part of the same trace hierarchy. From now on, the event and its
1428 * class share the same lifetime guarantees and the reference is no
1431 BT_LOGT_STR("Putting the event's class.");
1432 bt_ctf_object_put_ref(event
->common
.class);
1433 BT_LOGT("Appended event to stream: "
1434 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1435 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1436 stream
, bt_ctf_stream_get_name(stream
), event
,
1437 bt_ctf_event_class_common_get_name(
1438 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1439 bt_ctf_event_class_common_get_id(
1440 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1447 * Orphan the event; we were not successful in associating it to
1450 bt_ctf_object_set_parent(&event
->common
.base
, NULL
);
1455 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(struct bt_ctf_stream
*stream
)
1457 struct bt_ctf_field
*packet_context
= NULL
;
1460 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1464 packet_context
= stream
->packet_context
;
1465 if (packet_context
) {
1466 bt_ctf_object_get_ref(packet_context
);
1469 return packet_context
;
1473 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1474 struct bt_ctf_field
*field
)
1477 struct bt_ctf_field_type
*field_type
;
1480 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1485 field_type
= bt_ctf_field_get_type(field
);
1486 if (bt_ctf_field_type_common_compare((void *) field_type
,
1487 stream
->common
.stream_class
->packet_context_field_type
)) {
1488 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1489 "stream-addr=%p, stream-name=\"%s\", "
1490 "packet-context-field-addr=%p, "
1491 "packet-context-ft-addr=%p",
1492 stream
, bt_ctf_stream_get_name(stream
),
1498 bt_ctf_object_put_ref(field_type
);
1499 bt_ctf_object_put_ref(stream
->packet_context
);
1500 stream
->packet_context
= bt_ctf_object_get_ref(field
);
1501 BT_LOGT("Set stream's packet context field: "
1502 "stream-addr=%p, stream-name=\"%s\", "
1503 "packet-context-field-addr=%p",
1504 stream
, bt_ctf_stream_get_name(stream
), field
);
1510 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(struct bt_ctf_stream
*stream
)
1512 struct bt_ctf_field
*packet_header
= NULL
;
1515 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1519 packet_header
= stream
->packet_header
;
1520 if (packet_header
) {
1521 bt_ctf_object_get_ref(packet_header
);
1524 return packet_header
;
1528 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1529 struct bt_ctf_field
*field
)
1532 struct bt_ctf_trace
*trace
= NULL
;
1533 struct bt_ctf_field_type
*field_type
= NULL
;
1536 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1541 trace
= (struct bt_ctf_trace
*)
1542 bt_ctf_object_get_parent(&stream
->common
.base
);
1545 if (trace
->common
.packet_header_field_type
) {
1546 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1547 "stream-addr=%p, stream-name=\"%s\", "
1548 "packet-header-field-addr=%p, "
1549 "expected-ft-addr=%p",
1550 stream
, bt_ctf_stream_get_name(stream
),
1551 field
, trace
->common
.packet_header_field_type
);
1556 goto skip_validation
;
1559 field_type
= bt_ctf_field_get_type(field
);
1560 BT_ASSERT_DBG(field_type
);
1562 if (bt_ctf_field_type_common_compare((void *) field_type
,
1563 trace
->common
.packet_header_field_type
)) {
1564 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1565 "stream-addr=%p, stream-name=\"%s\", "
1566 "packet-header-field-addr=%p, "
1567 "packet-header-ft-addr=%p",
1568 stream
, bt_ctf_stream_get_name(stream
),
1575 bt_ctf_object_put_ref(stream
->packet_header
);
1576 stream
->packet_header
= bt_ctf_object_get_ref(field
);
1577 BT_LOGT("Set stream's packet header field: "
1578 "stream-addr=%p, stream-name=\"%s\", "
1579 "packet-header-field-addr=%p",
1580 stream
, bt_ctf_stream_get_name(stream
), field
);
1582 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1583 bt_ctf_object_put_ref(field_type
);
1588 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1590 struct bt_ctf_field
*member
;
1592 member
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1594 bt_ctf_field_common_reset_recursive((void *) member
);
1595 bt_ctf_object_put_ref(member
);
1600 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1604 uint64_t packet_context_offset_bits
= 0;
1605 struct bt_ctf_trace
*trace
;
1606 enum bt_ctf_byte_order native_byte_order
;
1607 bool has_packet_size
= false;
1608 uint64_t packet_size_bits
= 0;
1609 uint64_t content_size_bits
= 0;
1612 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1617 if (stream
->packet_context
) {
1618 struct bt_ctf_field
*packet_size_field
;
1620 packet_size_field
= bt_ctf_field_structure_get_field_by_name(
1621 stream
->packet_context
, "packet_size");
1622 has_packet_size
= packet_size_field
;
1623 bt_ctf_object_put_ref(packet_size_field
);
1626 if (stream
->flushed_packet_count
== 1) {
1627 if (!stream
->packet_context
) {
1628 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1633 if (!has_packet_size
) {
1634 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1640 BT_LOGT("Flushing stream's current packet: stream-addr=%p, "
1641 "stream-name=\"%s\", packet-index=%u", stream
,
1642 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1643 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1644 stream
->common
.stream_class
));
1645 BT_ASSERT_DBG(trace
);
1646 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1648 ret
= auto_populate_packet_header(stream
);
1650 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1655 /* Initialize packet/content sizes to `0`; we will overwrite later */
1656 ret
= auto_populate_packet_context(stream
, true, 0, 0);
1658 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1663 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
1665 /* bt_ctfser_open_packet() logs errors */
1670 if (stream
->packet_header
) {
1671 BT_LOGT_STR("Serializing packet header field (initial).");
1672 ret
= bt_ctf_field_serialize_recursive(stream
->packet_header
,
1673 &stream
->ctfser
, native_byte_order
);
1675 BT_LOGW("Cannot serialize stream's packet header field: "
1676 "field-addr=%p", stream
->packet_header
);
1681 if (stream
->packet_context
) {
1682 /* Save packet context's position to overwrite it later */
1683 packet_context_offset_bits
=
1684 bt_ctfser_get_offset_in_current_packet_bits(
1687 /* Write packet context */
1688 BT_LOGT_STR("Serializing packet context field (initial).");
1689 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1690 &stream
->ctfser
, native_byte_order
);
1692 BT_LOGW("Cannot serialize stream's packet context field: "
1693 "field-addr=%p", stream
->packet_context
);
1698 BT_LOGT("Serializing events: count=%u", stream
->events
->len
);
1700 for (i
= 0; i
< stream
->events
->len
; i
++) {
1701 struct bt_ctf_event
*event
= g_ptr_array_index(
1703 struct bt_ctf_event_class
*event_class
=
1704 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1705 BT_CTF_TO_COMMON(event
)));
1707 BT_LOGT("Serializing event: index=%zu, event-addr=%p, "
1708 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1709 "ser-offset=%" PRIu64
,
1710 i
, event
, bt_ctf_event_class_get_name(event_class
),
1711 bt_ctf_event_class_get_id(event_class
),
1712 bt_ctfser_get_offset_in_current_packet_bits(
1715 /* Write event header */
1716 if (event
->common
.header_field
) {
1717 BT_LOGT_STR("Serializing event's header field.");
1718 ret
= bt_ctf_field_serialize_recursive(
1719 (void *) event
->common
.header_field
->field
,
1720 &stream
->ctfser
, native_byte_order
);
1722 BT_LOGW("Cannot serialize event's header field: "
1724 event
->common
.header_field
->field
);
1729 /* Write stream event context */
1730 if (event
->common
.stream_event_context_field
) {
1731 BT_LOGT_STR("Serializing event's stream event context field.");
1732 ret
= bt_ctf_field_serialize_recursive(
1733 (void *) event
->common
.stream_event_context_field
,
1734 &stream
->ctfser
, native_byte_order
);
1736 BT_LOGW("Cannot serialize event's stream event context field: "
1738 event
->common
.stream_event_context_field
);
1743 /* Write event content */
1744 ret
= bt_ctf_event_serialize(event
, &stream
->ctfser
,
1747 /* bt_ctf_event_serialize() logs errors */
1752 content_size_bits
= bt_ctfser_get_offset_in_current_packet_bits(
1755 if (!has_packet_size
&& content_size_bits
% 8 != 0) {
1756 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1757 "but current content size is not a multiple of 8 bits: "
1758 "content-size=%" PRIu64
", "
1759 "packet-size=%" PRIu64
,
1766 /* Set packet size; make it a multiple of 8 */
1767 packet_size_bits
= (content_size_bits
+ 7) & ~UINT64_C(7);
1769 if (stream
->packet_context
) {
1771 * The whole packet is serialized at this point. Make
1772 * sure that, if `packet_size` is missing, the current
1773 * content size is equal to the current packet size.
1775 struct bt_ctf_field
*field
=
1776 bt_ctf_field_structure_get_field_by_name(
1777 stream
->packet_context
, "content_size");
1779 bt_ctf_object_put_ref(field
);
1781 if (content_size_bits
!= packet_size_bits
) {
1782 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1783 "but current packet's content size is not equal to its packet size: "
1784 "content-size=%" PRIu64
", "
1785 "packet-size=%" PRIu64
,
1786 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
),
1794 * Overwrite the packet context now that the stream
1795 * position's packet and content sizes have the correct
1798 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
1799 packet_context_offset_bits
);
1800 ret
= auto_populate_packet_context(stream
, false,
1801 packet_size_bits
, content_size_bits
);
1803 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1808 BT_LOGT("Rewriting (serializing) packet context field.");
1809 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1810 &stream
->ctfser
, native_byte_order
);
1812 BT_LOGW("Cannot serialize stream's packet context field: "
1813 "field-addr=%p", stream
->packet_context
);
1818 g_ptr_array_set_size(stream
->events
, 0);
1819 stream
->flushed_packet_count
++;
1820 bt_ctfser_close_current_packet(&stream
->ctfser
, packet_size_bits
/ 8);
1823 /* Reset automatically-set fields. */
1824 if (stream
->packet_context
) {
1825 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1826 reset_structure_field(stream
->packet_context
, "timestamp_end");
1827 reset_structure_field(stream
->packet_context
, "packet_size");
1828 reset_structure_field(stream
->packet_context
, "content_size");
1829 reset_structure_field(stream
->packet_context
, "events_discarded");
1833 BT_LOGT("Flushed stream's current packet: "
1834 "content-size=%" PRIu64
", packet-size=%" PRIu64
,
1835 content_size_bits
, packet_size_bits
);
1843 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
)
1845 struct bt_ctf_stream
*stream
= (void *) obj
;
1847 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1848 stream
, bt_ctf_stream_get_name(stream
));
1850 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream
));
1851 bt_ctfser_fini(&stream
->ctfser
);
1853 if (stream
->events
) {
1854 BT_LOGD_STR("Putting events.");
1855 g_ptr_array_free(stream
->events
, TRUE
);
1858 BT_LOGD_STR("Putting packet header field.");
1859 bt_ctf_object_put_ref(stream
->packet_header
);
1860 BT_LOGD_STR("Putting packet context field.");
1861 bt_ctf_object_put_ref(stream
->packet_context
);
1866 int _set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1867 uint64_t value
, bt_ctf_bool force
)
1870 struct bt_ctf_field_type
*field_type
= NULL
;
1871 struct bt_ctf_field
*integer
;
1873 BT_ASSERT_DBG(structure
);
1874 BT_ASSERT_DBG(name
);
1876 integer
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1878 /* Field not found, not an error. */
1879 BT_LOGT("Field not found: struct-field-addr=%p, "
1880 "name=\"%s\", force=%d", structure
, name
, force
);
1884 /* Make sure the payload has not already been set. */
1885 if (!force
&& bt_ctf_field_is_set_recursive(integer
)) {
1886 /* Payload already set, not an error */
1887 BT_LOGT("Field's payload is already set: struct-field-addr=%p, "
1888 "name=\"%s\", force=%d", structure
, name
, force
);
1892 field_type
= bt_ctf_field_get_type(integer
);
1893 BT_ASSERT_DBG(field_type
);
1894 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1896 * The user most likely meant for us to populate this field
1897 * automatically. However, we can only do this if the field
1898 * is an integer. Return an error.
1900 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1901 "field-addr=%p, ft-addr=%p, ft-id=%s",
1902 integer
, field_type
,
1903 bt_ctf_field_type_id_string((int)
1904 bt_ctf_field_type_get_type_id(field_type
)));
1909 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
1910 ret
= bt_ctf_field_integer_signed_set_value(integer
,
1913 ret
= bt_ctf_field_integer_unsigned_set_value(integer
, value
);
1915 ret
= !ret
? 1 : ret
;
1917 bt_ctf_object_put_ref(integer
);
1918 bt_ctf_object_put_ref(field_type
);
1923 * Returns the following codes:
1924 * 1 if the field was found and set,
1925 * 0 if nothing was done (field not found, or was already set),
1926 * <0 if an error was encountered
1929 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1932 return _set_structure_field_integer(structure
, name
, value
, BT_CTF_FALSE
);
1936 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
1937 struct bt_ctf_stream
*stream
)
1939 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream
)));
1943 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1945 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream
));
1949 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
1951 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream
));