2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
10 #include <babeltrace2/babeltrace.h>
12 #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp)
13 #define BT_LOG_OUTPUT_LEVEL (stream->log_level)
14 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM"
15 #include "logging/comp-logging.h"
17 #include "common/assert.h"
18 #include "compat/endian.h" /* IWYU pragma: keep */
19 #include "ctfser/ctfser.h"
21 #include "fs-sink-ctf-meta.hpp"
22 #include "fs-sink-stream.hpp"
23 #include "fs-sink-trace.hpp"
24 #include "fs-sink.hpp"
25 #include "translate-trace-ir-to-ctf-ir.hpp"
27 void fs_sink_stream_destroy(struct fs_sink_stream
*stream
)
33 bt_ctfser_fini(&stream
->ctfser
);
35 if (stream
->file_name
) {
36 g_string_free(stream
->file_name
, TRUE
);
37 stream
->file_name
= NULL
;
40 bt_packet_put_ref(stream
->packet_state
.packet
);
47 static bool stream_file_name_exists(struct fs_sink_trace
*trace
, const char *name
)
53 g_hash_table_iter_init(&iter
, trace
->streams
);
55 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
56 struct fs_sink_stream
*stream
= (fs_sink_stream
*) value
;
58 if (strcmp(name
, stream
->file_name
->str
) == 0) {
68 static GString
*sanitize_stream_file_name(const char *file_name
)
70 GString
*san_file_name
= g_string_new(NULL
);
74 BT_ASSERT(san_file_name
);
76 basename
= g_path_get_basename(file_name
);
78 for (ch
= basename
; *ch
!= '\0'; ch
++) {
80 g_string_append_c(san_file_name
, '_');
82 g_string_append_c(san_file_name
, *ch
);
86 /* Do not allow `.` and `..` either */
87 if (strcmp(san_file_name
->str
, ".") == 0 || strcmp(san_file_name
->str
, "..") == 0) {
88 g_string_assign(san_file_name
, "stream");
95 static GString
*make_unique_stream_file_name(struct fs_sink_trace
*trace
, const char *base
)
97 GString
*san_base
= sanitize_stream_file_name(base
);
98 GString
*name
= g_string_new(san_base
->str
);
99 unsigned int suffix
= 0;
103 while (stream_file_name_exists(trace
, name
->str
) || strcmp(name
->str
, "metadata") == 0) {
104 g_string_printf(name
, "%s-%u", san_base
->str
, suffix
);
108 g_string_free(san_base
, TRUE
);
112 static void set_stream_file_name(struct fs_sink_stream
*stream
)
114 const char *base_name
= bt_stream_get_name(stream
->ir_stream
);
117 base_name
= "stream";
120 BT_ASSERT(!stream
->file_name
);
121 stream
->file_name
= make_unique_stream_file_name(stream
->trace
, base_name
);
124 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
125 const bt_stream
*ir_stream
)
127 fs_sink_stream
*stream
= new fs_sink_stream
;
129 GString
*path
= g_string_new(trace
->path
->str
);
131 stream
->log_level
= trace
->log_level
;
132 stream
->trace
= trace
;
133 stream
->ir_stream
= ir_stream
;
134 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
135 stream
->packet_state
.end_cs
= UINT64_C(-1);
136 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
137 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
138 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
139 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(
140 trace
->fs_sink
, trace
->trace
, bt_stream_borrow_class_const(ir_stream
), &stream
->sc
);
145 set_stream_file_name(stream
);
146 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
147 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
, stream
->log_level
);
152 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
156 fs_sink_stream_destroy(stream
);
161 g_string_free(path
, TRUE
);
167 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
168 const bt_field
*field
);
170 static inline int write_bool_field(struct fs_sink_stream
*stream
,
171 struct fs_sink_ctf_field_class_bool
*fc
, const bt_field
*field
)
174 * CTF 1.8 has no boolean field class type, so this component
175 * translates this boolean field to an 8-bit unsigned integer
176 * field which has the value 0 (false) or 1 (true).
178 return bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_bool_get_value(field
) ? 1 : 0,
179 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
182 static inline int write_bit_array_field(struct fs_sink_stream
*stream
,
183 struct fs_sink_ctf_field_class_bit_array
*fc
,
184 const bt_field
*field
)
187 * CTF 1.8 has no bit array field class type, so this component
188 * translates this bit array field to an unsigned integer field.
190 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
191 bt_field_bit_array_get_value_as_integer(field
),
192 fc
->base
.alignment
, fc
->size
, BYTE_ORDER
);
195 static inline int write_int_field(struct fs_sink_stream
*stream
,
196 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
201 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
, bt_field_integer_signed_get_value(field
),
202 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
204 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
205 bt_field_integer_unsigned_get_value(field
),
206 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
212 static inline int write_float_field(struct fs_sink_stream
*stream
,
213 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
218 if (fc
->base
.size
== 32) {
219 val
= (double) bt_field_real_single_precision_get_value(field
);
220 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
222 val
= bt_field_real_double_precision_get_value(field
);
223 ret
= bt_ctfser_write_float64(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
229 static inline int write_string_field(struct fs_sink_stream
*stream
, const bt_field
*field
)
231 return bt_ctfser_write_string(&stream
->ctfser
, bt_field_string_get_value(field
));
234 static inline int write_array_base_field_elements(struct fs_sink_stream
*stream
,
235 struct fs_sink_ctf_field_class_array_base
*fc
,
236 const bt_field
*field
)
239 uint64_t len
= bt_field_array_get_length(field
);
242 for (i
= 0; i
< len
; i
++) {
243 const bt_field
*elem_field
= bt_field_array_borrow_element_field_by_index_const(field
, i
);
244 ret
= write_field(stream
, fc
->elem_fc
, elem_field
);
245 if (G_UNLIKELY(ret
)) {
254 static inline int write_sequence_field(struct fs_sink_stream
*stream
,
255 struct fs_sink_ctf_field_class_sequence
*fc
,
256 const bt_field
*field
)
260 if (fc
->length_is_before
) {
261 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_array_get_length(field
), 8, 32,
263 if (G_UNLIKELY(ret
)) {
268 ret
= write_array_base_field_elements(stream
, &fc
->base
, field
);
274 static inline int write_struct_field(struct fs_sink_stream
*stream
,
275 struct fs_sink_ctf_field_class_struct
*fc
,
276 const bt_field
*field
, bool align_struct
)
281 if (G_LIKELY(align_struct
)) {
282 ret
= bt_ctfser_align_offset_in_current_packet(&stream
->ctfser
, fc
->base
.alignment
);
283 if (G_UNLIKELY(ret
)) {
288 for (i
= 0; i
< fc
->members
->len
; i
++) {
289 const bt_field
*memb_field
=
290 bt_field_structure_borrow_member_field_by_index_const(field
, i
);
291 struct fs_sink_ctf_field_class
*member_fc
=
292 fs_sink_ctf_field_class_struct_borrow_member_by_index(fc
, i
)->fc
;
294 ret
= write_field(stream
, member_fc
, memb_field
);
295 if (G_UNLIKELY(ret
)) {
304 static inline int write_option_field(struct fs_sink_stream
*stream
,
305 struct fs_sink_ctf_field_class_option
*fc
,
306 const bt_field
*field
)
309 const bt_field
*content_field
= bt_field_option_borrow_field_const(field
);
311 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, content_field
? 1 : 0, 8, 8, BYTE_ORDER
);
312 if (G_UNLIKELY(ret
)) {
317 * CTF 1.8 has no option field class type, so this component
318 * translates the option field class to a variant field class
319 * where the options are:
321 * * An empty structure field class (field occupies 0 bits).
322 * * The optional field class itself.
324 * If `content_field` is `NULL`, do not write anything (empty
328 ret
= write_field(stream
, fc
->content_fc
, content_field
);
335 static inline int write_variant_field(struct fs_sink_stream
*stream
,
336 struct fs_sink_ctf_field_class_variant
*fc
,
337 const bt_field
*field
)
339 uint64_t opt_index
= bt_field_variant_get_selected_option_index(field
);
342 if (fc
->tag_is_before
) {
343 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, opt_index
, 8, 16, BYTE_ORDER
);
344 if (G_UNLIKELY(ret
)) {
349 ret
= write_field(stream
,
350 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc
, opt_index
)->fc
,
351 bt_field_variant_borrow_selected_option_field_const(field
));
357 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
358 const bt_field
*field
)
363 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL
:
364 ret
= write_bool_field(stream
, fs_sink_ctf_field_class_as_bool(fc
), field
);
366 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY
:
367 ret
= write_bit_array_field(stream
, fs_sink_ctf_field_class_as_bit_array(fc
), field
);
369 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
370 ret
= write_int_field(stream
, fs_sink_ctf_field_class_as_int(fc
), field
);
372 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
373 ret
= write_float_field(stream
, fs_sink_ctf_field_class_as_float(fc
), field
);
375 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
376 ret
= write_string_field(stream
, field
);
378 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
379 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(fc
), field
, true);
381 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
382 ret
= write_array_base_field_elements(stream
, fs_sink_ctf_field_class_as_array_base(fc
),
385 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
386 ret
= write_sequence_field(stream
, fs_sink_ctf_field_class_as_sequence(fc
), field
);
388 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
389 ret
= write_option_field(stream
, fs_sink_ctf_field_class_as_option(fc
), field
);
391 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
392 ret
= write_variant_field(stream
, fs_sink_ctf_field_class_as_variant(fc
), field
);
401 static inline int write_event_header(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
402 struct fs_sink_ctf_event_class
*ec
)
407 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
408 &stream
->ctfser
, bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
409 if (G_UNLIKELY(ret
)) {
414 if (stream
->sc
->default_clock_class
) {
416 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
417 &stream
->ctfser
, bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
418 if (G_UNLIKELY(ret
)) {
427 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
428 const bt_event
*event
, struct fs_sink_ctf_event_class
*ec
)
431 const bt_field
*field
;
434 ret
= write_event_header(stream
, cs
, ec
);
435 if (G_UNLIKELY(ret
)) {
440 if (stream
->sc
->event_common_context_fc
) {
441 field
= bt_event_borrow_common_context_field_const(event
);
442 BT_ASSERT_DBG(field
);
443 ret
= write_struct_field(
444 stream
, fs_sink_ctf_field_class_as_struct(stream
->sc
->event_common_context_fc
), field
,
446 if (G_UNLIKELY(ret
)) {
451 /* Specific context */
452 if (ec
->spec_context_fc
) {
453 field
= bt_event_borrow_specific_context_field_const(event
);
454 BT_ASSERT_DBG(field
);
455 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->spec_context_fc
),
457 if (G_UNLIKELY(ret
)) {
462 /* Specific context */
463 if (ec
->payload_fc
) {
464 field
= bt_event_borrow_payload_field_const(event
);
465 BT_ASSERT_DBG(field
);
466 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->payload_fc
), field
,
468 if (G_UNLIKELY(ret
)) {
477 static int write_packet_context(struct fs_sink_stream
*stream
)
481 /* Packet total size */
482 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
483 &stream
->ctfser
, stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
488 /* Packet content size */
489 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
490 &stream
->ctfser
, stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
495 if (stream
->sc
->packets_have_ts_begin
) {
497 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
498 &stream
->ctfser
, stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
504 if (stream
->sc
->packets_have_ts_end
) {
506 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
507 &stream
->ctfser
, stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
513 if (stream
->sc
->has_discarded_events
) {
514 /* Discarded event counter */
515 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
516 &stream
->ctfser
, stream
->packet_state
.discarded_events_counter
, 8, 64, BYTE_ORDER
);
522 /* Sequence number */
523 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, stream
->packet_state
.seq_num
,
530 if (stream
->sc
->packet_context_fc
) {
531 const bt_field
*packet_context_field
;
533 BT_ASSERT(stream
->packet_state
.packet
);
534 packet_context_field
= bt_packet_borrow_context_field_const(stream
->packet_state
.packet
);
535 BT_ASSERT(packet_context_field
);
536 ret
= write_struct_field(stream
,
537 fs_sink_ctf_field_class_as_struct(stream
->sc
->packet_context_fc
),
538 packet_context_field
, false);
548 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
549 const bt_packet
*packet
)
554 BT_ASSERT(!stream
->packet_state
.is_open
);
555 bt_packet_put_ref(stream
->packet_state
.packet
);
556 stream
->packet_state
.packet
= packet
;
557 bt_packet_get_ref(stream
->packet_state
.packet
);
559 stream
->packet_state
.beginning_cs
= bt_clock_snapshot_get_value(cs
);
563 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
565 /* bt_ctfser_open_packet() logs errors */
569 /* Packet header: magic */
570 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, UINT64_C(0xc1fc1fc1), 8, 32,
573 BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
574 stream
->file_name
->str
);
578 /* Packet header: UUID */
579 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
580 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
581 &stream
->ctfser
, (uint64_t) stream
->sc
->trace
->uuid
[i
], 8, 8, BYTE_ORDER
);
583 BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
584 stream
->file_name
->str
);
589 /* Packet header: stream class ID */
590 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
591 &stream
->ctfser
, bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
593 BT_COMP_LOGE("Error writing packet header stream class id: "
594 "stream-file-name=%s, stream-class-id=%" PRIu64
,
595 stream
->file_name
->str
, bt_stream_class_get_id(stream
->sc
->ir_sc
));
599 /* Packet header: stream ID */
600 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
601 &stream
->ctfser
, bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
603 BT_COMP_LOGE("Error writing packet header stream id: "
604 "stream-file-name=%s, stream-id=%" PRIu64
,
605 stream
->file_name
->str
, bt_stream_get_id(stream
->ir_stream
));
609 /* Save packet context's offset to rewrite it later */
610 stream
->packet_state
.context_offset_bits
=
611 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
613 /* Write packet context just to advance to content (first event) */
614 ret
= write_packet_context(stream
);
619 stream
->packet_state
.is_open
= true;
625 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
)
629 BT_ASSERT(stream
->packet_state
.is_open
);
632 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
635 stream
->packet_state
.content_size
=
636 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
637 stream
->packet_state
.total_size
= (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
639 /* Rewrite packet context */
640 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
641 stream
->packet_state
.context_offset_bits
);
642 ret
= write_packet_context(stream
);
648 bt_ctfser_close_current_packet(&stream
->ctfser
, stream
->packet_state
.total_size
/ 8);
650 /* Partially copy current packet state to previous packet state */
651 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
652 stream
->prev_packet_state
.discarded_events_counter
=
653 stream
->packet_state
.discarded_events_counter
;
654 stream
->prev_packet_state
.seq_num
= stream
->packet_state
.seq_num
;
656 /* Reset current packet state */
657 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
658 stream
->packet_state
.end_cs
= UINT64_C(-1);
659 stream
->packet_state
.content_size
= 0;
660 stream
->packet_state
.total_size
= 0;
661 stream
->packet_state
.seq_num
+= 1;
662 stream
->packet_state
.context_offset_bits
= 0;
663 stream
->packet_state
.is_open
= false;
664 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);