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"
32 #include <babeltrace2-ctf-writer/field-types.h>
33 #include <babeltrace2-ctf-writer/object.h>
34 #include <babeltrace2-ctf-writer/stream-class.h>
35 #include <babeltrace2-ctf-writer/stream.h>
36 #include <babeltrace2-ctf-writer/trace.h>
38 #include "common/align.h"
39 #include "common/assert.h"
40 #include "compat/compiler.h"
41 #include "ctfser/ctfser.h"
43 #include "assert-pre.h"
44 #include "event-class.h"
47 #include "stream-class.h"
53 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common
*stream
)
55 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
56 stream
, bt_ctf_stream_common_get_name(stream
));
59 g_string_free(stream
->name
, TRUE
);
64 int bt_ctf_stream_common_initialize(
65 struct bt_ctf_stream_common
*stream
,
66 struct bt_ctf_stream_class_common
*stream_class
, const char *name
,
67 uint64_t id
, bt_ctf_object_release_func release_func
)
70 struct bt_ctf_trace_common
*trace
= NULL
;
72 bt_ctf_object_init_shared_with_parent(&stream
->base
, release_func
);
75 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
79 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
80 "stream-class-name=\"%s\", stream-name=\"%s\", "
82 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
84 trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
86 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
87 "stream-class-addr=%p, stream-class-name=\"%s\", "
90 bt_ctf_stream_class_common_get_name(stream_class
), name
);
96 * Validate that the given ID is unique amongst all the
97 * existing trace's streams created from the same stream
102 for (i
= 0; i
< trace
->streams
->len
; i
++) {
103 struct bt_ctf_stream_common
*trace_stream
=
104 g_ptr_array_index(trace
->streams
, i
);
106 if (trace_stream
->stream_class
!= (void *) stream_class
) {
110 if (trace_stream
->id
== id
) {
111 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
118 * Acquire reference to parent since stream will become publicly
119 * reachable; it needs its parent to remain valid.
121 bt_ctf_object_set_parent(&stream
->base
, &trace
->base
);
122 stream
->stream_class
= stream_class
;
123 stream
->id
= (int64_t) id
;
126 stream
->name
= g_string_new(name
);
128 BT_LOGE_STR("Failed to allocate a GString.");
133 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace
);
135 /* Add this stream to the trace's streams */
136 BT_LOGD("Created common stream object: addr=%p", stream
);
147 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
);
150 int try_set_structure_field_integer(struct bt_ctf_field
*, const char *, uint64_t);
153 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
156 struct bt_ctf_field_type
*field_type
= NULL
;
159 BT_LOGW_STR("Invalid parameter: field is NULL.");
164 field_type
= bt_ctf_field_get_type(field
);
165 BT_ASSERT_DBG(field_type
);
167 if (bt_ctf_field_type_get_type_id(field_type
) !=
168 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
169 /* Not an integer and the value is unset, error. */
170 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
171 "field-addr=%p, ft-addr=%p, ft-id=%s",
173 bt_ctf_field_type_id_string((int)
174 bt_ctf_field_type_get_type_id(field_type
)));
179 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
180 ret
= bt_ctf_field_integer_signed_set_value(field
, (int64_t) value
);
182 /* Value is out of range, error. */
183 BT_LOGW("Cannot set signed integer field's value: "
184 "addr=%p, value=%" PRId64
,
185 field
, (int64_t) value
);
189 ret
= bt_ctf_field_integer_unsigned_set_value(field
, value
);
191 /* Value is out of range, error. */
192 BT_LOGW("Cannot set unsigned integer field's value: "
193 "addr=%p, value=%" PRIu64
,
199 bt_ctf_object_put_ref(field_type
);
204 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
207 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field_by_name(
208 stream
->packet_header
, "magic");
209 const uint32_t magic_value
= 0xc1fc1fc1;
211 BT_ASSERT_DBG(stream
);
214 /* No magic field found. Not an error, skip. */
215 BT_LOGT("No field named `magic` in packet header: skipping: "
216 "stream-addr=%p, stream-name=\"%s\"",
217 stream
, bt_ctf_stream_get_name(stream
));
221 ret
= bt_ctf_field_integer_unsigned_set_value(magic_field
,
222 (uint64_t) magic_value
);
225 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
226 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
227 stream
, bt_ctf_stream_get_name(stream
),
228 magic_field
, (uint64_t) magic_value
);
230 BT_LOGT("Set packet header field's `magic` field's value: "
231 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
232 stream
, bt_ctf_stream_get_name(stream
),
233 magic_field
, (uint64_t) magic_value
);
236 bt_ctf_object_put_ref(magic_field
);
241 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
245 struct bt_ctf_trace
*trace
= NULL
;
246 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field_by_name(
247 stream
->packet_header
, "uuid");
249 BT_ASSERT_DBG(stream
);
252 /* No uuid field found. Not an error, skip. */
253 BT_LOGT("No field named `uuid` in packet header: skipping: "
254 "stream-addr=%p, stream-name=\"%s\"",
255 stream
, bt_ctf_stream_get_name(stream
));
259 trace
= (struct bt_ctf_trace
*)
260 bt_ctf_object_get_parent(&stream
->common
.base
);
262 for (i
= 0; i
< 16; i
++) {
263 struct bt_ctf_field
*uuid_element
=
264 bt_ctf_field_array_get_field(uuid_field
, i
);
266 ret
= bt_ctf_field_integer_unsigned_set_value(
267 uuid_element
, (uint64_t) trace
->common
.uuid
[i
]);
268 bt_ctf_object_put_ref(uuid_element
);
270 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
271 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
272 "value=%" PRIu64
", index=%" PRId64
,
273 stream
, bt_ctf_stream_get_name(stream
),
274 uuid_element
, (uint64_t) trace
->common
.uuid
[i
], i
);
279 BT_LOGT("Set packet header field's `uuid` field's value: "
280 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
281 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
284 bt_ctf_object_put_ref(uuid_field
);
285 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
289 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
293 struct bt_ctf_field
*stream_id_field
=
294 bt_ctf_field_structure_get_field_by_name(
295 stream
->packet_header
, "stream_id");
297 if (!stream_id_field
) {
298 /* No stream_id field found. Not an error, skip. */
299 BT_LOGT("No field named `stream_id` in packet header: skipping: "
300 "stream-addr=%p, stream-name=\"%s\"",
301 stream
, bt_ctf_stream_get_name(stream
));
305 stream_id
= stream
->common
.stream_class
->id
;
306 ret
= bt_ctf_field_integer_unsigned_set_value(stream_id_field
,
307 (uint64_t) stream_id
);
309 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
310 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
311 stream
, bt_ctf_stream_get_name(stream
),
312 stream_id_field
, (uint64_t) stream_id
);
314 BT_LOGT("Set packet header field's `stream_id` field's value: "
315 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
316 stream
, bt_ctf_stream_get_name(stream
),
317 stream_id_field
, (uint64_t) stream_id
);
321 bt_ctf_object_put_ref(stream_id_field
);
326 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
330 if (!stream
->packet_header
) {
334 ret
= set_packet_header_magic(stream
);
336 BT_LOGW("Cannot set packet header's magic number field: "
337 "stream-addr=%p, stream-name=\"%s\"",
338 stream
, bt_ctf_stream_get_name(stream
));
342 ret
= set_packet_header_uuid(stream
);
344 BT_LOGW("Cannot set packet header's UUID field: "
345 "stream-addr=%p, stream-name=\"%s\"",
346 stream
, bt_ctf_stream_get_name(stream
));
350 ret
= set_packet_header_stream_id(stream
);
352 BT_LOGW("Cannot set packet header's stream class ID field: "
353 "stream-addr=%p, stream-name=\"%s\"",
354 stream
, bt_ctf_stream_get_name(stream
));
358 BT_LOGT("Automatically populated stream's packet header's known fields: "
359 "stream-addr=%p, stream-name=\"%s\"",
360 stream
, bt_ctf_stream_get_name(stream
));
367 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
,
368 uint64_t packet_size_bits
)
371 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
372 stream
->packet_context
, "packet_size");
374 ret
= bt_ctf_field_integer_unsigned_set_value(field
, packet_size_bits
);
376 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
377 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
378 stream
, bt_ctf_stream_get_name(stream
),
379 field
, packet_size_bits
);
381 BT_LOGT("Set packet context field's `packet_size` field's value: "
382 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
383 stream
, bt_ctf_stream_get_name(stream
),
384 field
, packet_size_bits
);
387 bt_ctf_object_put_ref(field
);
392 int set_packet_context_content_size(struct bt_ctf_stream
*stream
,
393 uint64_t content_size_bits
)
396 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
397 stream
->packet_context
, "content_size");
399 BT_ASSERT_DBG(stream
);
402 /* No content size field found. Not an error, skip. */
403 BT_LOGT("No field named `content_size` in packet context: skipping: "
404 "stream-addr=%p, stream-name=\"%s\"",
405 stream
, bt_ctf_stream_get_name(stream
));
409 ret
= bt_ctf_field_integer_unsigned_set_value(field
, content_size_bits
);
411 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
412 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
413 stream
, bt_ctf_stream_get_name(stream
),
414 field
, content_size_bits
);
416 BT_LOGT("Set packet context field's `content_size` field's value: "
417 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
418 stream
, bt_ctf_stream_get_name(stream
),
419 field
, content_size_bits
);
423 bt_ctf_object_put_ref(field
);
428 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
431 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
432 stream
->packet_context
, "events_discarded");
434 BT_ASSERT_DBG(stream
);
437 /* No discarded events count field found. Not an error, skip. */
438 BT_LOGT("No field named `events_discarded` in packet context: skipping: "
439 "stream-addr=%p, stream-name=\"%s\"",
440 stream
, bt_ctf_stream_get_name(stream
));
445 * If the field is set by the user, make sure that the value is
446 * greater than or equal to the stream's current count of
447 * discarded events. We do not allow wrapping here. If it's
448 * valid, update the stream's current count.
450 if (bt_ctf_field_is_set_recursive(field
)) {
453 ret
= bt_ctf_field_integer_unsigned_get_value(field
,
456 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
457 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
458 stream
, bt_ctf_stream_get_name(stream
), field
);
462 if (user_val
< stream
->discarded_events
) {
463 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
464 "value is lesser than the stream's current discarded events count: "
465 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
466 "value=%" PRIu64
", "
467 "stream-discarded-events-count=%" PRIu64
,
468 stream
, bt_ctf_stream_get_name(stream
), field
,
469 user_val
, stream
->discarded_events
);
473 stream
->discarded_events
= user_val
;
475 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
476 stream
->discarded_events
);
478 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
479 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
480 stream
, bt_ctf_stream_get_name(stream
),
481 field
, stream
->discarded_events
);
483 BT_LOGT("Set packet context field's `events_discarded` field's value: "
484 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
485 stream
, bt_ctf_stream_get_name(stream
),
486 field
, stream
->discarded_events
);
491 bt_ctf_object_put_ref(field
);
496 void update_clock_value(uint64_t *val
, uint64_t new_val
,
497 unsigned int new_val_size
)
499 const uint64_t pow2
= 1ULL << new_val_size
;
500 const uint64_t mask
= pow2
- 1;
503 #ifdef BT_LOG_ENABLED_TRACE
504 uint64_t old_val
= *val
;
507 if (new_val_size
== 64) {
512 val_masked
= *val
& mask
;
514 if (new_val
< val_masked
) {
523 BT_LOGT("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
529 int visit_field_update_clock_value(struct bt_ctf_field
*field
, uint64_t *val
)
532 struct bt_ctf_field_common
*field_common
= (void *) field
;
538 switch (bt_ctf_field_get_type_id(field
)) {
539 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
541 struct bt_ctf_clock_class
*cc
=
542 bt_ctf_field_type_integer_get_mapped_clock_class(
543 (void *) field_common
->type
);
551 bt_ctf_object_put_ref(cc
);
552 val_size
= bt_ctf_field_type_integer_get_size(
553 (void *) field_common
->type
);
554 BT_ASSERT_DBG(val_size
>= 1);
556 if (bt_ctf_field_type_integer_is_signed(
557 (void *) field_common
->type
)) {
560 ret
= bt_ctf_field_integer_signed_get_value(field
, &ival
);
561 uval
= (uint64_t) ival
;
563 ret
= bt_ctf_field_integer_unsigned_get_value(field
, &uval
);
571 update_clock_value(val
, uval
, val_size
);
574 case BT_CTF_FIELD_TYPE_ID_ENUM
:
576 struct bt_ctf_field
*int_field
=
577 bt_ctf_field_enumeration_get_container(field
);
579 BT_ASSERT_DBG(int_field
);
580 ret
= visit_field_update_clock_value(int_field
, val
);
581 bt_ctf_object_put_ref(int_field
);
584 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
587 int64_t len
= bt_ctf_field_type_array_get_length(
588 (void *) field_common
->type
);
590 BT_ASSERT_DBG(len
>= 0);
592 for (i
= 0; i
< len
; i
++) {
593 struct bt_ctf_field
*elem_field
=
594 bt_ctf_field_array_get_field(field
, i
);
596 BT_ASSERT_DBG(elem_field
);
597 ret
= visit_field_update_clock_value(elem_field
, val
);
598 bt_ctf_object_put_ref(elem_field
);
605 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
608 int64_t len
= bt_ctf_field_common_sequence_get_length(
616 for (i
= 0; i
< len
; i
++) {
617 struct bt_ctf_field
*elem_field
=
618 bt_ctf_field_sequence_get_field(field
, i
);
620 BT_ASSERT_DBG(elem_field
);
621 ret
= visit_field_update_clock_value(elem_field
, val
);
622 bt_ctf_object_put_ref(elem_field
);
629 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
632 int64_t len
= bt_ctf_field_type_structure_get_field_count(
633 (void *) field_common
->type
);
635 BT_ASSERT_DBG(len
>= 0);
637 for (i
= 0; i
< len
; i
++) {
638 struct bt_ctf_field
*member_field
=
639 bt_ctf_field_structure_get_field_by_index(field
, i
);
641 BT_ASSERT_DBG(member_field
);
642 ret
= visit_field_update_clock_value(member_field
, val
);
643 bt_ctf_object_put_ref(member_field
);
650 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
652 struct bt_ctf_field
*cur_field
=
653 bt_ctf_field_variant_get_current_field(field
);
660 ret
= visit_field_update_clock_value(cur_field
, val
);
661 bt_ctf_object_put_ref(cur_field
);
673 int visit_event_update_clock_value(struct bt_ctf_event
*event
, uint64_t *val
)
676 struct bt_ctf_field
*field
;
678 field
= bt_ctf_event_get_header(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_stream_event_context(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 "
692 "event's stream event context.");
696 field
= bt_ctf_event_get_context(event
);
697 ret
= visit_field_update_clock_value(field
, val
);
698 bt_ctf_object_put_ref(field
);
700 BT_LOGW_STR("Cannot automatically update clock value in "
705 field
= bt_ctf_event_get_payload_field(event
);
706 ret
= visit_field_update_clock_value(field
, val
);
707 bt_ctf_object_put_ref(field
);
709 BT_LOGW_STR("Cannot automatically update clock value in "
719 int set_packet_context_timestamps(struct bt_ctf_stream
*stream
)
723 uint64_t cur_clock_value
;
724 uint64_t init_clock_value
= 0;
725 struct bt_ctf_field
*ts_begin_field
= bt_ctf_field_structure_get_field_by_name(
726 stream
->packet_context
, "timestamp_begin");
727 struct bt_ctf_field
*ts_end_field
= bt_ctf_field_structure_get_field_by_name(
728 stream
->packet_context
, "timestamp_end");
729 struct bt_ctf_field_common
*packet_context
=
730 (void *) stream
->packet_context
;
734 if (ts_begin_field
&& bt_ctf_field_is_set_recursive(ts_begin_field
)) {
735 /* Use provided `timestamp_begin` value as starting value */
736 ret
= bt_ctf_field_integer_unsigned_get_value(ts_begin_field
, &val
);
737 BT_ASSERT_DBG(ret
== 0);
738 init_clock_value
= val
;
739 } else if (stream
->last_ts_end
!= -1ULL) {
740 /* Use last packet's ending timestamp as starting value */
741 init_clock_value
= stream
->last_ts_end
;
744 cur_clock_value
= init_clock_value
;
746 if (stream
->last_ts_end
!= -1ULL &&
747 cur_clock_value
< stream
->last_ts_end
) {
748 BT_LOGW("Packet's initial timestamp is less than previous "
749 "packet's final timestamp: "
750 "stream-addr=%p, stream-name=\"%s\", "
751 "cur-packet-ts-begin=%" PRIu64
", "
752 "prev-packet-ts-end=%" PRIu64
,
753 stream
, bt_ctf_stream_get_name(stream
),
754 cur_clock_value
, stream
->last_ts_end
);
760 * Visit all the packet context fields, followed by all the
761 * fields of all the events, in order, updating our current
762 * clock value as we visit.
764 * While visiting the packet context fields, do not consider
765 * `timestamp_begin` and `timestamp_end` because this function's
766 * purpose is to set them anyway. Also do not consider
767 * `packet_size`, `content_size`, `events_discarded`, and
768 * `packet_seq_num` if they are not set because those are
769 * autopopulating fields.
771 len
= bt_ctf_field_type_structure_get_field_count(
772 (void *) packet_context
->type
);
773 BT_ASSERT_DBG(len
>= 0);
775 for (i
= 0; i
< len
; i
++) {
776 const char *member_name
;
777 struct bt_ctf_field
*member_field
;
779 ret
= bt_ctf_field_type_structure_get_field_by_index(
780 (void *) packet_context
->type
, &member_name
, NULL
, i
);
781 BT_ASSERT_DBG(ret
== 0);
783 if (strcmp(member_name
, "timestamp_begin") == 0 ||
784 strcmp(member_name
, "timestamp_end") == 0) {
788 member_field
= bt_ctf_field_structure_get_field_by_index(
789 stream
->packet_context
, i
);
790 BT_ASSERT_DBG(member_field
);
792 if (strcmp(member_name
, "packet_size") == 0 &&
793 !bt_ctf_field_is_set_recursive(member_field
)) {
794 bt_ctf_object_put_ref(member_field
);
798 if (strcmp(member_name
, "content_size") == 0 &&
799 !bt_ctf_field_is_set_recursive(member_field
)) {
800 bt_ctf_object_put_ref(member_field
);
804 if (strcmp(member_name
, "events_discarded") == 0 &&
805 !bt_ctf_field_is_set_recursive(member_field
)) {
806 bt_ctf_object_put_ref(member_field
);
810 if (strcmp(member_name
, "packet_seq_num") == 0 &&
811 !bt_ctf_field_is_set_recursive(member_field
)) {
812 bt_ctf_object_put_ref(member_field
);
816 ret
= visit_field_update_clock_value(member_field
,
818 bt_ctf_object_put_ref(member_field
);
820 BT_LOGW("Cannot automatically update clock value "
821 "in stream's packet context: "
822 "stream-addr=%p, stream-name=\"%s\", "
824 stream
, bt_ctf_stream_get_name(stream
),
830 for (i
= 0; i
< stream
->events
->len
; i
++) {
831 struct bt_ctf_event
*event
= g_ptr_array_index(stream
->events
, i
);
833 BT_ASSERT_DBG(event
);
834 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
836 BT_LOGW("Cannot automatically update clock value "
837 "in stream's packet context: "
838 "stream-addr=%p, stream-name=\"%s\", "
839 "index=%" PRIu64
", event-addr=%p, "
840 "event-class-id=%" PRId64
", "
841 "event-class-name=\"%s\"",
842 stream
, bt_ctf_stream_get_name(stream
),
844 bt_ctf_event_class_common_get_id(event
->common
.class),
845 bt_ctf_event_class_common_get_name(event
->common
.class));
851 * Everything is visited, thus the current clock value
852 * corresponds to the ending timestamp. Validate this value
853 * against the provided value of `timestamp_end`, if any,
856 if (ts_end_field
&& bt_ctf_field_is_set_recursive(ts_end_field
)) {
857 ret
= bt_ctf_field_integer_unsigned_get_value(ts_end_field
, &val
);
858 BT_ASSERT_DBG(ret
== 0);
860 if (val
< cur_clock_value
) {
861 BT_LOGW("Packet's final timestamp is less than "
862 "computed packet's final timestamp: "
863 "stream-addr=%p, stream-name=\"%s\", "
864 "cur-packet-ts-end=%" PRIu64
", "
865 "computed-packet-ts-end=%" PRIu64
,
866 stream
, bt_ctf_stream_get_name(stream
),
867 val
, cur_clock_value
);
872 stream
->last_ts_end
= val
;
875 if (ts_end_field
&& !bt_ctf_field_is_set_recursive(ts_end_field
)) {
876 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
877 BT_ASSERT_DBG(ret
== 0);
878 stream
->last_ts_end
= cur_clock_value
;
882 stream
->last_ts_end
= cur_clock_value
;
885 /* Set `timestamp_begin` field to initial clock value */
886 if (ts_begin_field
&& !bt_ctf_field_is_set_recursive(ts_begin_field
)) {
887 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
888 BT_ASSERT_DBG(ret
== 0);
892 bt_ctf_object_put_ref(ts_begin_field
);
893 bt_ctf_object_put_ref(ts_end_field
);
898 int auto_populate_packet_context(struct bt_ctf_stream
*stream
, bool set_ts
,
899 uint64_t packet_size_bits
, uint64_t content_size_bits
)
903 if (!stream
->packet_context
) {
907 ret
= set_packet_context_packet_size(stream
, packet_size_bits
);
909 BT_LOGW("Cannot set packet context's packet size field: "
910 "stream-addr=%p, stream-name=\"%s\"",
911 stream
, bt_ctf_stream_get_name(stream
));
915 ret
= set_packet_context_content_size(stream
, content_size_bits
);
917 BT_LOGW("Cannot set packet context's content size field: "
918 "stream-addr=%p, stream-name=\"%s\"",
919 stream
, bt_ctf_stream_get_name(stream
));
924 ret
= set_packet_context_timestamps(stream
);
926 BT_LOGW("Cannot set packet context's timestamp fields: "
927 "stream-addr=%p, stream-name=\"%s\"",
928 stream
, bt_ctf_stream_get_name(stream
));
933 ret
= set_packet_context_events_discarded(stream
);
935 BT_LOGW("Cannot set packet context's discarded events count field: "
936 "stream-addr=%p, stream-name=\"%s\"",
937 stream
, bt_ctf_stream_get_name(stream
));
941 BT_LOGT("Automatically populated stream's packet context's known fields: "
942 "stream-addr=%p, stream-name=\"%s\"",
943 stream
, bt_ctf_stream_get_name(stream
));
950 void release_event(struct bt_ctf_event
*event
)
952 if (bt_ctf_object_get_ref_count(&event
->common
.base
)) {
954 * The event is being orphaned, but it must guarantee the
955 * existence of its event class for the duration of its
958 bt_ctf_object_get_ref(event
->common
.class);
959 BT_CTF_OBJECT_PUT_REF_AND_RESET(event
->common
.base
.parent
);
961 bt_ctf_object_try_spec_release(&event
->common
.base
);
966 int create_stream_file(struct bt_ctf_writer
*writer
,
967 struct bt_ctf_stream
*stream
)
970 GString
*filename
= g_string_new(NULL
);
971 int64_t stream_class_id
;
972 char *file_path
= NULL
;
974 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
975 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
976 writer
, stream
, bt_ctf_stream_get_name(stream
),
977 stream
->common
.stream_class
,
978 stream
->common
.stream_class
->name
->str
);
980 if (stream
->common
.name
&& stream
->common
.name
->len
> 0) {
981 /* Use stream name's base name as prefix */
982 gchar
*basename
= g_path_get_basename(stream
->common
.name
->str
);
984 BT_ASSERT_DBG(basename
);
986 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
987 g_string_assign(filename
, "stream");
989 g_string_assign(filename
, basename
);
996 if (stream
->common
.stream_class
->name
&&
997 stream
->common
.stream_class
->name
->len
> 0) {
998 /* Use stream class name's base name as prefix */
1000 g_path_get_basename(
1001 stream
->common
.stream_class
->name
->str
);
1003 BT_ASSERT_DBG(basename
);
1005 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
1006 g_string_assign(filename
, "stream");
1008 g_string_assign(filename
, basename
);
1015 /* Default to using `stream-` as prefix */
1016 g_string_assign(filename
, "stream");
1019 stream_class_id
= bt_ctf_stream_class_common_get_id(stream
->common
.stream_class
);
1020 BT_ASSERT_DBG(stream_class_id
>= 0);
1021 BT_ASSERT_DBG(stream
->common
.id
>= 0);
1022 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
1023 stream_class_id
, stream
->common
.id
);
1025 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
1031 ret
= bt_ctfser_init(&stream
->ctfser
, file_path
,
1032 BT_LOG_OUTPUT_LEVEL
);
1035 /* bt_ctfser_init() logs errors */
1039 BT_LOGD("Created stream file for writing: "
1040 "stream-addr=%p, stream-name=\"%s\", "
1041 "filename=\"%s\"", stream
, bt_ctf_stream_get_name(stream
),
1045 g_string_free(filename
, TRUE
);
1050 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
1051 struct bt_ctf_stream_class
*stream_class
,
1052 const char *name
, uint64_t id
)
1056 struct bt_ctf_stream
*stream
= NULL
;
1057 struct bt_ctf_trace
*trace
= NULL
;
1058 struct bt_ctf_writer
*writer
= NULL
;
1060 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1061 "stream-class-name=\"%s\", stream-name=\"%s\", "
1062 "stream-id=%" PRIu64
,
1063 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1065 stream
= g_new0(struct bt_ctf_stream
, 1);
1067 BT_LOGE_STR("Failed to allocate one stream.");
1072 id
= stream_class
->next_stream_id
;
1075 ret
= bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream
),
1076 BT_CTF_TO_COMMON(stream_class
), name
, id
, bt_ctf_stream_destroy
);
1078 /* bt_ctf_stream_common_initialize() logs errors */
1082 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1083 BT_CTF_TO_COMMON(stream_class
)));
1085 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1086 "stream-class-addr=%p, stream-class-name=\"%s\", "
1087 "stream-name=\"%s\"",
1088 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1093 writer
= (struct bt_ctf_writer
*)
1094 bt_ctf_object_get_parent(&trace
->common
.base
);
1095 stream
->last_ts_end
= -1ULL;
1096 BT_LOGD("CTF writer stream object belongs writer's trace: "
1097 "writer-addr=%p", writer
);
1098 BT_ASSERT_DBG(writer
);
1100 if (stream_class
->common
.packet_context_field_type
) {
1101 BT_LOGD("Creating stream's packet context field: "
1103 stream_class
->common
.packet_context_field_type
);
1104 stream
->packet_context
= bt_ctf_field_create(
1105 (void *) stream_class
->common
.packet_context_field_type
);
1106 if (!stream
->packet_context
) {
1107 BT_LOGW_STR("Cannot create stream's packet context field.");
1111 /* Initialize events_discarded */
1112 ret
= try_set_structure_field_integer(
1113 stream
->packet_context
, "events_discarded", 0);
1115 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1116 "ret=%d, packet-context-field-addr=%p",
1117 ret
, stream
->packet_context
);
1122 stream
->events
= g_ptr_array_new_with_free_func(
1123 (GDestroyNotify
) release_event
);
1124 if (!stream
->events
) {
1125 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1129 if (trace
->common
.packet_header_field_type
) {
1130 BT_LOGD("Creating stream's packet header field: "
1131 "ft-addr=%p", trace
->common
.packet_header_field_type
);
1132 stream
->packet_header
=
1133 bt_ctf_field_create(
1134 (void *) trace
->common
.packet_header_field_type
);
1135 if (!stream
->packet_header
) {
1136 BT_LOGW_STR("Cannot create stream's packet header field.");
1142 * Attempt to populate the default trace packet header fields
1143 * (magic, uuid and stream_id). This will _not_ fail shall the
1144 * fields not be found or be of an incompatible type; they will
1145 * simply not be populated automatically. The user will have to
1146 * make sure to set the trace packet header fields himself
1149 ret
= auto_populate_packet_header(stream
);
1151 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1155 /* Create file associated with this stream */
1156 fd
= create_stream_file(writer
, stream
);
1158 BT_LOGW_STR("Cannot create stream file.");
1162 /* Freeze the writer */
1163 BT_LOGD_STR("Freezing stream's CTF writer.");
1164 bt_ctf_writer_freeze(writer
);
1166 /* Add this stream to the trace's streams */
1167 g_ptr_array_add(trace
->common
.streams
, stream
);
1168 stream_class
->next_stream_id
++;
1169 BT_LOGD("Created stream object: addr=%p", stream
);
1173 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
1176 bt_ctf_object_put_ref(writer
);
1180 struct bt_ctf_stream
*bt_ctf_stream_create(
1181 struct bt_ctf_stream_class
*stream_class
,
1182 const char *name
, uint64_t id_param
)
1184 return bt_ctf_stream_create_with_id(stream_class
,
1188 int bt_ctf_stream_get_discarded_events_count(
1189 struct bt_ctf_stream
*stream
, uint64_t *count
)
1194 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1200 BT_LOGW_STR("Invalid parameter: count is NULL.");
1205 *count
= (uint64_t) stream
->discarded_events
;
1212 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1216 struct bt_ctf_field
*events_discarded_field
= NULL
;
1218 if (!stream
->packet_context
) {
1222 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1223 stream
->packet_context
, "events_discarded");
1224 if (!events_discarded_field
) {
1228 ret
= bt_ctf_field_integer_unsigned_set_value(
1229 events_discarded_field
, count
);
1231 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1232 "field-addr=%p, value=%" PRIu64
,
1233 events_discarded_field
, count
);
1238 bt_ctf_object_put_ref(events_discarded_field
);
1242 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1243 uint64_t event_count
)
1247 struct bt_ctf_field
*events_discarded_field
= NULL
;
1250 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1254 BT_LOGT("Appending discarded events to stream: "
1255 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1256 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1258 if (!stream
->packet_context
) {
1259 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1263 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1264 stream
->packet_context
, "events_discarded");
1265 if (!events_discarded_field
) {
1266 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1270 new_count
= stream
->discarded_events
+ event_count
;
1271 if (new_count
< stream
->discarded_events
) {
1272 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1273 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1274 stream
->discarded_events
, new_count
);
1278 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1280 /* set_packet_context_events_discarded_field() logs errors */
1284 stream
->discarded_events
= new_count
;
1285 BT_LOGT("Appended discarded events to stream: "
1286 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1287 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1290 bt_ctf_object_put_ref(events_discarded_field
);
1293 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1294 struct bt_ctf_event
*event
)
1297 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1298 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1299 struct bt_ctf_stream_class
*stream_class
=
1300 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1301 BT_CTF_TO_COMMON(stream
)));
1302 int64_t event_class_id
;
1304 BT_ASSERT_DBG(event
);
1306 if (!event
->common
.header_field
) {
1310 if (event
->common
.frozen
) {
1311 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1316 BT_LOGT("Automatically populating event's header field: "
1317 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1318 stream
, bt_ctf_stream_get_name(stream
), event
);
1320 id_field
= bt_ctf_field_structure_get_field_by_name(
1321 (void *) event
->common
.header_field
->field
, "id");
1322 event_class_id
= bt_ctf_event_class_common_get_id(event
->common
.class);
1323 BT_ASSERT_DBG(event_class_id
>= 0);
1325 if (id_field
&& bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1326 ret
= set_integer_field_value(id_field
, event_class_id
);
1328 BT_LOGW("Cannot set event header's `id` field's value: "
1329 "addr=%p, value=%" PRIu64
, id_field
,
1336 * The conditions to automatically set the timestamp are:
1338 * 1. The event header field "timestamp" exists and is an
1340 * 2. This stream's class has a registered clock (set with
1341 * bt_ctf_stream_class_set_clock()).
1342 * 3. The "timestamp" field is not set.
1344 timestamp_field
= bt_ctf_field_structure_get_field_by_name(
1345 (void *) event
->common
.header_field
->field
, "timestamp");
1346 if (timestamp_field
&& stream_class
->clock
&&
1347 bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
&&
1348 !bt_ctf_field_is_set_recursive(timestamp_field
)) {
1349 mapped_clock_class
=
1350 bt_ctf_field_type_integer_get_mapped_clock_class(
1351 (void *) ((struct bt_ctf_field_common
*) timestamp_field
)->type
);
1352 if (mapped_clock_class
) {
1355 BT_ASSERT_DBG(mapped_clock_class
==
1356 stream_class
->clock
->clock_class
);
1357 ret
= bt_ctf_clock_get_value(
1358 stream_class
->clock
,
1360 BT_ASSERT_DBG(ret
== 0);
1361 ret
= set_integer_field_value(timestamp_field
,
1364 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1365 "addr=%p, value=%" PRIu64
,
1366 timestamp_field
, timestamp
);
1372 BT_LOGT("Automatically populated event's header field: "
1373 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1374 stream
, bt_ctf_stream_get_name(stream
), event
);
1377 bt_ctf_object_put_ref(id_field
);
1378 bt_ctf_object_put_ref(timestamp_field
);
1379 bt_ctf_object_put_ref(mapped_clock_class
);
1383 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1384 struct bt_ctf_event
*event
)
1389 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1395 BT_LOGW_STR("Invalid parameter: event is NULL.");
1400 BT_LOGT("Appending event to stream: "
1401 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1402 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1403 stream
, bt_ctf_stream_get_name(stream
), event
,
1404 bt_ctf_event_class_common_get_name(
1405 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1406 bt_ctf_event_class_common_get_id(
1407 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1410 * The event is not supposed to have a parent stream at this
1411 * point. The only other way an event can have a parent stream
1412 * is if it was assigned when setting a packet to the event,
1413 * in which case the packet's stream is not a writer stream,
1414 * and thus the user is trying to append an event which belongs
1415 * to another stream.
1417 if (event
->common
.base
.parent
) {
1422 bt_ctf_object_set_parent(&event
->common
.base
, &stream
->common
.base
);
1423 BT_LOGT_STR("Automatically populating the header of the event to append.");
1424 ret
= auto_populate_event_header(stream
, event
);
1426 /* auto_populate_event_header() reports errors */
1430 /* Make sure the various scopes of the event are set */
1431 BT_LOGT_STR("Validating event to append.");
1432 BT_CTF_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event
)) == 0,
1433 "Invalid event: event-addr=%p", event
);
1435 /* Save the new event and freeze it */
1436 BT_LOGT_STR("Freezing the event to append.");
1437 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event
), true);
1438 g_ptr_array_add(stream
->events
, event
);
1441 * Event had to hold a reference to its event class as long as it wasn't
1442 * part of the same trace hierarchy. From now on, the event and its
1443 * class share the same lifetime guarantees and the reference is no
1446 BT_LOGT_STR("Putting the event's class.");
1447 bt_ctf_object_put_ref(event
->common
.class);
1448 BT_LOGT("Appended event to stream: "
1449 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1450 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1451 stream
, bt_ctf_stream_get_name(stream
), event
,
1452 bt_ctf_event_class_common_get_name(
1453 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1454 bt_ctf_event_class_common_get_id(
1455 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1462 * Orphan the event; we were not successful in associating it to
1465 bt_ctf_object_set_parent(&event
->common
.base
, NULL
);
1469 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(struct bt_ctf_stream
*stream
)
1471 struct bt_ctf_field
*packet_context
= NULL
;
1474 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1478 packet_context
= stream
->packet_context
;
1479 if (packet_context
) {
1480 bt_ctf_object_get_ref(packet_context
);
1483 return packet_context
;
1486 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1487 struct bt_ctf_field
*field
)
1490 struct bt_ctf_field_type
*field_type
;
1493 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1498 field_type
= bt_ctf_field_get_type(field
);
1499 if (bt_ctf_field_type_common_compare((void *) field_type
,
1500 stream
->common
.stream_class
->packet_context_field_type
)) {
1501 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1502 "stream-addr=%p, stream-name=\"%s\", "
1503 "packet-context-field-addr=%p, "
1504 "packet-context-ft-addr=%p",
1505 stream
, bt_ctf_stream_get_name(stream
),
1511 bt_ctf_object_put_ref(field_type
);
1512 bt_ctf_object_put_ref(stream
->packet_context
);
1513 stream
->packet_context
= bt_ctf_object_get_ref(field
);
1514 BT_LOGT("Set stream's packet context field: "
1515 "stream-addr=%p, stream-name=\"%s\", "
1516 "packet-context-field-addr=%p",
1517 stream
, bt_ctf_stream_get_name(stream
), field
);
1522 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(struct bt_ctf_stream
*stream
)
1524 struct bt_ctf_field
*packet_header
= NULL
;
1527 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1531 packet_header
= stream
->packet_header
;
1532 if (packet_header
) {
1533 bt_ctf_object_get_ref(packet_header
);
1536 return packet_header
;
1539 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1540 struct bt_ctf_field
*field
)
1543 struct bt_ctf_trace
*trace
= NULL
;
1544 struct bt_ctf_field_type
*field_type
= NULL
;
1547 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1552 trace
= (struct bt_ctf_trace
*)
1553 bt_ctf_object_get_parent(&stream
->common
.base
);
1556 if (trace
->common
.packet_header_field_type
) {
1557 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1558 "stream-addr=%p, stream-name=\"%s\", "
1559 "packet-header-field-addr=%p, "
1560 "expected-ft-addr=%p",
1561 stream
, bt_ctf_stream_get_name(stream
),
1562 field
, trace
->common
.packet_header_field_type
);
1567 goto skip_validation
;
1570 field_type
= bt_ctf_field_get_type(field
);
1571 BT_ASSERT_DBG(field_type
);
1573 if (bt_ctf_field_type_common_compare((void *) field_type
,
1574 trace
->common
.packet_header_field_type
)) {
1575 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1576 "stream-addr=%p, stream-name=\"%s\", "
1577 "packet-header-field-addr=%p, "
1578 "packet-header-ft-addr=%p",
1579 stream
, bt_ctf_stream_get_name(stream
),
1586 bt_ctf_object_put_ref(stream
->packet_header
);
1587 stream
->packet_header
= bt_ctf_object_get_ref(field
);
1588 BT_LOGT("Set stream's packet header field: "
1589 "stream-addr=%p, stream-name=\"%s\", "
1590 "packet-header-field-addr=%p",
1591 stream
, bt_ctf_stream_get_name(stream
), field
);
1593 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1594 bt_ctf_object_put_ref(field_type
);
1599 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1601 struct bt_ctf_field
*member
;
1603 member
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1605 bt_ctf_field_common_reset_recursive((void *) member
);
1606 bt_ctf_object_put_ref(member
);
1610 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1614 uint64_t packet_context_offset_bits
= 0;
1615 struct bt_ctf_trace
*trace
;
1616 enum bt_ctf_byte_order native_byte_order
;
1617 bool has_packet_size
= false;
1618 uint64_t packet_size_bits
= 0;
1619 uint64_t content_size_bits
= 0;
1622 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1627 if (stream
->packet_context
) {
1628 struct bt_ctf_field
*packet_size_field
;
1630 packet_size_field
= bt_ctf_field_structure_get_field_by_name(
1631 stream
->packet_context
, "packet_size");
1632 has_packet_size
= packet_size_field
;
1633 bt_ctf_object_put_ref(packet_size_field
);
1636 if (stream
->flushed_packet_count
== 1) {
1637 if (!stream
->packet_context
) {
1638 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1643 if (!has_packet_size
) {
1644 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1650 BT_LOGT("Flushing stream's current packet: stream-addr=%p, "
1651 "stream-name=\"%s\", packet-index=%u", stream
,
1652 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1653 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1654 stream
->common
.stream_class
));
1655 BT_ASSERT_DBG(trace
);
1656 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1658 ret
= auto_populate_packet_header(stream
);
1660 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1665 /* Initialize packet/content sizes to `0`; we will overwrite later */
1666 ret
= auto_populate_packet_context(stream
, true, 0, 0);
1668 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1673 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
1675 /* bt_ctfser_open_packet() logs errors */
1680 if (stream
->packet_header
) {
1681 BT_LOGT_STR("Serializing packet header field (initial).");
1682 ret
= bt_ctf_field_serialize_recursive(stream
->packet_header
,
1683 &stream
->ctfser
, native_byte_order
);
1685 BT_LOGW("Cannot serialize stream's packet header field: "
1686 "field-addr=%p", stream
->packet_header
);
1691 if (stream
->packet_context
) {
1692 /* Save packet context's position to overwrite it later */
1693 packet_context_offset_bits
=
1694 bt_ctfser_get_offset_in_current_packet_bits(
1697 /* Write packet context */
1698 BT_LOGT_STR("Serializing packet context field (initial).");
1699 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1700 &stream
->ctfser
, native_byte_order
);
1702 BT_LOGW("Cannot serialize stream's packet context field: "
1703 "field-addr=%p", stream
->packet_context
);
1708 BT_LOGT("Serializing events: count=%u", stream
->events
->len
);
1710 for (i
= 0; i
< stream
->events
->len
; i
++) {
1711 struct bt_ctf_event
*event
= g_ptr_array_index(
1713 struct bt_ctf_event_class
*event_class
=
1714 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1715 BT_CTF_TO_COMMON(event
)));
1717 BT_LOGT("Serializing event: index=%zu, event-addr=%p, "
1718 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1719 "ser-offset=%" PRIu64
,
1720 i
, event
, bt_ctf_event_class_get_name(event_class
),
1721 bt_ctf_event_class_get_id(event_class
),
1722 bt_ctfser_get_offset_in_current_packet_bits(
1725 /* Write event header */
1726 if (event
->common
.header_field
) {
1727 BT_LOGT_STR("Serializing event's header field.");
1728 ret
= bt_ctf_field_serialize_recursive(
1729 (void *) event
->common
.header_field
->field
,
1730 &stream
->ctfser
, native_byte_order
);
1732 BT_LOGW("Cannot serialize event's header field: "
1734 event
->common
.header_field
->field
);
1739 /* Write stream event context */
1740 if (event
->common
.stream_event_context_field
) {
1741 BT_LOGT_STR("Serializing event's stream event context field.");
1742 ret
= bt_ctf_field_serialize_recursive(
1743 (void *) event
->common
.stream_event_context_field
,
1744 &stream
->ctfser
, native_byte_order
);
1746 BT_LOGW("Cannot serialize event's stream event context field: "
1748 event
->common
.stream_event_context_field
);
1753 /* Write event content */
1754 ret
= bt_ctf_event_serialize(event
, &stream
->ctfser
,
1757 /* bt_ctf_event_serialize() logs errors */
1762 content_size_bits
= bt_ctfser_get_offset_in_current_packet_bits(
1765 if (!has_packet_size
&& content_size_bits
% 8 != 0) {
1766 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1767 "but current content size is not a multiple of 8 bits: "
1768 "content-size=%" PRIu64
", "
1769 "packet-size=%" PRIu64
,
1776 /* Set packet size; make it a multiple of 8 */
1777 packet_size_bits
= (content_size_bits
+ 7) & ~UINT64_C(7);
1779 if (stream
->packet_context
) {
1781 * The whole packet is serialized at this point. Make
1782 * sure that, if `packet_size` is missing, the current
1783 * content size is equal to the current packet size.
1785 struct bt_ctf_field
*field
=
1786 bt_ctf_field_structure_get_field_by_name(
1787 stream
->packet_context
, "content_size");
1789 bt_ctf_object_put_ref(field
);
1791 if (content_size_bits
!= packet_size_bits
) {
1792 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1793 "but current packet's content size is not equal to its packet size: "
1794 "content-size=%" PRIu64
", "
1795 "packet-size=%" PRIu64
,
1796 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
),
1804 * Overwrite the packet context now that the stream
1805 * position's packet and content sizes have the correct
1808 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
1809 packet_context_offset_bits
);
1810 ret
= auto_populate_packet_context(stream
, false,
1811 packet_size_bits
, content_size_bits
);
1813 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1818 BT_LOGT("Rewriting (serializing) packet context field.");
1819 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1820 &stream
->ctfser
, native_byte_order
);
1822 BT_LOGW("Cannot serialize stream's packet context field: "
1823 "field-addr=%p", stream
->packet_context
);
1828 g_ptr_array_set_size(stream
->events
, 0);
1829 stream
->flushed_packet_count
++;
1830 bt_ctfser_close_current_packet(&stream
->ctfser
, packet_size_bits
/ 8);
1833 /* Reset automatically-set fields. */
1834 if (stream
->packet_context
) {
1835 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1836 reset_structure_field(stream
->packet_context
, "timestamp_end");
1837 reset_structure_field(stream
->packet_context
, "packet_size");
1838 reset_structure_field(stream
->packet_context
, "content_size");
1839 reset_structure_field(stream
->packet_context
, "events_discarded");
1843 BT_LOGT("Flushed stream's current packet: "
1844 "content-size=%" PRIu64
", packet-size=%" PRIu64
,
1845 content_size_bits
, packet_size_bits
);
1853 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
)
1855 struct bt_ctf_stream
*stream
= (void *) obj
;
1857 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1858 stream
, bt_ctf_stream_get_name(stream
));
1860 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream
));
1861 bt_ctfser_fini(&stream
->ctfser
);
1863 if (stream
->events
) {
1864 BT_LOGD_STR("Putting events.");
1865 g_ptr_array_free(stream
->events
, TRUE
);
1868 BT_LOGD_STR("Putting packet header field.");
1869 bt_ctf_object_put_ref(stream
->packet_header
);
1870 BT_LOGD_STR("Putting packet context field.");
1871 bt_ctf_object_put_ref(stream
->packet_context
);
1876 int _set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1877 uint64_t value
, bt_ctf_bool force
)
1880 struct bt_ctf_field_type
*field_type
= NULL
;
1881 struct bt_ctf_field
*integer
;
1883 BT_ASSERT_DBG(structure
);
1884 BT_ASSERT_DBG(name
);
1886 integer
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1888 /* Field not found, not an error. */
1889 BT_LOGT("Field not found: struct-field-addr=%p, "
1890 "name=\"%s\", force=%d", structure
, name
, force
);
1894 /* Make sure the payload has not already been set. */
1895 if (!force
&& bt_ctf_field_is_set_recursive(integer
)) {
1896 /* Payload already set, not an error */
1897 BT_LOGT("Field's payload is already set: struct-field-addr=%p, "
1898 "name=\"%s\", force=%d", structure
, name
, force
);
1902 field_type
= bt_ctf_field_get_type(integer
);
1903 BT_ASSERT_DBG(field_type
);
1904 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1906 * The user most likely meant for us to populate this field
1907 * automatically. However, we can only do this if the field
1908 * is an integer. Return an error.
1910 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1911 "field-addr=%p, ft-addr=%p, ft-id=%s",
1912 integer
, field_type
,
1913 bt_ctf_field_type_id_string((int)
1914 bt_ctf_field_type_get_type_id(field_type
)));
1919 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
1920 ret
= bt_ctf_field_integer_signed_set_value(integer
,
1923 ret
= bt_ctf_field_integer_unsigned_set_value(integer
, value
);
1925 ret
= !ret
? 1 : ret
;
1927 bt_ctf_object_put_ref(integer
);
1928 bt_ctf_object_put_ref(field_type
);
1933 * Returns the following codes:
1934 * 1 if the field was found and set,
1935 * 0 if nothing was done (field not found, or was already set),
1936 * <0 if an error was encoutered
1939 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, const char *name
,
1942 return _set_structure_field_integer(structure
, name
, value
, BT_CTF_FALSE
);
1945 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
1946 struct bt_ctf_stream
*stream
)
1948 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream
)));
1951 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1953 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream
));
1956 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
1958 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream
));