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>
54 void bt_ctf_stream_destroy(struct bt_object
*obj
);
56 int try_set_structure_field_integer(struct bt_ctf_field
*, char *, uint64_t);
59 int set_integer_field_value(struct bt_ctf_field
* field
, uint64_t value
)
62 struct bt_ctf_field_type
*field_type
= NULL
;
65 BT_LOGW_STR("Invalid parameter: field is NULL.");
70 field_type
= bt_ctf_field_get_type(field
);
73 if (bt_ctf_field_type_get_type_id(field_type
) !=
74 BT_CTF_FIELD_TYPE_ID_INTEGER
) {
75 /* Not an integer and the value is unset, error. */
76 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
77 "field-addr=%p, ft-addr=%p, ft-id=%s",
79 bt_ctf_field_type_id_string(field_type
->id
));
84 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
85 ret
= bt_ctf_field_signed_integer_set_value(field
, (int64_t) value
);
87 /* Value is out of range, error. */
88 BT_LOGW("Cannot set signed integer field's value: "
89 "addr=%p, value=%" PRId64
,
90 field
, (int64_t) value
);
94 ret
= bt_ctf_field_unsigned_integer_set_value(field
, value
);
96 /* Value is out of range, error. */
97 BT_LOGW("Cannot set unsigned integer field's value: "
98 "addr=%p, value=%" PRIu64
,
109 int set_packet_header_magic(struct bt_ctf_stream
*stream
)
112 struct bt_ctf_field
*magic_field
= bt_ctf_field_structure_get_field(
113 stream
->packet_header
, "magic");
114 const uint32_t magic_value
= 0xc1fc1fc1;
119 /* No magic field found. Not an error, skip. */
120 BT_LOGV("No field named `magic` in packet header: skipping: "
121 "stream-addr=%p, stream-name=\"%s\"",
122 stream
, bt_ctf_stream_get_name(stream
));
126 ret
= bt_ctf_field_unsigned_integer_set_value(magic_field
,
127 (uint64_t) magic_value
);
130 BT_LOGW("Cannot set packet header field's `magic` integer field's value: "
131 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
132 stream
, bt_ctf_stream_get_name(stream
),
133 magic_field
, (uint64_t) magic_value
);
135 BT_LOGV("Set packet header field's `magic` field's value: "
136 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
137 stream
, bt_ctf_stream_get_name(stream
),
138 magic_field
, (uint64_t) magic_value
);
146 int set_packet_header_uuid(struct bt_ctf_stream
*stream
)
150 struct bt_ctf_trace
*trace
= NULL
;
151 struct bt_ctf_field
*uuid_field
= bt_ctf_field_structure_get_field(
152 stream
->packet_header
, "uuid");
157 /* No uuid field found. Not an error, skip. */
158 BT_LOGV("No field named `uuid` in packet header: skipping: "
159 "stream-addr=%p, stream-name=\"%s\"",
160 stream
, bt_ctf_stream_get_name(stream
));
164 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
165 for (i
= 0; i
< 16; i
++) {
166 struct bt_ctf_field
*uuid_element
=
167 bt_ctf_field_array_get_field(uuid_field
, i
);
169 ret
= bt_ctf_field_unsigned_integer_set_value(
170 uuid_element
, (uint64_t) trace
->uuid
[i
]);
171 bt_put(uuid_element
);
173 BT_LOGW("Cannot set integer field's value (for `uuid` packet header field): "
174 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
175 "value=%" PRIu64
", index=%" PRId64
,
176 stream
, bt_ctf_stream_get_name(stream
),
177 uuid_element
, (uint64_t) trace
->uuid
[i
], i
);
182 BT_LOGV("Set packet header field's `uuid` field's value: "
183 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
184 stream
, bt_ctf_stream_get_name(stream
), uuid_field
);
192 int set_packet_header_stream_id(struct bt_ctf_stream
*stream
)
196 struct bt_ctf_field
*stream_id_field
= bt_ctf_field_structure_get_field(
197 stream
->packet_header
, "stream_id");
199 if (!stream_id_field
) {
200 /* No stream_id field found. Not an error, skip. */
201 BT_LOGV("No field named `stream_id` in packet header: skipping: "
202 "stream-addr=%p, stream-name=\"%s\"",
203 stream
, bt_ctf_stream_get_name(stream
));
207 stream_id
= stream
->stream_class
->id
;
208 ret
= bt_ctf_field_unsigned_integer_set_value(stream_id_field
,
209 (uint64_t) stream_id
);
211 BT_LOGW("Cannot set packet header field's `stream_id` integer field's value: "
212 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
213 stream
, bt_ctf_stream_get_name(stream
),
214 stream_id_field
, (uint64_t) stream_id
);
216 BT_LOGV("Set packet header field's `stream_id` field's value: "
217 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
218 stream
, bt_ctf_stream_get_name(stream
),
219 stream_id_field
, (uint64_t) stream_id
);
223 bt_put(stream_id_field
);
228 int auto_populate_packet_header(struct bt_ctf_stream
*stream
)
232 if (!stream
->packet_header
) {
236 ret
= set_packet_header_magic(stream
);
238 BT_LOGW("Cannot set packet header's magic number field: "
239 "stream-addr=%p, stream-name=\"%s\"",
240 stream
, bt_ctf_stream_get_name(stream
));
244 ret
= set_packet_header_uuid(stream
);
246 BT_LOGW("Cannot set packet header's UUID field: "
247 "stream-addr=%p, stream-name=\"%s\"",
248 stream
, bt_ctf_stream_get_name(stream
));
252 ret
= set_packet_header_stream_id(stream
);
254 BT_LOGW("Cannot set packet header's stream class ID field: "
255 "stream-addr=%p, stream-name=\"%s\"",
256 stream
, bt_ctf_stream_get_name(stream
));
260 BT_LOGV("Automatically populated stream's packet header's known fields: "
261 "stream-addr=%p, stream-name=\"%s\"",
262 stream
, bt_ctf_stream_get_name(stream
));
269 int set_packet_context_packet_size(struct bt_ctf_stream
*stream
)
272 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
273 stream
->packet_context
, "packet_size");
278 /* No packet size field found. Not an error, skip. */
279 BT_LOGV("No field named `packet_size` in packet context: skipping: "
280 "stream-addr=%p, stream-name=\"%s\"",
281 stream
, bt_ctf_stream_get_name(stream
));
285 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
286 stream
->pos
.packet_size
);
288 BT_LOGW("Cannot set packet context field's `packet_size` integer field's value: "
289 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
290 stream
, bt_ctf_stream_get_name(stream
),
291 field
, stream
->pos
.packet_size
);
293 BT_LOGV("Set packet context field's `packet_size` field's value: "
294 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
295 stream
, bt_ctf_stream_get_name(stream
),
296 field
, stream
->pos
.packet_size
);
305 int set_packet_context_content_size(struct bt_ctf_stream
*stream
)
308 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
309 stream
->packet_context
, "content_size");
314 /* No content size field found. Not an error, skip. */
315 BT_LOGV("No field named `content_size` in packet context: skipping: "
316 "stream-addr=%p, stream-name=\"%s\"",
317 stream
, bt_ctf_stream_get_name(stream
));
321 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
324 BT_LOGW("Cannot set packet context field's `content_size` integer field's value: "
325 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
326 stream
, bt_ctf_stream_get_name(stream
),
327 field
, stream
->pos
.offset
);
329 BT_LOGV("Set packet context field's `content_size` field's value: "
330 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRId64
,
331 stream
, bt_ctf_stream_get_name(stream
),
332 field
, stream
->pos
.offset
);
341 int set_packet_context_events_discarded(struct bt_ctf_stream
*stream
)
344 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
345 stream
->packet_context
, "events_discarded");
350 /* No discarded events count field found. Not an error, skip. */
351 BT_LOGV("No field named `events_discarded` in packet context: skipping: "
352 "stream-addr=%p, stream-name=\"%s\"",
353 stream
, bt_ctf_stream_get_name(stream
));
358 * If the field is set by the user, make sure that the value is
359 * greater than or equal to the stream's current count of
360 * discarded events. We do not allow wrapping here. If it's
361 * valid, update the stream's current count.
363 if (bt_ctf_field_is_set(field
)) {
366 ret
= bt_ctf_field_unsigned_integer_get_value(field
,
369 BT_LOGW("Cannot get packet context `events_discarded` field's unsigned value: "
370 "stream-addr=%p, stream-name=\"%s\", field-addr=%p",
371 stream
, bt_ctf_stream_get_name(stream
), field
);
375 if (user_val
< stream
->discarded_events
) {
376 BT_LOGW("Invalid packet context `events_discarded` field's unsigned value: "
377 "value is lesser than the stream's current discarded events count: "
378 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, "
379 "value=%" PRIu64
", "
380 "stream-discarded-events-count=%" PRIu64
,
381 stream
, bt_ctf_stream_get_name(stream
), field
,
382 user_val
, stream
->discarded_events
);
386 stream
->discarded_events
= user_val
;
388 ret
= bt_ctf_field_unsigned_integer_set_value(field
,
389 stream
->discarded_events
);
391 BT_LOGW("Cannot set packet context field's `events_discarded` integer field's value: "
392 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
393 stream
, bt_ctf_stream_get_name(stream
),
394 field
, stream
->discarded_events
);
396 BT_LOGV("Set packet context field's `events_discarded` field's value: "
397 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
398 stream
, bt_ctf_stream_get_name(stream
),
399 field
, stream
->discarded_events
);
409 int get_event_header_timestamp(struct bt_ctf_stream
*stream
,
410 struct bt_ctf_field
*event_header
, uint64_t *timestamp
)
413 struct bt_ctf_field
*timestamp_field
= NULL
;
414 struct bt_ctf_clock_class
*ts_field_mapped_clock_class
= NULL
;
419 BT_LOGV_STR("Event header does not exist.");
423 timestamp_field
= bt_ctf_field_structure_get_field(event_header
,
425 if (!timestamp_field
) {
426 BT_LOGV("Cannot get event header's `timestamp` field: "
427 "event-header-field-addr=%p", event_header
);
431 if (!bt_ctf_field_type_is_integer(timestamp_field
->type
)) {
432 BT_LOGV("Event header's `timestamp` field's type is not an integer field type: "
433 "event-header-field-addr=%p", event_header
);
437 ts_field_mapped_clock_class
=
438 bt_ctf_field_type_integer_get_mapped_clock_class(
439 timestamp_field
->type
);
440 if (!ts_field_mapped_clock_class
) {
441 BT_LOGV("Event header's `timestamp` field's type is not mapped to a clock class: "
442 "event-header-field-addr=%p", event_header
);
446 if (ts_field_mapped_clock_class
!=
447 stream
->stream_class
->clock
->clock_class
) {
448 BT_LOGV("Event header's `timestamp` field's type is not mapped to the stream's clock's class: "
449 "event-header-field-addr=%p", event_header
);
453 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_field
,
456 BT_LOGW("Cannot get unsigned integer field's value: "
457 "event-header-field-addr=%p, "
458 "timestamp-field-addr=%p",
459 event_header
, timestamp_field
);
464 bt_put(timestamp_field
);
465 bt_put(ts_field_mapped_clock_class
);
470 int set_packet_context_timestamp_field(struct bt_ctf_stream
*stream
,
471 const char *field_name
, struct bt_ctf_event
*event
)
474 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
475 stream
->packet_context
, field_name
);
476 struct bt_ctf_clock_class
*field_mapped_clock_class
= NULL
;
482 /* No beginning timestamp field found. Not an error, skip. */
483 BT_LOGV("No field named `%s` in packet context: skipping: "
484 "stream-addr=%p, stream-name=\"%s\"", field_name
,
485 stream
, bt_ctf_stream_get_name(stream
));
489 if (!stream
->stream_class
->clock
) {
490 BT_LOGV("Stream has no clock: skipping: "
491 "stream-addr=%p, stream-name=\"%s\"",
492 stream
, bt_ctf_stream_get_name(stream
));
496 field_mapped_clock_class
=
497 bt_ctf_field_type_integer_get_mapped_clock_class(field
->type
);
498 if (!field_mapped_clock_class
) {
499 BT_LOGV("Packet context's `%s` field's type is not mapped to a clock class: skipping: "
500 "stream-addr=%p, stream-name=\"%s\", "
501 "field-addr=%p, ft-addr=%p", field_name
,
502 stream
, bt_ctf_stream_get_name(stream
),
507 if (field_mapped_clock_class
!=
508 stream
->stream_class
->clock
->clock_class
) {
509 BT_LOGV("Packet context's `%s` field's type is not mapped to the stream's clock's class: skipping: "
510 "stream-addr=%p, stream-name=\"%s\", "
511 "field-addr=%p, ft-addr=%p, "
512 "ft-mapped-clock-class-addr=%p, "
513 "ft-mapped-clock-class-name=\"%s\", "
514 "stream-clock-class-addr=%p, "
515 "stream-clock-class-name=\"%s\"",
517 stream
, bt_ctf_stream_get_name(stream
),
519 field_mapped_clock_class
,
520 bt_ctf_clock_class_get_name(field_mapped_clock_class
),
521 stream
->stream_class
->clock
->clock_class
,
522 bt_ctf_clock_class_get_name(
523 stream
->stream_class
->clock
->clock_class
));
527 if (get_event_header_timestamp(stream
, event
->event_header
, &ts
)) {
528 BT_LOGW("Cannot get event's timestamp: "
529 "event-header-field-addr=%p",
530 event
->event_header
);
535 ret
= bt_ctf_field_unsigned_integer_set_value(field
, ts
);
537 BT_LOGW("Cannot set packet context field's `%s` integer field's value: "
538 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
539 field_name
, stream
, bt_ctf_stream_get_name(stream
),
540 field
, stream
->discarded_events
);
542 BT_LOGV("Set packet context field's `%s` field's value: "
543 "stream-addr=%p, stream-name=\"%s\", field-addr=%p, value=%" PRIu64
,
544 field_name
, stream
, bt_ctf_stream_get_name(stream
),
545 field
, stream
->discarded_events
);
550 bt_put(field_mapped_clock_class
);
555 int set_packet_context_timestamp_begin(struct bt_ctf_stream
*stream
)
559 if (stream
->events
->len
== 0) {
560 BT_LOGV("Current packet contains no events: skipping: "
561 "stream-addr=%p, stream-name=\"%s\"",
562 stream
, bt_ctf_stream_get_name(stream
));
566 ret
= set_packet_context_timestamp_field(stream
, "timestamp_begin",
567 g_ptr_array_index(stream
->events
, 0));
574 int set_packet_context_timestamp_end(struct bt_ctf_stream
*stream
)
578 if (stream
->events
->len
== 0) {
579 BT_LOGV("Current packet contains no events: skipping: "
580 "stream-addr=%p, stream-name=\"%s\"",
581 stream
, bt_ctf_stream_get_name(stream
));
585 ret
= set_packet_context_timestamp_field(stream
, "timestamp_end",
586 g_ptr_array_index(stream
->events
, stream
->events
->len
- 1));
593 int auto_populate_packet_context(struct bt_ctf_stream
*stream
)
597 if (!stream
->packet_context
) {
601 ret
= set_packet_context_packet_size(stream
);
603 BT_LOGW("Cannot set packet context's packet size field: "
604 "stream-addr=%p, stream-name=\"%s\"",
605 stream
, bt_ctf_stream_get_name(stream
));
609 ret
= set_packet_context_content_size(stream
);
611 BT_LOGW("Cannot set packet context's content size field: "
612 "stream-addr=%p, stream-name=\"%s\"",
613 stream
, bt_ctf_stream_get_name(stream
));
617 ret
= set_packet_context_timestamp_begin(stream
);
619 BT_LOGW("Cannot set packet context's beginning timestamp field: "
620 "stream-addr=%p, stream-name=\"%s\"",
621 stream
, bt_ctf_stream_get_name(stream
));
625 ret
= set_packet_context_timestamp_end(stream
);
627 BT_LOGW("Cannot set packet context's end timestamp field: "
628 "stream-addr=%p, stream-name=\"%s\"",
629 stream
, bt_ctf_stream_get_name(stream
));
633 ret
= set_packet_context_events_discarded(stream
);
635 BT_LOGW("Cannot set packet context's discarded events count field: "
636 "stream-addr=%p, stream-name=\"%s\"",
637 stream
, bt_ctf_stream_get_name(stream
));
641 BT_LOGV("Automatically populated stream's packet context's known fields: "
642 "stream-addr=%p, stream-name=\"%s\"",
643 stream
, bt_ctf_stream_get_name(stream
));
650 void release_event(struct bt_ctf_event
*event
)
652 if (bt_object_get_ref_count(event
)) {
654 * The event is being orphaned, but it must guarantee the
655 * existence of its event class for the duration of its
658 bt_get(event
->event_class
);
659 BT_PUT(event
->base
.parent
);
661 bt_object_release(event
);
666 int create_stream_file(struct bt_ctf_writer
*writer
,
667 struct bt_ctf_stream
*stream
)
670 GString
*filename
= g_string_new(NULL
);
671 int64_t stream_class_id
;
672 char *file_path
= NULL
;
674 BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
675 "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
676 writer
, stream
, bt_ctf_stream_get_name(stream
),
677 stream
->stream_class
, stream
->stream_class
->name
->str
);
679 if (stream
->name
&& stream
->name
->len
> 0) {
680 /* Use stream name's base name as prefix */
681 gchar
*basename
= g_path_get_basename(stream
->name
->str
);
685 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
686 g_string_assign(filename
, "stream");
688 g_string_assign(filename
, basename
);
695 if (stream
->stream_class
->name
&&
696 stream
->stream_class
->name
->len
> 0) {
697 /* Use stream class name's base name as prefix */
699 g_path_get_basename(stream
->stream_class
->name
->str
);
703 if (strcmp(basename
, G_DIR_SEPARATOR_S
) == 0) {
704 g_string_assign(filename
, "stream");
706 g_string_assign(filename
, basename
);
713 /* Default to using `stream-` as prefix */
714 g_string_assign(filename
, "stream");
717 stream_class_id
= bt_ctf_stream_class_get_id(stream
->stream_class
);
718 assert(stream_class_id
>= 0);
719 assert(stream
->id
>= 0);
720 g_string_append_printf(filename
, "-%" PRId64
"-%" PRId64
,
721 stream_class_id
, stream
->id
);
723 file_path
= g_build_filename(writer
->path
->str
, filename
->str
, NULL
);
724 if (file_path
== NULL
) {
730 O_RDWR
| O_CREAT
| O_TRUNC
,
731 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
734 BT_LOGW_ERRNO("Failed to open stream file for writing",
735 ": file_path=\"%s\", filename=\"%s\", ret=%d",
736 file_path
, filename
->str
, fd
);
740 BT_LOGD("Created stream file for writing: "
741 "stream-addr=%p, stream-name=\"%s\", "
742 "filename=\"%s\", fd=%d", stream
, bt_ctf_stream_get_name(stream
),
746 g_string_free(filename
, TRUE
);
751 void set_stream_fd(struct bt_ctf_stream
*stream
, int fd
)
753 (void) bt_ctf_stream_pos_init(&stream
->pos
, fd
, O_RDWR
);
758 void component_destroy_listener(struct bt_component
*component
, void *data
)
760 struct bt_ctf_stream
*stream
= data
;
762 BT_LOGD("Component is being destroyed, stream is notified: "
763 "comp-addr=%p, stream-addr=%p", component
, stream
);
764 g_hash_table_remove(stream
->comp_cur_port
, component
);
768 struct bt_ctf_stream
*bt_ctf_stream_create_with_id_no_check(
769 struct bt_ctf_stream_class
*stream_class
,
770 const char *name
, uint64_t id
)
773 struct bt_ctf_stream
*stream
= NULL
;
774 struct bt_ctf_trace
*trace
= NULL
;
775 struct bt_ctf_writer
*writer
= NULL
;
778 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
782 BT_LOGD("Creating stream object: stream-class-addr=%p, "
783 "stream-class-name=\"%s\", stream-name=\"%s\", "
784 "stream-id=%" PRIu64
,
785 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
787 trace
= bt_ctf_stream_class_borrow_trace(stream_class
);
789 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
790 "stream-class-addr=%p, stream-class-name=\"%s\", "
791 "stream-name=\"%s\"",
792 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
797 if (bt_ctf_trace_is_static(trace
)) {
799 * A static trace has the property that all its stream
800 * classes, clock classes, and streams are definitive:
801 * no more can be added, and each object is also frozen.
803 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is part of a static trace: "
804 "stream-class-addr=%p, stream-class-name=\"%s\", "
805 "stream-name=\"%s\", trace-addr=%p",
806 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
813 * Validate that the given ID is unique amongst all the
814 * existing trace's streams created from the same stream
819 for (i
= 0; i
< trace
->streams
->len
; i
++) {
820 struct bt_ctf_stream
*trace_stream
=
821 g_ptr_array_index(trace
->streams
, i
);
823 if (trace_stream
->stream_class
!= stream_class
) {
827 if (trace_stream
->id
== id
) {
828 BT_LOGW_STR("Invalid parameter: another stream in the same trace already has this ID.");
834 stream
= g_new0(struct bt_ctf_stream
, 1);
836 BT_LOGE_STR("Failed to allocate one stream.");
840 bt_object_init(stream
, bt_ctf_stream_destroy
);
842 * Acquire reference to parent since stream will become publicly
843 * reachable; it needs its parent to remain valid.
845 bt_object_set_parent(stream
, trace
);
846 stream
->stream_class
= stream_class
;
848 stream
->id
= (int64_t) id
;
850 stream
->destroy_listeners
= g_array_new(FALSE
, TRUE
,
851 sizeof(struct bt_ctf_stream_destroy_listener
));
852 if (!stream
->destroy_listeners
) {
853 BT_LOGE_STR("Failed to allocate a GArray.");
858 stream
->name
= g_string_new(name
);
860 BT_LOGE_STR("Failed to allocate a GString.");
865 BT_LOGD("Set stream's trace parent: trace-addr=%p", trace
);
867 if (trace
->is_created_by_writer
) {
869 writer
= (struct bt_ctf_writer
*) bt_object_get_parent(trace
);
870 stream
->id
= (int64_t) stream_class
->next_stream_id
++;
872 BT_LOGD("Stream object belongs to a writer's trace: "
873 "writer-addr=%p", writer
);
876 if (stream_class
->packet_context_type
) {
877 BT_LOGD("Creating stream's packet context field: "
878 "ft-addr=%p", stream_class
->packet_context_type
);
879 stream
->packet_context
= bt_ctf_field_create(
880 stream_class
->packet_context_type
);
881 if (!stream
->packet_context
) {
882 BT_LOGW_STR("Cannot create stream's packet context field.");
886 /* Initialize events_discarded */
887 ret
= try_set_structure_field_integer(
888 stream
->packet_context
, "events_discarded", 0);
890 BT_LOGW("Cannot set `events_discarded` field in packet context: "
891 "ret=%d, packet-context-field-addr=%p",
892 ret
, stream
->packet_context
);
897 stream
->events
= g_ptr_array_new_with_free_func(
898 (GDestroyNotify
) release_event
);
899 if (!stream
->events
) {
900 BT_LOGE_STR("Failed to allocate a GPtrArray.");
904 if (trace
->packet_header_type
) {
905 BT_LOGD("Creating stream's packet header field: "
906 "ft-addr=%p", trace
->packet_header_type
);
907 stream
->packet_header
=
908 bt_ctf_field_create(trace
->packet_header_type
);
909 if (!stream
->packet_header
) {
910 BT_LOGW_STR("Cannot create stream's packet header field.");
916 * Attempt to populate the default trace packet header fields
917 * (magic, uuid and stream_id). This will _not_ fail shall the
918 * fields not be found or be of an incompatible type; they will
919 * simply not be populated automatically. The user will have to
920 * make sure to set the trace packet header fields himself
923 ret
= auto_populate_packet_header(stream
);
925 BT_LOGW_STR("Cannot automatically populate the stream's packet header.");
929 /* Create file associated with this stream */
930 fd
= create_stream_file(writer
, stream
);
932 BT_LOGW_STR("Cannot create stream file.");
936 set_stream_fd(stream
, fd
);
938 /* Freeze the writer */
939 BT_LOGD_STR("Freezing stream's CTF writer.");
940 bt_ctf_writer_freeze(writer
);
942 /* Non-writer stream indicated by a negative FD */
943 set_stream_fd(stream
, -1);
944 stream
->comp_cur_port
= g_hash_table_new(g_direct_hash
,
946 if (!stream
->comp_cur_port
) {
947 BT_LOGE_STR("Failed to allocate a GHashTable.");
952 /* Add this stream to the trace's streams */
953 g_ptr_array_add(trace
->streams
, stream
);
954 BT_LOGD("Created stream object: addr=%p", stream
);
965 struct bt_ctf_stream
*bt_ctf_stream_create_with_id(
966 struct bt_ctf_stream_class
*stream_class
,
967 const char *name
, uint64_t id_param
)
969 struct bt_ctf_trace
*trace
;
970 struct bt_ctf_stream
*stream
= NULL
;
971 int64_t id
= (int64_t) id_param
;
974 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
979 BT_LOGW("Invalid parameter: invalid stream's ID: "
980 "name=\"%s\", id=%" PRIu64
,
985 trace
= bt_ctf_stream_class_borrow_trace(stream_class
);
987 BT_LOGW("Invalid parameter: cannot create stream from a stream class which is not part of trace: "
988 "stream-class-addr=%p, stream-class-name=\"%s\", "
989 "stream-name=\"%s\", stream-id=%" PRIu64
,
990 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
995 if (trace
->is_created_by_writer
) {
996 BT_LOGW("Invalid parameter: cannot create a CTF writer stream with this function; use bt_ctf_stream_create(): "
997 "stream-class-addr=%p, stream-class-name=\"%s\", "
998 "stream-name=\"%s\", stream-id=%" PRIu64
,
999 stream_class
, bt_ctf_stream_class_get_name(stream_class
),
1004 stream
= bt_ctf_stream_create_with_id_no_check(stream_class
,
1011 struct bt_ctf_stream
*bt_ctf_stream_create(
1012 struct bt_ctf_stream_class
*stream_class
,
1015 return bt_ctf_stream_create_with_id_no_check(stream_class
,
1019 struct bt_ctf_stream_class
*bt_ctf_stream_get_class(
1020 struct bt_ctf_stream
*stream
)
1022 struct bt_ctf_stream_class
*stream_class
= NULL
;
1025 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1029 stream_class
= stream
->stream_class
;
1030 bt_get(stream_class
);
1032 return stream_class
;
1035 int bt_ctf_stream_get_discarded_events_count(
1036 struct bt_ctf_stream
*stream
, uint64_t *count
)
1041 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1047 BT_LOGW_STR("Invalid parameter: count is NULL.");
1052 if (stream
->pos
.fd
< 0) {
1053 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1054 "stream-addr=%p, stream-name=\"%s\"",
1055 stream
, bt_ctf_stream_get_name(stream
));
1060 *count
= (uint64_t) stream
->discarded_events
;
1067 int set_packet_context_events_discarded_field(struct bt_ctf_stream
*stream
,
1071 struct bt_ctf_field
*events_discarded_field
= NULL
;
1073 if (!stream
->packet_context
) {
1077 events_discarded_field
= bt_ctf_field_structure_get_field(
1078 stream
->packet_context
, "events_discarded");
1079 if (!events_discarded_field
) {
1083 ret
= bt_ctf_field_unsigned_integer_set_value(
1084 events_discarded_field
, count
);
1086 BT_LOGW("Cannot set packet context's `events_discarded` field: "
1087 "field-addr=%p, value=%" PRIu64
,
1088 events_discarded_field
, count
);
1093 bt_put(events_discarded_field
);
1097 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream
*stream
,
1098 uint64_t event_count
)
1102 struct bt_ctf_field
*events_discarded_field
= NULL
;
1105 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1109 BT_LOGV("Appending discarded events to stream: "
1110 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1111 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1113 if (!stream
->packet_context
) {
1114 BT_LOGW_STR("Invalid parameter: stream has no packet context field.");
1118 if (stream
->pos
.fd
< 0) {
1119 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1123 events_discarded_field
= bt_ctf_field_structure_get_field(
1124 stream
->packet_context
, "events_discarded");
1125 if (!events_discarded_field
) {
1126 BT_LOGW_STR("No field named `events_discarded` in stream's packet context.");
1130 new_count
= stream
->discarded_events
+ event_count
;
1131 if (new_count
< stream
->discarded_events
) {
1132 BT_LOGW("New discarded events count is less than the stream's current discarded events count: "
1133 "cur-count=%" PRIu64
", new-count=%" PRIu64
,
1134 stream
->discarded_events
, new_count
);
1138 ret
= set_packet_context_events_discarded_field(stream
, new_count
);
1140 /* set_packet_context_events_discarded_field() logs errors */
1144 stream
->discarded_events
= new_count
;
1145 BT_LOGV("Appended discarded events to stream: "
1146 "stream-addr=%p, stream-name=\"%s\", append-count=%" PRIu64
,
1147 stream
, bt_ctf_stream_get_name(stream
), event_count
);
1150 bt_put(events_discarded_field
);
1153 static int auto_populate_event_header(struct bt_ctf_stream
*stream
,
1154 struct bt_ctf_event
*event
)
1157 struct bt_ctf_field
*id_field
= NULL
, *timestamp_field
= NULL
;
1158 struct bt_ctf_clock_class
*mapped_clock_class
= NULL
;
1159 int64_t event_class_id
;
1163 if (!event
->event_header
) {
1167 if (event
->frozen
) {
1168 BT_LOGW_STR("Cannot populate event header field: event is frozen.");
1173 BT_LOGV("Automatically populating event's header field: "
1174 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1175 stream
, bt_ctf_stream_get_name(stream
), event
);
1177 id_field
= bt_ctf_field_structure_get_field(event
->event_header
, "id");
1178 event_class_id
= bt_ctf_event_class_get_id(event
->event_class
);
1179 if (event_class_id
< 0) {
1180 BT_LOGE("Event class ID cannot be found");
1184 if (id_field
&& bt_ctf_field_type_is_integer(id_field
->type
)) {
1185 ret
= set_integer_field_value(id_field
, event_class_id
);
1187 BT_LOGW("Cannot set event header's `id` field's value: "
1188 "addr=%p, value=%" PRIu64
, id_field
,
1195 * The conditions to automatically set the timestamp are:
1197 * 1. The event header field "timestamp" exists and is an
1199 * 2. This stream's class has a registered clock (set with
1200 * bt_ctf_stream_class_set_clock()).
1201 * 3. The event header field "timestamp" has its type mapped to
1202 * a clock class which is also the clock class of this
1203 * stream's class's registered clock.
1205 timestamp_field
= bt_ctf_field_structure_get_field(event
->event_header
,
1207 if (timestamp_field
&& stream
->stream_class
->clock
&&
1208 bt_ctf_field_type_is_integer(timestamp_field
->type
)) {
1209 struct bt_ctf_clock_class
*stream_class_clock_class
=
1210 stream
->stream_class
->clock
->clock_class
;
1212 mapped_clock_class
=
1213 bt_ctf_field_type_integer_get_mapped_clock_class(
1214 timestamp_field
->type
);
1215 if (mapped_clock_class
== stream_class_clock_class
) {
1218 ret
= bt_ctf_clock_get_value(
1219 stream
->stream_class
->clock
,
1222 ret
= set_integer_field_value(timestamp_field
,
1225 BT_LOGW("Cannot set event header's `timestamp` field's value: "
1226 "addr=%p, value=%" PRIu64
,
1227 timestamp_field
, timestamp
);
1233 BT_LOGV("Automatically populated event's header field: "
1234 "stream-addr=%p, stream-name=\"%s\", event-addr=%p",
1235 stream
, bt_ctf_stream_get_name(stream
), event
);
1239 bt_put(timestamp_field
);
1240 bt_put(mapped_clock_class
);
1244 int bt_ctf_stream_append_event(struct bt_ctf_stream
*stream
,
1245 struct bt_ctf_event
*event
)
1250 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1256 BT_LOGW_STR("Invalid parameter: event is NULL.");
1261 if (stream
->pos
.fd
< 0) {
1262 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1267 BT_LOGV("Appending event to stream: "
1268 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1269 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1270 stream
, bt_ctf_stream_get_name(stream
), event
,
1271 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event
)),
1272 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event
)));
1275 * The event is not supposed to have a parent stream at this
1276 * point. The only other way an event can have a parent stream
1277 * is if it was assigned when setting a packet to the event,
1278 * in which case the packet's stream is not a writer stream,
1279 * and thus the user is trying to append an event which belongs
1280 * to another stream.
1282 if (event
->base
.parent
) {
1287 bt_object_set_parent(event
, stream
);
1288 BT_LOGV_STR("Automatically populating the header of the event to append.");
1289 ret
= auto_populate_event_header(stream
, event
);
1291 /* auto_populate_event_header() reports errors */
1295 /* Make sure the various scopes of the event are set */
1296 BT_LOGV_STR("Validating event to append.");
1297 ret
= bt_ctf_event_validate(event
);
1302 /* Save the new event and freeze it */
1303 BT_LOGV_STR("Freezing the event to append.");
1304 bt_ctf_event_freeze(event
);
1305 g_ptr_array_add(stream
->events
, event
);
1308 * Event had to hold a reference to its event class as long as it wasn't
1309 * part of the same trace hierarchy. From now on, the event and its
1310 * class share the same lifetime guarantees and the reference is no
1313 BT_LOGV_STR("Putting the event's class.");
1314 bt_put(event
->event_class
);
1315 BT_LOGV("Appended event to stream: "
1316 "stream-addr=%p, stream-name=\"%s\", event-addr=%p, "
1317 "event-class-name=\"%s\", event-class-id=%" PRId64
,
1318 stream
, bt_ctf_stream_get_name(stream
), event
,
1319 bt_ctf_event_class_get_name(bt_ctf_event_borrow_event_class(event
)),
1320 bt_ctf_event_class_get_id(bt_ctf_event_borrow_event_class(event
)));
1327 * Orphan the event; we were not successful in associating it to
1330 bt_object_set_parent(event
, NULL
);
1335 struct bt_ctf_field
*bt_ctf_stream_get_packet_context(
1336 struct bt_ctf_stream
*stream
)
1338 struct bt_ctf_field
*packet_context
= NULL
;
1341 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1345 if (stream
->pos
.fd
< 0) {
1346 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1347 "stream-addr=%p, stream-name=\"%s\"", stream
,
1348 bt_ctf_stream_get_name(stream
));
1352 packet_context
= stream
->packet_context
;
1353 if (packet_context
) {
1354 bt_get(packet_context
);
1357 return packet_context
;
1360 int bt_ctf_stream_set_packet_context(struct bt_ctf_stream
*stream
,
1361 struct bt_ctf_field
*field
)
1364 struct bt_ctf_field_type
*field_type
;
1367 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1372 if (stream
->pos
.fd
< 0) {
1373 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1378 field_type
= bt_ctf_field_get_type(field
);
1379 if (bt_ctf_field_type_compare(field_type
,
1380 stream
->stream_class
->packet_context_type
)) {
1381 BT_LOGW("Invalid parameter: packet context's field type is different from the stream's packet context field type: "
1382 "stream-addr=%p, stream-name=\"%s\", "
1383 "packet-context-field-addr=%p, "
1384 "packet-context-ft-addr=%p",
1385 stream
, bt_ctf_stream_get_name(stream
),
1392 bt_put(stream
->packet_context
);
1393 stream
->packet_context
= bt_get(field
);
1394 BT_LOGV("Set stream's packet context field: "
1395 "stream-addr=%p, stream-name=\"%s\", "
1396 "packet-context-field-addr=%p",
1397 stream
, bt_ctf_stream_get_name(stream
), field
);
1402 struct bt_ctf_field
*bt_ctf_stream_get_packet_header(
1403 struct bt_ctf_stream
*stream
)
1405 struct bt_ctf_field
*packet_header
= NULL
;
1408 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1412 if (stream
->pos
.fd
< 0) {
1413 BT_LOGW("Invalid parameter: stream is not a CTF writer stream: "
1414 "stream-addr=%p, stream-name=\"%s\"", stream
,
1415 bt_ctf_stream_get_name(stream
));
1419 packet_header
= stream
->packet_header
;
1420 if (packet_header
) {
1421 bt_get(packet_header
);
1424 return packet_header
;
1427 int bt_ctf_stream_set_packet_header(struct bt_ctf_stream
*stream
,
1428 struct bt_ctf_field
*field
)
1431 struct bt_ctf_trace
*trace
= NULL
;
1432 struct bt_ctf_field_type
*field_type
= NULL
;
1435 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1440 if (stream
->pos
.fd
< 0) {
1441 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1446 trace
= (struct bt_ctf_trace
*) bt_object_get_parent(stream
);
1449 if (trace
->packet_header_type
) {
1450 BT_LOGW("Invalid parameter: setting no packet header but packet header field type is not NULL: "
1451 "stream-addr=%p, stream-name=\"%s\", "
1452 "packet-header-field-addr=%p, "
1453 "expected-ft-addr=%p",
1454 stream
, bt_ctf_stream_get_name(stream
),
1455 field
, trace
->packet_header_type
);
1460 goto skip_validation
;
1463 field_type
= bt_ctf_field_get_type(field
);
1466 if (bt_ctf_field_type_compare(field_type
, trace
->packet_header_type
)) {
1467 BT_LOGW("Invalid parameter: packet header's field type is different from the stream's packet header field type: "
1468 "stream-addr=%p, stream-name=\"%s\", "
1469 "packet-header-field-addr=%p, "
1470 "packet-header-ft-addr=%p",
1471 stream
, bt_ctf_stream_get_name(stream
),
1478 bt_put(stream
->packet_header
);
1479 stream
->packet_header
= bt_get(field
);
1480 BT_LOGV("Set stream's packet header field: "
1481 "stream-addr=%p, stream-name=\"%s\", "
1482 "packet-header-field-addr=%p",
1483 stream
, bt_ctf_stream_get_name(stream
), field
);
1491 void reset_structure_field(struct bt_ctf_field
*structure
, const char *name
)
1493 struct bt_ctf_field
*member
;
1495 member
= bt_ctf_field_structure_get_field(structure
, name
);
1497 (void) bt_ctf_field_reset(member
);
1502 int bt_ctf_stream_flush(struct bt_ctf_stream
*stream
)
1506 struct bt_ctf_stream_pos packet_context_pos
;
1507 struct bt_ctf_trace
*trace
;
1508 enum bt_ctf_byte_order native_byte_order
;
1509 bool has_packet_size
= false;
1512 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1517 if (stream
->pos
.fd
< 0) {
1518 BT_LOGW_STR("Invalid parameter: stream is not a CTF writer stream.");
1523 if (stream
->packet_context
) {
1524 struct bt_ctf_field
*packet_size_field
;
1526 packet_size_field
= bt_ctf_field_structure_get_field(
1527 stream
->packet_context
, "packet_size");
1528 has_packet_size
= (packet_size_field
!= NULL
);
1529 bt_put(packet_size_field
);
1532 if (stream
->flushed_packet_count
== 1) {
1533 if (!stream
->packet_context
) {
1534 BT_LOGW_STR("Cannot flush a stream which has no packet context field more than once.");
1539 if (!has_packet_size
) {
1540 BT_LOGW_STR("Cannot flush a stream which has no packet context's `packet_size` field more than once.");
1546 BT_LOGV("Flushing stream's current packet: stream-addr=%p, "
1547 "stream-name=\"%s\", packet-index=%u", stream
,
1548 bt_ctf_stream_get_name(stream
), stream
->flushed_packet_count
);
1549 trace
= bt_ctf_stream_class_borrow_trace(stream
->stream_class
);
1551 native_byte_order
= bt_ctf_trace_get_native_byte_order(trace
);
1553 ret
= auto_populate_packet_header(stream
);
1555 BT_LOGW_STR("Cannot automatically populate the stream's packet header field.");
1560 ret
= auto_populate_packet_context(stream
);
1562 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1567 /* mmap the next packet */
1568 BT_LOGV("Seeking to the next packet: pos-offset=%" PRId64
,
1569 stream
->pos
.offset
);
1570 bt_ctf_stream_pos_packet_seek(&stream
->pos
, 0, SEEK_CUR
);
1571 assert(stream
->pos
.packet_size
% 8 == 0);
1573 if (stream
->packet_header
) {
1574 BT_LOGV_STR("Serializing packet header field.");
1575 ret
= bt_ctf_field_serialize(stream
->packet_header
, &stream
->pos
,
1578 BT_LOGW("Cannot serialize stream's packet header field: "
1579 "field-addr=%p", stream
->packet_header
);
1584 if (stream
->packet_context
) {
1585 /* Write packet context */
1586 memcpy(&packet_context_pos
, &stream
->pos
,
1587 sizeof(packet_context_pos
));
1588 BT_LOGV_STR("Serializing packet context field.");
1589 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1590 &stream
->pos
, native_byte_order
);
1592 BT_LOGW("Cannot serialize stream's packet context field: "
1593 "field-addr=%p", stream
->packet_context
);
1598 BT_LOGV("Serializing events: count=%u", stream
->events
->len
);
1600 for (i
= 0; i
< stream
->events
->len
; i
++) {
1601 struct bt_ctf_event
*event
= g_ptr_array_index(
1603 struct bt_ctf_event_class
*event_class
=
1604 bt_ctf_event_borrow_event_class(event
);
1606 BT_LOGV("Serializing event: index=%zu, event-addr=%p, "
1607 "event-class-name=\"%s\", event-class-id=%" PRId64
", "
1608 "pos-offset=%" PRId64
", packet-size=%" PRIu64
,
1609 i
, event
, bt_ctf_event_class_get_name(event_class
),
1610 bt_ctf_event_class_get_id(event_class
),
1611 stream
->pos
.offset
, stream
->pos
.packet_size
);
1613 /* Write event header */
1614 if (event
->event_header
) {
1615 BT_LOGV_STR("Serializing event's header field.");
1616 ret
= bt_ctf_field_serialize(event
->event_header
,
1617 &stream
->pos
, native_byte_order
);
1619 BT_LOGW("Cannot serialize event's header field: "
1620 "field-addr=%p", event
->event_header
);
1625 /* Write stream event context */
1626 if (event
->stream_event_context
) {
1627 BT_LOGV_STR("Serializing event's stream event context field.");
1628 ret
= bt_ctf_field_serialize(
1629 event
->stream_event_context
, &stream
->pos
,
1632 BT_LOGW("Cannot serialize event's stream event context field: "
1633 "field-addr=%p", event
->stream_event_context
);
1638 /* Write event content */
1639 ret
= bt_ctf_event_serialize(event
, &stream
->pos
,
1642 /* bt_ctf_event_serialize() logs errors */
1647 if (!has_packet_size
&& stream
->pos
.offset
% 8 != 0) {
1648 BT_LOGW("Stream's packet context field type has no `packet_size` field, "
1649 "but current content size is not a multiple of 8 bits: "
1650 "content-size=%" PRId64
", "
1651 "packet-size=%" PRIu64
,
1653 stream
->pos
.packet_size
);
1658 assert(stream
->pos
.packet_size
% 8 == 0);
1661 * Remove extra padding bytes.
1663 stream
->pos
.packet_size
= (stream
->pos
.offset
+ 7) & ~7;
1665 if (stream
->packet_context
) {
1667 * The whole packet is serialized at this point. Make sure that,
1668 * if `packet_size` is missing, the current content size is
1669 * equal to the current packet size.
1671 struct bt_ctf_field
*field
= bt_ctf_field_structure_get_field(
1672 stream
->packet_context
, "content_size");
1676 if (stream
->pos
.offset
!= stream
->pos
.packet_size
) {
1677 BT_LOGW("Stream's packet context's `content_size` field is missing, "
1678 "but current packet's content size is not equal to its packet size: "
1679 "content-size=%" PRId64
", "
1680 "packet-size=%" PRIu64
,
1682 stream
->pos
.packet_size
);
1689 * Overwrite the packet context now that the stream
1690 * position's packet and content sizes have the correct
1693 * Copy base_mma as the packet may have been remapped
1694 * (e.g. when a packet is resized).
1696 packet_context_pos
.base_mma
= stream
->pos
.base_mma
;
1697 ret
= auto_populate_packet_context(stream
);
1699 BT_LOGW_STR("Cannot automatically populate the stream's packet context field.");
1704 BT_LOGV("Rewriting (serializing) packet context field.");
1705 ret
= bt_ctf_field_serialize(stream
->packet_context
,
1706 &packet_context_pos
, native_byte_order
);
1708 BT_LOGW("Cannot serialize stream's packet context field: "
1709 "field-addr=%p", stream
->packet_context
);
1714 g_ptr_array_set_size(stream
->events
, 0);
1715 stream
->flushed_packet_count
++;
1716 stream
->size
+= stream
->pos
.packet_size
/ CHAR_BIT
;
1719 /* Reset automatically-set fields. */
1720 if (stream
->packet_context
) {
1721 reset_structure_field(stream
->packet_context
, "timestamp_begin");
1722 reset_structure_field(stream
->packet_context
, "timestamp_end");
1723 reset_structure_field(stream
->packet_context
, "packet_size");
1724 reset_structure_field(stream
->packet_context
, "content_size");
1725 reset_structure_field(stream
->packet_context
, "events_discarded");
1730 * We failed to write the packet. Its size is therefore set to 0
1731 * to ensure the next mapping is done in the same place rather
1732 * than advancing by "stream->pos.packet_size", which would
1733 * leave a corrupted packet in the trace.
1735 stream
->pos
.packet_size
= 0;
1737 BT_LOGV("Flushed stream's current packet: content-size=%" PRId64
", "
1738 "packet-size=%" PRIu64
,
1739 stream
->pos
.offset
, stream
->pos
.packet_size
);
1746 /* Pre-2.0 CTF writer backward compatibility */
1747 void bt_ctf_stream_get(struct bt_ctf_stream
*stream
)
1752 /* Pre-2.0 CTF writer backward compatibility */
1753 void bt_ctf_stream_put(struct bt_ctf_stream
*stream
)
1759 void bt_ctf_stream_destroy(struct bt_object
*obj
)
1761 struct bt_ctf_stream
*stream
;
1764 stream
= container_of(obj
, struct bt_ctf_stream
, base
);
1765 BT_LOGD("Destroying stream object: addr=%p, name=\"%s\"",
1766 stream
, bt_ctf_stream_get_name(stream
));
1768 /* Call destroy listeners in reverse registration order */
1769 for (i
= stream
->destroy_listeners
->len
- 1; i
>= 0; i
--) {
1770 struct bt_ctf_stream_destroy_listener
*listener
=
1771 &g_array_index(stream
->destroy_listeners
,
1772 struct bt_ctf_stream_destroy_listener
, i
);
1774 BT_LOGD("Calling destroy listener: func=%p, data=%p, index=%d",
1775 listener
->func
, listener
->data
, i
);
1776 listener
->func(stream
, listener
->data
);
1779 (void) bt_ctf_stream_pos_fini(&stream
->pos
);
1780 if (stream
->pos
.fd
>= 0) {
1784 * Truncate the file's size to the minimum required to fit the
1785 * last packet as we might have grown it too much on the last
1789 ret
= ftruncate(stream
->pos
.fd
, stream
->size
);
1790 } while (ret
== -1 && errno
== EINTR
);
1792 BT_LOGE_ERRNO("Failed to truncate stream file",
1793 ": ret=%d, size=%" PRIu64
,
1794 ret
, (uint64_t) stream
->size
);
1797 if (close(stream
->pos
.fd
)) {
1798 BT_LOGE_ERRNO("Failed to close stream file",
1803 if (stream
->events
) {
1804 BT_LOGD_STR("Putting events.");
1805 g_ptr_array_free(stream
->events
, TRUE
);
1809 g_string_free(stream
->name
, TRUE
);
1812 if (stream
->comp_cur_port
) {
1813 GHashTableIter ht_iter
;
1814 gpointer comp_gptr
, port_gptr
;
1817 * Since we're destroying the stream, remove the destroy
1818 * listeners that it registered for each component in
1819 * its component-port mapping hash table. Otherwise they
1820 * would be called and the stream would be accessed once
1821 * it's freed or another stream would be accessed.
1823 g_hash_table_iter_init(&ht_iter
, stream
->comp_cur_port
);
1825 while (g_hash_table_iter_next(&ht_iter
, &comp_gptr
, &port_gptr
)) {
1827 bt_component_remove_destroy_listener((void *) comp_gptr
,
1828 component_destroy_listener
, stream
);
1831 g_hash_table_destroy(stream
->comp_cur_port
);
1834 if (stream
->destroy_listeners
) {
1835 g_array_free(stream
->destroy_listeners
, TRUE
);
1838 BT_LOGD_STR("Putting packet header field.");
1839 bt_put(stream
->packet_header
);
1840 BT_LOGD_STR("Putting packet context field.");
1841 bt_put(stream
->packet_context
);
1846 int _set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1847 uint64_t value
, bt_bool force
)
1850 struct bt_ctf_field_type
*field_type
= NULL
;
1851 struct bt_ctf_field
*integer
;
1856 integer
= bt_ctf_field_structure_get_field(structure
, name
);
1858 /* Field not found, not an error. */
1859 BT_LOGV("Field not found: struct-field-addr=%p, "
1860 "name=\"%s\", force=%d", structure
, name
, force
);
1864 /* Make sure the payload has not already been set. */
1865 if (!force
&& bt_ctf_field_is_set(integer
)) {
1866 /* Payload already set, not an error */
1867 BT_LOGV("Field's payload is already set: struct-field-addr=%p, "
1868 "name=\"%s\", force=%d", structure
, name
, force
);
1872 field_type
= bt_ctf_field_get_type(integer
);
1874 if (bt_ctf_field_type_get_type_id(field_type
) != BT_CTF_FIELD_TYPE_ID_INTEGER
) {
1876 * The user most likely meant for us to populate this field
1877 * automatically. However, we can only do this if the field
1878 * is an integer. Return an error.
1880 BT_LOGW("Invalid parameter: field's type is not an integer field type: "
1881 "field-addr=%p, ft-addr=%p, ft-id=%s",
1882 integer
, field_type
,
1883 bt_ctf_field_type_id_string(field_type
->id
));
1888 if (bt_ctf_field_type_integer_get_signed(field_type
)) {
1889 ret
= bt_ctf_field_signed_integer_set_value(integer
,
1892 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, value
);
1894 ret
= !ret
? 1 : ret
;
1902 * Returns the following codes:
1903 * 1 if the field was found and set,
1904 * 0 if nothing was done (field not found, or was already set),
1905 * <0 if an error was encoutered
1908 int try_set_structure_field_integer(struct bt_ctf_field
*structure
, char *name
,
1911 return _set_structure_field_integer(structure
, name
, value
, BT_FALSE
);
1914 const char *bt_ctf_stream_get_name(struct bt_ctf_stream
*stream
)
1916 const char *name
= NULL
;
1919 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1923 name
= stream
->name
? stream
->name
->str
: NULL
;
1929 int bt_ctf_stream_is_writer(struct bt_ctf_stream
*stream
)
1934 BT_LOGW_STR("Invalid parameter: stream is NULL.");
1938 ret
= (stream
->pos
.fd
>= 0);
1945 void bt_ctf_stream_map_component_to_port(struct bt_ctf_stream
*stream
,
1946 struct bt_component
*comp
,
1947 struct bt_port
*port
)
1952 assert(stream
->comp_cur_port
);
1955 * Do not take a reference to the component here because we
1956 * don't want the component to exist as long as this stream
1957 * exists. Instead, keep a weak reference, but add a destroy
1958 * listener so that we remove this hash table entry when we know
1959 * the component is destroyed.
1961 BT_LOGV("Adding component's destroy listener for stream: "
1962 "stream-addr=%p, stream-name=\"%s\", comp-addr=%p, "
1963 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1964 stream
, bt_ctf_stream_get_name(stream
),
1965 comp
, bt_component_get_name(comp
), port
,
1966 bt_port_get_name(port
));
1967 bt_component_add_destroy_listener(comp
, component_destroy_listener
,
1969 g_hash_table_insert(stream
->comp_cur_port
, comp
, port
);
1970 BT_LOGV_STR("Mapped component to port for stream.");
1974 struct bt_port
*bt_ctf_stream_port_for_component(struct bt_ctf_stream
*stream
,
1975 struct bt_component
*comp
)
1979 assert(stream
->comp_cur_port
);
1980 return g_hash_table_lookup(stream
->comp_cur_port
, comp
);
1984 void bt_ctf_stream_add_destroy_listener(struct bt_ctf_stream
*stream
,
1985 bt_ctf_stream_destroy_listener_func func
, void *data
)
1987 struct bt_ctf_stream_destroy_listener listener
;
1991 listener
.func
= func
;
1992 listener
.data
= data
;
1993 g_array_append_val(stream
->destroy_listeners
, listener
);
1994 BT_LOGV("Added stream destroy listener: stream-addr=%p, "
1995 "stream-name=\"%s\", func=%p, data=%p",
1996 stream
, bt_ctf_stream_get_name(stream
), func
, data
);
2000 void bt_ctf_stream_remove_destroy_listener(struct bt_ctf_stream
*stream
,
2001 bt_ctf_stream_destroy_listener_func func
, void *data
)
2008 for (i
= 0; i
< stream
->destroy_listeners
->len
; i
++) {
2009 struct bt_ctf_stream_destroy_listener
*listener
=
2010 &g_array_index(stream
->destroy_listeners
,
2011 struct bt_ctf_stream_destroy_listener
, i
);
2013 if (listener
->func
== func
&& listener
->data
== data
) {
2014 g_array_remove_index(stream
->destroy_listeners
, i
);
2016 BT_LOGV("Removed stream destroy listener: stream-addr=%p, "
2017 "stream-name=\"%s\", func=%p, data=%p",
2018 stream
, bt_ctf_stream_get_name(stream
),
2024 int64_t bt_ctf_stream_get_id(struct bt_ctf_stream
*stream
)
2029 BT_LOGW_STR("Invalid parameter: stream is NULL.");
2036 BT_LOGV("Stream's ID is not set: addr=%p, name=\"%s\"",
2037 stream
, bt_ctf_stream_get_name(stream
));