2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp)
24 #define BT_LOG_OUTPUT_LEVEL (stream->log_level)
25 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM"
26 #include "logging/comp-logging.h"
28 #include <babeltrace2/babeltrace.h>
32 #include "common/assert.h"
33 #include "ctfser/ctfser.h"
34 #include "compat/endian.h"
37 #include "fs-sink-trace.h"
38 #include "fs-sink-stream.h"
39 #include "translate-trace-ir-to-ctf-ir.h"
42 void fs_sink_stream_destroy(struct fs_sink_stream
*stream
)
48 bt_ctfser_fini(&stream
->ctfser
);
50 if (stream
->file_name
) {
51 g_string_free(stream
->file_name
, TRUE
);
52 stream
->file_name
= NULL
;
55 bt_packet_put_ref(stream
->packet_state
.packet
);
63 bool stream_file_name_exists(struct fs_sink_trace
*trace
, const char *name
)
69 g_hash_table_iter_init(&iter
, trace
->streams
);
71 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
72 struct fs_sink_stream
*stream
= value
;
74 if (strcmp(name
, stream
->file_name
->str
) == 0) {
85 GString
*sanitize_stream_file_name(const char *file_name
)
87 GString
*san_file_name
= g_string_new(NULL
);
91 BT_ASSERT(san_file_name
);
93 basename
= g_path_get_basename(file_name
);
95 for (ch
= basename
; *ch
!= '\0'; ch
++) {
97 g_string_append_c(san_file_name
, '_');
99 g_string_append_c(san_file_name
, *ch
);
103 /* Do not allow `.` and `..` either */
104 if (strcmp(san_file_name
->str
, ".") == 0 ||
105 strcmp(san_file_name
->str
, "..") == 0) {
106 g_string_assign(san_file_name
, "stream");
110 return san_file_name
;
114 GString
*make_unique_stream_file_name(struct fs_sink_trace
*trace
,
117 GString
*san_base
= sanitize_stream_file_name(base
);
118 GString
*name
= g_string_new(san_base
->str
);
119 unsigned int suffix
= 0;
123 while (stream_file_name_exists(trace
, name
->str
) &&
124 strcmp(name
->str
, "metadata") == 0) {
125 g_string_printf(name
, "%s-%u", san_base
->str
, suffix
);
129 g_string_free(san_base
, TRUE
);
134 void set_stream_file_name(struct fs_sink_stream
*stream
)
136 const char *base_name
= bt_stream_get_name(stream
->ir_stream
);
139 base_name
= "stream";
142 BT_ASSERT(!stream
->file_name
);
143 stream
->file_name
= make_unique_stream_file_name(stream
->trace
,
148 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
149 const bt_stream
*ir_stream
)
151 struct fs_sink_stream
*stream
= g_new0(struct fs_sink_stream
, 1);
153 GString
*path
= g_string_new(trace
->path
->str
);
159 stream
->log_level
= trace
->log_level
;
160 stream
->trace
= trace
;
161 stream
->ir_stream
= ir_stream
;
162 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
163 stream
->packet_state
.end_cs
= UINT64_C(-1);
164 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
165 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
166 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
167 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(trace
->fs_sink
,
168 trace
->trace
, bt_stream_borrow_class_const(ir_stream
),
174 set_stream_file_name(stream
);
175 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
176 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
,
182 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
186 fs_sink_stream_destroy(stream
);
191 g_string_free(path
, TRUE
);
198 int write_field(struct fs_sink_stream
*stream
,
199 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
);
202 int write_bool_field(struct fs_sink_stream
*stream
,
203 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
206 * CTF 1.8 has no boolean field class type, so this component
207 * translates this boolean field to an 8-bit unsigned integer
208 * field which has the value 0 (false) or 1 (true).
210 return bt_ctfser_write_unsigned_int(&stream
->ctfser
,
211 bt_field_bool_get_value(field
) ? 1 : 0,
212 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
216 int write_int_field(struct fs_sink_stream
*stream
,
217 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
222 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
,
223 bt_field_integer_signed_get_value(field
),
224 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
226 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
227 bt_field_integer_unsigned_get_value(field
),
228 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
235 int write_float_field(struct fs_sink_stream
*stream
,
236 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
239 double val
= bt_field_real_get_value(field
);
241 if (fc
->base
.size
== 32) {
242 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
,
243 fc
->base
.base
.alignment
, BYTE_ORDER
);
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_variant_field(struct fs_sink_stream
*stream
,
340 struct fs_sink_ctf_field_class_variant
*fc
,
341 const bt_field
*field
)
344 bt_field_variant_get_selected_option_field_index(field
);
347 if (fc
->tag_is_before
) {
348 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
349 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
,
358 bt_field_variant_borrow_selected_option_field_const(field
));
365 int write_field(struct fs_sink_stream
*stream
,
366 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
)
371 case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL
:
372 ret
= write_bool_field(stream
, (void *) fc
, field
);
374 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
375 ret
= write_int_field(stream
, (void *) fc
, field
);
377 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
378 ret
= write_float_field(stream
, (void *) fc
, field
);
380 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
381 ret
= write_string_field(stream
, (void *) fc
, field
);
383 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
384 ret
= write_struct_field(stream
, (void *) fc
, field
, true);
386 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
387 ret
= write_array_field_elements(stream
, (void *) fc
, field
);
389 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
390 ret
= write_sequence_field(stream
, (void *) fc
, field
);
392 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
393 ret
= write_variant_field(stream
, (void *) fc
, field
);
403 int write_event_header(struct fs_sink_stream
*stream
,
404 const bt_clock_snapshot
*cs
, struct fs_sink_ctf_event_class
*ec
)
409 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
410 bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
411 if (G_UNLIKELY(ret
)) {
416 if (stream
->sc
->default_clock_class
) {
418 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
419 bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
420 if (G_UNLIKELY(ret
)) {
430 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
,
431 const bt_clock_snapshot
*cs
, const bt_event
*event
,
432 struct fs_sink_ctf_event_class
*ec
)
435 const bt_field
*field
;
438 ret
= write_event_header(stream
, cs
, ec
);
439 if (G_UNLIKELY(ret
)) {
444 if (stream
->sc
->event_common_context_fc
) {
445 field
= bt_event_borrow_common_context_field_const(event
);
447 ret
= write_struct_field(stream
,
448 (void *) stream
->sc
->event_common_context_fc
,
450 if (G_UNLIKELY(ret
)) {
455 /* Specific context */
456 if (ec
->spec_context_fc
) {
457 field
= bt_event_borrow_specific_context_field_const(event
);
459 ret
= write_struct_field(stream
, (void *) ec
->spec_context_fc
,
461 if (G_UNLIKELY(ret
)) {
466 /* Specific context */
467 if (ec
->payload_fc
) {
468 field
= bt_event_borrow_payload_field_const(event
);
470 ret
= write_struct_field(stream
, (void *) ec
->payload_fc
,
472 if (G_UNLIKELY(ret
)) {
482 int write_packet_context(struct fs_sink_stream
*stream
)
486 /* Packet total size */
487 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
488 stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
493 /* Packet content size */
494 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
495 stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
500 if (stream
->sc
->packets_have_ts_begin
) {
502 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
503 stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
509 if (stream
->sc
->packets_have_ts_end
) {
511 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
512 stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
518 if (stream
->sc
->has_discarded_events
) {
519 /* Discarded event counter */
520 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
521 stream
->packet_state
.discarded_events_counter
, 8, 64,
528 /* Sequence number */
529 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
530 stream
->packet_state
.seq_num
, 8, 64, BYTE_ORDER
);
536 if (stream
->sc
->packet_context_fc
) {
537 const bt_field
*packet_context_field
;
539 BT_ASSERT(stream
->packet_state
.packet
);
540 packet_context_field
= bt_packet_borrow_context_field_const(
541 stream
->packet_state
.packet
);
542 BT_ASSERT(packet_context_field
);
543 ret
= write_struct_field(stream
,
544 (void *) stream
->sc
->packet_context_fc
,
545 packet_context_field
, false);
556 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
,
557 const bt_clock_snapshot
*cs
, 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
=
568 bt_clock_snapshot_get_value(cs
);
572 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
574 /* bt_ctfser_open_packet() logs errors */
578 /* Packet header: magic */
579 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
580 UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER
);
582 BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
583 stream
->file_name
->str
);
587 /* Packet header: UUID */
588 for (i
= 0; i
< BT_UUID_LEN
; i
++) {
589 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
590 (uint64_t) stream
->sc
->trace
->uuid
[i
], 8, 8, BYTE_ORDER
);
592 BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
593 stream
->file_name
->str
);
598 /* Packet header: stream class ID */
599 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
600 bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
602 BT_COMP_LOGE("Error writing packet header stream class id: "
603 "stream-file-name=%s, stream-class-id=%"PRIu64
,
604 stream
->file_name
->str
,
605 bt_stream_class_get_id(stream
->sc
->ir_sc
));
609 /* Packet header: stream ID */
610 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
611 bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
613 BT_COMP_LOGE("Error writing packet header stream id: "
614 "stream-file-name=%s, stream-id=%"PRIu64
,
615 stream
->file_name
->str
,
616 bt_stream_get_id(stream
->ir_stream
));
620 /* Save packet context's offset to rewrite it later */
621 stream
->packet_state
.context_offset_bits
=
622 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
624 /* Write packet context just to advance to content (first event) */
625 ret
= write_packet_context(stream
);
630 stream
->packet_state
.is_open
= true;
637 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
,
638 const bt_clock_snapshot
*cs
)
642 BT_ASSERT(stream
->packet_state
.is_open
);
645 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
648 stream
->packet_state
.content_size
=
649 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
650 stream
->packet_state
.total_size
=
651 (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
653 /* Rewrite packet context */
654 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
655 stream
->packet_state
.context_offset_bits
);
656 ret
= write_packet_context(stream
);
662 bt_ctfser_close_current_packet(&stream
->ctfser
,
663 stream
->packet_state
.total_size
/ 8);
665 /* Partially copy current packet state to previous packet state */
666 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
667 stream
->prev_packet_state
.discarded_events_counter
=
668 stream
->packet_state
.discarded_events_counter
;
669 stream
->prev_packet_state
.seq_num
=
670 stream
->packet_state
.seq_num
;
672 /* Reset current packet state */
673 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
674 stream
->packet_state
.end_cs
= UINT64_C(-1);
675 stream
->packet_state
.content_size
= 0;
676 stream
->packet_state
.total_size
= 0;
677 stream
->packet_state
.seq_num
+= 1;
678 stream
->packet_state
.context_offset_bits
= 0;
679 stream
->packet_state
.is_open
= false;
680 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);