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 <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/align-internal.h>
29 #include <babeltrace/assert-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ctf-writer/event-class-internal.h>
32 #include <babeltrace/ctf-writer/event-internal.h>
33 #include <babeltrace/ctf-writer/field-types.h>
34 #include <babeltrace/ctf-writer/fields-internal.h>
35 #include <babeltrace/ctf-writer/stream-class-internal.h>
36 #include <babeltrace/ctf-writer/stream-class.h>
37 #include <babeltrace/ctf-writer/stream-internal.h>
38 #include <babeltrace/ctf-writer/stream.h>
39 #include <babeltrace/ctf-writer/trace-internal.h>
40 #include <babeltrace/ctf-writer/trace.h>
41 #include <babeltrace/ctf-writer/writer-internal.h>
42 #include <babeltrace/ctf-writer/object.h>
48 void bt_ctf_stream_common_finalize(struct bt_ctf_stream_common
*stream
)
50 BT_LOGD("Finalizing common stream object: addr=%p, name=\"%s\"",
51 stream
, bt_ctf_stream_common_get_name(stream
));
54 g_string_free(stream
->name
, TRUE
);
59 int bt_ctf_stream_common_initialize(
60 struct bt_ctf_stream_common
*stream
,
61 struct bt_ctf_stream_class_common
*stream_class
, const char *name
,
62 uint64_t id
, bt_ctf_object_release_func release_func
)
65 struct bt_ctf_trace_common
*trace
= NULL
;
67 bt_ctf_object_init_shared_with_parent(&stream
->base
, release_func
);
70 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
74 BT_LOGD("Initializing common stream object: stream-class-addr=%p, "
75 "stream-class-name=\"%s\", stream-name=\"%s\", "
77 stream_class
, bt_ctf_stream_class_common_get_name(stream_class
),
79 trace
= bt_ctf_stream_class_common_borrow_trace(stream_class
);
81 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
82 "stream-class-addr=%p, stream-class-name=\"%s\", "
85 bt_ctf_stream_class_common_get_name(stream_class
), name
);
91 * Validate that the given ID is unique amongst all the
92 * existing trace's streams created from the same stream
97 for (i
= 0; i
< trace
->streams
->len
; i
++) {
98 struct bt_ctf_stream_common
*trace_stream
=
99 g_ptr_array_index(trace
->streams
, i
);
101 if (trace_stream
->stream_class
!= (void *) stream_class
) {
105 if (trace_stream
->id
== id
) {
106 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
113 * Acquire reference to parent since stream will become publicly
114 * reachable; it needs its parent to remain valid.
116 bt_ctf_object_set_parent(&stream
->base
, &trace
->base
);
117 stream
->stream_class
= stream_class
;
118 stream
->id
= (int64_t) id
;
121 stream
->name
= g_string_new(name
);
123 BT_LOGE_STR("Failed to allocate a GString.");
128 BT_LOGD("Set common stream's trace parent: trace-addr=%p", trace
);
130 /* Add this stream to the trace's streams */
131 BT_LOGD("Created common stream object: addr=%p", stream
);
142 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
);
145 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
148 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
151 struct bt_ctf_field_type
*field_type
= NULL
;
154 BT_LOGW_STR("Invalid parameter: field is NULL.");
159 field_type
= bt_ctf_field_get_type(field
);
160 BT_ASSERT(field_type
);
162 if (bt_ctf_field_type_get_type_id(field_type
) !=
163 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
164 /* Not an integer and the value is unset, error. */
165 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
166 "field-addr=%p, ft-addr=%p, ft-id=%s",
168 bt_ctf_field_type_id_string((int)
169 bt_ctf_field_type_get_type_id(field_type
)));
174 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
175 ret
= bt_ctf_field_integer_signed_set_value(field
, (int64_t) value
);
177 /* Value is out of range, error. */
178 BT_LOGW("Cannot set signed integer field's value: "
179 "addr=%p, value=%" PRId64
,
180 field
, (int64_t) value
);
184 ret
= bt_ctf_field_integer_unsigned_set_value(field
, value
);
186 /* Value is out of range, error. */
187 BT_LOGW("Cannot set unsigned integer field's value: "
188 "addr=%p, value=%" PRIu64
,
194 bt_ctf_object_put_ref(field_type
);
199 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
202 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field_by_name(
203 stream
->packet_header
, "magic");
204 const uint32_t magic_value
= 0xc1fc1fc1;
209 /* No magic field found. Not an error, skip. */
210 BT_LOGV("No field named `magic` in packet header: skipping: "
211 "stream-addr=%p, stream-name=\"%s\"",
212 stream
, bt_ctf_stream_get_name(stream
));
216 ret
= bt_ctf_field_integer_unsigned_set_value(magic_field
,
217 (uint64_t) magic_value
);
220 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
221 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
222 stream
, bt_ctf_stream_get_name(stream
),
223 magic_field
, (uint64_t) magic_value
);
225 BT_LOGV("Set packet header field's `magic` 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
);
231 bt_ctf_object_put_ref(magic_field
);
236 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
240 struct bt_ctf_trace
*trace
= NULL
;
241 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field_by_name(
242 stream
->packet_header
, "uuid");
247 /* No uuid field found. Not an error, skip. */
248 BT_LOGV("No field named `uuid` in packet header: skipping: "
249 "stream-addr=%p, stream-name=\"%s\"",
250 stream
, bt_ctf_stream_get_name(stream
));
254 trace
= (struct bt_ctf_trace
*)
255 bt_ctf_object_get_parent(&stream
->common
.base
);
257 for (i
= 0; i
< 16; i
++) {
258 struct bt_ctf_field
*uuid_element
=
259 bt_ctf_field_array_get_field(uuid_field
, i
);
261 ret
= bt_ctf_field_integer_unsigned_set_value(
262 uuid_element
, (uint64_t) trace
->common
.uuid
[i
]);
263 bt_ctf_object_put_ref(uuid_element
);
265 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
266 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
267 "value=%" PRIu64
", index=%" PRId64
,
268 stream
, bt_ctf_stream_get_name(stream
),
269 uuid_element
, (uint64_t) trace
->common
.uuid
[i
], i
);
274 BT_LOGV("Set packet header field's `uuid` field's value: "
275 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
276 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
279 bt_ctf_object_put_ref(uuid_field
);
280 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
284 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
288 struct bt_ctf_field
*stream_id_field
=
289 bt_ctf_field_structure_get_field_by_name(
290 stream
->packet_header
, "stream_id");
292 if (!stream_id_field
) {
293 /* No stream_id field found. Not an error, skip. */
294 BT_LOGV("No field named `stream_id` in packet header: skipping: "
295 "stream-addr=%p, stream-name=\"%s\"",
296 stream
, bt_ctf_stream_get_name(stream
));
300 stream_id
= stream
->common
.stream_class
->id
;
301 ret
= bt_ctf_field_integer_unsigned_set_value(stream_id_field
,
302 (uint64_t) stream_id
);
304 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
305 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
306 stream
, bt_ctf_stream_get_name(stream
),
307 stream_id_field
, (uint64_t) stream_id
);
309 BT_LOGV("Set packet header field's `stream_id` 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
);
316 bt_ctf_object_put_ref(stream_id_field
);
321 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
325 if (!stream
->packet_header
) {
329 ret
= set_packet_header_magic(stream
);
331 BT_LOGW("Cannot set packet header's magic number field: "
332 "stream-addr=%p, stream-name=\"%s\"",
333 stream
, bt_ctf_stream_get_name(stream
));
337 ret
= set_packet_header_uuid(stream
);
339 BT_LOGW("Cannot set packet header's UUID field: "
340 "stream-addr=%p, stream-name=\"%s\"",
341 stream
, bt_ctf_stream_get_name(stream
));
345 ret
= set_packet_header_stream_id(stream
);
347 BT_LOGW("Cannot set packet header's stream class ID field: "
348 "stream-addr=%p, stream-name=\"%s\"",
349 stream
, bt_ctf_stream_get_name(stream
));
353 BT_LOGV("Automatically populated stream's packet header's known fields: "
354 "stream-addr=%p, stream-name=\"%s\"",
355 stream
, bt_ctf_stream_get_name(stream
));
362 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
)
365 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
366 stream
->packet_context
, "packet_size");
371 /* No packet size field found. Not an error, skip. */
372 BT_LOGV("No field named `packet_size` in packet context: skipping: "
373 "stream-addr=%p, stream-name=\"%s\"",
374 stream
, bt_ctf_stream_get_name(stream
));
378 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
379 stream
->pos
.packet_size
);
381 BT_LOGW("Cannot set packet context field's `packet_size` integer 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
, stream
->pos
.packet_size
);
386 BT_LOGV("Set packet context field's `packet_size` field's value: "
387 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
388 stream
, bt_ctf_stream_get_name(stream
),
389 field
, stream
->pos
.packet_size
);
393 bt_ctf_object_put_ref(field
);
398 int set_packet_context_content_size(struct bt_ctf_stream
*stream
)
401 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
402 stream
->packet_context
, "content_size");
407 /* No content size field found. Not an error, skip. */
408 BT_LOGV("No field named `content_size` in packet context: skipping: "
409 "stream-addr=%p, stream-name=\"%s\"",
410 stream
, bt_ctf_stream_get_name(stream
));
414 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
417 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
418 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
419 stream
, bt_ctf_stream_get_name(stream
),
420 field
, stream
->pos
.offset
);
422 BT_LOGV("Set packet context field's `content_size` field's value: "
423 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
424 stream
, bt_ctf_stream_get_name(stream
),
425 field
, stream
->pos
.offset
);
429 bt_ctf_object_put_ref(field
);
434 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
437 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
438 stream
->packet_context
, "events_discarded");
443 /* No discarded events count field found. Not an error, skip. */
444 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
445 "stream-addr=%p, stream-name=\"%s\"",
446 stream
, bt_ctf_stream_get_name(stream
));
451 * If the field is set by the user, make sure that the value is
452 * greater than or equal to the stream's current count of
453 * discarded events. We do not allow wrapping here. If it's
454 * valid, update the stream's current count.
456 if (bt_ctf_field_is_set_recursive(field
)) {
459 ret
= bt_ctf_field_integer_unsigned_get_value(field
,
462 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
463 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
464 stream
, bt_ctf_stream_get_name(stream
), field
);
468 if (user_val
< stream
->discarded_events
) {
469 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
470 "value is lesser than the stream's current discarded events count: "
471 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
472 "value=%" PRIu64
", "
473 "stream-discarded-events-count=%" PRIu64
,
474 stream
, bt_ctf_stream_get_name(stream
), field
,
475 user_val
, stream
->discarded_events
);
479 stream
->discarded_events
= user_val
;
481 ret
= bt_ctf_field_integer_unsigned_set_value(field
,
482 stream
->discarded_events
);
484 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
485 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
486 stream
, bt_ctf_stream_get_name(stream
),
487 field
, stream
->discarded_events
);
489 BT_LOGV("Set packet context field's `events_discarded` field's value: "
490 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
491 stream
, bt_ctf_stream_get_name(stream
),
492 field
, stream
->discarded_events
);
497 bt_ctf_object_put_ref(field
);
502 void update_clock_value(uint64_t *val
, uint64_t new_val
,
503 unsigned int new_val_size
)
505 const uint64_t pow2
= 1ULL << new_val_size
;
506 const uint64_t mask
= pow2
- 1;
509 #ifdef BT_LOG_ENABLED_VERBOSE
510 uint64_t old_val
= *val
;
513 if (new_val_size
== 64) {
518 val_masked
= *val
& mask
;
520 if (new_val
< val_masked
) {
529 BT_LOGV("Updated clock value: old-val=%" PRIu64
", new-val=%" PRIu64
,
535 int visit_field_update_clock_value(struct bt_ctf_field
*field
, uint64_t *val
)
538 struct bt_ctf_field_common
*field_common
= (void *) field
;
544 switch (bt_ctf_field_get_type_id(field
)) {
545 case BT_CTF_FIELD_TYPE_ID_INTEGER
:
547 struct bt_ctf_clock_class
*cc
=
548 bt_ctf_field_type_integer_get_mapped_clock_class(
549 (void *) field_common
->type
);
557 bt_ctf_object_put_ref(cc
);
558 val_size
= bt_ctf_field_type_integer_get_size(
559 (void *) field_common
->type
);
560 BT_ASSERT(val_size
>= 1);
562 if (bt_ctf_field_type_integer_is_signed(
563 (void *) field_common
->type
)) {
566 ret
= bt_ctf_field_integer_signed_get_value(field
, &ival
);
567 uval
= (uint64_t) ival
;
569 ret
= bt_ctf_field_integer_unsigned_get_value(field
, &uval
);
577 update_clock_value(val
, uval
, val_size
);
580 case BT_CTF_FIELD_TYPE_ID_ENUM
:
582 struct bt_ctf_field
*int_field
=
583 bt_ctf_field_enumeration_get_container(field
);
585 BT_ASSERT(int_field
);
586 ret
= visit_field_update_clock_value(int_field
, val
);
587 bt_ctf_object_put_ref(int_field
);
590 case BT_CTF_FIELD_TYPE_ID_ARRAY
:
593 int64_t len
= bt_ctf_field_type_array_get_length(
594 (void *) field_common
->type
);
598 for (i
= 0; i
< len
; i
++) {
599 struct bt_ctf_field
*elem_field
=
600 bt_ctf_field_array_get_field(field
, i
);
602 BT_ASSERT(elem_field
);
603 ret
= visit_field_update_clock_value(elem_field
, val
);
604 bt_ctf_object_put_ref(elem_field
);
611 case BT_CTF_FIELD_TYPE_ID_SEQUENCE
:
614 int64_t len
= bt_ctf_field_common_sequence_get_length(
622 for (i
= 0; i
< len
; i
++) {
623 struct bt_ctf_field
*elem_field
=
624 bt_ctf_field_sequence_get_field(field
, i
);
626 BT_ASSERT(elem_field
);
627 ret
= visit_field_update_clock_value(elem_field
, val
);
628 bt_ctf_object_put_ref(elem_field
);
635 case BT_CTF_FIELD_TYPE_ID_STRUCT
:
638 int64_t len
= bt_ctf_field_type_structure_get_field_count(
639 (void *) field_common
->type
);
643 for (i
= 0; i
< len
; i
++) {
644 struct bt_ctf_field
*member_field
=
645 bt_ctf_field_structure_get_field_by_index(field
, i
);
647 BT_ASSERT(member_field
);
648 ret
= visit_field_update_clock_value(member_field
, val
);
649 bt_ctf_object_put_ref(member_field
);
656 case BT_CTF_FIELD_TYPE_ID_VARIANT
:
658 struct bt_ctf_field
*cur_field
=
659 bt_ctf_field_variant_get_current_field(field
);
666 ret
= visit_field_update_clock_value(cur_field
, val
);
667 bt_ctf_object_put_ref(cur_field
);
678 int visit_event_update_clock_value(struct bt_ctf_event
*event
, uint64_t *val
)
681 struct bt_ctf_field
*field
;
683 field
= bt_ctf_event_get_header(event
);
684 ret
= visit_field_update_clock_value(field
, val
);
685 bt_ctf_object_put_ref(field
);
687 BT_LOGW_STR("Cannot automatically update clock value in "
692 field
= bt_ctf_event_get_stream_event_context(event
);
693 ret
= visit_field_update_clock_value(field
, val
);
694 bt_ctf_object_put_ref(field
);
696 BT_LOGW_STR("Cannot automatically update clock value in "
697 "event's stream event context.");
701 field
= bt_ctf_event_get_context(event
);
702 ret
= visit_field_update_clock_value(field
, val
);
703 bt_ctf_object_put_ref(field
);
705 BT_LOGW_STR("Cannot automatically update clock value in "
710 field
= bt_ctf_event_get_payload_field(event
);
711 ret
= visit_field_update_clock_value(field
, val
);
712 bt_ctf_object_put_ref(field
);
714 BT_LOGW_STR("Cannot automatically update clock value in "
724 int set_packet_context_timestamps(struct bt_ctf_stream
*stream
)
728 uint64_t cur_clock_value
;
729 uint64_t init_clock_value
= 0;
730 struct bt_ctf_field
*ts_begin_field
= bt_ctf_field_structure_get_field_by_name(
731 stream
->packet_context
, "timestamp_begin");
732 struct bt_ctf_field
*ts_end_field
= bt_ctf_field_structure_get_field_by_name(
733 stream
->packet_context
, "timestamp_end");
734 struct bt_ctf_field_common
*packet_context
=
735 (void *) stream
->packet_context
;
739 if (ts_begin_field
&& bt_ctf_field_is_set_recursive(ts_begin_field
)) {
740 /* Use provided `timestamp_begin` value as starting value */
741 ret
= bt_ctf_field_integer_unsigned_get_value(ts_begin_field
, &val
);
743 init_clock_value
= val
;
744 } else if (stream
->last_ts_end
!= -1ULL) {
745 /* Use last packet's ending timestamp as starting value */
746 init_clock_value
= stream
->last_ts_end
;
749 cur_clock_value
= init_clock_value
;
751 if (stream
->last_ts_end
!= -1ULL &&
752 cur_clock_value
< stream
->last_ts_end
) {
753 BT_LOGW("Packet's initial timestamp is less than previous "
754 "packet's final timestamp: "
755 "stream-addr=%p, stream-name=\"%s\", "
756 "cur-packet-ts-begin=%" PRIu64
", "
757 "prev-packet-ts-end=%" PRIu64
,
758 stream
, bt_ctf_stream_get_name(stream
),
759 cur_clock_value
, stream
->last_ts_end
);
765 * Visit all the packet context fields, followed by all the
766 * fields of all the events, in order, updating our current
767 * clock value as we visit.
769 * While visiting the packet context fields, do not consider
770 * `timestamp_begin` and `timestamp_end` because this function's
771 * purpose is to set them anyway. Also do not consider
772 * `packet_size`, `content_size`, `events_discarded`, and
773 * `packet_seq_num` if they are not set because those are
774 * autopopulating fields.
776 len
= bt_ctf_field_type_structure_get_field_count(
777 (void *) packet_context
->type
);
780 for (i
= 0; i
< len
; i
++) {
781 const char *member_name
;
782 struct bt_ctf_field
*member_field
;
784 ret
= bt_ctf_field_type_structure_get_field_by_index(
785 (void *) packet_context
->type
, &member_name
, NULL
, i
);
788 if (strcmp(member_name
, "timestamp_begin") == 0 ||
789 strcmp(member_name
, "timestamp_end") == 0) {
793 member_field
= bt_ctf_field_structure_get_field_by_index(
794 stream
->packet_context
, i
);
795 BT_ASSERT(member_field
);
797 if (strcmp(member_name
, "packet_size") == 0 &&
798 !bt_ctf_field_is_set_recursive(member_field
)) {
799 bt_ctf_object_put_ref(member_field
);
803 if (strcmp(member_name
, "content_size") == 0 &&
804 !bt_ctf_field_is_set_recursive(member_field
)) {
805 bt_ctf_object_put_ref(member_field
);
809 if (strcmp(member_name
, "events_discarded") == 0 &&
810 !bt_ctf_field_is_set_recursive(member_field
)) {
811 bt_ctf_object_put_ref(member_field
);
815 if (strcmp(member_name
, "packet_seq_num") == 0 &&
816 !bt_ctf_field_is_set_recursive(member_field
)) {
817 bt_ctf_object_put_ref(member_field
);
821 ret
= visit_field_update_clock_value(member_field
,
823 bt_ctf_object_put_ref(member_field
);
825 BT_LOGW("Cannot automatically update clock value "
826 "in stream's packet context: "
827 "stream-addr=%p, stream-name=\"%s\", "
829 stream
, bt_ctf_stream_get_name(stream
),
835 for (i
= 0; i
< stream
->events
->len
; i
++) {
836 struct bt_ctf_event
*event
= g_ptr_array_index(stream
->events
, i
);
839 ret
= visit_event_update_clock_value(event
, &cur_clock_value
);
841 BT_LOGW("Cannot automatically update clock value "
842 "in stream's packet context: "
843 "stream-addr=%p, stream-name=\"%s\", "
844 "index=%" PRIu64
", event-addr=%p, "
845 "event-class-id=%" PRId64
", "
846 "event-class-name=\"%s\"",
847 stream
, bt_ctf_stream_get_name(stream
),
849 bt_ctf_event_class_common_get_id(event
->common
.class),
850 bt_ctf_event_class_common_get_name(event
->common
.class));
856 * Everything is visited, thus the current clock value
857 * corresponds to the ending timestamp. Validate this value
858 * against the provided value of `timestamp_end`, if any,
861 if (ts_end_field
&& bt_ctf_field_is_set_recursive(ts_end_field
)) {
862 ret
= bt_ctf_field_integer_unsigned_get_value(ts_end_field
, &val
);
865 if (val
< cur_clock_value
) {
866 BT_LOGW("Packet's final timestamp is less than "
867 "computed packet's final timestamp: "
868 "stream-addr=%p, stream-name=\"%s\", "
869 "cur-packet-ts-end=%" PRIu64
", "
870 "computed-packet-ts-end=%" PRIu64
,
871 stream
, bt_ctf_stream_get_name(stream
),
872 val
, cur_clock_value
);
877 stream
->last_ts_end
= val
;
880 if (ts_end_field
&& !bt_ctf_field_is_set_recursive(ts_end_field
)) {
881 ret
= set_integer_field_value(ts_end_field
, cur_clock_value
);
883 stream
->last_ts_end
= cur_clock_value
;
887 stream
->last_ts_end
= cur_clock_value
;
890 /* Set `timestamp_begin` field to initial clock value */
891 if (ts_begin_field
&& !bt_ctf_field_is_set_recursive(ts_begin_field
)) {
892 ret
= set_integer_field_value(ts_begin_field
, init_clock_value
);
897 bt_ctf_object_put_ref(ts_begin_field
);
898 bt_ctf_object_put_ref(ts_end_field
);
903 int auto_populate_packet_context(struct bt_ctf_stream
*stream
, bool set_ts
)
907 if (!stream
->packet_context
) {
911 ret
= set_packet_context_packet_size(stream
);
913 BT_LOGW("Cannot set packet context's packet size field: "
914 "stream-addr=%p, stream-name=\"%s\"",
915 stream
, bt_ctf_stream_get_name(stream
));
919 ret
= set_packet_context_content_size(stream
);
921 BT_LOGW("Cannot set packet context's content size field: "
922 "stream-addr=%p, stream-name=\"%s\"",
923 stream
, bt_ctf_stream_get_name(stream
));
928 ret
= set_packet_context_timestamps(stream
);
930 BT_LOGW("Cannot set packet context's timestamp fields: "
931 "stream-addr=%p, stream-name=\"%s\"",
932 stream
, bt_ctf_stream_get_name(stream
));
937 ret
= set_packet_context_events_discarded(stream
);
939 BT_LOGW("Cannot set packet context's discarded events count field: "
940 "stream-addr=%p, stream-name=\"%s\"",
941 stream
, bt_ctf_stream_get_name(stream
));
945 BT_LOGV("Automatically populated stream's packet context's known fields: "
946 "stream-addr=%p, stream-name=\"%s\"",
947 stream
, bt_ctf_stream_get_name(stream
));
954 void release_event(struct bt_ctf_event
*event
)
956 if (bt_ctf_object_get_ref_count(&event
->common
.base
)) {
958 * The event is being orphaned, but it must guarantee the
959 * existence of its event class for the duration of its
962 bt_ctf_object_get_ref(event
->common
.class);
963 BT_CTF_OBJECT_PUT_REF_AND_RESET(event
->common
.base
.parent
);
965 bt_ctf_object_try_spec_release(&event
->common
.base
);
970 int create_stream_file(struct bt_ctf_writer
*writer
,
971 struct bt_ctf_stream
*stream
)
974 GString
*filename
= g_string_new(NULL
);
975 int64_t stream_class_id
;
976 char *file_path
= NULL
;
978 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
979 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
980 writer
, stream
, bt_ctf_stream_get_name(stream
),
981 stream
->common
.stream_class
,
982 stream
->common
.stream_class
->name
->str
);
984 if (stream
->common
.name
&& stream
->common
.name
->len
> 0) {
985 /* Use stream name's base name as prefix */
986 gchar
*basename
= g_path_get_basename(stream
->common
.name
->str
);
990 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
991 g_string_assign(filename
, "stream");
993 g_string_assign(filename
, basename
);
1000 if (stream
->common
.stream_class
->name
&&
1001 stream
->common
.stream_class
->name
->len
> 0) {
1002 /* Use stream class name's base name as prefix */
1004 g_path_get_basename(
1005 stream
->common
.stream_class
->name
->str
);
1007 BT_ASSERT(basename
);
1009 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
1010 g_string_assign(filename
, "stream");
1012 g_string_assign(filename
, basename
);
1019 /* Default to using `stream-` as prefix */
1020 g_string_assign(filename
, "stream");
1023 stream_class_id
= bt_ctf_stream_class_common_get_id(stream
->common
.stream_class
);
1024 BT_ASSERT(stream_class_id
>= 0);
1025 BT_ASSERT(stream
->common
.id
>= 0);
1026 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
1027 stream_class_id
, stream
->common
.id
);
1029 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
1030 if (file_path
== NULL
) {
1035 fd
= open(file_path
,
1036 O_RDWR
| O_CREAT
| O_TRUNC
,
1037 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
1040 BT_LOGW_ERRNO("Failed to open stream file for writing",
1041 ": file_path=\"%s\", filename=\"%s\", ret=%d",
1042 file_path
, filename
->str
, fd
);
1046 BT_LOGD("Created stream file for writing: "
1047 "stream-addr=%p, stream-name=\"%s\", "
1048 "filename=\"%s\", fd=%d", stream
, bt_ctf_stream_get_name(stream
),
1052 g_string_free(filename
, TRUE
);
1057 void set_stream_fd(struct bt_ctf_stream
*stream
, int fd
)
1059 (void) bt_ctf_stream_pos_init(&stream
->pos
, fd
, O_RDWR
);
1060 stream
->pos
.fd
= fd
;
1064 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
1065 struct bt_ctf_stream_class
*stream_class
,
1066 const char *name
, uint64_t id
)
1070 struct bt_ctf_stream
*stream
= NULL
;
1071 struct bt_ctf_trace
*trace
= NULL
;
1072 struct bt_ctf_writer
*writer
= NULL
;
1074 BT_LOGD("Creating CTF writer stream object: stream-class-addr=%p, "
1075 "stream-class-name=\"%s\", stream-name=\"%s\", "
1076 "stream-id=%" PRIu64
,
1077 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1079 stream
= g_new0(struct bt_ctf_stream
, 1);
1081 BT_LOGE_STR("Failed to allocate one stream.");
1086 id
= stream_class
->next_stream_id
;
1089 ret
= bt_ctf_stream_common_initialize(BT_CTF_TO_COMMON(stream
),
1090 BT_CTF_TO_COMMON(stream_class
), name
, id
, bt_ctf_stream_destroy
);
1092 /* bt_ctf_stream_common_initialize() logs errors */
1096 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1097 BT_CTF_TO_COMMON(stream_class
)));
1099 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
1100 "stream-class-addr=%p, stream-class-name=\"%s\", "
1101 "stream-name=\"%s\"",
1102 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1107 stream
->pos
.fd
= -1;
1108 writer
= (struct bt_ctf_writer
*)
1109 bt_ctf_object_get_parent(&trace
->common
.base
);
1110 stream
->last_ts_end
= -1ULL;
1111 BT_LOGD("CTF writer stream object belongs writer's trace: "
1112 "writer-addr=%p", writer
);
1115 if (stream_class
->common
.packet_context_field_type
) {
1116 BT_LOGD("Creating stream's packet context field: "
1118 stream_class
->common
.packet_context_field_type
);
1119 stream
->packet_context
= bt_ctf_field_create(
1120 (void *) stream_class
->common
.packet_context_field_type
);
1121 if (!stream
->packet_context
) {
1122 BT_LOGW_STR("Cannot create stream's packet context field.");
1126 /* Initialize events_discarded */
1127 ret
= try_set_structure_field_integer(
1128 stream
->packet_context
, "events_discarded", 0);
1130 BT_LOGW("Cannot set `events_discarded` field in packet context: "
1131 "ret=%d, packet-context-field-addr=%p",
1132 ret
, stream
->packet_context
);
1137 stream
->events
= g_ptr_array_new_with_free_func(
1138 (GDestroyNotify
) release_event
);
1139 if (!stream
->events
) {
1140 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1144 if (trace
->common
.packet_header_field_type
) {
1145 BT_LOGD("Creating stream's packet header field: "
1146 "ft-addr=%p", trace
->common
.packet_header_field_type
);
1147 stream
->packet_header
=
1148 bt_ctf_field_create(
1149 (void *) trace
->common
.packet_header_field_type
);
1150 if (!stream
->packet_header
) {
1151 BT_LOGW_STR("Cannot create stream's packet header field.");
1157 * Attempt to populate the default trace packet header fields
1158 * (magic, uuid and stream_id). This will _not_ fail shall the
1159 * fields not be found or be of an incompatible type; they will
1160 * simply not be populated automatically. The user will have to
1161 * make sure to set the trace packet header fields himself
1164 ret
= auto_populate_packet_header(stream
);
1166 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
1170 /* Create file associated with this stream */
1171 fd
= create_stream_file(writer
, stream
);
1173 BT_LOGW_STR("Cannot create stream file.");
1177 set_stream_fd(stream
, fd
);
1179 /* Freeze the writer */
1180 BT_LOGD_STR("Freezing stream's CTF writer.");
1181 bt_ctf_writer_freeze(writer
);
1183 /* Add this stream to the trace's streams */
1184 g_ptr_array_add(trace
->common
.streams
, stream
);
1185 stream_class
->next_stream_id
++;
1186 BT_LOGD("Created stream object: addr=%p", stream
);
1190 BT_CTF_OBJECT_PUT_REF_AND_RESET(stream
);
1193 bt_ctf_object_put_ref(writer
);
1197 struct bt_ctf_stream
*bt_ctf_stream_create(
1198 struct bt_ctf_stream_class
*stream_class
,
1199 const char *name
, uint64_t id_param
)
1201 return bt_ctf_stream_create_with_id(stream_class
,
1205 int bt_ctf_stream_get_discarded_events_count(
1206 struct bt_ctf_stream
*stream
, uint64_t *count
)
1211 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1217 BT_LOGW_STR("Invalid parameter: count is NULL.");
1222 if (stream
->pos
.fd
< 0) {
1223 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1224 "stream-addr=%p, stream-name=\"%s\"",
1225 stream
, bt_ctf_stream_get_name(stream
));
1230 *count
= (uint64_t) stream
->discarded_events
;
1237 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1241 struct bt_ctf_field
*events_discarded_field
= NULL
;
1243 if (!stream
->packet_context
) {
1247 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1248 stream
->packet_context
, "events_discarded");
1249 if (!events_discarded_field
) {
1253 ret
= bt_ctf_field_integer_unsigned_set_value(
1254 events_discarded_field
, count
);
1256 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1257 "field-addr=%p, value=%" PRIu64
,
1258 events_discarded_field
, count
);
1263 bt_ctf_object_put_ref(events_discarded_field
);
1267 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1268 uint64_t event_count
)
1272 struct bt_ctf_field
*events_discarded_field
= NULL
;
1275 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1279 BT_LOGV("Appending discarded events to stream: "
1280 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1281 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1283 if (!stream
->packet_context
) {
1284 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1288 if (stream
->pos
.fd
< 0) {
1289 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1293 events_discarded_field
= bt_ctf_field_structure_get_field_by_name(
1294 stream
->packet_context
, "events_discarded");
1295 if (!events_discarded_field
) {
1296 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1300 new_count
= stream
->discarded_events
+ event_count
;
1301 if (new_count
< stream
->discarded_events
) {
1302 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1303 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1304 stream
->discarded_events
, new_count
);
1308 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1310 /* set_packet_context_events_discarded_field() logs errors */
1314 stream
->discarded_events
= new_count
;
1315 BT_LOGV("Appended discarded events to stream: "
1316 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1317 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1320 bt_ctf_object_put_ref(events_discarded_field
);
1323 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1324 struct bt_ctf_event
*event
)
1327 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1328 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1329 struct bt_ctf_stream_class
*stream_class
=
1330 BT_CTF_FROM_COMMON(bt_ctf_stream_common_borrow_class(
1331 BT_CTF_TO_COMMON(stream
)));
1332 int64_t event_class_id
;
1336 if (!event
->common
.header_field
) {
1340 if (event
->common
.frozen
) {
1341 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1346 BT_LOGV("Automatically populating event's header field: "
1347 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1348 stream
, bt_ctf_stream_get_name(stream
), event
);
1350 id_field
= bt_ctf_field_structure_get_field_by_name(
1351 (void *) event
->common
.header_field
->field
, "id");
1352 event_class_id
= bt_ctf_event_class_common_get_id(event
->common
.class);
1353 BT_ASSERT(event_class_id
>= 0);
1355 if (id_field
&& bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1356 ret
= set_integer_field_value(id_field
, event_class_id
);
1358 BT_LOGW("Cannot set event header's `id` field's value: "
1359 "addr=%p, value=%" PRIu64
, id_field
,
1366 * The conditions to automatically set the timestamp are:
1368 * 1. The event header field "timestamp" exists and is an
1370 * 2. This stream's class has a registered clock (set with
1371 * bt_ctf_stream_class_set_clock()).
1372 * 3. The "timestamp" field is not set.
1374 timestamp_field
= bt_ctf_field_structure_get_field_by_name(
1375 (void *) event
->common
.header_field
->field
, "timestamp");
1376 if (timestamp_field
&& stream_class
->clock
&&
1377 bt_ctf_field_get_type_id(id_field
) == BT_CTF_FIELD_TYPE_ID_INTEGER
&&
1378 !bt_ctf_field_is_set_recursive(timestamp_field
)) {
1379 mapped_clock_class
=
1380 bt_ctf_field_type_integer_get_mapped_clock_class(
1381 (void *) ((struct bt_ctf_field_common
*) timestamp_field
)->type
);
1382 if (mapped_clock_class
) {
1385 BT_ASSERT(mapped_clock_class
==
1386 stream_class
->clock
->clock_class
);
1387 ret
= bt_ctf_clock_get_value(
1388 stream_class
->clock
,
1390 BT_ASSERT(ret
== 0);
1391 ret
= set_integer_field_value(timestamp_field
,
1394 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1395 "addr=%p, value=%" PRIu64
,
1396 timestamp_field
, timestamp
);
1402 BT_LOGV("Automatically populated event's header field: "
1403 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1404 stream
, bt_ctf_stream_get_name(stream
), event
);
1407 bt_ctf_object_put_ref(id_field
);
1408 bt_ctf_object_put_ref(timestamp_field
);
1409 bt_ctf_object_put_ref(mapped_clock_class
);
1413 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1414 struct bt_ctf_event
*event
)
1419 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1425 BT_LOGW_STR("Invalid parameter: event is NULL.");
1430 if (stream
->pos
.fd
< 0) {
1431 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1436 BT_LOGV("Appending event to stream: "
1437 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1438 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1439 stream
, bt_ctf_stream_get_name(stream
), event
,
1440 bt_ctf_event_class_common_get_name(
1441 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1442 bt_ctf_event_class_common_get_id(
1443 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1446 * The event is not supposed to have a parent stream at this
1447 * point. The only other way an event can have a parent stream
1448 * is if it was assigned when setting a packet to the event,
1449 * in which case the packet's stream is not a writer stream,
1450 * and thus the user is trying to append an event which belongs
1451 * to another stream.
1453 if (event
->common
.base
.parent
) {
1458 bt_ctf_object_set_parent(&event
->common
.base
, &stream
->common
.base
);
1459 BT_LOGV_STR("Automatically populating the header of the event to append.");
1460 ret
= auto_populate_event_header(stream
, event
);
1462 /* auto_populate_event_header() reports errors */
1466 /* Make sure the various scopes of the event are set */
1467 BT_LOGV_STR("Validating event to append.");
1468 BT_ASSERT_PRE(bt_ctf_event_common_validate(BT_CTF_TO_COMMON(event
)) == 0,
1469 "Invalid event: event-addr=%p", event
);
1471 /* Save the new event and freeze it */
1472 BT_LOGV_STR("Freezing the event to append.");
1473 bt_ctf_event_common_set_is_frozen(BT_CTF_TO_COMMON(event
), true);
1474 g_ptr_array_add(stream
->events
, event
);
1477 * Event had to hold a reference to its event class as long as it wasn't
1478 * part of the same trace hierarchy. From now on, the event and its
1479 * class share the same lifetime guarantees and the reference is no
1482 BT_LOGV_STR("Putting the event's class.");
1483 bt_ctf_object_put_ref(event
->common
.class);
1484 BT_LOGV("Appended event to stream: "
1485 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1486 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1487 stream
, bt_ctf_stream_get_name(stream
), event
,
1488 bt_ctf_event_class_common_get_name(
1489 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))),
1490 bt_ctf_event_class_common_get_id(
1491 bt_ctf_event_common_borrow_class(BT_CTF_TO_COMMON(event
))));
1498 * Orphan the event; we were not successful in associating it to
1501 bt_ctf_object_set_parent(&event
->common
.base
, NULL
);
1505 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(struct bt_ctf_stream
*stream
)
1507 struct bt_ctf_field
*packet_context
= NULL
;
1510 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1514 if (stream
->pos
.fd
< 0) {
1515 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1516 "stream-addr=%p, stream-name=\"%s\"", stream
,
1517 bt_ctf_stream_get_name(stream
));
1521 packet_context
= stream
->packet_context
;
1522 if (packet_context
) {
1523 bt_ctf_object_get_ref(packet_context
);
1526 return packet_context
;
1529 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1530 struct bt_ctf_field
*field
)
1533 struct bt_ctf_field_type
*field_type
;
1536 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1541 if (stream
->pos
.fd
< 0) {
1542 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1547 field_type
= bt_ctf_field_get_type(field
);
1548 if (bt_ctf_field_type_common_compare((void *) field_type
,
1549 stream
->common
.stream_class
->packet_context_field_type
)) {
1550 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1551 "stream-addr=%p, stream-name=\"%s\", "
1552 "packet-context-field-addr=%p, "
1553 "packet-context-ft-addr=%p",
1554 stream
, bt_ctf_stream_get_name(stream
),
1560 bt_ctf_object_put_ref(field_type
);
1561 bt_ctf_object_put_ref(stream
->packet_context
);
1562 stream
->packet_context
= bt_ctf_object_get_ref(field
);
1563 BT_LOGV("Set stream's packet context field: "
1564 "stream-addr=%p, stream-name=\"%s\", "
1565 "packet-context-field-addr=%p",
1566 stream
, bt_ctf_stream_get_name(stream
), field
);
1571 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(struct bt_ctf_stream
*stream
)
1573 struct bt_ctf_field
*packet_header
= NULL
;
1576 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1580 if (stream
->pos
.fd
< 0) {
1581 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1582 "stream-addr=%p, stream-name=\"%s\"", stream
,
1583 bt_ctf_stream_get_name(stream
));
1587 packet_header
= stream
->packet_header
;
1588 if (packet_header
) {
1589 bt_ctf_object_get_ref(packet_header
);
1592 return packet_header
;
1595 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1596 struct bt_ctf_field
*field
)
1599 struct bt_ctf_trace
*trace
= NULL
;
1600 struct bt_ctf_field_type
*field_type
= NULL
;
1603 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1608 if (stream
->pos
.fd
< 0) {
1609 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1614 trace
= (struct bt_ctf_trace
*)
1615 bt_ctf_object_get_parent(&stream
->common
.base
);
1618 if (trace
->common
.packet_header_field_type
) {
1619 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1620 "stream-addr=%p, stream-name=\"%s\", "
1621 "packet-header-field-addr=%p, "
1622 "expected-ft-addr=%p",
1623 stream
, bt_ctf_stream_get_name(stream
),
1624 field
, trace
->common
.packet_header_field_type
);
1629 goto skip_validation
;
1632 field_type
= bt_ctf_field_get_type(field
);
1633 BT_ASSERT(field_type
);
1635 if (bt_ctf_field_type_common_compare((void *) field_type
,
1636 trace
->common
.packet_header_field_type
)) {
1637 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1638 "stream-addr=%p, stream-name=\"%s\", "
1639 "packet-header-field-addr=%p, "
1640 "packet-header-ft-addr=%p",
1641 stream
, bt_ctf_stream_get_name(stream
),
1648 bt_ctf_object_put_ref(stream
->packet_header
);
1649 stream
->packet_header
= bt_ctf_object_get_ref(field
);
1650 BT_LOGV("Set stream's packet header field: "
1651 "stream-addr=%p, stream-name=\"%s\", "
1652 "packet-header-field-addr=%p",
1653 stream
, bt_ctf_stream_get_name(stream
), field
);
1655 BT_CTF_OBJECT_PUT_REF_AND_RESET(trace
);
1656 bt_ctf_object_put_ref(field_type
);
1661 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1663 struct bt_ctf_field
*member
;
1665 member
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1667 bt_ctf_field_common_reset_recursive((void *) member
);
1668 bt_ctf_object_put_ref(member
);
1672 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1676 struct bt_ctf_stream_pos packet_context_pos
;
1677 struct bt_ctf_trace
*trace
;
1678 enum bt_ctf_byte_order native_byte_order
;
1679 bool has_packet_size
= false;
1682 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1687 if (stream
->pos
.fd
< 0) {
1688 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1693 if (stream
->packet_context
) {
1694 struct bt_ctf_field
*packet_size_field
;
1696 packet_size_field
= bt_ctf_field_structure_get_field_by_name(
1697 stream
->packet_context
, "packet_size");
1698 has_packet_size
= (packet_size_field
!= NULL
);
1699 bt_ctf_object_put_ref(packet_size_field
);
1702 if (stream
->flushed_packet_count
== 1) {
1703 if (!stream
->packet_context
) {
1704 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1709 if (!has_packet_size
) {
1710 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1716 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1717 "stream-name=\"%s\", packet-index=%u", stream
,
1718 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1719 trace
= BT_CTF_FROM_COMMON(bt_ctf_stream_class_common_borrow_trace(
1720 stream
->common
.stream_class
));
1722 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1724 ret
= auto_populate_packet_header(stream
);
1726 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1731 ret
= auto_populate_packet_context(stream
, true);
1733 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1738 /* mmap the next packet */
1739 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64
,
1740 stream
->pos
.offset
);
1741 bt_ctf_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
1742 BT_ASSERT(stream
->pos
.packet_size
% 8 == 0);
1744 if (stream
->packet_header
) {
1745 BT_LOGV_STR("Serializing packet header field.");
1746 ret
= bt_ctf_field_serialize_recursive(stream
->packet_header
,
1747 &stream
->pos
, native_byte_order
);
1749 BT_LOGW("Cannot serialize stream's packet header field: "
1750 "field-addr=%p", stream
->packet_header
);
1755 if (stream
->packet_context
) {
1756 /* Write packet context */
1757 memcpy(&packet_context_pos
, &stream
->pos
,
1758 sizeof(packet_context_pos
));
1759 BT_LOGV_STR("Serializing packet context field.");
1760 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1761 &stream
->pos
, native_byte_order
);
1763 BT_LOGW("Cannot serialize stream's packet context field: "
1764 "field-addr=%p", stream
->packet_context
);
1769 BT_LOGV("Serializing events: count=%u", stream
->events
->len
);
1771 for (i
= 0; i
< stream
->events
->len
; i
++) {
1772 struct bt_ctf_event
*event
= g_ptr_array_index(
1774 struct bt_ctf_event_class
*event_class
=
1775 BT_CTF_FROM_COMMON(bt_ctf_event_common_borrow_class(
1776 BT_CTF_TO_COMMON(event
)));
1778 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1779 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1780 "pos-offset=%" PRId64
", packet-size=%" PRIu64
,
1781 i
, event
, bt_ctf_event_class_get_name(event_class
),
1782 bt_ctf_event_class_get_id(event_class
),
1783 stream
->pos
.offset
, stream
->pos
.packet_size
);
1785 /* Write event header */
1786 if (event
->common
.header_field
) {
1787 BT_LOGV_STR("Serializing event's header field.");
1788 ret
= bt_ctf_field_serialize_recursive(
1789 (void *) event
->common
.header_field
->field
,
1790 &stream
->pos
, native_byte_order
);
1792 BT_LOGW("Cannot serialize event's header field: "
1794 event
->common
.header_field
->field
);
1799 /* Write stream event context */
1800 if (event
->common
.stream_event_context_field
) {
1801 BT_LOGV_STR("Serializing event's stream event context field.");
1802 ret
= bt_ctf_field_serialize_recursive(
1803 (void *) event
->common
.stream_event_context_field
,
1804 &stream
->pos
, native_byte_order
);
1806 BT_LOGW("Cannot serialize event's stream event context field: "
1808 event
->common
.stream_event_context_field
);
1813 /* Write event content */
1814 ret
= bt_ctf_event_serialize(event
,
1815 &stream
->pos
, native_byte_order
);
1817 /* bt_ctf_event_serialize() logs errors */
1822 if (!has_packet_size
&& stream
->pos
.offset
% 8 != 0) {
1823 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1824 "but current content size is not a multiple of 8 bits: "
1825 "content-size=%" PRId64
", "
1826 "packet-size=%" PRIu64
,
1828 stream
->pos
.packet_size
);
1833 BT_ASSERT(stream
->pos
.packet_size
% 8 == 0);
1836 * Remove extra padding bytes.
1838 stream
->pos
.packet_size
= (stream
->pos
.offset
+ 7) & ~7;
1840 if (stream
->packet_context
) {
1842 * The whole packet is serialized at this point. Make sure that,
1843 * if `packet_size` is missing, the current content size is
1844 * equal to the current packet size.
1846 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field_by_name(
1847 stream
->packet_context
, "content_size");
1849 bt_ctf_object_put_ref(field
);
1851 if (stream
->pos
.offset
!= stream
->pos
.packet_size
) {
1852 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1853 "but current packet's content size is not equal to its packet size: "
1854 "content-size=%" PRId64
", "
1855 "packet-size=%" PRIu64
,
1857 stream
->pos
.packet_size
);
1864 * Overwrite the packet context now that the stream
1865 * position's packet and content sizes have the correct
1868 * Copy base_mma as the packet may have been remapped
1869 * (e.g. when a packet is resized).
1871 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1872 ret
= auto_populate_packet_context(stream
, false);
1874 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1879 BT_LOGV("Rewriting (serializing) packet context field.");
1880 ret
= bt_ctf_field_serialize_recursive(stream
->packet_context
,
1881 &packet_context_pos
, native_byte_order
);
1883 BT_LOGW("Cannot serialize stream's packet context field: "
1884 "field-addr=%p", stream
->packet_context
);
1889 g_ptr_array_set_size(stream
->events
, 0);
1890 stream
->flushed_packet_count
++;
1891 stream
->size
+= stream
->pos
.packet_size
/ CHAR_BIT
;
1894 /* Reset automatically-set fields. */
1895 if (stream
->packet_context
) {
1896 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1897 reset_structure_field(stream
->packet_context
, "timestamp_end");
1898 reset_structure_field(stream
->packet_context
, "packet_size");
1899 reset_structure_field(stream
->packet_context
, "content_size");
1900 reset_structure_field(stream
->packet_context
, "events_discarded");
1905 * We failed to write the packet. Its size is therefore set to 0
1906 * to ensure the next mapping is done in the same place rather
1907 * than advancing by "stream->pos.packet_size", which would
1908 * leave a corrupted packet in the trace.
1910 stream
->pos
.packet_size
= 0;
1912 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64
", "
1913 "packet-size=%" PRIu64
,
1914 stream
->pos
.offset
, stream
->pos
.packet_size
);
1922 void bt_ctf_stream_destroy(struct bt_ctf_object
*obj
)
1924 struct bt_ctf_stream
*stream
= (void *) obj
;
1926 BT_LOGD("Destroying CTF writer stream object: addr=%p, name=\"%s\"",
1927 stream
, bt_ctf_stream_get_name(stream
));
1929 bt_ctf_stream_common_finalize(BT_CTF_TO_COMMON(stream
));
1930 (void) bt_ctf_stream_pos_fini(&stream
->pos
);
1932 if (stream
->pos
.fd
>= 0) {
1936 * Truncate the file's size to the minimum required to fit the
1937 * last packet as we might have grown it too much on the last
1941 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1942 } while (ret
== -1 && errno
== EINTR
);
1944 BT_LOGE_ERRNO("Failed to truncate stream file",
1945 ": ret=%d, size=%" PRIu64
,
1946 ret
, (uint64_t) stream
->size
);
1949 if (close(stream
->pos
.fd
)) {
1950 BT_LOGE_ERRNO("Failed to close stream file",
1955 if (stream
->events
) {
1956 BT_LOGD_STR("Putting events.");
1957 g_ptr_array_free(stream
->events
, TRUE
);
1960 BT_LOGD_STR("Putting packet header field.");
1961 bt_ctf_object_put_ref(stream
->packet_header
);
1962 BT_LOGD_STR("Putting packet context field.");
1963 bt_ctf_object_put_ref(stream
->packet_context
);
1968 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1969 uint64_t value
, bt_bool force
)
1972 struct bt_ctf_field_type
*field_type
= NULL
;
1973 struct bt_ctf_field
*integer
;
1975 BT_ASSERT(structure
);
1978 integer
= bt_ctf_field_structure_get_field_by_name(structure
, name
);
1980 /* Field not found, not an error. */
1981 BT_LOGV("Field not found: struct-field-addr=%p, "
1982 "name=\"%s\", force=%d", structure
, name
, force
);
1986 /* Make sure the payload has not already been set. */
1987 if (!force
&& bt_ctf_field_is_set_recursive(integer
)) {
1988 /* Payload already set, not an error */
1989 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1990 "name=\"%s\", force=%d", structure
, name
, force
);
1994 field_type
= bt_ctf_field_get_type(integer
);
1995 BT_ASSERT(field_type
);
1996 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1998 * The user most likely meant for us to populate this field
1999 * automatically. However, we can only do this if the field
2000 * is an integer. Return an error.
2002 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
2003 "field-addr=%p, ft-addr=%p, ft-id=%s",
2004 integer
, field_type
,
2005 bt_ctf_field_type_id_string((int)
2006 bt_ctf_field_type_get_type_id(field_type
)));
2011 if (bt_ctf_field_type_integer_is_signed(field_type
)) {
2012 ret
= bt_ctf_field_integer_signed_set_value(integer
,
2015 ret
= bt_ctf_field_integer_unsigned_set_value(integer
, value
);
2017 ret
= !ret
? 1 : ret
;
2019 bt_ctf_object_put_ref(integer
);
2020 bt_ctf_object_put_ref(field_type
);
2025 * Returns the following codes:
2026 * 1 if the field was found and set,
2027 * 0 if nothing was done (field not found, or was already set),
2028 * <0 if an error was encoutered
2031 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
2034 return _set_structure_field_integer(structure
, name
, value
, BT_FALSE
);
2037 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
2038 struct bt_ctf_stream
*stream
)
2040 return bt_ctf_object_get_ref(bt_ctf_stream_common_borrow_class(BT_CTF_TO_COMMON(stream
)));
2043 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
2045 return bt_ctf_stream_common_get_name(BT_CTF_TO_COMMON(stream
));
2048 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
2050 return bt_ctf_stream_common_get_id(BT_CTF_TO_COMMON(stream
));