4 * Babeltrace CTF IR - Stream
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "STREAM"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/ctf-ir/clock-class.h>
33 #include <babeltrace/ctf-writer/clock.h>
34 #include <babeltrace/ctf-writer/clock-internal.h>
35 #include <babeltrace/ctf-writer/event.h>
36 #include <babeltrace/ctf-ir/event-internal.h>
37 #include <babeltrace/ctf-ir/field-types-internal.h>
38 #include <babeltrace/ctf-ir/fields-internal.h>
39 #include <babeltrace/ctf-ir/stream.h>
40 #include <babeltrace/ctf-ir/stream-internal.h>
41 #include <babeltrace/ctf-ir/stream-class-internal.h>
42 #include <babeltrace/ctf-ir/trace.h>
43 #include <babeltrace/ctf-ir/trace-internal.h>
44 #include <babeltrace/ctf-writer/writer-internal.h>
45 #include <babeltrace/graph/component-internal.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/ctf-writer/functor-internal.h>
48 #include <babeltrace/compiler-internal.h>
49 #include <babeltrace/align-internal.h>
53 void bt_ctf_stream_destroy(struct bt_object
*obj
);
55 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
58 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
61 struct bt_ctf_field_type
*field_type
= NULL
;
64 BT_LOGW_STR("Invalid parameter: field is NULL.");
69 field_type
= bt_ctf_field_get_type(field
);
72 if (bt_ctf_field_type_get_type_id(field_type
) !=
73 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
74 /* Not an integer and the value is unset, error. */
75 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
76 "field-addr=%p, ft-addr=%p, ft-id=%s",
78 bt_ctf_field_type_id_string(field_type
->id
));
83 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
84 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
86 /* Value is out of range, error. */
87 BT_LOGW("Cannot set signed integer field's value: "
88 "addr=%p, value=%" PRId64
,
89 field
, (int64_t) value
);
93 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
95 /* Value is out of range, error. */
96 BT_LOGW("Cannot set unsigned integer field's value: "
97 "addr=%p, value=%" PRIu64
,
108 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
111 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
112 stream
->packet_header
, "magic");
113 const uint32_t magic_value
= 0xc1fc1fc1;
118 /* No magic field found. Not an error, skip. */
119 BT_LOGV("No field named `magic` in packet header: skipping: "
120 "stream-addr=%p, stream-name=\"%s\"",
121 stream
, bt_ctf_stream_get_name(stream
));
125 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
126 (uint64_t) magic_value
);
129 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
130 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
131 stream
, bt_ctf_stream_get_name(stream
),
132 magic_field
, (uint64_t) magic_value
);
134 BT_LOGV("Set packet header field's `magic` field's value: "
135 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
136 stream
, bt_ctf_stream_get_name(stream
),
137 magic_field
, (uint64_t) magic_value
);
145 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
149 struct bt_ctf_trace
*trace
= NULL
;
150 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
151 stream
->packet_header
, "uuid");
156 /* No uuid field found. Not an error, skip. */
157 BT_LOGV("No field named `uuid` in packet header: skipping: "
158 "stream-addr=%p, stream-name=\"%s\"",
159 stream
, bt_ctf_stream_get_name(stream
));
163 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
164 for (i
= 0; i
< 16; i
++) {
165 struct bt_ctf_field
*uuid_element
=
166 bt_ctf_field_array_get_field(uuid_field
, i
);
168 ret
= bt_ctf_field_unsigned_integer_set_value(
169 uuid_element
, (uint64_t) trace
->uuid
[i
]);
170 bt_put(uuid_element
);
172 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
173 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
174 "value=%" PRIu64
", index=%" PRId64
,
175 stream
, bt_ctf_stream_get_name(stream
),
176 uuid_element
, (uint64_t) trace
->uuid
[i
], i
);
181 BT_LOGV("Set packet header field's `uuid` field's value: "
182 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
183 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
191 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
195 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
196 stream
->packet_header
, "stream_id");
198 if (!stream_id_field
) {
199 /* No stream_id field found. Not an error, skip. */
200 BT_LOGV("No field named `stream_id` in packet header: skipping: "
201 "stream-addr=%p, stream-name=\"%s\"",
202 stream
, bt_ctf_stream_get_name(stream
));
206 stream_id
= stream
->stream_class
->id
;
207 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
208 (uint64_t) stream_id
);
210 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
211 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
212 stream
, bt_ctf_stream_get_name(stream
),
213 stream_id_field
, (uint64_t) stream_id
);
215 BT_LOGV("Set packet header field's `stream_id` field's value: "
216 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
217 stream
, bt_ctf_stream_get_name(stream
),
218 stream_id_field
, (uint64_t) stream_id
);
222 bt_put(stream_id_field
);
227 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
231 if (!stream
->packet_header
) {
235 ret
= set_packet_header_magic(stream
);
237 BT_LOGW("Cannot set packet header's magic number field: "
238 "stream-addr=%p, stream-name=\"%s\"",
239 stream
, bt_ctf_stream_get_name(stream
));
243 ret
= set_packet_header_uuid(stream
);
245 BT_LOGW("Cannot set packet header's UUID field: "
246 "stream-addr=%p, stream-name=\"%s\"",
247 stream
, bt_ctf_stream_get_name(stream
));
251 ret
= set_packet_header_stream_id(stream
);
253 BT_LOGW("Cannot set packet header's stream class ID field: "
254 "stream-addr=%p, stream-name=\"%s\"",
255 stream
, bt_ctf_stream_get_name(stream
));
259 BT_LOGV("Automatically populated stream's packet header's known fields: "
260 "stream-addr=%p, stream-name=\"%s\"",
261 stream
, bt_ctf_stream_get_name(stream
));
268 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
)
271 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
272 stream
->packet_context
, "packet_size");
277 /* No packet size field found. Not an error, skip. */
278 BT_LOGV("No field named `packet_size` in packet context: skipping: "
279 "stream-addr=%p, stream-name=\"%s\"",
280 stream
, bt_ctf_stream_get_name(stream
));
284 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
285 stream
->pos
.packet_size
);
287 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
288 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
289 stream
, bt_ctf_stream_get_name(stream
),
290 field
, stream
->pos
.packet_size
);
292 BT_LOGV("Set packet context field's `packet_size` field's value: "
293 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
294 stream
, bt_ctf_stream_get_name(stream
),
295 field
, stream
->pos
.packet_size
);
304 int set_packet_context_content_size(struct bt_ctf_stream
*stream
)
307 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
308 stream
->packet_context
, "content_size");
313 /* No content size field found. Not an error, skip. */
314 BT_LOGV("No field named `content_size` in packet context: skipping: "
315 "stream-addr=%p, stream-name=\"%s\"",
316 stream
, bt_ctf_stream_get_name(stream
));
320 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
323 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
324 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
325 stream
, bt_ctf_stream_get_name(stream
),
326 field
, stream
->pos
.offset
);
328 BT_LOGV("Set packet context field's `content_size` field's value: "
329 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
330 stream
, bt_ctf_stream_get_name(stream
),
331 field
, stream
->pos
.offset
);
340 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
343 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
344 stream
->packet_context
, "events_discarded");
349 /* No discarded events count field found. Not an error, skip. */
350 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
351 "stream-addr=%p, stream-name=\"%s\"",
352 stream
, bt_ctf_stream_get_name(stream
));
357 * If the field is set by the user, make sure that the value is
358 * greater than or equal to the stream's current count of
359 * discarded events. We do not allow wrapping here. If it's
360 * valid, update the stream's current count.
362 if (bt_ctf_field_is_set(field
)) {
365 ret
= bt_ctf_field_unsigned_integer_get_value(field
,
368 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
369 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
370 stream
, bt_ctf_stream_get_name(stream
), field
);
374 if (user_val
< stream
->discarded_events
) {
375 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
376 "value is lesser than the stream's current discarded events count: "
377 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
378 "value=%" PRIu64
", "
379 "stream-discarded-events-count=%" PRIu64
,
380 stream
, bt_ctf_stream_get_name(stream
), field
,
381 user_val
, stream
->discarded_events
);
385 stream
->discarded_events
= user_val
;
387 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
388 stream
->discarded_events
);
390 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
391 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
392 stream
, bt_ctf_stream_get_name(stream
),
393 field
, stream
->discarded_events
);
395 BT_LOGV("Set packet context field's `events_discarded` field's value: "
396 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
397 stream
, bt_ctf_stream_get_name(stream
),
398 field
, stream
->discarded_events
);
408 int get_event_header_timestamp(struct bt_ctf_stream
*stream
,
409 struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
412 struct bt_ctf_field
*timestamp_field
= NULL
;
413 struct bt_ctf_clock_class
*ts_field_mapped_clock_class
= NULL
;
418 BT_LOGV_STR("Event header does not exist.");
422 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
424 if (!timestamp_field
) {
425 BT_LOGV("Cannot get event header's `timestamp` field: "
426 "event-header-field-addr=%p", event_header
);
430 if (!bt_ctf_field_type_is_integer(timestamp_field
->type
)) {
431 BT_LOGV("Event header's `timestamp` field's type is not an integer field type: "
432 "event-header-field-addr=%p", event_header
);
436 ts_field_mapped_clock_class
=
437 bt_ctf_field_type_integer_get_mapped_clock_class(
438 timestamp_field
->type
);
439 if (!ts_field_mapped_clock_class
) {
440 BT_LOGV("Event header's `timestamp` field's type is not mapped to a clock class: "
441 "event-header-field-addr=%p", event_header
);
445 if (ts_field_mapped_clock_class
!=
446 stream
->stream_class
->clock
->clock_class
) {
447 BT_LOGV("Event header's `timestamp` field's type is not mapped to the stream's clock's class: "
448 "event-header-field-addr=%p", event_header
);
452 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
455 BT_LOGW("Cannot get unsigned integer field's value: "
456 "event-header-field-addr=%p, "
457 "timestamp-field-addr=%p",
458 event_header
, timestamp_field
);
463 bt_put(timestamp_field
);
464 bt_put(ts_field_mapped_clock_class
);
469 int set_packet_context_timestamp_field(struct bt_ctf_stream
*stream
,
470 const char *field_name
, struct bt_ctf_event
*event
)
473 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
474 stream
->packet_context
, field_name
);
475 struct bt_ctf_clock_class
*field_mapped_clock_class
= NULL
;
481 /* No beginning timestamp field found. Not an error, skip. */
482 BT_LOGV("No field named `%s` in packet context: skipping: "
483 "stream-addr=%p, stream-name=\"%s\"", field_name
,
484 stream
, bt_ctf_stream_get_name(stream
));
488 if (!stream
->stream_class
->clock
) {
489 BT_LOGV("Stream has no clock: skipping: "
490 "stream-addr=%p, stream-name=\"%s\"",
491 stream
, bt_ctf_stream_get_name(stream
));
495 field_mapped_clock_class
=
496 bt_ctf_field_type_integer_get_mapped_clock_class(field
->type
);
497 if (!field_mapped_clock_class
) {
498 BT_LOGV("Packet context's `%s` field's type is not mapped to a clock class: skipping: "
499 "stream-addr=%p, stream-name=\"%s\", "
500 "field-addr=%p, ft-addr=%p", field_name
,
501 stream
, bt_ctf_stream_get_name(stream
),
506 if (field_mapped_clock_class
!=
507 stream
->stream_class
->clock
->clock_class
) {
508 BT_LOGV("Packet context's `%s` field's type is not mapped to the stream's clock's class: skipping: "
509 "stream-addr=%p, stream-name=\"%s\", "
510 "field-addr=%p, ft-addr=%p, "
511 "ft-mapped-clock-class-addr=%p, "
512 "ft-mapped-clock-class-name=\"%s\", "
513 "stream-clock-class-addr=%p, "
514 "stream-clock-class-name=\"%s\"",
516 stream
, bt_ctf_stream_get_name(stream
),
518 field_mapped_clock_class
,
519 bt_ctf_clock_class_get_name(field_mapped_clock_class
),
520 stream
->stream_class
->clock
->clock_class
,
521 bt_ctf_clock_class_get_name(
522 stream
->stream_class
->clock
->clock_class
));
526 if (get_event_header_timestamp(stream
, event
->event_header
, &ts
)) {
527 BT_LOGW("Cannot get event's timestamp: "
528 "event-header-field-addr=%p",
529 event
->event_header
);
534 ret
= bt_ctf_field_unsigned_integer_set_value(field
, ts
);
536 BT_LOGW("Cannot set packet context field's `%s` integer field's value: "
537 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
538 field_name
, stream
, bt_ctf_stream_get_name(stream
),
539 field
, stream
->discarded_events
);
541 BT_LOGV("Set packet context field's `%s` field's value: "
542 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
543 field_name
, stream
, bt_ctf_stream_get_name(stream
),
544 field
, stream
->discarded_events
);
549 bt_put(field_mapped_clock_class
);
554 int set_packet_context_timestamp_begin(struct bt_ctf_stream
*stream
)
558 if (stream
->events
->len
== 0) {
559 BT_LOGV("Current packet contains no events: skipping: "
560 "stream-addr=%p, stream-name=\"%s\"",
561 stream
, bt_ctf_stream_get_name(stream
));
565 ret
= set_packet_context_timestamp_field(stream
, "timestamp_begin",
566 g_ptr_array_index(stream
->events
, 0));
573 int set_packet_context_timestamp_end(struct bt_ctf_stream
*stream
)
577 if (stream
->events
->len
== 0) {
578 BT_LOGV("Current packet contains no events: skipping: "
579 "stream-addr=%p, stream-name=\"%s\"",
580 stream
, bt_ctf_stream_get_name(stream
));
584 ret
= set_packet_context_timestamp_field(stream
, "timestamp_end",
585 g_ptr_array_index(stream
->events
, stream
->events
->len
- 1));
592 int auto_populate_packet_context(struct bt_ctf_stream
*stream
)
596 if (!stream
->packet_context
) {
600 ret
= set_packet_context_packet_size(stream
);
602 BT_LOGW("Cannot set packet context's packet size field: "
603 "stream-addr=%p, stream-name=\"%s\"",
604 stream
, bt_ctf_stream_get_name(stream
));
608 ret
= set_packet_context_content_size(stream
);
610 BT_LOGW("Cannot set packet context's content size field: "
611 "stream-addr=%p, stream-name=\"%s\"",
612 stream
, bt_ctf_stream_get_name(stream
));
616 ret
= set_packet_context_timestamp_begin(stream
);
618 BT_LOGW("Cannot set packet context's beginning timestamp field: "
619 "stream-addr=%p, stream-name=\"%s\"",
620 stream
, bt_ctf_stream_get_name(stream
));
624 ret
= set_packet_context_timestamp_end(stream
);
626 BT_LOGW("Cannot set packet context's end timestamp field: "
627 "stream-addr=%p, stream-name=\"%s\"",
628 stream
, bt_ctf_stream_get_name(stream
));
632 ret
= set_packet_context_events_discarded(stream
);
634 BT_LOGW("Cannot set packet context's discarded events count field: "
635 "stream-addr=%p, stream-name=\"%s\"",
636 stream
, bt_ctf_stream_get_name(stream
));
640 BT_LOGV("Automatically populated stream's packet context's known fields: "
641 "stream-addr=%p, stream-name=\"%s\"",
642 stream
, bt_ctf_stream_get_name(stream
));
649 void release_event(struct bt_ctf_event
*event
)
651 if (bt_object_get_ref_count(event
)) {
653 * The event is being orphaned, but it must guarantee the
654 * existence of its event class for the duration of its
657 bt_get(event
->event_class
);
658 BT_PUT(event
->base
.parent
);
660 bt_object_release(event
);
665 int create_stream_file(struct bt_ctf_writer
*writer
,
666 struct bt_ctf_stream
*stream
)
669 GString
*filename
= g_string_new(stream
->stream_class
->name
->str
);
671 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
672 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
673 writer
, stream
, bt_ctf_stream_get_name(stream
),
674 stream
->stream_class
, stream
->stream_class
->name
->str
);
676 if (stream
->stream_class
->name
->len
== 0) {
679 ret
= bt_ctf_stream_class_get_id(stream
->stream_class
);
681 BT_LOGW("Cannot get stream class's ID: "
682 "stream-class-addr=%p, "
683 "stream-class-name=\"%s\"",
684 stream
->stream_class
,
685 stream
->stream_class
->name
->str
);
690 g_string_printf(filename
, "stream_%" PRId64
, ret
);
693 g_string_append_printf(filename
, "_%" PRId64
, stream
->id
);
694 fd
= openat(writer
->trace_dir_fd
, filename
->str
,
695 O_RDWR
| O_CREAT
| O_TRUNC
,
696 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
698 BT_LOGW("Failed to open stream file for writing: %s: "
699 "writer-trace-dir-fd=%d, filename=\"%s\", "
700 "ret=%d, errno=%d", strerror(errno
),
701 writer
->trace_dir_fd
, filename
->str
, fd
, errno
);
705 BT_LOGD("Created stream file for writing: "
706 "stream-addr=%p, stream-name=\"%s\", writer-trace-dir-fd=%d, "
707 "filename=\"%s\", fd=%d", stream
, bt_ctf_stream_get_name(stream
),
708 writer
->trace_dir_fd
, filename
->str
, fd
);
711 g_string_free(filename
, TRUE
);
716 void set_stream_fd(struct bt_ctf_stream
*stream
, int fd
)
718 (void) bt_ctf_stream_pos_init(&stream
->pos
, fd
, O_RDWR
);
723 void component_destroy_listener(struct bt_component
*component
, void *data
)
725 struct bt_ctf_stream
*stream
= data
;
727 BT_LOGD("Component is being destroyed, stream is notified: "
728 "comp-addr=%p, stream-addr=%p", component
, stream
);
729 g_hash_table_remove(stream
->comp_cur_port
, component
);
733 struct bt_ctf_stream
*bt_ctf_stream_create_with_id_no_check(
734 struct bt_ctf_stream_class
*stream_class
,
735 const char *name
, uint64_t id
)
738 struct bt_ctf_stream
*stream
= NULL
;
739 struct bt_ctf_trace
*trace
= NULL
;
740 struct bt_ctf_writer
*writer
= NULL
;
743 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
747 BT_LOGD("Creating stream object: stream-class-addr=%p, "
748 "stream-class-name=\"%s\", stream-name=\"%s\", "
749 "stream-id=%" PRIu64
,
750 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
752 trace
= bt_ctf_stream_class_borrow_trace(stream_class
);
754 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
755 "stream-class-addr=%p, stream-class-name=\"%s\", "
756 "stream-name=\"%s\"",
757 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
762 if (bt_ctf_trace_is_static(trace
)) {
764 * A static trace has the property that all its stream
765 * classes, clock classes, and streams are definitive:
766 * no more can be added, and each object is also frozen.
768 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
769 "stream-class-addr=%p, stream-class-name=\"%s\", "
770 "stream-name=\"%s\", trace-addr=%p",
771 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
778 * Validate that the given ID is unique amongst all the
779 * existing trace's streams created from the same stream
784 for (i
= 0; i
< trace
->streams
->len
; i
++) {
785 struct bt_ctf_stream
*trace_stream
=
786 g_ptr_array_index(trace
->streams
, i
);
788 if (trace_stream
->stream_class
!= stream_class
) {
792 if (trace_stream
->id
== id
) {
793 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
799 stream
= g_new0(struct bt_ctf_stream
, 1);
801 BT_LOGE_STR("Failed to allocate one stream.");
805 bt_object_init(stream
, bt_ctf_stream_destroy
);
807 * Acquire reference to parent since stream will become publicly
808 * reachable; it needs its parent to remain valid.
810 bt_object_set_parent(stream
, trace
);
811 stream
->stream_class
= stream_class
;
813 stream
->id
= (int64_t) id
;
815 stream
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
816 sizeof(struct bt_ctf_stream_destroy_listener
));
817 if (!stream
->destroy_listeners
) {
818 BT_LOGE_STR("Failed to allocate a GArray.");
823 stream
->name
= g_string_new(name
);
825 BT_LOGE_STR("Failed to allocate a GString.");
830 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace
);
832 if (trace
->is_created_by_writer
) {
834 writer
= (struct bt_ctf_writer
*) bt_object_get_parent(trace
);
835 stream
->id
= (int64_t) stream_class
->next_stream_id
++;
837 BT_LOGD("Stream object belongs to a writer's trace: "
838 "writer-addr=%p", writer
);
841 if (stream_class
->packet_context_type
) {
842 BT_LOGD("Creating stream's packet context field: "
843 "ft-addr=%p", stream_class
->packet_context_type
);
844 stream
->packet_context
= bt_ctf_field_create(
845 stream_class
->packet_context_type
);
846 if (!stream
->packet_context
) {
847 BT_LOGW_STR("Cannot create stream's packet context field.");
851 /* Initialize events_discarded */
852 ret
= try_set_structure_field_integer(
853 stream
->packet_context
, "events_discarded", 0);
855 BT_LOGW("Cannot set `events_discarded` field in packet context: "
856 "ret=%d, packet-context-field-addr=%p",
857 ret
, stream
->packet_context
);
862 stream
->events
= g_ptr_array_new_with_free_func(
863 (GDestroyNotify
) release_event
);
864 if (!stream
->events
) {
865 BT_LOGE_STR("Failed to allocate a GPtrArray.");
869 if (trace
->packet_header_type
) {
870 BT_LOGD("Creating stream's packet header field: "
871 "ft-addr=%p", trace
->packet_header_type
);
872 stream
->packet_header
=
873 bt_ctf_field_create(trace
->packet_header_type
);
874 if (!stream
->packet_header
) {
875 BT_LOGW_STR("Cannot create stream's packet header field.");
881 * Attempt to populate the default trace packet header fields
882 * (magic, uuid and stream_id). This will _not_ fail shall the
883 * fields not be found or be of an incompatible type; they will
884 * simply not be populated automatically. The user will have to
885 * make sure to set the trace packet header fields himself
888 ret
= auto_populate_packet_header(stream
);
890 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
894 /* Create file associated with this stream */
895 fd
= create_stream_file(writer
, stream
);
897 BT_LOGW_STR("Cannot create stream file.");
901 set_stream_fd(stream
, fd
);
903 /* Freeze the writer */
904 BT_LOGD_STR("Freezing stream's CTF writer.");
905 bt_ctf_writer_freeze(writer
);
907 /* Non-writer stream indicated by a negative FD */
908 set_stream_fd(stream
, -1);
909 stream
->comp_cur_port
= g_hash_table_new(g_direct_hash
,
911 if (!stream
->comp_cur_port
) {
912 BT_LOGE_STR("Failed to allocate a GHashTable.");
917 /* Add this stream to the trace's streams */
918 g_ptr_array_add(trace
->streams
, stream
);
919 BT_LOGD("Created stream object: addr=%p", stream
);
930 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
931 struct bt_ctf_stream_class
*stream_class
,
932 const char *name
, uint64_t id_param
)
934 struct bt_ctf_trace
*trace
;
935 struct bt_ctf_stream
*stream
= NULL
;
936 int64_t id
= (int64_t) id_param
;
939 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
944 BT_LOGW("Invalid parameter: invalid stream's ID: "
945 "name=\"%s\", id=%" PRIu64
,
950 trace
= bt_ctf_stream_class_borrow_trace(stream_class
);
952 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
953 "stream-class-addr=%p, stream-class-name=\"%s\", "
954 "stream-name=\"%s\", stream-id=%" PRIu64
,
955 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
960 if (trace
->is_created_by_writer
) {
961 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_ctf_stream_create(): "
962 "stream-class-addr=%p, stream-class-name=\"%s\", "
963 "stream-name=\"%s\", stream-id=%" PRIu64
,
964 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
969 stream
= bt_ctf_stream_create_with_id_no_check(stream_class
,
976 struct bt_ctf_stream
*bt_ctf_stream_create(
977 struct bt_ctf_stream_class
*stream_class
,
980 return bt_ctf_stream_create_with_id_no_check(stream_class
,
984 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
985 struct bt_ctf_stream
*stream
)
987 struct bt_ctf_stream_class
*stream_class
= NULL
;
990 BT_LOGW_STR("Invalid parameter: stream is NULL.");
994 stream_class
= stream
->stream_class
;
995 bt_get(stream_class
);
1000 int64_t bt_ctf_stream_get_discarded_events_count(
1001 struct bt_ctf_stream
*stream
, uint64_t *count
)
1006 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1012 BT_LOGW_STR("Invalid parameter: count is NULL.");
1017 if (stream
->pos
.fd
< 0) {
1018 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1019 "stream-addr=%p, stream-name=\"%s\"",
1020 stream
, bt_ctf_stream_get_name(stream
));
1025 *count
= (uint64_t) stream
->discarded_events
;
1032 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1036 struct bt_ctf_field
*events_discarded_field
= NULL
;
1038 if (!stream
->packet_context
) {
1042 events_discarded_field
= bt_ctf_field_structure_get_field(
1043 stream
->packet_context
, "events_discarded");
1044 if (!events_discarded_field
) {
1048 ret
= bt_ctf_field_unsigned_integer_set_value(
1049 events_discarded_field
, count
);
1051 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1052 "field-addr=%p, value=%" PRIu64
,
1053 events_discarded_field
, count
);
1058 bt_put(events_discarded_field
);
1062 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1063 uint64_t event_count
)
1067 struct bt_ctf_field
*events_discarded_field
= NULL
;
1070 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1074 BT_LOGV("Appending discarded events to stream: "
1075 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1076 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1078 if (!stream
->packet_context
) {
1079 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1083 if (stream
->pos
.fd
< 0) {
1084 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1088 events_discarded_field
= bt_ctf_field_structure_get_field(
1089 stream
->packet_context
, "events_discarded");
1090 if (!events_discarded_field
) {
1091 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1095 new_count
= stream
->discarded_events
+ event_count
;
1096 if (new_count
< stream
->discarded_events
) {
1097 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1098 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1099 stream
->discarded_events
, new_count
);
1103 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1105 /* set_packet_context_events_discarded_field() logs errors */
1109 stream
->discarded_events
= new_count
;
1110 BT_LOGV("Appended discarded events to stream: "
1111 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1112 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1115 bt_put(events_discarded_field
);
1118 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1119 struct bt_ctf_event
*event
)
1122 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1123 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1124 uint64_t event_class_id
;
1128 if (event
->frozen
) {
1129 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1134 BT_LOGV("Automatically populating event's header field: "
1135 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1136 stream
, bt_ctf_stream_get_name(stream
), event
);
1138 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1139 event_class_id
= (uint64_t) bt_ctf_event_class_get_id(event
->event_class
);
1140 assert(event_class_id
>= 0);
1141 if (id_field
&& bt_ctf_field_type_is_integer(id_field
->type
)) {
1142 ret
= set_integer_field_value(id_field
, event_class_id
);
1144 BT_LOGW("Cannot set event header's `id` field's value: "
1145 "addr=%p, value=%" PRIu64
, id_field
,
1152 * The conditions to automatically set the timestamp are:
1154 * 1. The event header field "timestamp" exists and is an
1156 * 2. This stream's class has a registered clock (set with
1157 * bt_ctf_stream_class_set_clock()).
1158 * 3. The event header field "timestamp" has its type mapped to
1159 * a clock class which is also the clock class of this
1160 * stream's class's registered clock.
1162 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1164 if (timestamp_field
&& stream
->stream_class
->clock
&&
1165 bt_ctf_field_type_is_integer(timestamp_field
->type
)) {
1166 struct bt_ctf_clock_class
*stream_class_clock_class
=
1167 stream
->stream_class
->clock
->clock_class
;
1169 mapped_clock_class
=
1170 bt_ctf_field_type_integer_get_mapped_clock_class(
1171 timestamp_field
->type
);
1172 if (mapped_clock_class
== stream_class_clock_class
) {
1175 ret
= bt_ctf_clock_get_value(
1176 stream
->stream_class
->clock
,
1179 ret
= set_integer_field_value(timestamp_field
,
1182 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1183 "addr=%p, value=%" PRIu64
,
1184 timestamp_field
, timestamp
);
1190 BT_LOGV("Automatically populated event's header field: "
1191 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1192 stream
, bt_ctf_stream_get_name(stream
), event
);
1196 bt_put(timestamp_field
);
1197 bt_put(mapped_clock_class
);
1201 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1202 struct bt_ctf_event
*event
)
1207 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1213 BT_LOGW_STR("Invalid parameter: event is NULL.");
1218 if (stream
->pos
.fd
< 0) {
1219 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1224 BT_LOGV("Appending event to stream: "
1225 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1226 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1227 stream
, bt_ctf_stream_get_name(stream
), event
,
1228 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event
)),
1229 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event
)));
1232 * The event is not supposed to have a parent stream at this
1233 * point. The only other way an event can have a parent stream
1234 * is if it was assigned when setting a packet to the event,
1235 * in which case the packet's stream is not a writer stream,
1236 * and thus the user is trying to append an event which belongs
1237 * to another stream.
1239 if (event
->base
.parent
) {
1244 bt_object_set_parent(event
, stream
);
1245 BT_LOGV_STR("Automatically populating the header of the event to append.");
1246 ret
= auto_populate_event_header(stream
, event
);
1248 /* auto_populate_event_header() reports errors */
1252 /* Make sure the various scopes of the event are set */
1253 BT_LOGV_STR("Validating event to append.");
1254 ret
= bt_ctf_event_validate(event
);
1259 /* Save the new event and freeze it */
1260 BT_LOGV_STR("Freezing the event to append.");
1261 bt_ctf_event_freeze(event
);
1262 g_ptr_array_add(stream
->events
, event
);
1265 * Event had to hold a reference to its event class as long as it wasn't
1266 * part of the same trace hierarchy. From now on, the event and its
1267 * class share the same lifetime guarantees and the reference is no
1270 BT_LOGV_STR("Putting the event's class.");
1271 bt_put(event
->event_class
);
1272 BT_LOGV("Appended event to stream: "
1273 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1274 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1275 stream
, bt_ctf_stream_get_name(stream
), event
,
1276 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event
)),
1277 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event
)));
1284 * Orphan the event; we were not successful in associating it to
1287 bt_object_set_parent(event
, NULL
);
1292 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
1293 struct bt_ctf_stream
*stream
)
1295 struct bt_ctf_field
*packet_context
= NULL
;
1298 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1302 if (stream
->pos
.fd
< 0) {
1303 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1304 "stream-addr=%p, stream-name=\"%s\"", stream
,
1305 bt_ctf_stream_get_name(stream
));
1309 packet_context
= stream
->packet_context
;
1310 if (packet_context
) {
1311 bt_get(packet_context
);
1314 return packet_context
;
1317 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1318 struct bt_ctf_field
*field
)
1321 struct bt_ctf_field_type
*field_type
;
1324 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1329 if (stream
->pos
.fd
< 0) {
1330 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1335 field_type
= bt_ctf_field_get_type(field
);
1336 if (bt_ctf_field_type_compare(field_type
,
1337 stream
->stream_class
->packet_context_type
)) {
1338 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1339 "stream-addr=%p, stream-name=\"%s\", "
1340 "packet-context-field-addr=%p, "
1341 "packet-context-ft-addr=%p",
1342 stream
, bt_ctf_stream_get_name(stream
),
1349 bt_put(stream
->packet_context
);
1350 stream
->packet_context
= bt_get(field
);
1351 BT_LOGV("Set stream's packet context field: "
1352 "stream-addr=%p, stream-name=\"%s\", "
1353 "packet-context-field-addr=%p",
1354 stream
, bt_ctf_stream_get_name(stream
), field
);
1359 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
1360 struct bt_ctf_stream
*stream
)
1362 struct bt_ctf_field
*packet_header
= NULL
;
1365 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1369 if (stream
->pos
.fd
< 0) {
1370 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1371 "stream-addr=%p, stream-name=\"%s\"", stream
,
1372 bt_ctf_stream_get_name(stream
));
1376 packet_header
= stream
->packet_header
;
1377 if (packet_header
) {
1378 bt_get(packet_header
);
1381 return packet_header
;
1384 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1385 struct bt_ctf_field
*field
)
1388 struct bt_ctf_trace
*trace
= NULL
;
1389 struct bt_ctf_field_type
*field_type
= NULL
;
1392 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1397 if (stream
->pos
.fd
< 0) {
1398 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1403 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
1406 if (trace
->packet_header_type
) {
1407 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1408 "stream-addr=%p, stream-name=\"%s\", "
1409 "packet-header-field-addr=%p, "
1410 "expected-ft-addr=%p",
1411 stream
, bt_ctf_stream_get_name(stream
),
1412 field
, trace
->packet_header_type
);
1417 goto skip_validation
;
1420 field_type
= bt_ctf_field_get_type(field
);
1423 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
1424 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1425 "stream-addr=%p, stream-name=\"%s\", "
1426 "packet-header-field-addr=%p, "
1427 "packet-header-ft-addr=%p",
1428 stream
, bt_ctf_stream_get_name(stream
),
1435 bt_put(stream
->packet_header
);
1436 stream
->packet_header
= bt_get(field
);
1437 BT_LOGV("Set stream's packet header field: "
1438 "stream-addr=%p, stream-name=\"%s\", "
1439 "packet-header-field-addr=%p",
1440 stream
, bt_ctf_stream_get_name(stream
), field
);
1448 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1450 struct bt_ctf_field
*member
;
1452 member
= bt_ctf_field_structure_get_field(structure
, name
);
1454 (void) bt_ctf_field_reset(member
);
1458 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1462 struct bt_ctf_stream_pos packet_context_pos
;
1463 struct bt_ctf_trace
*trace
;
1464 enum bt_ctf_byte_order native_byte_order
;
1467 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1472 if (stream
->pos
.fd
< 0) {
1473 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1478 if (stream
->flushed_packet_count
== 1) {
1479 struct bt_ctf_field
*packet_size_field
;
1481 if (!stream
->packet_context
) {
1482 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1487 packet_size_field
= bt_ctf_field_structure_get_field(
1488 stream
->packet_context
, "packet_size");
1489 bt_put(packet_size_field
);
1490 if (!packet_size_field
) {
1491 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1497 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1498 "stream-name=\"%s\", packet-index=%u", stream
,
1499 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1500 trace
= bt_ctf_stream_class_borrow_trace(stream
->stream_class
);
1502 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1504 ret
= auto_populate_packet_header(stream
);
1506 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1511 ret
= auto_populate_packet_context(stream
);
1513 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1518 /* mmap the next packet */
1519 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64
,
1520 stream
->pos
.offset
);
1521 bt_ctf_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
1522 assert(stream
->pos
.packet_size
% 8 == 0);
1524 if (stream
->packet_header
) {
1525 BT_LOGV_STR("Serializing packet header field.");
1526 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
,
1529 BT_LOGW("Cannot serialize stream's packet header field: "
1530 "field-addr=%p", stream
->packet_header
);
1535 if (stream
->packet_context
) {
1536 /* Write packet context */
1537 memcpy(&packet_context_pos
, &stream
->pos
,
1538 sizeof(packet_context_pos
));
1539 BT_LOGV_STR("Serializing packet context field.");
1540 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1541 &stream
->pos
, native_byte_order
);
1543 BT_LOGW("Cannot serialize stream's packet context field: "
1544 "field-addr=%p", stream
->packet_context
);
1549 BT_LOGV("Serializing events: count=%u", stream
->events
->len
);
1551 for (i
= 0; i
< stream
->events
->len
; i
++) {
1552 struct bt_ctf_event
*event
= g_ptr_array_index(
1554 struct bt_ctf_event_class
*event_class
=
1555 bt_ctf_event_borrow_event_class(event
);
1557 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1558 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1559 "pos-offset=%" PRId64
", packet-size=%" PRIu64
,
1560 i
, event
, bt_ctf_event_class_get_name(event_class
),
1561 bt_ctf_event_class_get_id(event_class
),
1562 stream
->pos
.offset
, stream
->pos
.packet_size
);
1564 /* Write event header */
1565 BT_LOGV_STR("Serializing event's header field.");
1566 ret
= bt_ctf_field_serialize(event
->event_header
,
1567 &stream
->pos
, native_byte_order
);
1569 BT_LOGW("Cannot serialize event's header field: "
1570 "field-addr=%p", event
->event_header
);
1574 /* Write stream event context */
1575 if (event
->stream_event_context
) {
1576 BT_LOGV_STR("Serializing event's stream event context field.");
1577 ret
= bt_ctf_field_serialize(
1578 event
->stream_event_context
, &stream
->pos
,
1581 BT_LOGW("Cannot serialize event's stream event context field: "
1582 "field-addr=%p", event
->stream_event_context
);
1587 /* Write event content */
1588 ret
= bt_ctf_event_serialize(event
, &stream
->pos
,
1591 /* bt_ctf_event_serialize() logs errors */
1596 assert(stream
->pos
.packet_size
% 8 == 0);
1598 if (stream
->packet_context
) {
1600 * The whole packet is serialized at this point. Make sure that,
1601 * if `packet_size` is missing, the current content size is
1602 * equal to the current packet size.
1604 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
1605 stream
->packet_context
, "content_size");
1609 if (stream
->pos
.offset
!= stream
->pos
.packet_size
) {
1610 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1611 "but current packet's content size is not equal to its packet size: "
1612 "content-size=%" PRId64
", "
1613 "packet-size=%" PRIu64
,
1615 stream
->pos
.packet_size
);
1622 * Overwrite the packet context now that the stream
1623 * position's packet and content sizes have the correct
1626 * Copy base_mma as the packet may have been remapped
1627 * (e.g. when a packet is resized).
1629 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1630 ret
= auto_populate_packet_context(stream
);
1632 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1637 BT_LOGV("Rewriting (serializing) packet context field.");
1638 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1639 &packet_context_pos
, native_byte_order
);
1641 BT_LOGW("Cannot serialize stream's packet context field: "
1642 "field-addr=%p", stream
->packet_context
);
1647 g_ptr_array_set_size(stream
->events
, 0);
1648 stream
->flushed_packet_count
++;
1649 stream
->size
+= stream
->pos
.packet_size
/ CHAR_BIT
;
1651 /* Reset automatically-set fields. */
1652 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1653 reset_structure_field(stream
->packet_context
, "timestamp_end");
1654 reset_structure_field(stream
->packet_context
, "packet_size");
1655 reset_structure_field(stream
->packet_context
, "content_size");
1656 reset_structure_field(stream
->packet_context
, "events_discarded");
1660 * We failed to write the packet. Its size is therefore set to 0
1661 * to ensure the next mapping is done in the same place rather
1662 * than advancing by "stream->pos.packet_size", which would
1663 * leave a corrupted packet in the trace.
1665 stream
->pos
.packet_size
= 0;
1667 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64
", "
1668 "packet-size=%" PRIu64
,
1669 stream
->pos
.offset
, stream
->pos
.packet_size
);
1674 /* Pre-2.0 CTF writer backward compatibility */
1675 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
1680 /* Pre-2.0 CTF writer backward compatibility */
1681 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
1687 void bt_ctf_stream_destroy(struct bt_object
*obj
)
1689 struct bt_ctf_stream
*stream
;
1692 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
1693 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1694 stream
, bt_ctf_stream_get_name(stream
));
1696 /* Call destroy listeners in reverse registration order */
1697 for (i
= stream
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
1698 struct bt_ctf_stream_destroy_listener
*listener
=
1699 &g_array_index(stream
->destroy_listeners
,
1700 struct bt_ctf_stream_destroy_listener
, i
);
1702 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1703 listener
->func
, listener
->data
, i
);
1704 listener
->func(stream
, listener
->data
);
1707 (void) bt_ctf_stream_pos_fini(&stream
->pos
);
1708 if (stream
->pos
.fd
>= 0) {
1712 * Truncate the file's size to the minimum required to fit the
1713 * last packet as we might have grown it too much on the last
1717 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1718 } while (ret
== -1 && errno
== EINTR
);
1720 BT_LOGE("Failed to truncate stream file: %s: "
1721 "ret=%d, errno=%d, size=%" PRIu64
,
1722 strerror(errno
), ret
, errno
,
1723 (uint64_t) stream
->size
);
1726 if (close(stream
->pos
.fd
)) {
1727 BT_LOGE("Failed to close stream file: %s: "
1728 "ret=%d, errno=%d", strerror(errno
),
1733 if (stream
->events
) {
1734 BT_LOGD_STR("Putting events.");
1735 g_ptr_array_free(stream
->events
, TRUE
);
1739 g_string_free(stream
->name
, TRUE
);
1742 if (stream
->comp_cur_port
) {
1743 GHashTableIter ht_iter
;
1744 gpointer comp_gptr
, port_gptr
;
1747 * Since we're destroying the stream, remove the destroy
1748 * listeners that it registered for each component in
1749 * its component-port mapping hash table. Otherwise they
1750 * would be called and the stream would be accessed once
1751 * it's freed or another stream would be accessed.
1753 g_hash_table_iter_init(&ht_iter
, stream
->comp_cur_port
);
1755 while (g_hash_table_iter_next(&ht_iter
, &comp_gptr
, &port_gptr
)) {
1757 bt_component_remove_destroy_listener((void *) comp_gptr
,
1758 component_destroy_listener
, stream
);
1761 g_hash_table_destroy(stream
->comp_cur_port
);
1764 if (stream
->destroy_listeners
) {
1765 g_array_free(stream
->destroy_listeners
, TRUE
);
1768 BT_LOGD_STR("Putting packet header field.");
1769 bt_put(stream
->packet_header
);
1770 BT_LOGD_STR("Putting packet context field.");
1771 bt_put(stream
->packet_context
);
1776 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1777 uint64_t value
, bt_bool force
)
1780 struct bt_ctf_field_type
*field_type
= NULL
;
1781 struct bt_ctf_field
*integer
;
1786 integer
= bt_ctf_field_structure_get_field(structure
, name
);
1788 /* Field not found, not an error. */
1789 BT_LOGV("Field not found: struct-field-addr=%p, "
1790 "name=\"%s\", force=%d", structure
, name
, force
);
1794 /* Make sure the payload has not already been set. */
1795 if (!force
&& bt_ctf_field_is_set(integer
)) {
1796 /* Payload already set, not an error */
1797 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1798 "name=\"%s\", force=%d", structure
, name
, force
);
1802 field_type
= bt_ctf_field_get_type(integer
);
1804 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1806 * The user most likely meant for us to populate this field
1807 * automatically. However, we can only do this if the field
1808 * is an integer. Return an error.
1810 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1811 "field-addr=%p, ft-addr=%p, ft-id=%s",
1812 integer
, field_type
,
1813 bt_ctf_field_type_id_string(field_type
->id
));
1818 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
1819 ret
= bt_ctf_field_signed_integer_set_value(integer
,
1822 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
1824 ret
= !ret
? 1 : ret
;
1832 * Returns the following codes:
1833 * 1 if the field was found and set,
1834 * 0 if nothing was done (field not found, or was already set),
1835 * <0 if an error was encoutered
1838 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1841 return _set_structure_field_integer(structure
, name
, value
, BT_FALSE
);
1844 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1846 const char *name
= NULL
;
1849 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1853 name
= stream
->name
? stream
->name
->str
: NULL
;
1859 int bt_ctf_stream_is_writer(struct bt_ctf_stream
*stream
)
1864 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1868 ret
= (stream
->pos
.fd
>= 0);
1875 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream
*stream
,
1876 struct bt_component
*comp
,
1877 struct bt_port
*port
)
1882 assert(stream
->comp_cur_port
);
1885 * Do not take a reference to the component here because we
1886 * don't want the component to exist as long as this stream
1887 * exists. Instead, keep a weak reference, but add a destroy
1888 * listener so that we remove this hash table entry when we know
1889 * the component is destroyed.
1891 BT_LOGV("Adding component's destroy listener for stream: "
1892 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
1893 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1894 stream
, bt_ctf_stream_get_name(stream
),
1895 comp
, bt_component_get_name(comp
), port
,
1896 bt_port_get_name(port
));
1897 bt_component_add_destroy_listener(comp
, component_destroy_listener
,
1899 g_hash_table_insert(stream
->comp_cur_port
, comp
, port
);
1900 BT_LOGV_STR("Mapped component to port for stream.");
1904 struct bt_port
*bt_ctf_stream_port_for_component(struct bt_ctf_stream
*stream
,
1905 struct bt_component
*comp
)
1909 assert(stream
->comp_cur_port
);
1910 return g_hash_table_lookup(stream
->comp_cur_port
, comp
);
1914 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream
*stream
,
1915 bt_ctf_stream_destroy_listener_func func
, void *data
)
1917 struct bt_ctf_stream_destroy_listener listener
;
1921 listener
.func
= func
;
1922 listener
.data
= data
;
1923 g_array_append_val(stream
->destroy_listeners
, listener
);
1924 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
1925 "stream-name=\"%s\", func=%p, data=%p",
1926 stream
, bt_ctf_stream_get_name(stream
), func
, data
);
1930 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream
*stream
,
1931 bt_ctf_stream_destroy_listener_func func
, void *data
)
1938 for (i
= 0; i
< stream
->destroy_listeners
->len
; i
++) {
1939 struct bt_ctf_stream_destroy_listener
*listener
=
1940 &g_array_index(stream
->destroy_listeners
,
1941 struct bt_ctf_stream_destroy_listener
, i
);
1943 if (listener
->func
== func
&& listener
->data
== data
) {
1944 g_array_remove_index(stream
->destroy_listeners
, i
);
1946 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
1947 "stream-name=\"%s\", func=%p, data=%p",
1948 stream
, bt_ctf_stream_get_name(stream
),
1954 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
1959 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1966 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
1967 stream
, bt_ctf_stream_get_name(stream
));