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"
19 #include "ctfser/ctfser.h"
21 #include "fs-sink-stream.hpp"
22 #include "fs-sink-trace.hpp"
23 #include "fs-sink.hpp"
24 #include "translate-trace-ir-to-ctf-ir.hpp"
26 void fs_sink_stream_destroy(struct fs_sink_stream
*stream
)
32 bt_ctfser_fini(&stream
->ctfser
);
34 if (stream
->file_name
) {
35 g_string_free(stream
->file_name
, TRUE
);
36 stream
->file_name
= NULL
;
39 bt_packet_put_ref(stream
->packet_state
.packet
);
46 static bool stream_file_name_exists(struct fs_sink_trace
*trace
, const char *name
)
52 g_hash_table_iter_init(&iter
, trace
->streams
);
54 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
55 struct fs_sink_stream
*stream
= (fs_sink_stream
*) value
;
57 if (strcmp(name
, stream
->file_name
->str
) == 0) {
67 static GString
*sanitize_stream_file_name(const char *file_name
)
69 GString
*san_file_name
= g_string_new(NULL
);
73 BT_ASSERT(san_file_name
);
75 basename
= g_path_get_basename(file_name
);
77 for (ch
= basename
; *ch
!= '\0'; ch
++) {
79 g_string_append_c(san_file_name
, '_');
81 g_string_append_c(san_file_name
, *ch
);
85 /* Do not allow `.` and `..` either */
86 if (strcmp(san_file_name
->str
, ".") == 0 || strcmp(san_file_name
->str
, "..") == 0) {
87 g_string_assign(san_file_name
, "stream");
94 static GString
*make_unique_stream_file_name(struct fs_sink_trace
*trace
, const char *base
)
96 GString
*san_base
= sanitize_stream_file_name(base
);
97 GString
*name
= g_string_new(san_base
->str
);
98 unsigned int suffix
= 0;
102 while (stream_file_name_exists(trace
, name
->str
) || strcmp(name
->str
, "metadata") == 0) {
103 g_string_printf(name
, "%s-%u", san_base
->str
, suffix
);
107 g_string_free(san_base
, TRUE
);
111 static void set_stream_file_name(struct fs_sink_stream
*stream
)
113 const char *base_name
= bt_stream_get_name(stream
->ir_stream
);
116 base_name
= "stream";
119 BT_ASSERT(!stream
->file_name
);
120 stream
->file_name
= make_unique_stream_file_name(stream
->trace
, base_name
);
123 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
124 const bt_stream
*ir_stream
)
126 struct fs_sink_stream
*stream
= g_new0(struct fs_sink_stream
, 1);
128 GString
*path
= g_string_new(trace
->path
->str
);
134 stream
->log_level
= trace
->log_level
;
135 stream
->trace
= trace
;
136 stream
->ir_stream
= ir_stream
;
137 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
138 stream
->packet_state
.end_cs
= UINT64_C(-1);
139 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
140 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
141 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
142 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(
143 trace
->fs_sink
, trace
->trace
, bt_stream_borrow_class_const(ir_stream
), &stream
->sc
);
148 set_stream_file_name(stream
);
149 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
150 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
, stream
->log_level
);
155 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
159 fs_sink_stream_destroy(stream
);
164 g_string_free(path
, TRUE
);
170 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
171 const bt_field
*field
);
173 static inline int write_bool_field(struct fs_sink_stream
*stream
,
174 struct fs_sink_ctf_field_class_bool
*fc
, const bt_field
*field
)
177 * CTF 1.8 has no boolean field class type, so this component
178 * translates this boolean field to an 8-bit unsigned integer
179 * field which has the value 0 (false) or 1 (true).
181 return bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_bool_get_value(field
) ? 1 : 0,
182 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
185 static inline int write_bit_array_field(struct fs_sink_stream
*stream
,
186 struct fs_sink_ctf_field_class_bit_array
*fc
,
187 const bt_field
*field
)
190 * CTF 1.8 has no bit array field class type, so this component
191 * translates this bit array field to an unsigned integer field.
193 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
194 bt_field_bit_array_get_value_as_integer(field
),
195 fc
->base
.alignment
, fc
->size
, BYTE_ORDER
);
198 static inline int write_int_field(struct fs_sink_stream
*stream
,
199 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
204 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
, bt_field_integer_signed_get_value(field
),
205 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
207 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
208 bt_field_integer_unsigned_get_value(field
),
209 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
215 static inline int write_float_field(struct fs_sink_stream
*stream
,
216 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
221 if (fc
->base
.size
== 32) {
222 val
= (double) bt_field_real_single_precision_get_value(field
);
223 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
225 val
= bt_field_real_double_precision_get_value(field
);
226 ret
= bt_ctfser_write_float64(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
232 static inline int write_string_field(struct fs_sink_stream
*stream
, const bt_field
*field
)
234 return bt_ctfser_write_string(&stream
->ctfser
, bt_field_string_get_value(field
));
237 static inline int write_array_base_field_elements(struct fs_sink_stream
*stream
,
238 struct fs_sink_ctf_field_class_array_base
*fc
,
239 const bt_field
*field
)
242 uint64_t len
= bt_field_array_get_length(field
);
245 for (i
= 0; i
< len
; i
++) {
246 const bt_field
*elem_field
= bt_field_array_borrow_element_field_by_index_const(field
, i
);
247 ret
= write_field(stream
, fc
->elem_fc
, elem_field
);
248 if (G_UNLIKELY(ret
)) {
257 static inline int write_sequence_field(struct fs_sink_stream
*stream
,
258 struct fs_sink_ctf_field_class_sequence
*fc
,
259 const bt_field
*field
)
263 if (fc
->length_is_before
) {
264 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_array_get_length(field
), 8, 32,
266 if (G_UNLIKELY(ret
)) {
271 ret
= write_array_base_field_elements(stream
, &fc
->base
, field
);
277 static inline int write_struct_field(struct fs_sink_stream
*stream
,
278 struct fs_sink_ctf_field_class_struct
*fc
,
279 const bt_field
*field
, bool align_struct
)
284 if (G_LIKELY(align_struct
)) {
285 ret
= bt_ctfser_align_offset_in_current_packet(&stream
->ctfser
, fc
->base
.alignment
);
286 if (G_UNLIKELY(ret
)) {
291 for (i
= 0; i
< fc
->members
->len
; i
++) {
292 const bt_field
*memb_field
=
293 bt_field_structure_borrow_member_field_by_index_const(field
, i
);
294 struct fs_sink_ctf_field_class
*member_fc
=
295 fs_sink_ctf_field_class_struct_borrow_member_by_index(fc
, i
)->fc
;
297 ret
= write_field(stream
, member_fc
, memb_field
);
298 if (G_UNLIKELY(ret
)) {
307 static inline int write_option_field(struct fs_sink_stream
*stream
,
308 struct fs_sink_ctf_field_class_option
*fc
,
309 const bt_field
*field
)
312 const bt_field
*content_field
= bt_field_option_borrow_field_const(field
);
314 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, content_field
? 1 : 0, 8, 8, BYTE_ORDER
);
315 if (G_UNLIKELY(ret
)) {
320 * CTF 1.8 has no option field class type, so this component
321 * translates the option field class to a variant field class
322 * where the options are:
324 * * An empty structure field class (field occupies 0 bits).
325 * * The optional field class itself.
327 * If `content_field` is `NULL`, do not write anything (empty
331 ret
= write_field(stream
, fc
->content_fc
, content_field
);
338 static inline int write_variant_field(struct fs_sink_stream
*stream
,
339 struct fs_sink_ctf_field_class_variant
*fc
,
340 const bt_field
*field
)
342 uint64_t opt_index
= bt_field_variant_get_selected_option_index(field
);
345 if (fc
->tag_is_before
) {
346 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, opt_index
, 8, 16, BYTE_ORDER
);
347 if (G_UNLIKELY(ret
)) {
352 ret
= write_field(stream
,
353 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc
, opt_index
)->fc
,
354 bt_field_variant_borrow_selected_option_field_const(field
));
360 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
361 const bt_field
*field
)
366 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL
:
367 ret
= write_bool_field(stream
, fs_sink_ctf_field_class_as_bool(fc
), field
);
369 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY
:
370 ret
= write_bit_array_field(stream
, fs_sink_ctf_field_class_as_bit_array(fc
), field
);
372 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
373 ret
= write_int_field(stream
, fs_sink_ctf_field_class_as_int(fc
), field
);
375 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
376 ret
= write_float_field(stream
, fs_sink_ctf_field_class_as_float(fc
), field
);
378 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
379 ret
= write_string_field(stream
, field
);
381 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
382 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(fc
), field
, true);
384 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
385 ret
= write_array_base_field_elements(stream
, fs_sink_ctf_field_class_as_array_base(fc
),
388 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
389 ret
= write_sequence_field(stream
, fs_sink_ctf_field_class_as_sequence(fc
), field
);
391 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
392 ret
= write_option_field(stream
, fs_sink_ctf_field_class_as_option(fc
), field
);
394 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
395 ret
= write_variant_field(stream
, fs_sink_ctf_field_class_as_variant(fc
), field
);
404 static inline int write_event_header(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
405 struct fs_sink_ctf_event_class
*ec
)
410 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
411 &stream
->ctfser
, bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
412 if (G_UNLIKELY(ret
)) {
417 if (stream
->sc
->default_clock_class
) {
419 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
420 &stream
->ctfser
, bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
421 if (G_UNLIKELY(ret
)) {
430 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
431 const bt_event
*event
, struct fs_sink_ctf_event_class
*ec
)
434 const bt_field
*field
;
437 ret
= write_event_header(stream
, cs
, ec
);
438 if (G_UNLIKELY(ret
)) {
443 if (stream
->sc
->event_common_context_fc
) {
444 field
= bt_event_borrow_common_context_field_const(event
);
445 BT_ASSERT_DBG(field
);
446 ret
= write_struct_field(
447 stream
, fs_sink_ctf_field_class_as_struct(stream
->sc
->event_common_context_fc
), field
,
449 if (G_UNLIKELY(ret
)) {
454 /* Specific context */
455 if (ec
->spec_context_fc
) {
456 field
= bt_event_borrow_specific_context_field_const(event
);
457 BT_ASSERT_DBG(field
);
458 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->spec_context_fc
),
460 if (G_UNLIKELY(ret
)) {
465 /* Specific context */
466 if (ec
->payload_fc
) {
467 field
= bt_event_borrow_payload_field_const(event
);
468 BT_ASSERT_DBG(field
);
469 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->payload_fc
), field
,
471 if (G_UNLIKELY(ret
)) {
480 static int write_packet_context(struct fs_sink_stream
*stream
)
484 /* Packet total size */
485 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
486 &stream
->ctfser
, stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
491 /* Packet content size */
492 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
493 &stream
->ctfser
, stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
498 if (stream
->sc
->packets_have_ts_begin
) {
500 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
501 &stream
->ctfser
, stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
507 if (stream
->sc
->packets_have_ts_end
) {
509 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
510 &stream
->ctfser
, stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
516 if (stream
->sc
->has_discarded_events
) {
517 /* Discarded event counter */
518 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
519 &stream
->ctfser
, stream
->packet_state
.discarded_events_counter
, 8, 64, BYTE_ORDER
);
525 /* Sequence number */
526 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, stream
->packet_state
.seq_num
,
533 if (stream
->sc
->packet_context_fc
) {
534 const bt_field
*packet_context_field
;
536 BT_ASSERT(stream
->packet_state
.packet
);
537 packet_context_field
= bt_packet_borrow_context_field_const(stream
->packet_state
.packet
);
538 BT_ASSERT(packet_context_field
);
539 ret
= write_struct_field(stream
,
540 fs_sink_ctf_field_class_as_struct(stream
->sc
->packet_context_fc
),
541 packet_context_field
, false);
551 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
552 const bt_packet
*packet
)
557 BT_ASSERT(!stream
->packet_state
.is_open
);
558 bt_packet_put_ref(stream
->packet_state
.packet
);
559 stream
->packet_state
.packet
= packet
;
560 bt_packet_get_ref(stream
->packet_state
.packet
);
562 stream
->packet_state
.beginning_cs
= bt_clock_snapshot_get_value(cs
);
566 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
568 /* bt_ctfser_open_packet() logs errors */
572 /* Packet header: magic */
573 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, UINT64_C(0xc1fc1fc1), 8, 32,
576 BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
577 stream
->file_name
->str
);
581 /* Packet header: UUID */
582 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
583 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
584 &stream
->ctfser
, (uint64_t) stream
->sc
->trace
->uuid
[i
], 8, 8, BYTE_ORDER
);
586 BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
587 stream
->file_name
->str
);
592 /* Packet header: stream class ID */
593 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
594 &stream
->ctfser
, bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
596 BT_COMP_LOGE("Error writing packet header stream class id: "
597 "stream-file-name=%s, stream-class-id=%" PRIu64
,
598 stream
->file_name
->str
, bt_stream_class_get_id(stream
->sc
->ir_sc
));
602 /* Packet header: stream ID */
603 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
604 &stream
->ctfser
, bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
606 BT_COMP_LOGE("Error writing packet header stream id: "
607 "stream-file-name=%s, stream-id=%" PRIu64
,
608 stream
->file_name
->str
, bt_stream_get_id(stream
->ir_stream
));
612 /* Save packet context's offset to rewrite it later */
613 stream
->packet_state
.context_offset_bits
=
614 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
616 /* Write packet context just to advance to content (first event) */
617 ret
= write_packet_context(stream
);
622 stream
->packet_state
.is_open
= true;
628 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
)
632 BT_ASSERT(stream
->packet_state
.is_open
);
635 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
638 stream
->packet_state
.content_size
=
639 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
640 stream
->packet_state
.total_size
= (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
642 /* Rewrite packet context */
643 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
644 stream
->packet_state
.context_offset_bits
);
645 ret
= write_packet_context(stream
);
651 bt_ctfser_close_current_packet(&stream
->ctfser
, stream
->packet_state
.total_size
/ 8);
653 /* Partially copy current packet state to previous packet state */
654 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
655 stream
->prev_packet_state
.discarded_events_counter
=
656 stream
->packet_state
.discarded_events_counter
;
657 stream
->prev_packet_state
.seq_num
= stream
->packet_state
.seq_num
;
659 /* Reset current packet state */
660 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
661 stream
->packet_state
.end_cs
= UINT64_C(-1);
662 stream
->packet_state
.content_size
= 0;
663 stream
->packet_state
.total_size
= 0;
664 stream
->packet_state
.seq_num
+= 1;
665 stream
->packet_state
.context_offset_bits
= 0;
666 stream
->packet_state
.is_open
= false;
667 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);