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"
21 #include "fs-sink-trace.h"
22 #include "fs-sink-stream.h"
23 #include "translate-trace-ir-to-ctf-ir.h"
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
);
47 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
= value
;
58 if (strcmp(name
, stream
->file_name
->str
) == 0) {
69 GString
*sanitize_stream_file_name(const char *file_name
)
71 GString
*san_file_name
= g_string_new(NULL
);
75 BT_ASSERT(san_file_name
);
77 basename
= g_path_get_basename(file_name
);
79 for (ch
= basename
; *ch
!= '\0'; ch
++) {
81 g_string_append_c(san_file_name
, '_');
83 g_string_append_c(san_file_name
, *ch
);
87 /* Do not allow `.` and `..` either */
88 if (strcmp(san_file_name
->str
, ".") == 0 ||
89 strcmp(san_file_name
->str
, "..") == 0) {
90 g_string_assign(san_file_name
, "stream");
98 GString
*make_unique_stream_file_name(struct fs_sink_trace
*trace
,
101 GString
*san_base
= sanitize_stream_file_name(base
);
102 GString
*name
= g_string_new(san_base
->str
);
103 unsigned int suffix
= 0;
107 while (stream_file_name_exists(trace
, name
->str
) ||
108 strcmp(name
->str
, "metadata") == 0) {
109 g_string_printf(name
, "%s-%u", san_base
->str
, suffix
);
113 g_string_free(san_base
, TRUE
);
118 void set_stream_file_name(struct fs_sink_stream
*stream
)
120 const char *base_name
= bt_stream_get_name(stream
->ir_stream
);
123 base_name
= "stream";
126 BT_ASSERT(!stream
->file_name
);
127 stream
->file_name
= make_unique_stream_file_name(stream
->trace
,
132 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
133 const bt_stream
*ir_stream
)
135 struct fs_sink_stream
*stream
= g_new0(struct fs_sink_stream
, 1);
137 GString
*path
= g_string_new(trace
->path
->str
);
143 stream
->log_level
= trace
->log_level
;
144 stream
->trace
= trace
;
145 stream
->ir_stream
= ir_stream
;
146 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
147 stream
->packet_state
.end_cs
= UINT64_C(-1);
148 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
149 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
150 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
151 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(trace
->fs_sink
,
152 trace
->trace
, bt_stream_borrow_class_const(ir_stream
),
158 set_stream_file_name(stream
);
159 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
160 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
,
166 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
170 fs_sink_stream_destroy(stream
);
175 g_string_free(path
, TRUE
);
182 int write_field(struct fs_sink_stream
*stream
,
183 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
);
186 int write_bool_field(struct fs_sink_stream
*stream
,
187 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
190 * CTF 1.8 has no boolean field class type, so this component
191 * translates this boolean field to an 8-bit unsigned integer
192 * field which has the value 0 (false) or 1 (true).
194 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
195 bt_field_bool_get_value(field
) ? 1 : 0,
196 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
200 int write_bit_array_field(struct fs_sink_stream
*stream
,
201 struct fs_sink_ctf_field_class_bit_array
*fc
,
202 const bt_field
*field
)
205 * CTF 1.8 has no bit array field class type, so this component
206 * translates this bit array field to an unsigned integer field.
208 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
209 bt_field_bit_array_get_value_as_integer(field
),
210 fc
->base
.alignment
, fc
->size
, BYTE_ORDER
);
214 int write_int_field(struct fs_sink_stream
*stream
,
215 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
220 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
,
221 bt_field_integer_signed_get_value(field
),
222 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
224 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
225 bt_field_integer_unsigned_get_value(field
),
226 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
233 int write_float_field(struct fs_sink_stream
*stream
,
234 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
239 if (fc
->base
.size
== 32) {
240 val
= (double) bt_field_real_single_precision_get_value(field
);
241 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
,
242 fc
->base
.base
.alignment
, BYTE_ORDER
);
244 val
= bt_field_real_double_precision_get_value(field
);
245 ret
= bt_ctfser_write_float64(&stream
->ctfser
, val
,
246 fc
->base
.base
.alignment
, BYTE_ORDER
);
253 int write_string_field(struct fs_sink_stream
*stream
,
254 struct fs_sink_ctf_field_class_string
*fc
, const bt_field
*field
)
256 return bt_ctfser_write_string(&stream
->ctfser
,
257 bt_field_string_get_value(field
));
261 int write_array_field_elements(struct fs_sink_stream
*stream
,
262 struct fs_sink_ctf_field_class_array_base
*fc
,
263 const bt_field
*field
)
266 uint64_t len
= bt_field_array_get_length(field
);
269 for (i
= 0; i
< len
; i
++) {
270 const bt_field
*elem_field
=
271 bt_field_array_borrow_element_field_by_index_const(
273 ret
= write_field(stream
, fc
->elem_fc
, elem_field
);
274 if (G_UNLIKELY(ret
)) {
284 int write_sequence_field(struct fs_sink_stream
*stream
,
285 struct fs_sink_ctf_field_class_sequence
*fc
,
286 const bt_field
*field
)
290 if (fc
->length_is_before
) {
291 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
292 bt_field_array_get_length(field
), 8, 32, BYTE_ORDER
);
293 if (G_UNLIKELY(ret
)) {
298 ret
= write_array_field_elements(stream
, (void *) fc
, field
);
305 int write_struct_field(struct fs_sink_stream
*stream
,
306 struct fs_sink_ctf_field_class_struct
*fc
,
307 const bt_field
*field
, bool align_struct
)
312 if (G_LIKELY(align_struct
)) {
313 ret
= bt_ctfser_align_offset_in_current_packet(&stream
->ctfser
,
315 if (G_UNLIKELY(ret
)) {
320 for (i
= 0; i
< fc
->members
->len
; i
++) {
321 const bt_field
*memb_field
=
322 bt_field_structure_borrow_member_field_by_index_const(
324 struct fs_sink_ctf_field_class
*member_fc
=
325 fs_sink_ctf_field_class_struct_borrow_member_by_index(
328 ret
= write_field(stream
, member_fc
, memb_field
);
329 if (G_UNLIKELY(ret
)) {
339 int write_option_field(struct fs_sink_stream
*stream
,
340 struct fs_sink_ctf_field_class_option
*fc
,
341 const bt_field
*field
)
344 const bt_field
*content_field
=
345 bt_field_option_borrow_field_const(field
);
347 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
348 content_field
? 1 : 0, 8, 8, BYTE_ORDER
);
349 if (G_UNLIKELY(ret
)) {
354 * CTF 1.8 has no option field class type, so this component
355 * translates the option field class to a variant field class
356 * where the options are:
358 * * An empty structure field class (field occupies 0 bits).
359 * * The optional field class itself.
361 * If `content_field` is `NULL`, do not write anything (empty
365 ret
= write_field(stream
, fc
->content_fc
, content_field
);
373 int write_variant_field(struct fs_sink_stream
*stream
,
374 struct fs_sink_ctf_field_class_variant
*fc
,
375 const bt_field
*field
)
378 bt_field_variant_get_selected_option_index(field
);
381 if (fc
->tag_is_before
) {
382 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
383 opt_index
, 8, 16, BYTE_ORDER
);
384 if (G_UNLIKELY(ret
)) {
389 ret
= write_field(stream
,
390 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc
,
392 bt_field_variant_borrow_selected_option_field_const(field
));
399 int write_field(struct fs_sink_stream
*stream
,
400 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
)
405 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL
:
406 ret
= write_bool_field(stream
, (void *) fc
, field
);
408 case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY
:
409 ret
= write_bit_array_field(stream
, (void *) fc
, field
);
411 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
412 ret
= write_int_field(stream
, (void *) fc
, field
);
414 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
415 ret
= write_float_field(stream
, (void *) fc
, field
);
417 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
418 ret
= write_string_field(stream
, (void *) fc
, field
);
420 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
421 ret
= write_struct_field(stream
, (void *) fc
, field
, true);
423 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
424 ret
= write_array_field_elements(stream
, (void *) fc
, field
);
426 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
427 ret
= write_sequence_field(stream
, (void *) fc
, field
);
429 case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION
:
430 ret
= write_option_field(stream
, (void *) fc
, field
);
432 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
433 ret
= write_variant_field(stream
, (void *) fc
, field
);
443 int write_event_header(struct fs_sink_stream
*stream
,
444 const bt_clock_snapshot
*cs
, struct fs_sink_ctf_event_class
*ec
)
449 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
450 bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
451 if (G_UNLIKELY(ret
)) {
456 if (stream
->sc
->default_clock_class
) {
458 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
459 bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
460 if (G_UNLIKELY(ret
)) {
470 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
,
471 const bt_clock_snapshot
*cs
, const bt_event
*event
,
472 struct fs_sink_ctf_event_class
*ec
)
475 const bt_field
*field
;
478 ret
= write_event_header(stream
, cs
, ec
);
479 if (G_UNLIKELY(ret
)) {
484 if (stream
->sc
->event_common_context_fc
) {
485 field
= bt_event_borrow_common_context_field_const(event
);
486 BT_ASSERT_DBG(field
);
487 ret
= write_struct_field(stream
,
488 (void *) stream
->sc
->event_common_context_fc
,
490 if (G_UNLIKELY(ret
)) {
495 /* Specific context */
496 if (ec
->spec_context_fc
) {
497 field
= bt_event_borrow_specific_context_field_const(event
);
498 BT_ASSERT_DBG(field
);
499 ret
= write_struct_field(stream
, (void *) ec
->spec_context_fc
,
501 if (G_UNLIKELY(ret
)) {
506 /* Specific context */
507 if (ec
->payload_fc
) {
508 field
= bt_event_borrow_payload_field_const(event
);
509 BT_ASSERT_DBG(field
);
510 ret
= write_struct_field(stream
, (void *) ec
->payload_fc
,
512 if (G_UNLIKELY(ret
)) {
522 int write_packet_context(struct fs_sink_stream
*stream
)
526 /* Packet total size */
527 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
528 stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
533 /* Packet content size */
534 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
535 stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
540 if (stream
->sc
->packets_have_ts_begin
) {
542 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
543 stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
549 if (stream
->sc
->packets_have_ts_end
) {
551 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
552 stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
558 if (stream
->sc
->has_discarded_events
) {
559 /* Discarded event counter */
560 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
561 stream
->packet_state
.discarded_events_counter
, 8, 64,
568 /* Sequence number */
569 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
570 stream
->packet_state
.seq_num
, 8, 64, BYTE_ORDER
);
576 if (stream
->sc
->packet_context_fc
) {
577 const bt_field
*packet_context_field
;
579 BT_ASSERT(stream
->packet_state
.packet
);
580 packet_context_field
= bt_packet_borrow_context_field_const(
581 stream
->packet_state
.packet
);
582 BT_ASSERT(packet_context_field
);
583 ret
= write_struct_field(stream
,
584 (void *) stream
->sc
->packet_context_fc
,
585 packet_context_field
, false);
596 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
,
597 const bt_clock_snapshot
*cs
, const bt_packet
*packet
)
602 BT_ASSERT(!stream
->packet_state
.is_open
);
603 bt_packet_put_ref(stream
->packet_state
.packet
);
604 stream
->packet_state
.packet
= packet
;
605 bt_packet_get_ref(stream
->packet_state
.packet
);
607 stream
->packet_state
.beginning_cs
=
608 bt_clock_snapshot_get_value(cs
);
612 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
614 /* bt_ctfser_open_packet() logs errors */
618 /* Packet header: magic */
619 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
620 UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER
);
622 BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
623 stream
->file_name
->str
);
627 /* Packet header: UUID */
628 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
629 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
630 (uint64_t) stream
->sc
->trace
->uuid
[i
], 8, 8, BYTE_ORDER
);
632 BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
633 stream
->file_name
->str
);
638 /* Packet header: stream class ID */
639 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
640 bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
642 BT_COMP_LOGE("Error writing packet header stream class id: "
643 "stream-file-name=%s, stream-class-id=%"PRIu64
,
644 stream
->file_name
->str
,
645 bt_stream_class_get_id(stream
->sc
->ir_sc
));
649 /* Packet header: stream ID */
650 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
651 bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
653 BT_COMP_LOGE("Error writing packet header stream id: "
654 "stream-file-name=%s, stream-id=%"PRIu64
,
655 stream
->file_name
->str
,
656 bt_stream_get_id(stream
->ir_stream
));
660 /* Save packet context's offset to rewrite it later */
661 stream
->packet_state
.context_offset_bits
=
662 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
664 /* Write packet context just to advance to content (first event) */
665 ret
= write_packet_context(stream
);
670 stream
->packet_state
.is_open
= true;
677 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
,
678 const bt_clock_snapshot
*cs
)
682 BT_ASSERT(stream
->packet_state
.is_open
);
685 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
688 stream
->packet_state
.content_size
=
689 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
690 stream
->packet_state
.total_size
=
691 (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
693 /* Rewrite packet context */
694 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
695 stream
->packet_state
.context_offset_bits
);
696 ret
= write_packet_context(stream
);
702 bt_ctfser_close_current_packet(&stream
->ctfser
,
703 stream
->packet_state
.total_size
/ 8);
705 /* Partially copy current packet state to previous packet state */
706 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
707 stream
->prev_packet_state
.discarded_events_counter
=
708 stream
->packet_state
.discarded_events_counter
;
709 stream
->prev_packet_state
.seq_num
=
710 stream
->packet_state
.seq_num
;
712 /* Reset current packet state */
713 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
714 stream
->packet_state
.end_cs
= UINT64_C(-1);
715 stream
->packet_state
.content_size
= 0;
716 stream
->packet_state
.total_size
= 0;
717 stream
->packet_state
.seq_num
+= 1;
718 stream
->packet_state
.context_offset_bits
= 0;
719 stream
->packet_state
.is_open
= false;
720 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);