2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "CTF-WRITER/STREAM"
31 #include <babeltrace2-ctf-writer/field-types.h>
32 #include <babeltrace2-ctf-writer/object.h>
33 #include <babeltrace2-ctf-writer/stream-class.h>
34 #include <babeltrace2-ctf-writer/stream.h>
35 #include <babeltrace2-ctf-writer/trace.h>
37 #include "common/align.h"
38 #include "common/assert.h"
39 #include "compat/compiler.h"
40 #include "ctfser/ctfser.h"
42 #include "assert-pre.h"
43 #include "event-class.h"
46 #include "stream-class.h"
52 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common
*stream
)
54 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
55 stream
, bt_ctf_stream_common_get_name(stream
));
58 g_string_free(stream
->name
, TRUE
);
63 int bt_ctf_stream_common_initialize(
64 struct bt_ctf_stream_common
*stream
,
65 struct bt_ctf_stream_class_common
*stream_class
, const char *name
,
66 uint64_t id
, bt_ctf_object_release_func release_func
)
69 struct bt_ctf_trace_common
*trace
= NULL
;
71 bt_ctf_object_init_shared_with_parent(&stream
->base
, release_func
);
74 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
78 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
79 "stream-class-name=\"%s\", stream-name=\"%s\", "
81 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
83 trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
85 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
86 "stream-class-addr=%p, stream-class-name=\"%s\", "
89 bt_ctf_stream_class_common_get_name(stream_class
), name
);
95 * Validate that the given ID is unique amongst all the
96 * existing trace's streams created from the same stream
101 for (i
= 0; i
< trace
->streams
->len
; i
++) {
102 struct bt_ctf_stream_common
*trace_stream
=
103 g_ptr_array_index(trace
->streams
, i
);
105 if (trace_stream
->stream_class
!= (void *) stream_class
) {
109 if (trace_stream
->id
== id
) {
110 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
117 * Acquire reference to parent since stream will become publicly
118 * reachable; it needs its parent to remain valid.
120 bt_ctf_object_set_parent(&stream
->base
, &trace
->base
);
121 stream
->stream_class
= stream_class
;
122 stream
->id
= (int64_t) id
;
125 stream
->name
= g_string_new(name
);
127 BT_LOGE_STR("Failed to allocate a GString.");
132 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace
);
134 /* Add this stream to the trace's streams */
135 BT_LOGD("Created common stream object: addr=%p", stream
);
146 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
);
149 int try_set_structure_field_integer(struct bt_ctf_field
*, const char *, uint64_t);
152 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
155 struct bt_ctf_field_type
*field_type
= NULL
;
158 BT_LOGW_STR("Invalid parameter: field is NULL.");
163 field_type
= bt_ctf_field_get_type(field
);
164 BT_ASSERT_DBG(field_type
);
166 if (bt_ctf_field_type_get_type_id(field_type
) !=
167 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
168 /* Not an integer and the value is unset, error. */
169 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
170 "field-addr=%p, ft-addr=%p, ft-id=%s",
172 bt_ctf_field_type_id_string((int)
173 bt_ctf_field_type_get_type_id(field_type
)));
178 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
179 ret
= bt_ctf_field_integer_signed_set_value(field
, (int64_t) value
);
181 /* Value is out of range, error. */
182 BT_LOGW("Cannot set signed integer field's value: "
183 "addr=%p, value=%" PRId64
,
184 field
, (int64_t) value
);
188 ret
= bt_ctf_field_integer_unsigned_set_value(field
, value
);
190 /* Value is out of range, error. */
191 BT_LOGW("Cannot set unsigned integer field's value: "
192 "addr=%p, value=%" PRIu64
,
198 bt_ctf_object_put_ref(field_type
);
203 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
206 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field_by_name(
207 stream
->packet_header
, "magic");
208 const uint32_t magic_value
= 0xc1fc1fc1;
210 BT_ASSERT_DBG(stream
);
213 /* No magic field found. Not an error, skip. */
214 BT_LOGT("No field named `magic` in packet header: skipping: "
215 "stream-addr=%p, stream-name=\"%s\"",
216 stream
, bt_ctf_stream_get_name(stream
));
220 ret
= bt_ctf_field_integer_unsigned_set_value(magic_field
,
221 (uint64_t) magic_value
);
224 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
225 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
226 stream
, bt_ctf_stream_get_name(stream
),
227 magic_field
, (uint64_t) magic_value
);
229 BT_LOGT("Set packet header field's `magic` field's value: "
230 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
231 stream
, bt_ctf_stream_get_name(stream
),
232 magic_field
, (uint64_t) magic_value
);
235 bt_ctf_object_put_ref(magic_field
);
240 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
244 struct bt_ctf_trace
*trace
= NULL
;
245 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field_by_name(
246 stream
->packet_header
, "uuid");
248 BT_ASSERT_DBG(stream
);
251 /* No uuid field found. Not an error, skip. */
252 BT_LOGT("No field named `uuid` in packet header: skipping: "
253 "stream-addr=%p, stream-name=\"%s\"",
254 stream
, bt_ctf_stream_get_name(stream
));
258 trace
= (struct bt_ctf_trace
*)
259 bt_ctf_object_get_parent(&stream
->common
.base
);
261 for (i
= 0; i
< 16; i
++) {
262 struct bt_ctf_field
*uuid_element
=
263 bt_ctf_field_array_get_field(uuid_field
, i
);
265 ret
= bt_ctf_field_integer_unsigned_set_value(
266 uuid_element
, (uint64_t) trace
->common
.uuid
[i
]);
267 bt_ctf_object_put_ref(uuid_element
);
269 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
270 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
271 "value=%" PRIu64
", index=%" PRId64
,
272 stream
, bt_ctf_stream_get_name(stream
),
273 uuid_element
, (uint64_t) trace
->common
.uuid
[i
], i
);
278 BT_LOGT("Set packet header field's `uuid` field's value: "
279 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
280 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
283 bt_ctf_object_put_ref(uuid_field
);
284 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
288 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
292 struct bt_ctf_field
*stream_id_field
=
293 bt_ctf_field_structure_get_field_by_name(
294 stream
->packet_header
, "stream_id");
296 if (!stream_id_field
) {
297 /* No stream_id field found. Not an error, skip. */
298 BT_LOGT("No field named `stream_id` in packet header: skipping: "
299 "stream-addr=%p, stream-name=\"%s\"",
300 stream
, bt_ctf_stream_get_name(stream
));
304 stream_id
= stream
->common
.stream_class
->id
;
305 ret
= bt_ctf_field_integer_unsigned_set_value(stream_id_field
,
306 (uint64_t) stream_id
);
308 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
309 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
310 stream
, bt_ctf_stream_get_name(stream
),
311 stream_id_field
, (uint64_t) stream_id
);
313 BT_LOGT("Set packet header field's `stream_id` field's value: "
314 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
315 stream
, bt_ctf_stream_get_name(stream
),
316 stream_id_field
, (uint64_t) stream_id
);
320 bt_ctf_object_put_ref(stream_id_field
);
325 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
329 if (!stream
->packet_header
) {
333 ret
= set_packet_header_magic(stream
);
335 BT_LOGW("Cannot set packet header's magic number field: "
336 "stream-addr=%p, stream-name=\"%s\"",
337 stream
, bt_ctf_stream_get_name(stream
));
341 ret
= set_packet_header_uuid(stream
);
343 BT_LOGW("Cannot set packet header's UUID field: "
344 "stream-addr=%p, stream-name=\"%s\"",
345 stream
, bt_ctf_stream_get_name(stream
));
349 ret
= set_packet_header_stream_id(stream
);
351 BT_LOGW("Cannot set packet header's stream class ID field: "
352 "stream-addr=%p, stream-name=\"%s\"",
353 stream
, bt_ctf_stream_get_name(stream
));
357 BT_LOGT("Automatically populated stream's packet header's known fields: "
358 "stream-addr=%p, stream-name=\"%s\"",
359 stream
, bt_ctf_stream_get_name(stream
));
366 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
,
367 uint64_t packet_size_bits
)
370 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
371 stream
->packet_context
, "packet_size");
373 ret
= bt_ctf_field_integer_unsigned_set_value(field
, packet_size_bits
);
375 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
376 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
377 stream
, bt_ctf_stream_get_name(stream
),
378 field
, packet_size_bits
);
380 BT_LOGT("Set packet context field's `packet_size` field's value: "
381 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
382 stream
, bt_ctf_stream_get_name(stream
),
383 field
, packet_size_bits
);
386 bt_ctf_object_put_ref(field
);
391 int set_packet_context_content_size(struct bt_ctf_stream
*stream
,
392 uint64_t content_size_bits
)
395 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
396 stream
->packet_context
, "content_size");
398 BT_ASSERT_DBG(stream
);
401 /* No content size field found. Not an error, skip. */
402 BT_LOGT("No field named `content_size` in packet context: skipping: "
403 "stream-addr=%p, stream-name=\"%s\"",
404 stream
, bt_ctf_stream_get_name(stream
));
408 ret
= bt_ctf_field_integer_unsigned_set_value(field
, content_size_bits
);
410 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
411 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
412 stream
, bt_ctf_stream_get_name(stream
),
413 field
, content_size_bits
);
415 BT_LOGT("Set packet context field's `content_size` field's value: "
416 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
417 stream
, bt_ctf_stream_get_name(stream
),
418 field
, content_size_bits
);
422 bt_ctf_object_put_ref(field
);
427 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
430 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
431 stream
->packet_context
, "events_discarded");
433 BT_ASSERT_DBG(stream
);
436 /* No discarded events count field found. Not an error, skip. */
437 BT_LOGT("No field named `events_discarded` in packet context: skipping: "
438 "stream-addr=%p, stream-name=\"%s\"",
439 stream
, bt_ctf_stream_get_name(stream
));
444 * If the field is set by the user, make sure that the value is
445 * greater than or equal to the stream's current count of
446 * discarded events. We do not allow wrapping here. If it's
447 * valid, update the stream's current count.
449 if (bt_ctf_field_is_set_recursive(field
)) {
452 ret
= bt_ctf_field_integer_unsigned_get_value(field
,
455 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
456 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
457 stream
, bt_ctf_stream_get_name(stream
), field
);
461 if (user_val
< stream
->discarded_events
) {
462 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
463 "value is lesser than the stream's current discarded events count: "
464 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
465 "value=%" PRIu64
", "
466 "stream-discarded-events-count=%" PRIu64
,
467 stream
, bt_ctf_stream_get_name(stream
), field
,
468 user_val
, stream
->discarded_events
);
472 stream
->discarded_events
= user_val
;
474 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
475 stream
->discarded_events
);
477 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
478 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
479 stream
, bt_ctf_stream_get_name(stream
),
480 field
, stream
->discarded_events
);
482 BT_LOGT("Set packet context field's `events_discarded` field's value: "
483 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
484 stream
, bt_ctf_stream_get_name(stream
),
485 field
, stream
->discarded_events
);
490 bt_ctf_object_put_ref(field
);
495 void update_clock_value(uint64_t *val
, uint64_t new_val
,
496 unsigned int new_val_size
)
498 const uint64_t pow2
= 1ULL << new_val_size
;
499 const uint64_t mask
= pow2
- 1;
502 #ifdef BT_LOG_ENABLED_TRACE
503 uint64_t old_val
= *val
;
506 if (new_val_size
== 64) {
511 val_masked
= *val
& mask
;
513 if (new_val
< val_masked
) {
522 BT_LOGT("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
528 int visit_field_update_clock_value(struct bt_ctf_field
*field
, uint64_t *val
)
531 struct bt_ctf_field_common
*field_common
= (void *) field
;
537 switch (bt_ctf_field_get_type_id(field
)) {
538 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
540 struct bt_ctf_clock_class
*cc
=
541 bt_ctf_field_type_integer_get_mapped_clock_class(
542 (void *) field_common
->type
);
550 bt_ctf_object_put_ref(cc
);
551 val_size
= bt_ctf_field_type_integer_get_size(
552 (void *) field_common
->type
);
553 BT_ASSERT_DBG(val_size
>= 1);
555 if (bt_ctf_field_type_integer_is_signed(
556 (void *) field_common
->type
)) {
559 ret
= bt_ctf_field_integer_signed_get_value(field
, &ival
);
560 uval
= (uint64_t) ival
;
562 ret
= bt_ctf_field_integer_unsigned_get_value(field
, &uval
);
570 update_clock_value(val
, uval
, val_size
);
573 case BT_CTF_FIELD_TYPE_ID_ENUM
:
575 struct bt_ctf_field
*int_field
=
576 bt_ctf_field_enumeration_get_container(field
);
578 BT_ASSERT_DBG(int_field
);
579 ret
= visit_field_update_clock_value(int_field
, val
);
580 bt_ctf_object_put_ref(int_field
);
583 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
586 int64_t len
= bt_ctf_field_type_array_get_length(
587 (void *) field_common
->type
);
589 BT_ASSERT_DBG(len
>= 0);
591 for (i
= 0; i
< len
; i
++) {
592 struct bt_ctf_field
*elem_field
=
593 bt_ctf_field_array_get_field(field
, i
);
595 BT_ASSERT_DBG(elem_field
);
596 ret
= visit_field_update_clock_value(elem_field
, val
);
597 bt_ctf_object_put_ref(elem_field
);
604 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
607 int64_t len
= bt_ctf_field_common_sequence_get_length(
615 for (i
= 0; i
< len
; i
++) {
616 struct bt_ctf_field
*elem_field
=
617 bt_ctf_field_sequence_get_field(field
, i
);
619 BT_ASSERT_DBG(elem_field
);
620 ret
= visit_field_update_clock_value(elem_field
, val
);
621 bt_ctf_object_put_ref(elem_field
);
628 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
631 int64_t len
= bt_ctf_field_type_structure_get_field_count(
632 (void *) field_common
->type
);
634 BT_ASSERT_DBG(len
>= 0);
636 for (i
= 0; i
< len
; i
++) {
637 struct bt_ctf_field
*member_field
=
638 bt_ctf_field_structure_get_field_by_index(field
, i
);
640 BT_ASSERT_DBG(member_field
);
641 ret
= visit_field_update_clock_value(member_field
, val
);
642 bt_ctf_object_put_ref(member_field
);
649 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
651 struct bt_ctf_field
*cur_field
=
652 bt_ctf_field_variant_get_current_field(field
);
659 ret
= visit_field_update_clock_value(cur_field
, val
);
660 bt_ctf_object_put_ref(cur_field
);
671 int visit_event_update_clock_value(struct bt_ctf_event
*event
, uint64_t *val
)
674 struct bt_ctf_field
*field
;
676 field
= bt_ctf_event_get_header(event
);
677 ret
= visit_field_update_clock_value(field
, val
);
678 bt_ctf_object_put_ref(field
);
680 BT_LOGW_STR("Cannot automatically update clock value in "
685 field
= bt_ctf_event_get_stream_event_context(event
);
686 ret
= visit_field_update_clock_value(field
, val
);
687 bt_ctf_object_put_ref(field
);
689 BT_LOGW_STR("Cannot automatically update clock value in "
690 "event's stream event context.");
694 field
= bt_ctf_event_get_context(event
);
695 ret
= visit_field_update_clock_value(field
, val
);
696 bt_ctf_object_put_ref(field
);
698 BT_LOGW_STR("Cannot automatically update clock value in "
703 field
= bt_ctf_event_get_payload_field(event
);
704 ret
= visit_field_update_clock_value(field
, val
);
705 bt_ctf_object_put_ref(field
);
707 BT_LOGW_STR("Cannot automatically update clock value in "
717 int set_packet_context_timestamps(struct bt_ctf_stream
*stream
)
721 uint64_t cur_clock_value
;
722 uint64_t init_clock_value
= 0;
723 struct bt_ctf_field
*ts_begin_field
= bt_ctf_field_structure_get_field_by_name(
724 stream
->packet_context
, "timestamp_begin");
725 struct bt_ctf_field
*ts_end_field
= bt_ctf_field_structure_get_field_by_name(
726 stream
->packet_context
, "timestamp_end");
727 struct bt_ctf_field_common
*packet_context
=
728 (void *) stream
->packet_context
;
732 if (ts_begin_field
&& bt_ctf_field_is_set_recursive(ts_begin_field
)) {
733 /* Use provided `timestamp_begin` value as starting value */
734 ret
= bt_ctf_field_integer_unsigned_get_value(ts_begin_field
, &val
);
735 BT_ASSERT_DBG(ret
== 0);
736 init_clock_value
= val
;
737 } else if (stream
->last_ts_end
!= -1ULL) {
738 /* Use last packet's ending timestamp as starting value */
739 init_clock_value
= stream
->last_ts_end
;
742 cur_clock_value
= init_clock_value
;
744 if (stream
->last_ts_end
!= -1ULL &&
745 cur_clock_value
< stream
->last_ts_end
) {
746 BT_LOGW("Packet's initial timestamp is less than previous "
747 "packet's final timestamp: "
748 "stream-addr=%p, stream-name=\"%s\", "
749 "cur-packet-ts-begin=%" PRIu64
", "
750 "prev-packet-ts-end=%" PRIu64
,
751 stream
, bt_ctf_stream_get_name(stream
),
752 cur_clock_value
, stream
->last_ts_end
);
758 * Visit all the packet context fields, followed by all the
759 * fields of all the events, in order, updating our current
760 * clock value as we visit.
762 * While visiting the packet context fields, do not consider
763 * `timestamp_begin` and `timestamp_end` because this function's
764 * purpose is to set them anyway. Also do not consider
765 * `packet_size`, `content_size`, `events_discarded`, and
766 * `packet_seq_num` if they are not set because those are
767 * autopopulating fields.
769 len
= bt_ctf_field_type_structure_get_field_count(
770 (void *) packet_context
->type
);
771 BT_ASSERT_DBG(len
>= 0);
773 for (i
= 0; i
< len
; i
++) {
774 const char *member_name
;
775 struct bt_ctf_field
*member_field
;
777 ret
= bt_ctf_field_type_structure_get_field_by_index(
778 (void *) packet_context
->type
, &member_name
, NULL
, i
);
779 BT_ASSERT_DBG(ret
== 0);
781 if (strcmp(member_name
, "timestamp_begin") == 0 ||
782 strcmp(member_name
, "timestamp_end") == 0) {
786 member_field
= bt_ctf_field_structure_get_field_by_index(
787 stream
->packet_context
, i
);
788 BT_ASSERT_DBG(member_field
);
790 if (strcmp(member_name
, "packet_size") == 0 &&
791 !bt_ctf_field_is_set_recursive(member_field
)) {
792 bt_ctf_object_put_ref(member_field
);
796 if (strcmp(member_name
, "content_size") == 0 &&
797 !bt_ctf_field_is_set_recursive(member_field
)) {
798 bt_ctf_object_put_ref(member_field
);
802 if (strcmp(member_name
, "events_discarded") == 0 &&
803 !bt_ctf_field_is_set_recursive(member_field
)) {
804 bt_ctf_object_put_ref(member_field
);
808 if (strcmp(member_name
, "packet_seq_num") == 0 &&
809 !bt_ctf_field_is_set_recursive(member_field
)) {
810 bt_ctf_object_put_ref(member_field
);
814 ret
= visit_field_update_clock_value(member_field
,
816 bt_ctf_object_put_ref(member_field
);
818 BT_LOGW("Cannot automatically update clock value "
819 "in stream's packet context: "
820 "stream-addr=%p, stream-name=\"%s\", "
822 stream
, bt_ctf_stream_get_name(stream
),
828 for (i
= 0; i
< stream
->events
->len
; i
++) {
829 struct bt_ctf_event
*event
= g_ptr_array_index(stream
->events
, i
);
831 BT_ASSERT_DBG(event
);
832 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
834 BT_LOGW("Cannot automatically update clock value "
835 "in stream's packet context: "
836 "stream-addr=%p, stream-name=\"%s\", "
837 "index=%" PRIu64
", event-addr=%p, "
838 "event-class-id=%" PRId64
", "
839 "event-class-name=\"%s\"",
840 stream
, bt_ctf_stream_get_name(stream
),
842 bt_ctf_event_class_common_get_id(event
->common
.class),
843 bt_ctf_event_class_common_get_name(event
->common
.class));
849 * Everything is visited, thus the current clock value
850 * corresponds to the ending timestamp. Validate this value
851 * against the provided value of `timestamp_end`, if any,
854 if (ts_end_field
&& bt_ctf_field_is_set_recursive(ts_end_field
)) {
855 ret
= bt_ctf_field_integer_unsigned_get_value(ts_end_field
, &val
);
856 BT_ASSERT_DBG(ret
== 0);
858 if (val
< cur_clock_value
) {
859 BT_LOGW("Packet's final timestamp is less than "
860 "computed packet's final timestamp: "
861 "stream-addr=%p, stream-name=\"%s\", "
862 "cur-packet-ts-end=%" PRIu64
", "
863 "computed-packet-ts-end=%" PRIu64
,
864 stream
, bt_ctf_stream_get_name(stream
),
865 val
, cur_clock_value
);
870 stream
->last_ts_end
= val
;
873 if (ts_end_field
&& !bt_ctf_field_is_set_recursive(ts_end_field
)) {
874 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
875 BT_ASSERT_DBG(ret
== 0);
876 stream
->last_ts_end
= cur_clock_value
;
880 stream
->last_ts_end
= cur_clock_value
;
883 /* Set `timestamp_begin` field to initial clock value */
884 if (ts_begin_field
&& !bt_ctf_field_is_set_recursive(ts_begin_field
)) {
885 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
886 BT_ASSERT_DBG(ret
== 0);
890 bt_ctf_object_put_ref(ts_begin_field
);
891 bt_ctf_object_put_ref(ts_end_field
);
896 int auto_populate_packet_context(struct bt_ctf_stream
*stream
, bool set_ts
,
897 uint64_t packet_size_bits
, uint64_t content_size_bits
)
901 if (!stream
->packet_context
) {
905 ret
= set_packet_context_packet_size(stream
, packet_size_bits
);
907 BT_LOGW("Cannot set packet context's packet size field: "
908 "stream-addr=%p, stream-name=\"%s\"",
909 stream
, bt_ctf_stream_get_name(stream
));
913 ret
= set_packet_context_content_size(stream
, content_size_bits
);
915 BT_LOGW("Cannot set packet context's content size field: "
916 "stream-addr=%p, stream-name=\"%s\"",
917 stream
, bt_ctf_stream_get_name(stream
));
922 ret
= set_packet_context_timestamps(stream
);
924 BT_LOGW("Cannot set packet context's timestamp fields: "
925 "stream-addr=%p, stream-name=\"%s\"",
926 stream
, bt_ctf_stream_get_name(stream
));
931 ret
= set_packet_context_events_discarded(stream
);
933 BT_LOGW("Cannot set packet context's discarded events count field: "
934 "stream-addr=%p, stream-name=\"%s\"",
935 stream
, bt_ctf_stream_get_name(stream
));
939 BT_LOGT("Automatically populated stream's packet context's known fields: "
940 "stream-addr=%p, stream-name=\"%s\"",
941 stream
, bt_ctf_stream_get_name(stream
));
948 void release_event(struct bt_ctf_event
*event
)
950 if (bt_ctf_object_get_ref_count(&event
->common
.base
)) {
952 * The event is being orphaned, but it must guarantee the
953 * existence of its event class for the duration of its
956 bt_ctf_object_get_ref(event
->common
.class);
957 BT_CTF_OBJECT_PUT_REF_AND_RESET(event
->common
.base
.parent
);
959 bt_ctf_object_try_spec_release(&event
->common
.base
);
964 int create_stream_file(struct bt_ctf_writer
*writer
,
965 struct bt_ctf_stream
*stream
)
968 GString
*filename
= g_string_new(NULL
);
969 int64_t stream_class_id
;
970 char *file_path
= NULL
;
972 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
973 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
974 writer
, stream
, bt_ctf_stream_get_name(stream
),
975 stream
->common
.stream_class
,
976 stream
->common
.stream_class
->name
->str
);
978 if (stream
->common
.name
&& stream
->common
.name
->len
> 0) {
979 /* Use stream name's base name as prefix */
980 gchar
*basename
= g_path_get_basename(stream
->common
.name
->str
);
982 BT_ASSERT_DBG(basename
);
984 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
985 g_string_assign(filename
, "stream");
987 g_string_assign(filename
, basename
);
994 if (stream
->common
.stream_class
->name
&&
995 stream
->common
.stream_class
->name
->len
> 0) {
996 /* Use stream class name's base name as prefix */
999 stream
->common
.stream_class
->name
->str
);
1001 BT_ASSERT_DBG(basename
);
1003 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
1004 g_string_assign(filename
, "stream");
1006 g_string_assign(filename
, basename
);
1013 /* Default to using `stream-` as prefix */
1014 g_string_assign(filename
, "stream");
1017 stream_class_id
= bt_ctf_stream_class_common_get_id(stream
->common
.stream_class
);
1018 BT_ASSERT_DBG(stream_class_id
>= 0);
1019 BT_ASSERT_DBG(stream
->common
.id
>= 0);
1020 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
1021 stream_class_id
, stream
->common
.id
);
1023 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
1029 ret
= bt_ctfser_init(&stream
->ctfser
, file_path
,
1030 BT_LOG_OUTPUT_LEVEL
);
1033 /* bt_ctfser_init() logs errors */
1037 BT_LOGD("Created stream file for writing: "
1038 "stream-addr=%p, stream-name=\"%s\", "
1039 "filename=\"%s\"", stream
, bt_ctf_stream_get_name(stream
),
1043 g_string_free(filename
, TRUE
);
1048 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
1049 struct bt_ctf_stream_class
*stream_class
,
1050 const char *name
, uint64_t id
)
1054 struct bt_ctf_stream
*stream
= NULL
;
1055 struct bt_ctf_trace
*trace
= NULL
;
1056 struct bt_ctf_writer
*writer
= NULL
;
1058 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1059 "stream-class-name=\"%s\", stream-name=\"%s\", "
1060 "stream-id=%" PRIu64
,
1061 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1063 stream
= g_new0(struct bt_ctf_stream
, 1);
1065 BT_LOGE_STR("Failed to allocate one stream.");
1070 id
= stream_class
->next_stream_id
;
1073 ret
= bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream
),
1074 BT_CTF_TO_COMMON(stream_class
), name
, id
, bt_ctf_stream_destroy
);
1076 /* bt_ctf_stream_common_initialize() logs errors */
1080 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1081 BT_CTF_TO_COMMON(stream_class
)));
1083 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1084 "stream-class-addr=%p, stream-class-name=\"%s\", "
1085 "stream-name=\"%s\"",
1086 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1091 writer
= (struct bt_ctf_writer
*)
1092 bt_ctf_object_get_parent(&trace
->common
.base
);
1093 stream
->last_ts_end
= -1ULL;
1094 BT_LOGD("CTF writer stream object belongs writer's trace: "
1095 "writer-addr=%p", writer
);
1096 BT_ASSERT_DBG(writer
);
1098 if (stream_class
->common
.packet_context_field_type
) {
1099 BT_LOGD("Creating stream's packet context field: "
1101 stream_class
->common
.packet_context_field_type
);
1102 stream
->packet_context
= bt_ctf_field_create(
1103 (void *) stream_class
->common
.packet_context_field_type
);
1104 if (!stream
->packet_context
) {
1105 BT_LOGW_STR("Cannot create stream's packet context field.");
1109 /* Initialize events_discarded */
1110 ret
= try_set_structure_field_integer(
1111 stream
->packet_context
, "events_discarded", 0);
1113 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1114 "ret=%d, packet-context-field-addr=%p",
1115 ret
, stream
->packet_context
);
1120 stream
->events
= g_ptr_array_new_with_free_func(
1121 (GDestroyNotify
) release_event
);
1122 if (!stream
->events
) {
1123 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1127 if (trace
->common
.packet_header_field_type
) {
1128 BT_LOGD("Creating stream's packet header field: "
1129 "ft-addr=%p", trace
->common
.packet_header_field_type
);
1130 stream
->packet_header
=
1131 bt_ctf_field_create(
1132 (void *) trace
->common
.packet_header_field_type
);
1133 if (!stream
->packet_header
) {
1134 BT_LOGW_STR("Cannot create stream's packet header field.");
1140 * Attempt to populate the default trace packet header fields
1141 * (magic, uuid and stream_id). This will _not_ fail shall the
1142 * fields not be found or be of an incompatible type; they will
1143 * simply not be populated automatically. The user will have to
1144 * make sure to set the trace packet header fields himself
1147 ret
= auto_populate_packet_header(stream
);
1149 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1153 /* Create file associated with this stream */
1154 fd
= create_stream_file(writer
, stream
);
1156 BT_LOGW_STR("Cannot create stream file.");
1160 /* Freeze the writer */
1161 BT_LOGD_STR("Freezing stream's CTF writer.");
1162 bt_ctf_writer_freeze(writer
);
1164 /* Add this stream to the trace's streams */
1165 g_ptr_array_add(trace
->common
.streams
, stream
);
1166 stream_class
->next_stream_id
++;
1167 BT_LOGD("Created stream object: addr=%p", stream
);
1171 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
1174 bt_ctf_object_put_ref(writer
);
1178 struct bt_ctf_stream
*bt_ctf_stream_create(
1179 struct bt_ctf_stream_class
*stream_class
,
1180 const char *name
, uint64_t id_param
)
1182 return bt_ctf_stream_create_with_id(stream_class
,
1186 int bt_ctf_stream_get_discarded_events_count(
1187 struct bt_ctf_stream
*stream
, uint64_t *count
)
1192 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1198 BT_LOGW_STR("Invalid parameter: count is NULL.");
1203 *count
= (uint64_t) stream
->discarded_events
;
1210 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1214 struct bt_ctf_field
*events_discarded_field
= NULL
;
1216 if (!stream
->packet_context
) {
1220 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1221 stream
->packet_context
, "events_discarded");
1222 if (!events_discarded_field
) {
1226 ret
= bt_ctf_field_integer_unsigned_set_value(
1227 events_discarded_field
, count
);
1229 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1230 "field-addr=%p, value=%" PRIu64
,
1231 events_discarded_field
, count
);
1236 bt_ctf_object_put_ref(events_discarded_field
);
1240 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1241 uint64_t event_count
)
1245 struct bt_ctf_field
*events_discarded_field
= NULL
;
1248 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1252 BT_LOGT("Appending discarded events to stream: "
1253 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1254 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1256 if (!stream
->packet_context
) {
1257 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1261 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1262 stream
->packet_context
, "events_discarded");
1263 if (!events_discarded_field
) {
1264 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1268 new_count
= stream
->discarded_events
+ event_count
;
1269 if (new_count
< stream
->discarded_events
) {
1270 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1271 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1272 stream
->discarded_events
, new_count
);
1276 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1278 /* set_packet_context_events_discarded_field() logs errors */
1282 stream
->discarded_events
= new_count
;
1283 BT_LOGT("Appended discarded events to stream: "
1284 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1285 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1288 bt_ctf_object_put_ref(events_discarded_field
);
1291 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1292 struct bt_ctf_event
*event
)
1295 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1296 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1297 struct bt_ctf_stream_class
*stream_class
=
1298 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1299 BT_CTF_TO_COMMON(stream
)));
1300 int64_t event_class_id
;
1302 BT_ASSERT_DBG(event
);
1304 if (!event
->common
.header_field
) {
1308 if (event
->common
.frozen
) {
1309 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1314 BT_LOGT("Automatically populating event's header field: "
1315 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1316 stream
, bt_ctf_stream_get_name(stream
), event
);
1318 id_field
= bt_ctf_field_structure_get_field_by_name(
1319 (void *) event
->common
.header_field
->field
, "id");
1320 event_class_id
= bt_ctf_event_class_common_get_id(event
->common
.class);
1321 BT_ASSERT_DBG(event_class_id
>= 0);
1323 if (id_field
&& bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1324 ret
= set_integer_field_value(id_field
, event_class_id
);
1326 BT_LOGW("Cannot set event header's `id` field's value: "
1327 "addr=%p, value=%" PRIu64
, id_field
,
1334 * The conditions to automatically set the timestamp are:
1336 * 1. The event header field "timestamp" exists and is an
1338 * 2. This stream's class has a registered clock (set with
1339 * bt_ctf_stream_class_set_clock()).
1340 * 3. The "timestamp" field is not set.
1342 timestamp_field
= bt_ctf_field_structure_get_field_by_name(
1343 (void *) event
->common
.header_field
->field
, "timestamp");
1344 if (timestamp_field
&& stream_class
->clock
&&
1345 bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
&&
1346 !bt_ctf_field_is_set_recursive(timestamp_field
)) {
1347 mapped_clock_class
=
1348 bt_ctf_field_type_integer_get_mapped_clock_class(
1349 (void *) ((struct bt_ctf_field_common
*) timestamp_field
)->type
);
1350 if (mapped_clock_class
) {
1353 BT_ASSERT_DBG(mapped_clock_class
==
1354 stream_class
->clock
->clock_class
);
1355 ret
= bt_ctf_clock_get_value(
1356 stream_class
->clock
,
1358 BT_ASSERT_DBG(ret
== 0);
1359 ret
= set_integer_field_value(timestamp_field
,
1362 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1363 "addr=%p, value=%" PRIu64
,
1364 timestamp_field
, timestamp
);
1370 BT_LOGT("Automatically populated event's header field: "
1371 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1372 stream
, bt_ctf_stream_get_name(stream
), event
);
1375 bt_ctf_object_put_ref(id_field
);
1376 bt_ctf_object_put_ref(timestamp_field
);
1377 bt_ctf_object_put_ref(mapped_clock_class
);
1381 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1382 struct bt_ctf_event
*event
)
1387 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1393 BT_LOGW_STR("Invalid parameter: event is NULL.");
1398 BT_LOGT("Appending event to stream: "
1399 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1400 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1401 stream
, bt_ctf_stream_get_name(stream
), event
,
1402 bt_ctf_event_class_common_get_name(
1403 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1404 bt_ctf_event_class_common_get_id(
1405 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1408 * The event is not supposed to have a parent stream at this
1409 * point. The only other way an event can have a parent stream
1410 * is if it was assigned when setting a packet to the event,
1411 * in which case the packet's stream is not a writer stream,
1412 * and thus the user is trying to append an event which belongs
1413 * to another stream.
1415 if (event
->common
.base
.parent
) {
1420 bt_ctf_object_set_parent(&event
->common
.base
, &stream
->common
.base
);
1421 BT_LOGT_STR("Automatically populating the header of the event to append.");
1422 ret
= auto_populate_event_header(stream
, event
);
1424 /* auto_populate_event_header() reports errors */
1428 /* Make sure the various scopes of the event are set */
1429 BT_LOGT_STR("Validating event to append.");
1430 BT_CTF_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event
)) == 0,
1431 "Invalid event: event-addr=%p", event
);
1433 /* Save the new event and freeze it */
1434 BT_LOGT_STR("Freezing the event to append.");
1435 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event
), true);
1436 g_ptr_array_add(stream
->events
, event
);
1439 * Event had to hold a reference to its event class as long as it wasn't
1440 * part of the same trace hierarchy. From now on, the event and its
1441 * class share the same lifetime guarantees and the reference is no
1444 BT_LOGT_STR("Putting the event's class.");
1445 bt_ctf_object_put_ref(event
->common
.class);
1446 BT_LOGT("Appended event to stream: "
1447 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1448 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1449 stream
, bt_ctf_stream_get_name(stream
), event
,
1450 bt_ctf_event_class_common_get_name(
1451 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1452 bt_ctf_event_class_common_get_id(
1453 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1460 * Orphan the event; we were not successful in associating it to
1463 bt_ctf_object_set_parent(&event
->common
.base
, NULL
);
1467 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(struct bt_ctf_stream
*stream
)
1469 struct bt_ctf_field
*packet_context
= NULL
;
1472 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1476 packet_context
= stream
->packet_context
;
1477 if (packet_context
) {
1478 bt_ctf_object_get_ref(packet_context
);
1481 return packet_context
;
1484 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1485 struct bt_ctf_field
*field
)
1488 struct bt_ctf_field_type
*field_type
;
1491 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1496 field_type
= bt_ctf_field_get_type(field
);
1497 if (bt_ctf_field_type_common_compare((void *) field_type
,
1498 stream
->common
.stream_class
->packet_context_field_type
)) {
1499 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1500 "stream-addr=%p, stream-name=\"%s\", "
1501 "packet-context-field-addr=%p, "
1502 "packet-context-ft-addr=%p",
1503 stream
, bt_ctf_stream_get_name(stream
),
1509 bt_ctf_object_put_ref(field_type
);
1510 bt_ctf_object_put_ref(stream
->packet_context
);
1511 stream
->packet_context
= bt_ctf_object_get_ref(field
);
1512 BT_LOGT("Set stream's packet context field: "
1513 "stream-addr=%p, stream-name=\"%s\", "
1514 "packet-context-field-addr=%p",
1515 stream
, bt_ctf_stream_get_name(stream
), field
);
1520 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(struct bt_ctf_stream
*stream
)
1522 struct bt_ctf_field
*packet_header
= NULL
;
1525 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1529 packet_header
= stream
->packet_header
;
1530 if (packet_header
) {
1531 bt_ctf_object_get_ref(packet_header
);
1534 return packet_header
;
1537 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1538 struct bt_ctf_field
*field
)
1541 struct bt_ctf_trace
*trace
= NULL
;
1542 struct bt_ctf_field_type
*field_type
= NULL
;
1545 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1550 trace
= (struct bt_ctf_trace
*)
1551 bt_ctf_object_get_parent(&stream
->common
.base
);
1554 if (trace
->common
.packet_header_field_type
) {
1555 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1556 "stream-addr=%p, stream-name=\"%s\", "
1557 "packet-header-field-addr=%p, "
1558 "expected-ft-addr=%p",
1559 stream
, bt_ctf_stream_get_name(stream
),
1560 field
, trace
->common
.packet_header_field_type
);
1565 goto skip_validation
;
1568 field_type
= bt_ctf_field_get_type(field
);
1569 BT_ASSERT_DBG(field_type
);
1571 if (bt_ctf_field_type_common_compare((void *) field_type
,
1572 trace
->common
.packet_header_field_type
)) {
1573 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1574 "stream-addr=%p, stream-name=\"%s\", "
1575 "packet-header-field-addr=%p, "
1576 "packet-header-ft-addr=%p",
1577 stream
, bt_ctf_stream_get_name(stream
),
1584 bt_ctf_object_put_ref(stream
->packet_header
);
1585 stream
->packet_header
= bt_ctf_object_get_ref(field
);
1586 BT_LOGT("Set stream's packet header field: "
1587 "stream-addr=%p, stream-name=\"%s\", "
1588 "packet-header-field-addr=%p",
1589 stream
, bt_ctf_stream_get_name(stream
), field
);
1591 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1592 bt_ctf_object_put_ref(field_type
);
1597 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1599 struct bt_ctf_field
*member
;
1601 member
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1603 bt_ctf_field_common_reset_recursive((void *) member
);
1604 bt_ctf_object_put_ref(member
);
1608 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1612 uint64_t packet_context_offset_bits
= 0;
1613 struct bt_ctf_trace
*trace
;
1614 enum bt_ctf_byte_order native_byte_order
;
1615 bool has_packet_size
= false;
1616 uint64_t packet_size_bits
= 0;
1617 uint64_t content_size_bits
= 0;
1620 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1625 if (stream
->packet_context
) {
1626 struct bt_ctf_field
*packet_size_field
;
1628 packet_size_field
= bt_ctf_field_structure_get_field_by_name(
1629 stream
->packet_context
, "packet_size");
1630 has_packet_size
= packet_size_field
;
1631 bt_ctf_object_put_ref(packet_size_field
);
1634 if (stream
->flushed_packet_count
== 1) {
1635 if (!stream
->packet_context
) {
1636 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1641 if (!has_packet_size
) {
1642 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1648 BT_LOGT("Flushing stream's current packet: stream-addr=%p, "
1649 "stream-name=\"%s\", packet-index=%u", stream
,
1650 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1651 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1652 stream
->common
.stream_class
));
1653 BT_ASSERT_DBG(trace
);
1654 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1656 ret
= auto_populate_packet_header(stream
);
1658 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1663 /* Initialize packet/content sizes to `0`; we will overwrite later */
1664 ret
= auto_populate_packet_context(stream
, true, 0, 0);
1666 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1671 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
1673 /* bt_ctfser_open_packet() logs errors */
1678 if (stream
->packet_header
) {
1679 BT_LOGT_STR("Serializing packet header field (initial).");
1680 ret
= bt_ctf_field_serialize_recursive(stream
->packet_header
,
1681 &stream
->ctfser
, native_byte_order
);
1683 BT_LOGW("Cannot serialize stream's packet header field: "
1684 "field-addr=%p", stream
->packet_header
);
1689 if (stream
->packet_context
) {
1690 /* Save packet context's position to overwrite it later */
1691 packet_context_offset_bits
=
1692 bt_ctfser_get_offset_in_current_packet_bits(
1695 /* Write packet context */
1696 BT_LOGT_STR("Serializing packet context field (initial).");
1697 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1698 &stream
->ctfser
, native_byte_order
);
1700 BT_LOGW("Cannot serialize stream's packet context field: "
1701 "field-addr=%p", stream
->packet_context
);
1706 BT_LOGT("Serializing events: count=%u", stream
->events
->len
);
1708 for (i
= 0; i
< stream
->events
->len
; i
++) {
1709 struct bt_ctf_event
*event
= g_ptr_array_index(
1711 struct bt_ctf_event_class
*event_class
=
1712 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1713 BT_CTF_TO_COMMON(event
)));
1715 BT_LOGT("Serializing event: index=%zu, event-addr=%p, "
1716 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1717 "ser-offset=%" PRIu64
,
1718 i
, event
, bt_ctf_event_class_get_name(event_class
),
1719 bt_ctf_event_class_get_id(event_class
),
1720 bt_ctfser_get_offset_in_current_packet_bits(
1723 /* Write event header */
1724 if (event
->common
.header_field
) {
1725 BT_LOGT_STR("Serializing event's header field.");
1726 ret
= bt_ctf_field_serialize_recursive(
1727 (void *) event
->common
.header_field
->field
,
1728 &stream
->ctfser
, native_byte_order
);
1730 BT_LOGW("Cannot serialize event's header field: "
1732 event
->common
.header_field
->field
);
1737 /* Write stream event context */
1738 if (event
->common
.stream_event_context_field
) {
1739 BT_LOGT_STR("Serializing event's stream event context field.");
1740 ret
= bt_ctf_field_serialize_recursive(
1741 (void *) event
->common
.stream_event_context_field
,
1742 &stream
->ctfser
, native_byte_order
);
1744 BT_LOGW("Cannot serialize event's stream event context field: "
1746 event
->common
.stream_event_context_field
);
1751 /* Write event content */
1752 ret
= bt_ctf_event_serialize(event
, &stream
->ctfser
,
1755 /* bt_ctf_event_serialize() logs errors */
1760 content_size_bits
= bt_ctfser_get_offset_in_current_packet_bits(
1763 if (!has_packet_size
&& content_size_bits
% 8 != 0) {
1764 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1765 "but current content size is not a multiple of 8 bits: "
1766 "content-size=%" PRIu64
", "
1767 "packet-size=%" PRIu64
,
1774 /* Set packet size; make it a multiple of 8 */
1775 packet_size_bits
= (content_size_bits
+ 7) & ~UINT64_C(7);
1777 if (stream
->packet_context
) {
1779 * The whole packet is serialized at this point. Make
1780 * sure that, if `packet_size` is missing, the current
1781 * content size is equal to the current packet size.
1783 struct bt_ctf_field
*field
=
1784 bt_ctf_field_structure_get_field_by_name(
1785 stream
->packet_context
, "content_size");
1787 bt_ctf_object_put_ref(field
);
1789 if (content_size_bits
!= packet_size_bits
) {
1790 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1791 "but current packet's content size is not equal to its packet size: "
1792 "content-size=%" PRIu64
", "
1793 "packet-size=%" PRIu64
,
1794 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
),
1802 * Overwrite the packet context now that the stream
1803 * position's packet and content sizes have the correct
1806 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
1807 packet_context_offset_bits
);
1808 ret
= auto_populate_packet_context(stream
, false,
1809 packet_size_bits
, content_size_bits
);
1811 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1816 BT_LOGT("Rewriting (serializing) packet context field.");
1817 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1818 &stream
->ctfser
, native_byte_order
);
1820 BT_LOGW("Cannot serialize stream's packet context field: "
1821 "field-addr=%p", stream
->packet_context
);
1826 g_ptr_array_set_size(stream
->events
, 0);
1827 stream
->flushed_packet_count
++;
1828 bt_ctfser_close_current_packet(&stream
->ctfser
, packet_size_bits
/ 8);
1831 /* Reset automatically-set fields. */
1832 if (stream
->packet_context
) {
1833 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1834 reset_structure_field(stream
->packet_context
, "timestamp_end");
1835 reset_structure_field(stream
->packet_context
, "packet_size");
1836 reset_structure_field(stream
->packet_context
, "content_size");
1837 reset_structure_field(stream
->packet_context
, "events_discarded");
1841 BT_LOGT("Flushed stream's current packet: "
1842 "content-size=%" PRIu64
", packet-size=%" PRIu64
,
1843 content_size_bits
, packet_size_bits
);
1851 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
)
1853 struct bt_ctf_stream
*stream
= (void *) obj
;
1855 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1856 stream
, bt_ctf_stream_get_name(stream
));
1858 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream
));
1859 bt_ctfser_fini(&stream
->ctfser
);
1861 if (stream
->events
) {
1862 BT_LOGD_STR("Putting events.");
1863 g_ptr_array_free(stream
->events
, TRUE
);
1866 BT_LOGD_STR("Putting packet header field.");
1867 bt_ctf_object_put_ref(stream
->packet_header
);
1868 BT_LOGD_STR("Putting packet context field.");
1869 bt_ctf_object_put_ref(stream
->packet_context
);
1874 int _set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1875 uint64_t value
, bt_ctf_bool force
)
1878 struct bt_ctf_field_type
*field_type
= NULL
;
1879 struct bt_ctf_field
*integer
;
1881 BT_ASSERT_DBG(structure
);
1882 BT_ASSERT_DBG(name
);
1884 integer
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1886 /* Field not found, not an error. */
1887 BT_LOGT("Field not found: struct-field-addr=%p, "
1888 "name=\"%s\", force=%d", structure
, name
, force
);
1892 /* Make sure the payload has not already been set. */
1893 if (!force
&& bt_ctf_field_is_set_recursive(integer
)) {
1894 /* Payload already set, not an error */
1895 BT_LOGT("Field's payload is already set: struct-field-addr=%p, "
1896 "name=\"%s\", force=%d", structure
, name
, force
);
1900 field_type
= bt_ctf_field_get_type(integer
);
1901 BT_ASSERT_DBG(field_type
);
1902 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1904 * The user most likely meant for us to populate this field
1905 * automatically. However, we can only do this if the field
1906 * is an integer. Return an error.
1908 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1909 "field-addr=%p, ft-addr=%p, ft-id=%s",
1910 integer
, field_type
,
1911 bt_ctf_field_type_id_string((int)
1912 bt_ctf_field_type_get_type_id(field_type
)));
1917 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
1918 ret
= bt_ctf_field_integer_signed_set_value(integer
,
1921 ret
= bt_ctf_field_integer_unsigned_set_value(integer
, value
);
1923 ret
= !ret
? 1 : ret
;
1925 bt_ctf_object_put_ref(integer
);
1926 bt_ctf_object_put_ref(field_type
);
1931 * Returns the following codes:
1932 * 1 if the field was found and set,
1933 * 0 if nothing was done (field not found, or was already set),
1934 * <0 if an error was encoutered
1937 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1940 return _set_structure_field_integer(structure
, name
, value
, BT_CTF_FALSE
);
1943 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
1944 struct bt_ctf_stream
*stream
)
1946 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream
)));
1949 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1951 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream
));
1954 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
1956 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream
));