2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
7 #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (stream->log_level)
9 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM"
10 #include "logging/comp-logging.h"
12 #include <babeltrace2/babeltrace.h>
16 #include "common/assert.h"
17 #include "ctfser/ctfser.h"
18 #include "compat/endian.h"
20 #include "fs-sink.hpp"
21 #include "fs-sink-trace.hpp"
22 #include "fs-sink-stream.hpp"
23 #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
);
124 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
125 const bt_stream
*ir_stream
)
127 struct fs_sink_stream
*stream
= g_new0(struct fs_sink_stream
, 1);
129 GString
*path
= g_string_new(trace
->path
->str
);
135 stream
->log_level
= trace
->log_level
;
136 stream
->trace
= trace
;
137 stream
->ir_stream
= ir_stream
;
138 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
139 stream
->packet_state
.end_cs
= UINT64_C(-1);
140 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
141 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
142 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
143 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(
144 trace
->fs_sink
, trace
->trace
, bt_stream_borrow_class_const(ir_stream
), &stream
->sc
);
149 set_stream_file_name(stream
);
150 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
151 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
, stream
->log_level
);
156 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
160 fs_sink_stream_destroy(stream
);
165 g_string_free(path
, TRUE
);
171 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
172 const bt_field
*field
);
174 static inline int write_bool_field(struct fs_sink_stream
*stream
,
175 struct fs_sink_ctf_field_class_bool
*fc
, const bt_field
*field
)
178 * CTF 1.8 has no boolean field class type, so this component
179 * translates this boolean field to an 8-bit unsigned integer
180 * field which has the value 0 (false) or 1 (true).
182 return bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_bool_get_value(field
) ? 1 : 0,
183 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
186 static inline int write_bit_array_field(struct fs_sink_stream
*stream
,
187 struct fs_sink_ctf_field_class_bit_array
*fc
,
188 const bt_field
*field
)
191 * CTF 1.8 has no bit array field class type, so this component
192 * translates this bit array field to an unsigned integer field.
194 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
195 bt_field_bit_array_get_value_as_integer(field
),
196 fc
->base
.alignment
, fc
->size
, BYTE_ORDER
);
199 static inline int write_int_field(struct fs_sink_stream
*stream
,
200 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
205 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
, bt_field_integer_signed_get_value(field
),
206 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
208 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
209 bt_field_integer_unsigned_get_value(field
),
210 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
216 static inline int write_float_field(struct fs_sink_stream
*stream
,
217 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
222 if (fc
->base
.size
== 32) {
223 val
= (double) bt_field_real_single_precision_get_value(field
);
224 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
226 val
= bt_field_real_double_precision_get_value(field
);
227 ret
= bt_ctfser_write_float64(&stream
->ctfser
, val
, fc
->base
.base
.alignment
, BYTE_ORDER
);
233 static inline int write_string_field(struct fs_sink_stream
*stream
,
234 struct fs_sink_ctf_field_class_string
*fc
,
235 const bt_field
*field
)
237 return bt_ctfser_write_string(&stream
->ctfser
, bt_field_string_get_value(field
));
240 static inline int write_array_base_field_elements(struct fs_sink_stream
*stream
,
241 struct fs_sink_ctf_field_class_array_base
*fc
,
242 const bt_field
*field
)
245 uint64_t len
= bt_field_array_get_length(field
);
248 for (i
= 0; i
< len
; i
++) {
249 const bt_field
*elem_field
= bt_field_array_borrow_element_field_by_index_const(field
, i
);
250 ret
= write_field(stream
, fc
->elem_fc
, elem_field
);
251 if (G_UNLIKELY(ret
)) {
260 static inline int write_sequence_field(struct fs_sink_stream
*stream
,
261 struct fs_sink_ctf_field_class_sequence
*fc
,
262 const bt_field
*field
)
266 if (fc
->length_is_before
) {
267 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, bt_field_array_get_length(field
), 8, 32,
269 if (G_UNLIKELY(ret
)) {
274 ret
= write_array_base_field_elements(stream
, &fc
->base
, field
);
280 static inline int write_struct_field(struct fs_sink_stream
*stream
,
281 struct fs_sink_ctf_field_class_struct
*fc
,
282 const bt_field
*field
, bool align_struct
)
287 if (G_LIKELY(align_struct
)) {
288 ret
= bt_ctfser_align_offset_in_current_packet(&stream
->ctfser
, fc
->base
.alignment
);
289 if (G_UNLIKELY(ret
)) {
294 for (i
= 0; i
< fc
->members
->len
; i
++) {
295 const bt_field
*memb_field
=
296 bt_field_structure_borrow_member_field_by_index_const(field
, i
);
297 struct fs_sink_ctf_field_class
*member_fc
=
298 fs_sink_ctf_field_class_struct_borrow_member_by_index(fc
, i
)->fc
;
300 ret
= write_field(stream
, member_fc
, memb_field
);
301 if (G_UNLIKELY(ret
)) {
310 static inline int write_option_field(struct fs_sink_stream
*stream
,
311 struct fs_sink_ctf_field_class_option
*fc
,
312 const bt_field
*field
)
315 const bt_field
*content_field
= bt_field_option_borrow_field_const(field
);
317 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, content_field
? 1 : 0, 8, 8, BYTE_ORDER
);
318 if (G_UNLIKELY(ret
)) {
323 * CTF 1.8 has no option field class type, so this component
324 * translates the option field class to a variant field class
325 * where the options are:
327 * * An empty structure field class (field occupies 0 bits).
328 * * The optional field class itself.
330 * If `content_field` is `NULL`, do not write anything (empty
334 ret
= write_field(stream
, fc
->content_fc
, content_field
);
341 static inline int write_variant_field(struct fs_sink_stream
*stream
,
342 struct fs_sink_ctf_field_class_variant
*fc
,
343 const bt_field
*field
)
345 uint64_t opt_index
= bt_field_variant_get_selected_option_index(field
);
348 if (fc
->tag_is_before
) {
349 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
, opt_index
, 8, 16, BYTE_ORDER
);
350 if (G_UNLIKELY(ret
)) {
355 ret
= write_field(stream
,
356 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc
, opt_index
)->fc
,
357 bt_field_variant_borrow_selected_option_field_const(field
));
363 static int write_field(struct fs_sink_stream
*stream
, struct fs_sink_ctf_field_class
*fc
,
364 const bt_field
*field
)
369 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL
:
370 ret
= write_bool_field(stream
, fs_sink_ctf_field_class_as_bool(fc
), field
);
372 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY
:
373 ret
= write_bit_array_field(stream
, fs_sink_ctf_field_class_as_bit_array(fc
), field
);
375 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
376 ret
= write_int_field(stream
, fs_sink_ctf_field_class_as_int(fc
), field
);
378 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
379 ret
= write_float_field(stream
, fs_sink_ctf_field_class_as_float(fc
), field
);
381 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
382 ret
= write_string_field(stream
, fs_sink_ctf_field_class_as_string(fc
), field
);
384 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
385 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(fc
), field
, true);
387 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
388 ret
= write_array_base_field_elements(stream
, fs_sink_ctf_field_class_as_array_base(fc
),
391 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
392 ret
= write_sequence_field(stream
, fs_sink_ctf_field_class_as_sequence(fc
), field
);
394 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
395 ret
= write_option_field(stream
, fs_sink_ctf_field_class_as_option(fc
), field
);
397 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
398 ret
= write_variant_field(stream
, fs_sink_ctf_field_class_as_variant(fc
), field
);
407 static inline int write_event_header(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
408 struct fs_sink_ctf_event_class
*ec
)
413 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
414 &stream
->ctfser
, bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
415 if (G_UNLIKELY(ret
)) {
420 if (stream
->sc
->default_clock_class
) {
422 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
423 &stream
->ctfser
, bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
424 if (G_UNLIKELY(ret
)) {
434 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
435 const bt_event
*event
, struct fs_sink_ctf_event_class
*ec
)
438 const bt_field
*field
;
441 ret
= write_event_header(stream
, cs
, ec
);
442 if (G_UNLIKELY(ret
)) {
447 if (stream
->sc
->event_common_context_fc
) {
448 field
= bt_event_borrow_common_context_field_const(event
);
449 BT_ASSERT_DBG(field
);
450 ret
= write_struct_field(
451 stream
, fs_sink_ctf_field_class_as_struct(stream
->sc
->event_common_context_fc
), field
,
453 if (G_UNLIKELY(ret
)) {
458 /* Specific context */
459 if (ec
->spec_context_fc
) {
460 field
= bt_event_borrow_specific_context_field_const(event
);
461 BT_ASSERT_DBG(field
);
462 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->spec_context_fc
),
464 if (G_UNLIKELY(ret
)) {
469 /* Specific context */
470 if (ec
->payload_fc
) {
471 field
= bt_event_borrow_payload_field_const(event
);
472 BT_ASSERT_DBG(field
);
473 ret
= write_struct_field(stream
, fs_sink_ctf_field_class_as_struct(ec
->payload_fc
), field
,
475 if (G_UNLIKELY(ret
)) {
484 static int write_packet_context(struct fs_sink_stream
*stream
)
488 /* Packet total size */
489 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
490 &stream
->ctfser
, stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
495 /* Packet content size */
496 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
497 &stream
->ctfser
, stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
502 if (stream
->sc
->packets_have_ts_begin
) {
504 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
505 &stream
->ctfser
, stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
511 if (stream
->sc
->packets_have_ts_end
) {
513 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
514 &stream
->ctfser
, stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
520 if (stream
->sc
->has_discarded_events
) {
521 /* Discarded event counter */
522 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
523 &stream
->ctfser
, stream
->packet_state
.discarded_events_counter
, 8, 64, BYTE_ORDER
);
529 /* Sequence number */
530 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, stream
->packet_state
.seq_num
,
537 if (stream
->sc
->packet_context_fc
) {
538 const bt_field
*packet_context_field
;
540 BT_ASSERT(stream
->packet_state
.packet
);
541 packet_context_field
= bt_packet_borrow_context_field_const(stream
->packet_state
.packet
);
542 BT_ASSERT(packet_context_field
);
543 ret
= write_struct_field(stream
,
544 fs_sink_ctf_field_class_as_struct(stream
->sc
->packet_context_fc
),
545 packet_context_field
, false);
556 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
,
557 const bt_packet
*packet
)
562 BT_ASSERT(!stream
->packet_state
.is_open
);
563 bt_packet_put_ref(stream
->packet_state
.packet
);
564 stream
->packet_state
.packet
= packet
;
565 bt_packet_get_ref(stream
->packet_state
.packet
);
567 stream
->packet_state
.beginning_cs
= bt_clock_snapshot_get_value(cs
);
571 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
573 /* bt_ctfser_open_packet() logs errors */
577 /* Packet header: magic */
578 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
, UINT64_C(0xc1fc1fc1), 8, 32,
581 BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
582 stream
->file_name
->str
);
586 /* Packet header: UUID */
587 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
588 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
589 &stream
->ctfser
, (uint64_t) stream
->sc
->trace
->uuid
[i
], 8, 8, BYTE_ORDER
);
591 BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
592 stream
->file_name
->str
);
597 /* Packet header: stream class ID */
598 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
599 &stream
->ctfser
, bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
601 BT_COMP_LOGE("Error writing packet header stream class id: "
602 "stream-file-name=%s, stream-class-id=%" PRIu64
,
603 stream
->file_name
->str
, bt_stream_class_get_id(stream
->sc
->ir_sc
));
607 /* Packet header: stream ID */
608 ret
= bt_ctfser_write_byte_aligned_unsigned_int(
609 &stream
->ctfser
, bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
611 BT_COMP_LOGE("Error writing packet header stream id: "
612 "stream-file-name=%s, stream-id=%" PRIu64
,
613 stream
->file_name
->str
, bt_stream_get_id(stream
->ir_stream
));
617 /* Save packet context's offset to rewrite it later */
618 stream
->packet_state
.context_offset_bits
=
619 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
621 /* Write packet context just to advance to content (first event) */
622 ret
= write_packet_context(stream
);
627 stream
->packet_state
.is_open
= true;
634 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
, const bt_clock_snapshot
*cs
)
638 BT_ASSERT(stream
->packet_state
.is_open
);
641 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
644 stream
->packet_state
.content_size
=
645 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
646 stream
->packet_state
.total_size
= (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
648 /* Rewrite packet context */
649 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
650 stream
->packet_state
.context_offset_bits
);
651 ret
= write_packet_context(stream
);
657 bt_ctfser_close_current_packet(&stream
->ctfser
, stream
->packet_state
.total_size
/ 8);
659 /* Partially copy current packet state to previous packet state */
660 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
661 stream
->prev_packet_state
.discarded_events_counter
=
662 stream
->packet_state
.discarded_events_counter
;
663 stream
->prev_packet_state
.seq_num
= stream
->packet_state
.seq_num
;
665 /* Reset current packet state */
666 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
667 stream
->packet_state
.end_cs
= UINT64_C(-1);
668 stream
->packet_state
.content_size
= 0;
669 stream
->packet_state
.total_size
= 0;
670 stream
->packet_state
.seq_num
+= 1;
671 stream
->packet_state
.context_offset_bits
= 0;
672 stream
->packet_state
.is_open
= false;
673 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);