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"
25 #include <babeltrace2/lib-logging-internal.h>
27 #include <babeltrace2/assert-pre-internal.h>
28 #include <babeltrace2/align-internal.h>
29 #include <babeltrace2/assert-internal.h>
30 #include <babeltrace2/compiler-internal.h>
31 #include <babeltrace2/ctf-writer/event-class-internal.h>
32 #include <babeltrace2/ctf-writer/event-internal.h>
33 #include <babeltrace2/ctf-writer/field-types.h>
34 #include <babeltrace2/ctf-writer/fields-internal.h>
35 #include <babeltrace2/ctf-writer/stream-class-internal.h>
36 #include <babeltrace2/ctf-writer/stream-class.h>
37 #include <babeltrace2/ctf-writer/stream-internal.h>
38 #include <babeltrace2/ctf-writer/stream.h>
39 #include <babeltrace2/ctf-writer/trace-internal.h>
40 #include <babeltrace2/ctf-writer/trace.h>
41 #include <babeltrace2/ctf-writer/writer-internal.h>
42 #include <babeltrace2/ctf-writer/object.h>
43 #include <babeltrace2/ctfser-internal.h>
49 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common
*stream
)
51 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
52 stream
, bt_ctf_stream_common_get_name(stream
));
55 g_string_free(stream
->name
, TRUE
);
60 int bt_ctf_stream_common_initialize(
61 struct bt_ctf_stream_common
*stream
,
62 struct bt_ctf_stream_class_common
*stream_class
, const char *name
,
63 uint64_t id
, bt_ctf_object_release_func release_func
)
66 struct bt_ctf_trace_common
*trace
= NULL
;
68 bt_ctf_object_init_shared_with_parent(&stream
->base
, release_func
);
71 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
75 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
76 "stream-class-name=\"%s\", stream-name=\"%s\", "
78 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
80 trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
82 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
83 "stream-class-addr=%p, stream-class-name=\"%s\", "
86 bt_ctf_stream_class_common_get_name(stream_class
), name
);
92 * Validate that the given ID is unique amongst all the
93 * existing trace's streams created from the same stream
98 for (i
= 0; i
< trace
->streams
->len
; i
++) {
99 struct bt_ctf_stream_common
*trace_stream
=
100 g_ptr_array_index(trace
->streams
, i
);
102 if (trace_stream
->stream_class
!= (void *) stream_class
) {
106 if (trace_stream
->id
== id
) {
107 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
114 * Acquire reference to parent since stream will become publicly
115 * reachable; it needs its parent to remain valid.
117 bt_ctf_object_set_parent(&stream
->base
, &trace
->base
);
118 stream
->stream_class
= stream_class
;
119 stream
->id
= (int64_t) id
;
122 stream
->name
= g_string_new(name
);
124 BT_LOGE_STR("Failed to allocate a GString.");
129 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace
);
131 /* Add this stream to the trace's streams */
132 BT_LOGD("Created common stream object: addr=%p", stream
);
143 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
);
146 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
149 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
152 struct bt_ctf_field_type
*field_type
= NULL
;
155 BT_LOGW_STR("Invalid parameter: field is NULL.");
160 field_type
= bt_ctf_field_get_type(field
);
161 BT_ASSERT(field_type
);
163 if (bt_ctf_field_type_get_type_id(field_type
) !=
164 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
165 /* Not an integer and the value is unset, error. */
166 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
167 "field-addr=%p, ft-addr=%p, ft-id=%s",
169 bt_ctf_field_type_id_string((int)
170 bt_ctf_field_type_get_type_id(field_type
)));
175 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
176 ret
= bt_ctf_field_integer_signed_set_value(field
, (int64_t) value
);
178 /* Value is out of range, error. */
179 BT_LOGW("Cannot set signed integer field's value: "
180 "addr=%p, value=%" PRId64
,
181 field
, (int64_t) value
);
185 ret
= bt_ctf_field_integer_unsigned_set_value(field
, value
);
187 /* Value is out of range, error. */
188 BT_LOGW("Cannot set unsigned integer field's value: "
189 "addr=%p, value=%" PRIu64
,
195 bt_ctf_object_put_ref(field_type
);
200 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
203 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field_by_name(
204 stream
->packet_header
, "magic");
205 const uint32_t magic_value
= 0xc1fc1fc1;
210 /* No magic field found. Not an error, skip. */
211 BT_LOGV("No field named `magic` in packet header: skipping: "
212 "stream-addr=%p, stream-name=\"%s\"",
213 stream
, bt_ctf_stream_get_name(stream
));
217 ret
= bt_ctf_field_integer_unsigned_set_value(magic_field
,
218 (uint64_t) magic_value
);
221 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
222 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
223 stream
, bt_ctf_stream_get_name(stream
),
224 magic_field
, (uint64_t) magic_value
);
226 BT_LOGV("Set packet header field's `magic` field's value: "
227 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
228 stream
, bt_ctf_stream_get_name(stream
),
229 magic_field
, (uint64_t) magic_value
);
232 bt_ctf_object_put_ref(magic_field
);
237 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
241 struct bt_ctf_trace
*trace
= NULL
;
242 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field_by_name(
243 stream
->packet_header
, "uuid");
248 /* No uuid field found. Not an error, skip. */
249 BT_LOGV("No field named `uuid` in packet header: skipping: "
250 "stream-addr=%p, stream-name=\"%s\"",
251 stream
, bt_ctf_stream_get_name(stream
));
255 trace
= (struct bt_ctf_trace
*)
256 bt_ctf_object_get_parent(&stream
->common
.base
);
258 for (i
= 0; i
< 16; i
++) {
259 struct bt_ctf_field
*uuid_element
=
260 bt_ctf_field_array_get_field(uuid_field
, i
);
262 ret
= bt_ctf_field_integer_unsigned_set_value(
263 uuid_element
, (uint64_t) trace
->common
.uuid
[i
]);
264 bt_ctf_object_put_ref(uuid_element
);
266 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
267 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
268 "value=%" PRIu64
", index=%" PRId64
,
269 stream
, bt_ctf_stream_get_name(stream
),
270 uuid_element
, (uint64_t) trace
->common
.uuid
[i
], i
);
275 BT_LOGV("Set packet header field's `uuid` field's value: "
276 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
277 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
280 bt_ctf_object_put_ref(uuid_field
);
281 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
285 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
289 struct bt_ctf_field
*stream_id_field
=
290 bt_ctf_field_structure_get_field_by_name(
291 stream
->packet_header
, "stream_id");
293 if (!stream_id_field
) {
294 /* No stream_id field found. Not an error, skip. */
295 BT_LOGV("No field named `stream_id` in packet header: skipping: "
296 "stream-addr=%p, stream-name=\"%s\"",
297 stream
, bt_ctf_stream_get_name(stream
));
301 stream_id
= stream
->common
.stream_class
->id
;
302 ret
= bt_ctf_field_integer_unsigned_set_value(stream_id_field
,
303 (uint64_t) stream_id
);
305 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
306 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
307 stream
, bt_ctf_stream_get_name(stream
),
308 stream_id_field
, (uint64_t) stream_id
);
310 BT_LOGV("Set packet header field's `stream_id` field's value: "
311 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
312 stream
, bt_ctf_stream_get_name(stream
),
313 stream_id_field
, (uint64_t) stream_id
);
317 bt_ctf_object_put_ref(stream_id_field
);
322 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
326 if (!stream
->packet_header
) {
330 ret
= set_packet_header_magic(stream
);
332 BT_LOGW("Cannot set packet header's magic number field: "
333 "stream-addr=%p, stream-name=\"%s\"",
334 stream
, bt_ctf_stream_get_name(stream
));
338 ret
= set_packet_header_uuid(stream
);
340 BT_LOGW("Cannot set packet header's UUID field: "
341 "stream-addr=%p, stream-name=\"%s\"",
342 stream
, bt_ctf_stream_get_name(stream
));
346 ret
= set_packet_header_stream_id(stream
);
348 BT_LOGW("Cannot set packet header's stream class ID field: "
349 "stream-addr=%p, stream-name=\"%s\"",
350 stream
, bt_ctf_stream_get_name(stream
));
354 BT_LOGV("Automatically populated stream's packet header's known fields: "
355 "stream-addr=%p, stream-name=\"%s\"",
356 stream
, bt_ctf_stream_get_name(stream
));
363 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
,
364 uint64_t packet_size_bits
)
367 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
368 stream
->packet_context
, "packet_size");
370 ret
= bt_ctf_field_integer_unsigned_set_value(field
, packet_size_bits
);
372 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
373 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
374 stream
, bt_ctf_stream_get_name(stream
),
375 field
, packet_size_bits
);
377 BT_LOGV("Set packet context field's `packet_size` field's value: "
378 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
379 stream
, bt_ctf_stream_get_name(stream
),
380 field
, packet_size_bits
);
383 bt_ctf_object_put_ref(field
);
388 int set_packet_context_content_size(struct bt_ctf_stream
*stream
,
389 uint64_t content_size_bits
)
392 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
393 stream
->packet_context
, "content_size");
398 /* No content size field found. Not an error, skip. */
399 BT_LOGV("No field named `content_size` in packet context: skipping: "
400 "stream-addr=%p, stream-name=\"%s\"",
401 stream
, bt_ctf_stream_get_name(stream
));
405 ret
= bt_ctf_field_integer_unsigned_set_value(field
, content_size_bits
);
407 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
408 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
409 stream
, bt_ctf_stream_get_name(stream
),
410 field
, content_size_bits
);
412 BT_LOGV("Set packet context field's `content_size` field's value: "
413 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
414 stream
, bt_ctf_stream_get_name(stream
),
415 field
, content_size_bits
);
419 bt_ctf_object_put_ref(field
);
424 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
427 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
428 stream
->packet_context
, "events_discarded");
433 /* No discarded events count field found. Not an error, skip. */
434 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
435 "stream-addr=%p, stream-name=\"%s\"",
436 stream
, bt_ctf_stream_get_name(stream
));
441 * If the field is set by the user, make sure that the value is
442 * greater than or equal to the stream's current count of
443 * discarded events. We do not allow wrapping here. If it's
444 * valid, update the stream's current count.
446 if (bt_ctf_field_is_set_recursive(field
)) {
449 ret
= bt_ctf_field_integer_unsigned_get_value(field
,
452 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
453 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
454 stream
, bt_ctf_stream_get_name(stream
), field
);
458 if (user_val
< stream
->discarded_events
) {
459 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
460 "value is lesser than the stream's current discarded events count: "
461 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
462 "value=%" PRIu64
", "
463 "stream-discarded-events-count=%" PRIu64
,
464 stream
, bt_ctf_stream_get_name(stream
), field
,
465 user_val
, stream
->discarded_events
);
469 stream
->discarded_events
= user_val
;
471 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
472 stream
->discarded_events
);
474 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
475 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
476 stream
, bt_ctf_stream_get_name(stream
),
477 field
, stream
->discarded_events
);
479 BT_LOGV("Set packet context field's `events_discarded` field's value: "
480 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
481 stream
, bt_ctf_stream_get_name(stream
),
482 field
, stream
->discarded_events
);
487 bt_ctf_object_put_ref(field
);
492 void update_clock_value(uint64_t *val
, uint64_t new_val
,
493 unsigned int new_val_size
)
495 const uint64_t pow2
= 1ULL << new_val_size
;
496 const uint64_t mask
= pow2
- 1;
499 #ifdef BT_LOG_ENABLED_VERBOSE
500 uint64_t old_val
= *val
;
503 if (new_val_size
== 64) {
508 val_masked
= *val
& mask
;
510 if (new_val
< val_masked
) {
519 BT_LOGV("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
525 int visit_field_update_clock_value(struct bt_ctf_field
*field
, uint64_t *val
)
528 struct bt_ctf_field_common
*field_common
= (void *) field
;
534 switch (bt_ctf_field_get_type_id(field
)) {
535 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
537 struct bt_ctf_clock_class
*cc
=
538 bt_ctf_field_type_integer_get_mapped_clock_class(
539 (void *) field_common
->type
);
547 bt_ctf_object_put_ref(cc
);
548 val_size
= bt_ctf_field_type_integer_get_size(
549 (void *) field_common
->type
);
550 BT_ASSERT(val_size
>= 1);
552 if (bt_ctf_field_type_integer_is_signed(
553 (void *) field_common
->type
)) {
556 ret
= bt_ctf_field_integer_signed_get_value(field
, &ival
);
557 uval
= (uint64_t) ival
;
559 ret
= bt_ctf_field_integer_unsigned_get_value(field
, &uval
);
567 update_clock_value(val
, uval
, val_size
);
570 case BT_CTF_FIELD_TYPE_ID_ENUM
:
572 struct bt_ctf_field
*int_field
=
573 bt_ctf_field_enumeration_get_container(field
);
575 BT_ASSERT(int_field
);
576 ret
= visit_field_update_clock_value(int_field
, val
);
577 bt_ctf_object_put_ref(int_field
);
580 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
583 int64_t len
= bt_ctf_field_type_array_get_length(
584 (void *) field_common
->type
);
588 for (i
= 0; i
< len
; i
++) {
589 struct bt_ctf_field
*elem_field
=
590 bt_ctf_field_array_get_field(field
, i
);
592 BT_ASSERT(elem_field
);
593 ret
= visit_field_update_clock_value(elem_field
, val
);
594 bt_ctf_object_put_ref(elem_field
);
601 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
604 int64_t len
= bt_ctf_field_common_sequence_get_length(
612 for (i
= 0; i
< len
; i
++) {
613 struct bt_ctf_field
*elem_field
=
614 bt_ctf_field_sequence_get_field(field
, i
);
616 BT_ASSERT(elem_field
);
617 ret
= visit_field_update_clock_value(elem_field
, val
);
618 bt_ctf_object_put_ref(elem_field
);
625 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
628 int64_t len
= bt_ctf_field_type_structure_get_field_count(
629 (void *) field_common
->type
);
633 for (i
= 0; i
< len
; i
++) {
634 struct bt_ctf_field
*member_field
=
635 bt_ctf_field_structure_get_field_by_index(field
, i
);
637 BT_ASSERT(member_field
);
638 ret
= visit_field_update_clock_value(member_field
, val
);
639 bt_ctf_object_put_ref(member_field
);
646 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
648 struct bt_ctf_field
*cur_field
=
649 bt_ctf_field_variant_get_current_field(field
);
656 ret
= visit_field_update_clock_value(cur_field
, val
);
657 bt_ctf_object_put_ref(cur_field
);
668 int visit_event_update_clock_value(struct bt_ctf_event
*event
, uint64_t *val
)
671 struct bt_ctf_field
*field
;
673 field
= bt_ctf_event_get_header(event
);
674 ret
= visit_field_update_clock_value(field
, val
);
675 bt_ctf_object_put_ref(field
);
677 BT_LOGW_STR("Cannot automatically update clock value in "
682 field
= bt_ctf_event_get_stream_event_context(event
);
683 ret
= visit_field_update_clock_value(field
, val
);
684 bt_ctf_object_put_ref(field
);
686 BT_LOGW_STR("Cannot automatically update clock value in "
687 "event's stream event context.");
691 field
= bt_ctf_event_get_context(event
);
692 ret
= visit_field_update_clock_value(field
, val
);
693 bt_ctf_object_put_ref(field
);
695 BT_LOGW_STR("Cannot automatically update clock value in "
700 field
= bt_ctf_event_get_payload_field(event
);
701 ret
= visit_field_update_clock_value(field
, val
);
702 bt_ctf_object_put_ref(field
);
704 BT_LOGW_STR("Cannot automatically update clock value in "
714 int set_packet_context_timestamps(struct bt_ctf_stream
*stream
)
718 uint64_t cur_clock_value
;
719 uint64_t init_clock_value
= 0;
720 struct bt_ctf_field
*ts_begin_field
= bt_ctf_field_structure_get_field_by_name(
721 stream
->packet_context
, "timestamp_begin");
722 struct bt_ctf_field
*ts_end_field
= bt_ctf_field_structure_get_field_by_name(
723 stream
->packet_context
, "timestamp_end");
724 struct bt_ctf_field_common
*packet_context
=
725 (void *) stream
->packet_context
;
729 if (ts_begin_field
&& bt_ctf_field_is_set_recursive(ts_begin_field
)) {
730 /* Use provided `timestamp_begin` value as starting value */
731 ret
= bt_ctf_field_integer_unsigned_get_value(ts_begin_field
, &val
);
733 init_clock_value
= val
;
734 } else if (stream
->last_ts_end
!= -1ULL) {
735 /* Use last packet's ending timestamp as starting value */
736 init_clock_value
= stream
->last_ts_end
;
739 cur_clock_value
= init_clock_value
;
741 if (stream
->last_ts_end
!= -1ULL &&
742 cur_clock_value
< stream
->last_ts_end
) {
743 BT_LOGW("Packet's initial timestamp is less than previous "
744 "packet's final timestamp: "
745 "stream-addr=%p, stream-name=\"%s\", "
746 "cur-packet-ts-begin=%" PRIu64
", "
747 "prev-packet-ts-end=%" PRIu64
,
748 stream
, bt_ctf_stream_get_name(stream
),
749 cur_clock_value
, stream
->last_ts_end
);
755 * Visit all the packet context fields, followed by all the
756 * fields of all the events, in order, updating our current
757 * clock value as we visit.
759 * While visiting the packet context fields, do not consider
760 * `timestamp_begin` and `timestamp_end` because this function's
761 * purpose is to set them anyway. Also do not consider
762 * `packet_size`, `content_size`, `events_discarded`, and
763 * `packet_seq_num` if they are not set because those are
764 * autopopulating fields.
766 len
= bt_ctf_field_type_structure_get_field_count(
767 (void *) packet_context
->type
);
770 for (i
= 0; i
< len
; i
++) {
771 const char *member_name
;
772 struct bt_ctf_field
*member_field
;
774 ret
= bt_ctf_field_type_structure_get_field_by_index(
775 (void *) packet_context
->type
, &member_name
, NULL
, i
);
778 if (strcmp(member_name
, "timestamp_begin") == 0 ||
779 strcmp(member_name
, "timestamp_end") == 0) {
783 member_field
= bt_ctf_field_structure_get_field_by_index(
784 stream
->packet_context
, i
);
785 BT_ASSERT(member_field
);
787 if (strcmp(member_name
, "packet_size") == 0 &&
788 !bt_ctf_field_is_set_recursive(member_field
)) {
789 bt_ctf_object_put_ref(member_field
);
793 if (strcmp(member_name
, "content_size") == 0 &&
794 !bt_ctf_field_is_set_recursive(member_field
)) {
795 bt_ctf_object_put_ref(member_field
);
799 if (strcmp(member_name
, "events_discarded") == 0 &&
800 !bt_ctf_field_is_set_recursive(member_field
)) {
801 bt_ctf_object_put_ref(member_field
);
805 if (strcmp(member_name
, "packet_seq_num") == 0 &&
806 !bt_ctf_field_is_set_recursive(member_field
)) {
807 bt_ctf_object_put_ref(member_field
);
811 ret
= visit_field_update_clock_value(member_field
,
813 bt_ctf_object_put_ref(member_field
);
815 BT_LOGW("Cannot automatically update clock value "
816 "in stream's packet context: "
817 "stream-addr=%p, stream-name=\"%s\", "
819 stream
, bt_ctf_stream_get_name(stream
),
825 for (i
= 0; i
< stream
->events
->len
; i
++) {
826 struct bt_ctf_event
*event
= g_ptr_array_index(stream
->events
, i
);
829 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
831 BT_LOGW("Cannot automatically update clock value "
832 "in stream's packet context: "
833 "stream-addr=%p, stream-name=\"%s\", "
834 "index=%" PRIu64
", event-addr=%p, "
835 "event-class-id=%" PRId64
", "
836 "event-class-name=\"%s\"",
837 stream
, bt_ctf_stream_get_name(stream
),
839 bt_ctf_event_class_common_get_id(event
->common
.class),
840 bt_ctf_event_class_common_get_name(event
->common
.class));
846 * Everything is visited, thus the current clock value
847 * corresponds to the ending timestamp. Validate this value
848 * against the provided value of `timestamp_end`, if any,
851 if (ts_end_field
&& bt_ctf_field_is_set_recursive(ts_end_field
)) {
852 ret
= bt_ctf_field_integer_unsigned_get_value(ts_end_field
, &val
);
855 if (val
< cur_clock_value
) {
856 BT_LOGW("Packet's final timestamp is less than "
857 "computed packet's final timestamp: "
858 "stream-addr=%p, stream-name=\"%s\", "
859 "cur-packet-ts-end=%" PRIu64
", "
860 "computed-packet-ts-end=%" PRIu64
,
861 stream
, bt_ctf_stream_get_name(stream
),
862 val
, cur_clock_value
);
867 stream
->last_ts_end
= val
;
870 if (ts_end_field
&& !bt_ctf_field_is_set_recursive(ts_end_field
)) {
871 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
873 stream
->last_ts_end
= cur_clock_value
;
877 stream
->last_ts_end
= cur_clock_value
;
880 /* Set `timestamp_begin` field to initial clock value */
881 if (ts_begin_field
&& !bt_ctf_field_is_set_recursive(ts_begin_field
)) {
882 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
887 bt_ctf_object_put_ref(ts_begin_field
);
888 bt_ctf_object_put_ref(ts_end_field
);
893 int auto_populate_packet_context(struct bt_ctf_stream
*stream
, bool set_ts
,
894 uint64_t packet_size_bits
, uint64_t content_size_bits
)
898 if (!stream
->packet_context
) {
902 ret
= set_packet_context_packet_size(stream
, packet_size_bits
);
904 BT_LOGW("Cannot set packet context's packet size field: "
905 "stream-addr=%p, stream-name=\"%s\"",
906 stream
, bt_ctf_stream_get_name(stream
));
910 ret
= set_packet_context_content_size(stream
, content_size_bits
);
912 BT_LOGW("Cannot set packet context's content size field: "
913 "stream-addr=%p, stream-name=\"%s\"",
914 stream
, bt_ctf_stream_get_name(stream
));
919 ret
= set_packet_context_timestamps(stream
);
921 BT_LOGW("Cannot set packet context's timestamp fields: "
922 "stream-addr=%p, stream-name=\"%s\"",
923 stream
, bt_ctf_stream_get_name(stream
));
928 ret
= set_packet_context_events_discarded(stream
);
930 BT_LOGW("Cannot set packet context's discarded events count field: "
931 "stream-addr=%p, stream-name=\"%s\"",
932 stream
, bt_ctf_stream_get_name(stream
));
936 BT_LOGV("Automatically populated stream's packet context's known fields: "
937 "stream-addr=%p, stream-name=\"%s\"",
938 stream
, bt_ctf_stream_get_name(stream
));
945 void release_event(struct bt_ctf_event
*event
)
947 if (bt_ctf_object_get_ref_count(&event
->common
.base
)) {
949 * The event is being orphaned, but it must guarantee the
950 * existence of its event class for the duration of its
953 bt_ctf_object_get_ref(event
->common
.class);
954 BT_CTF_OBJECT_PUT_REF_AND_RESET(event
->common
.base
.parent
);
956 bt_ctf_object_try_spec_release(&event
->common
.base
);
961 int create_stream_file(struct bt_ctf_writer
*writer
,
962 struct bt_ctf_stream
*stream
)
965 GString
*filename
= g_string_new(NULL
);
966 int64_t stream_class_id
;
967 char *file_path
= NULL
;
969 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
970 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
971 writer
, stream
, bt_ctf_stream_get_name(stream
),
972 stream
->common
.stream_class
,
973 stream
->common
.stream_class
->name
->str
);
975 if (stream
->common
.name
&& stream
->common
.name
->len
> 0) {
976 /* Use stream name's base name as prefix */
977 gchar
*basename
= g_path_get_basename(stream
->common
.name
->str
);
981 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
982 g_string_assign(filename
, "stream");
984 g_string_assign(filename
, basename
);
991 if (stream
->common
.stream_class
->name
&&
992 stream
->common
.stream_class
->name
->len
> 0) {
993 /* Use stream class name's base name as prefix */
996 stream
->common
.stream_class
->name
->str
);
1000 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
1001 g_string_assign(filename
, "stream");
1003 g_string_assign(filename
, basename
);
1010 /* Default to using `stream-` as prefix */
1011 g_string_assign(filename
, "stream");
1014 stream_class_id
= bt_ctf_stream_class_common_get_id(stream
->common
.stream_class
);
1015 BT_ASSERT(stream_class_id
>= 0);
1016 BT_ASSERT(stream
->common
.id
>= 0);
1017 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
1018 stream_class_id
, stream
->common
.id
);
1020 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
1021 if (file_path
== NULL
) {
1026 ret
= bt_ctfser_init(&stream
->ctfser
, file_path
);
1029 /* bt_ctfser_init() logs errors */
1033 BT_LOGD("Created stream file for writing: "
1034 "stream-addr=%p, stream-name=\"%s\", "
1035 "filename=\"%s\"", stream
, bt_ctf_stream_get_name(stream
),
1039 g_string_free(filename
, TRUE
);
1044 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
1045 struct bt_ctf_stream_class
*stream_class
,
1046 const char *name
, uint64_t id
)
1050 struct bt_ctf_stream
*stream
= NULL
;
1051 struct bt_ctf_trace
*trace
= NULL
;
1052 struct bt_ctf_writer
*writer
= NULL
;
1054 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1055 "stream-class-name=\"%s\", stream-name=\"%s\", "
1056 "stream-id=%" PRIu64
,
1057 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1059 stream
= g_new0(struct bt_ctf_stream
, 1);
1061 BT_LOGE_STR("Failed to allocate one stream.");
1066 id
= stream_class
->next_stream_id
;
1069 ret
= bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream
),
1070 BT_CTF_TO_COMMON(stream_class
), name
, id
, bt_ctf_stream_destroy
);
1072 /* bt_ctf_stream_common_initialize() logs errors */
1076 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1077 BT_CTF_TO_COMMON(stream_class
)));
1079 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1080 "stream-class-addr=%p, stream-class-name=\"%s\", "
1081 "stream-name=\"%s\"",
1082 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1087 writer
= (struct bt_ctf_writer
*)
1088 bt_ctf_object_get_parent(&trace
->common
.base
);
1089 stream
->last_ts_end
= -1ULL;
1090 BT_LOGD("CTF writer stream object belongs writer's trace: "
1091 "writer-addr=%p", writer
);
1094 if (stream_class
->common
.packet_context_field_type
) {
1095 BT_LOGD("Creating stream's packet context field: "
1097 stream_class
->common
.packet_context_field_type
);
1098 stream
->packet_context
= bt_ctf_field_create(
1099 (void *) stream_class
->common
.packet_context_field_type
);
1100 if (!stream
->packet_context
) {
1101 BT_LOGW_STR("Cannot create stream's packet context field.");
1105 /* Initialize events_discarded */
1106 ret
= try_set_structure_field_integer(
1107 stream
->packet_context
, "events_discarded", 0);
1109 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1110 "ret=%d, packet-context-field-addr=%p",
1111 ret
, stream
->packet_context
);
1116 stream
->events
= g_ptr_array_new_with_free_func(
1117 (GDestroyNotify
) release_event
);
1118 if (!stream
->events
) {
1119 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1123 if (trace
->common
.packet_header_field_type
) {
1124 BT_LOGD("Creating stream's packet header field: "
1125 "ft-addr=%p", trace
->common
.packet_header_field_type
);
1126 stream
->packet_header
=
1127 bt_ctf_field_create(
1128 (void *) trace
->common
.packet_header_field_type
);
1129 if (!stream
->packet_header
) {
1130 BT_LOGW_STR("Cannot create stream's packet header field.");
1136 * Attempt to populate the default trace packet header fields
1137 * (magic, uuid and stream_id). This will _not_ fail shall the
1138 * fields not be found or be of an incompatible type; they will
1139 * simply not be populated automatically. The user will have to
1140 * make sure to set the trace packet header fields himself
1143 ret
= auto_populate_packet_header(stream
);
1145 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1149 /* Create file associated with this stream */
1150 fd
= create_stream_file(writer
, stream
);
1152 BT_LOGW_STR("Cannot create stream file.");
1156 /* Freeze the writer */
1157 BT_LOGD_STR("Freezing stream's CTF writer.");
1158 bt_ctf_writer_freeze(writer
);
1160 /* Add this stream to the trace's streams */
1161 g_ptr_array_add(trace
->common
.streams
, stream
);
1162 stream_class
->next_stream_id
++;
1163 BT_LOGD("Created stream object: addr=%p", stream
);
1167 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
1170 bt_ctf_object_put_ref(writer
);
1174 struct bt_ctf_stream
*bt_ctf_stream_create(
1175 struct bt_ctf_stream_class
*stream_class
,
1176 const char *name
, uint64_t id_param
)
1178 return bt_ctf_stream_create_with_id(stream_class
,
1182 int bt_ctf_stream_get_discarded_events_count(
1183 struct bt_ctf_stream
*stream
, uint64_t *count
)
1188 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1194 BT_LOGW_STR("Invalid parameter: count is NULL.");
1199 *count
= (uint64_t) stream
->discarded_events
;
1206 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1210 struct bt_ctf_field
*events_discarded_field
= NULL
;
1212 if (!stream
->packet_context
) {
1216 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1217 stream
->packet_context
, "events_discarded");
1218 if (!events_discarded_field
) {
1222 ret
= bt_ctf_field_integer_unsigned_set_value(
1223 events_discarded_field
, count
);
1225 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1226 "field-addr=%p, value=%" PRIu64
,
1227 events_discarded_field
, count
);
1232 bt_ctf_object_put_ref(events_discarded_field
);
1236 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1237 uint64_t event_count
)
1241 struct bt_ctf_field
*events_discarded_field
= NULL
;
1244 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1248 BT_LOGV("Appending discarded events to stream: "
1249 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1250 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1252 if (!stream
->packet_context
) {
1253 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1257 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1258 stream
->packet_context
, "events_discarded");
1259 if (!events_discarded_field
) {
1260 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1264 new_count
= stream
->discarded_events
+ event_count
;
1265 if (new_count
< stream
->discarded_events
) {
1266 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1267 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1268 stream
->discarded_events
, new_count
);
1272 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1274 /* set_packet_context_events_discarded_field() logs errors */
1278 stream
->discarded_events
= new_count
;
1279 BT_LOGV("Appended discarded events to stream: "
1280 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1281 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1284 bt_ctf_object_put_ref(events_discarded_field
);
1287 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1288 struct bt_ctf_event
*event
)
1291 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1292 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1293 struct bt_ctf_stream_class
*stream_class
=
1294 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1295 BT_CTF_TO_COMMON(stream
)));
1296 int64_t event_class_id
;
1300 if (!event
->common
.header_field
) {
1304 if (event
->common
.frozen
) {
1305 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1310 BT_LOGV("Automatically populating event's header field: "
1311 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1312 stream
, bt_ctf_stream_get_name(stream
), event
);
1314 id_field
= bt_ctf_field_structure_get_field_by_name(
1315 (void *) event
->common
.header_field
->field
, "id");
1316 event_class_id
= bt_ctf_event_class_common_get_id(event
->common
.class);
1317 BT_ASSERT(event_class_id
>= 0);
1319 if (id_field
&& bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1320 ret
= set_integer_field_value(id_field
, event_class_id
);
1322 BT_LOGW("Cannot set event header's `id` field's value: "
1323 "addr=%p, value=%" PRIu64
, id_field
,
1330 * The conditions to automatically set the timestamp are:
1332 * 1. The event header field "timestamp" exists and is an
1334 * 2. This stream's class has a registered clock (set with
1335 * bt_ctf_stream_class_set_clock()).
1336 * 3. The "timestamp" field is not set.
1338 timestamp_field
= bt_ctf_field_structure_get_field_by_name(
1339 (void *) event
->common
.header_field
->field
, "timestamp");
1340 if (timestamp_field
&& stream_class
->clock
&&
1341 bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
&&
1342 !bt_ctf_field_is_set_recursive(timestamp_field
)) {
1343 mapped_clock_class
=
1344 bt_ctf_field_type_integer_get_mapped_clock_class(
1345 (void *) ((struct bt_ctf_field_common
*) timestamp_field
)->type
);
1346 if (mapped_clock_class
) {
1349 BT_ASSERT(mapped_clock_class
==
1350 stream_class
->clock
->clock_class
);
1351 ret
= bt_ctf_clock_get_value(
1352 stream_class
->clock
,
1354 BT_ASSERT(ret
== 0);
1355 ret
= set_integer_field_value(timestamp_field
,
1358 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1359 "addr=%p, value=%" PRIu64
,
1360 timestamp_field
, timestamp
);
1366 BT_LOGV("Automatically populated event's header field: "
1367 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1368 stream
, bt_ctf_stream_get_name(stream
), event
);
1371 bt_ctf_object_put_ref(id_field
);
1372 bt_ctf_object_put_ref(timestamp_field
);
1373 bt_ctf_object_put_ref(mapped_clock_class
);
1377 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1378 struct bt_ctf_event
*event
)
1383 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1389 BT_LOGW_STR("Invalid parameter: event is NULL.");
1394 BT_LOGV("Appending event to stream: "
1395 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1396 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1397 stream
, bt_ctf_stream_get_name(stream
), event
,
1398 bt_ctf_event_class_common_get_name(
1399 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1400 bt_ctf_event_class_common_get_id(
1401 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1404 * The event is not supposed to have a parent stream at this
1405 * point. The only other way an event can have a parent stream
1406 * is if it was assigned when setting a packet to the event,
1407 * in which case the packet's stream is not a writer stream,
1408 * and thus the user is trying to append an event which belongs
1409 * to another stream.
1411 if (event
->common
.base
.parent
) {
1416 bt_ctf_object_set_parent(&event
->common
.base
, &stream
->common
.base
);
1417 BT_LOGV_STR("Automatically populating the header of the event to append.");
1418 ret
= auto_populate_event_header(stream
, event
);
1420 /* auto_populate_event_header() reports errors */
1424 /* Make sure the various scopes of the event are set */
1425 BT_LOGV_STR("Validating event to append.");
1426 BT_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event
)) == 0,
1427 "Invalid event: event-addr=%p", event
);
1429 /* Save the new event and freeze it */
1430 BT_LOGV_STR("Freezing the event to append.");
1431 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event
), true);
1432 g_ptr_array_add(stream
->events
, event
);
1435 * Event had to hold a reference to its event class as long as it wasn't
1436 * part of the same trace hierarchy. From now on, the event and its
1437 * class share the same lifetime guarantees and the reference is no
1440 BT_LOGV_STR("Putting the event's class.");
1441 bt_ctf_object_put_ref(event
->common
.class);
1442 BT_LOGV("Appended event to stream: "
1443 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1444 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1445 stream
, bt_ctf_stream_get_name(stream
), event
,
1446 bt_ctf_event_class_common_get_name(
1447 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1448 bt_ctf_event_class_common_get_id(
1449 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1456 * Orphan the event; we were not successful in associating it to
1459 bt_ctf_object_set_parent(&event
->common
.base
, NULL
);
1463 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(struct bt_ctf_stream
*stream
)
1465 struct bt_ctf_field
*packet_context
= NULL
;
1468 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1472 packet_context
= stream
->packet_context
;
1473 if (packet_context
) {
1474 bt_ctf_object_get_ref(packet_context
);
1477 return packet_context
;
1480 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1481 struct bt_ctf_field
*field
)
1484 struct bt_ctf_field_type
*field_type
;
1487 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1492 field_type
= bt_ctf_field_get_type(field
);
1493 if (bt_ctf_field_type_common_compare((void *) field_type
,
1494 stream
->common
.stream_class
->packet_context_field_type
)) {
1495 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1496 "stream-addr=%p, stream-name=\"%s\", "
1497 "packet-context-field-addr=%p, "
1498 "packet-context-ft-addr=%p",
1499 stream
, bt_ctf_stream_get_name(stream
),
1505 bt_ctf_object_put_ref(field_type
);
1506 bt_ctf_object_put_ref(stream
->packet_context
);
1507 stream
->packet_context
= bt_ctf_object_get_ref(field
);
1508 BT_LOGV("Set stream's packet context field: "
1509 "stream-addr=%p, stream-name=\"%s\", "
1510 "packet-context-field-addr=%p",
1511 stream
, bt_ctf_stream_get_name(stream
), field
);
1516 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(struct bt_ctf_stream
*stream
)
1518 struct bt_ctf_field
*packet_header
= NULL
;
1521 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1525 packet_header
= stream
->packet_header
;
1526 if (packet_header
) {
1527 bt_ctf_object_get_ref(packet_header
);
1530 return packet_header
;
1533 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1534 struct bt_ctf_field
*field
)
1537 struct bt_ctf_trace
*trace
= NULL
;
1538 struct bt_ctf_field_type
*field_type
= NULL
;
1541 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1546 trace
= (struct bt_ctf_trace
*)
1547 bt_ctf_object_get_parent(&stream
->common
.base
);
1550 if (trace
->common
.packet_header_field_type
) {
1551 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1552 "stream-addr=%p, stream-name=\"%s\", "
1553 "packet-header-field-addr=%p, "
1554 "expected-ft-addr=%p",
1555 stream
, bt_ctf_stream_get_name(stream
),
1556 field
, trace
->common
.packet_header_field_type
);
1561 goto skip_validation
;
1564 field_type
= bt_ctf_field_get_type(field
);
1565 BT_ASSERT(field_type
);
1567 if (bt_ctf_field_type_common_compare((void *) field_type
,
1568 trace
->common
.packet_header_field_type
)) {
1569 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1570 "stream-addr=%p, stream-name=\"%s\", "
1571 "packet-header-field-addr=%p, "
1572 "packet-header-ft-addr=%p",
1573 stream
, bt_ctf_stream_get_name(stream
),
1580 bt_ctf_object_put_ref(stream
->packet_header
);
1581 stream
->packet_header
= bt_ctf_object_get_ref(field
);
1582 BT_LOGV("Set stream's packet header field: "
1583 "stream-addr=%p, stream-name=\"%s\", "
1584 "packet-header-field-addr=%p",
1585 stream
, bt_ctf_stream_get_name(stream
), field
);
1587 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1588 bt_ctf_object_put_ref(field_type
);
1593 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1595 struct bt_ctf_field
*member
;
1597 member
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1599 bt_ctf_field_common_reset_recursive((void *) member
);
1600 bt_ctf_object_put_ref(member
);
1604 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1608 uint64_t packet_context_offset_bits
= 0;
1609 struct bt_ctf_trace
*trace
;
1610 enum bt_ctf_byte_order native_byte_order
;
1611 bool has_packet_size
= false;
1612 uint64_t packet_size_bits
= 0;
1613 uint64_t content_size_bits
= 0;
1616 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1621 if (stream
->packet_context
) {
1622 struct bt_ctf_field
*packet_size_field
;
1624 packet_size_field
= bt_ctf_field_structure_get_field_by_name(
1625 stream
->packet_context
, "packet_size");
1626 has_packet_size
= (packet_size_field
!= NULL
);
1627 bt_ctf_object_put_ref(packet_size_field
);
1630 if (stream
->flushed_packet_count
== 1) {
1631 if (!stream
->packet_context
) {
1632 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1637 if (!has_packet_size
) {
1638 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1644 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1645 "stream-name=\"%s\", packet-index=%u", stream
,
1646 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1647 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1648 stream
->common
.stream_class
));
1650 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1652 ret
= auto_populate_packet_header(stream
);
1654 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1659 /* Initialize packet/content sizes to `0`; we will overwrite later */
1660 ret
= auto_populate_packet_context(stream
, true, 0, 0);
1662 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1667 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
1669 /* bt_ctfser_open_packet() logs errors */
1674 if (stream
->packet_header
) {
1675 BT_LOGV_STR("Serializing packet header field (initial).");
1676 ret
= bt_ctf_field_serialize_recursive(stream
->packet_header
,
1677 &stream
->ctfser
, native_byte_order
);
1679 BT_LOGW("Cannot serialize stream's packet header field: "
1680 "field-addr=%p", stream
->packet_header
);
1685 if (stream
->packet_context
) {
1686 /* Save packet context's position to overwrite it later */
1687 packet_context_offset_bits
=
1688 bt_ctfser_get_offset_in_current_packet_bits(
1691 /* Write packet context */
1692 BT_LOGV_STR("Serializing packet context field (initial).");
1693 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1694 &stream
->ctfser
, native_byte_order
);
1696 BT_LOGW("Cannot serialize stream's packet context field: "
1697 "field-addr=%p", stream
->packet_context
);
1702 BT_LOGV("Serializing events: count=%u", stream
->events
->len
);
1704 for (i
= 0; i
< stream
->events
->len
; i
++) {
1705 struct bt_ctf_event
*event
= g_ptr_array_index(
1707 struct bt_ctf_event_class
*event_class
=
1708 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1709 BT_CTF_TO_COMMON(event
)));
1711 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1712 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1713 "ser-offset=%" PRIu64
,
1714 i
, event
, bt_ctf_event_class_get_name(event_class
),
1715 bt_ctf_event_class_get_id(event_class
),
1716 bt_ctfser_get_offset_in_current_packet_bits(
1719 /* Write event header */
1720 if (event
->common
.header_field
) {
1721 BT_LOGV_STR("Serializing event's header field.");
1722 ret
= bt_ctf_field_serialize_recursive(
1723 (void *) event
->common
.header_field
->field
,
1724 &stream
->ctfser
, native_byte_order
);
1726 BT_LOGW("Cannot serialize event's header field: "
1728 event
->common
.header_field
->field
);
1733 /* Write stream event context */
1734 if (event
->common
.stream_event_context_field
) {
1735 BT_LOGV_STR("Serializing event's stream event context field.");
1736 ret
= bt_ctf_field_serialize_recursive(
1737 (void *) event
->common
.stream_event_context_field
,
1738 &stream
->ctfser
, native_byte_order
);
1740 BT_LOGW("Cannot serialize event's stream event context field: "
1742 event
->common
.stream_event_context_field
);
1747 /* Write event content */
1748 ret
= bt_ctf_event_serialize(event
, &stream
->ctfser
,
1751 /* bt_ctf_event_serialize() logs errors */
1756 content_size_bits
= bt_ctfser_get_offset_in_current_packet_bits(
1759 if (!has_packet_size
&& content_size_bits
% 8 != 0) {
1760 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1761 "but current content size is not a multiple of 8 bits: "
1762 "content-size=%" PRIu64
", "
1763 "packet-size=%" PRIu64
,
1770 /* Set packet size; make it a multiple of 8 */
1771 packet_size_bits
= (content_size_bits
+ 7) & ~UINT64_C(7);
1773 if (stream
->packet_context
) {
1775 * The whole packet is serialized at this point. Make
1776 * sure that, if `packet_size` is missing, the current
1777 * content size is equal to the current packet size.
1779 struct bt_ctf_field
*field
=
1780 bt_ctf_field_structure_get_field_by_name(
1781 stream
->packet_context
, "content_size");
1783 bt_ctf_object_put_ref(field
);
1785 if (content_size_bits
!= packet_size_bits
) {
1786 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1787 "but current packet's content size is not equal to its packet size: "
1788 "content-size=%" PRIu64
", "
1789 "packet-size=%" PRIu64
,
1790 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
),
1798 * Overwrite the packet context now that the stream
1799 * position's packet and content sizes have the correct
1802 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
1803 packet_context_offset_bits
);
1804 ret
= auto_populate_packet_context(stream
, false,
1805 packet_size_bits
, content_size_bits
);
1807 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1812 BT_LOGV("Rewriting (serializing) packet context field.");
1813 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1814 &stream
->ctfser
, native_byte_order
);
1816 BT_LOGW("Cannot serialize stream's packet context field: "
1817 "field-addr=%p", stream
->packet_context
);
1822 g_ptr_array_set_size(stream
->events
, 0);
1823 stream
->flushed_packet_count
++;
1824 bt_ctfser_close_current_packet(&stream
->ctfser
, packet_size_bits
/ 8);
1827 /* Reset automatically-set fields. */
1828 if (stream
->packet_context
) {
1829 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1830 reset_structure_field(stream
->packet_context
, "timestamp_end");
1831 reset_structure_field(stream
->packet_context
, "packet_size");
1832 reset_structure_field(stream
->packet_context
, "content_size");
1833 reset_structure_field(stream
->packet_context
, "events_discarded");
1837 BT_LOGV("Flushed stream's current packet: "
1838 "content-size=%" PRIu64
", packet-size=%" PRIu64
,
1839 content_size_bits
, packet_size_bits
);
1847 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
)
1849 struct bt_ctf_stream
*stream
= (void *) obj
;
1851 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1852 stream
, bt_ctf_stream_get_name(stream
));
1854 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream
));
1855 bt_ctfser_fini(&stream
->ctfser
);
1857 if (stream
->events
) {
1858 BT_LOGD_STR("Putting events.");
1859 g_ptr_array_free(stream
->events
, TRUE
);
1862 BT_LOGD_STR("Putting packet header field.");
1863 bt_ctf_object_put_ref(stream
->packet_header
);
1864 BT_LOGD_STR("Putting packet context field.");
1865 bt_ctf_object_put_ref(stream
->packet_context
);
1870 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1871 uint64_t value
, bt_bool force
)
1874 struct bt_ctf_field_type
*field_type
= NULL
;
1875 struct bt_ctf_field
*integer
;
1877 BT_ASSERT(structure
);
1880 integer
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1882 /* Field not found, not an error. */
1883 BT_LOGV("Field not found: struct-field-addr=%p, "
1884 "name=\"%s\", force=%d", structure
, name
, force
);
1888 /* Make sure the payload has not already been set. */
1889 if (!force
&& bt_ctf_field_is_set_recursive(integer
)) {
1890 /* Payload already set, not an error */
1891 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1892 "name=\"%s\", force=%d", structure
, name
, force
);
1896 field_type
= bt_ctf_field_get_type(integer
);
1897 BT_ASSERT(field_type
);
1898 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1900 * The user most likely meant for us to populate this field
1901 * automatically. However, we can only do this if the field
1902 * is an integer. Return an error.
1904 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1905 "field-addr=%p, ft-addr=%p, ft-id=%s",
1906 integer
, field_type
,
1907 bt_ctf_field_type_id_string((int)
1908 bt_ctf_field_type_get_type_id(field_type
)));
1913 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
1914 ret
= bt_ctf_field_integer_signed_set_value(integer
,
1917 ret
= bt_ctf_field_integer_unsigned_set_value(integer
, value
);
1919 ret
= !ret
? 1 : ret
;
1921 bt_ctf_object_put_ref(integer
);
1922 bt_ctf_object_put_ref(field_type
);
1927 * Returns the following codes:
1928 * 1 if the field was found and set,
1929 * 0 if nothing was done (field not found, or was already set),
1930 * <0 if an error was encoutered
1933 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1936 return _set_structure_field_integer(structure
, name
, value
, BT_FALSE
);
1939 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
1940 struct bt_ctf_stream
*stream
)
1942 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream
)));
1945 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1947 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream
));
1950 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
1952 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream
));