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_LOG_OUTPUT_LEVEL (stream->log_level)
24 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM"
25 #include "logging/log.h"
27 #include <babeltrace2/babeltrace.h>
31 #include "common/assert.h"
32 #include "ctfser/ctfser.h"
33 #include "compat/endian.h"
35 #include "fs-sink-trace.h"
36 #include "fs-sink-stream.h"
37 #include "translate-trace-ir-to-ctf-ir.h"
40 void fs_sink_stream_destroy(struct fs_sink_stream
*stream
)
46 bt_ctfser_fini(&stream
->ctfser
);
48 if (stream
->file_name
) {
49 g_string_free(stream
->file_name
, TRUE
);
50 stream
->file_name
= NULL
;
53 bt_packet_put_ref(stream
->packet_state
.packet
);
61 bool stream_file_name_exists(struct fs_sink_trace
*trace
, const char *name
)
67 g_hash_table_iter_init(&iter
, trace
->streams
);
69 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
70 struct fs_sink_stream
*stream
= value
;
72 if (strcmp(name
, stream
->file_name
->str
) == 0) {
83 GString
*sanitize_stream_file_name(const char *file_name
)
85 GString
*san_file_name
= g_string_new(NULL
);
89 BT_ASSERT(san_file_name
);
91 basename
= g_path_get_basename(file_name
);
93 for (ch
= basename
; *ch
!= '\0'; ch
++) {
95 g_string_append_c(san_file_name
, '_');
97 g_string_append_c(san_file_name
, *ch
);
101 /* Do not allow `.` and `..` either */
102 if (strcmp(san_file_name
->str
, ".") == 0 ||
103 strcmp(san_file_name
->str
, "..") == 0) {
104 g_string_assign(san_file_name
, "stream");
108 return san_file_name
;
112 GString
*make_unique_stream_file_name(struct fs_sink_trace
*trace
,
115 GString
*san_base
= sanitize_stream_file_name(base
);
116 GString
*name
= g_string_new(san_base
->str
);
117 unsigned int suffix
= 0;
121 while (stream_file_name_exists(trace
, name
->str
) &&
122 strcmp(name
->str
, "metadata") == 0) {
123 g_string_printf(name
, "%s-%u", san_base
->str
, suffix
);
127 g_string_free(san_base
, TRUE
);
132 void set_stream_file_name(struct fs_sink_stream
*stream
)
134 const char *base_name
= bt_stream_get_name(stream
->ir_stream
);
137 base_name
= "stream";
140 BT_ASSERT(!stream
->file_name
);
141 stream
->file_name
= make_unique_stream_file_name(stream
->trace
,
146 struct fs_sink_stream
*fs_sink_stream_create(struct fs_sink_trace
*trace
,
147 const bt_stream
*ir_stream
)
149 struct fs_sink_stream
*stream
= g_new0(struct fs_sink_stream
, 1);
151 GString
*path
= g_string_new(trace
->path
->str
);
157 stream
->log_level
= trace
->log_level
;
158 stream
->trace
= trace
;
159 stream
->ir_stream
= ir_stream
;
160 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
161 stream
->packet_state
.end_cs
= UINT64_C(-1);
162 stream
->prev_packet_state
.end_cs
= UINT64_C(-1);
163 stream
->prev_packet_state
.discarded_events_counter
= UINT64_C(-1);
164 stream
->prev_packet_state
.seq_num
= UINT64_C(-1);
165 ret
= try_translate_stream_class_trace_ir_to_ctf_ir(trace
->tc
,
166 bt_stream_borrow_class_const(ir_stream
), &stream
->sc
,
172 set_stream_file_name(stream
);
173 g_string_append_printf(path
, "/%s", stream
->file_name
->str
);
174 ret
= bt_ctfser_init(&stream
->ctfser
, path
->str
,
180 g_hash_table_insert(trace
->streams
, (gpointer
) ir_stream
, stream
);
184 fs_sink_stream_destroy(stream
);
189 g_string_free(path
, TRUE
);
196 int write_field(struct fs_sink_stream
*stream
,
197 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
);
200 int write_int_field(struct fs_sink_stream
*stream
,
201 struct fs_sink_ctf_field_class_int
*fc
, const bt_field
*field
)
206 ret
= bt_ctfser_write_signed_int(&stream
->ctfser
,
207 bt_field_signed_integer_get_value(field
),
208 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
210 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
211 bt_field_unsigned_integer_get_value(field
),
212 fc
->base
.base
.alignment
, fc
->base
.size
, BYTE_ORDER
);
219 int write_float_field(struct fs_sink_stream
*stream
,
220 struct fs_sink_ctf_field_class_float
*fc
, const bt_field
*field
)
223 double val
= bt_field_real_get_value(field
);
225 if (fc
->base
.size
== 32) {
226 ret
= bt_ctfser_write_float32(&stream
->ctfser
, val
,
227 fc
->base
.base
.alignment
, BYTE_ORDER
);
229 ret
= bt_ctfser_write_float64(&stream
->ctfser
, val
,
230 fc
->base
.base
.alignment
, BYTE_ORDER
);
237 int write_string_field(struct fs_sink_stream
*stream
,
238 struct fs_sink_ctf_field_class_string
*fc
, const bt_field
*field
)
240 return bt_ctfser_write_string(&stream
->ctfser
,
241 bt_field_string_get_value(field
));
245 int write_array_field_elements(struct fs_sink_stream
*stream
,
246 struct fs_sink_ctf_field_class_array_base
*fc
,
247 const bt_field
*field
)
250 uint64_t len
= bt_field_array_get_length(field
);
253 for (i
= 0; i
< len
; i
++) {
254 const bt_field
*elem_field
=
255 bt_field_array_borrow_element_field_by_index_const(
257 ret
= write_field(stream
, fc
->elem_fc
, elem_field
);
258 if (G_UNLIKELY(ret
)) {
268 int write_sequence_field(struct fs_sink_stream
*stream
,
269 struct fs_sink_ctf_field_class_sequence
*fc
,
270 const bt_field
*field
)
274 if (fc
->length_is_before
) {
275 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
276 bt_field_array_get_length(field
), 8, 32, BYTE_ORDER
);
277 if (G_UNLIKELY(ret
)) {
282 ret
= write_array_field_elements(stream
, (void *) fc
, field
);
289 int write_struct_field(struct fs_sink_stream
*stream
,
290 struct fs_sink_ctf_field_class_struct
*fc
,
291 const bt_field
*field
, bool align_struct
)
296 if (G_LIKELY(align_struct
)) {
297 ret
= bt_ctfser_align_offset_in_current_packet(&stream
->ctfser
,
299 if (G_UNLIKELY(ret
)) {
304 for (i
= 0; i
< fc
->members
->len
; i
++) {
305 const bt_field
*memb_field
=
306 bt_field_structure_borrow_member_field_by_index_const(
308 struct fs_sink_ctf_field_class
*member_fc
=
309 fs_sink_ctf_field_class_struct_borrow_member_by_index(
312 ret
= write_field(stream
, member_fc
, memb_field
);
313 if (G_UNLIKELY(ret
)) {
323 int write_variant_field(struct fs_sink_stream
*stream
,
324 struct fs_sink_ctf_field_class_variant
*fc
,
325 const bt_field
*field
)
328 bt_field_variant_get_selected_option_field_index(field
);
331 if (fc
->tag_is_before
) {
332 ret
= bt_ctfser_write_unsigned_int(&stream
->ctfser
,
333 opt_index
, 8, 16, BYTE_ORDER
);
334 if (G_UNLIKELY(ret
)) {
339 ret
= write_field(stream
,
340 fs_sink_ctf_field_class_variant_borrow_option_by_index(fc
,
342 bt_field_variant_borrow_selected_option_field_const(field
));
349 int write_field(struct fs_sink_stream
*stream
,
350 struct fs_sink_ctf_field_class
*fc
, const bt_field
*field
)
355 case FS_SINK_CTF_FIELD_CLASS_TYPE_INT
:
356 ret
= write_int_field(stream
, (void *) fc
, field
);
358 case FS_SINK_CTF_FIELD_CLASS_TYPE_FLOAT
:
359 ret
= write_float_field(stream
, (void *) fc
, field
);
361 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRING
:
362 ret
= write_string_field(stream
, (void *) fc
, field
);
364 case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT
:
365 ret
= write_struct_field(stream
, (void *) fc
, field
, true);
367 case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY
:
368 ret
= write_array_field_elements(stream
, (void *) fc
, field
);
370 case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE
:
371 ret
= write_sequence_field(stream
, (void *) fc
, field
);
373 case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT
:
374 ret
= write_variant_field(stream
, (void *) fc
, field
);
384 int write_event_header(struct fs_sink_stream
*stream
,
385 const bt_clock_snapshot
*cs
, struct fs_sink_ctf_event_class
*ec
)
390 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
391 bt_event_class_get_id(ec
->ir_ec
), 8, 64, BYTE_ORDER
);
392 if (G_UNLIKELY(ret
)) {
397 if (stream
->sc
->default_clock_class
) {
399 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
400 bt_clock_snapshot_get_value(cs
), 8, 64, BYTE_ORDER
);
401 if (G_UNLIKELY(ret
)) {
411 int fs_sink_stream_write_event(struct fs_sink_stream
*stream
,
412 const bt_clock_snapshot
*cs
, const bt_event
*event
,
413 struct fs_sink_ctf_event_class
*ec
)
416 const bt_field
*field
;
419 ret
= write_event_header(stream
, cs
, ec
);
420 if (G_UNLIKELY(ret
)) {
425 if (stream
->sc
->event_common_context_fc
) {
426 field
= bt_event_borrow_common_context_field_const(event
);
428 ret
= write_struct_field(stream
,
429 (void *) stream
->sc
->event_common_context_fc
,
431 if (G_UNLIKELY(ret
)) {
436 /* Specific context */
437 if (ec
->spec_context_fc
) {
438 field
= bt_event_borrow_specific_context_field_const(event
);
440 ret
= write_struct_field(stream
, (void *) ec
->spec_context_fc
,
442 if (G_UNLIKELY(ret
)) {
447 /* Specific context */
448 if (ec
->payload_fc
) {
449 field
= bt_event_borrow_payload_field_const(event
);
451 ret
= write_struct_field(stream
, (void *) ec
->payload_fc
,
453 if (G_UNLIKELY(ret
)) {
463 int write_packet_context(struct fs_sink_stream
*stream
)
467 /* Packet total size */
468 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
469 stream
->packet_state
.total_size
, 8, 64, BYTE_ORDER
);
474 /* Packet content size */
475 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
476 stream
->packet_state
.content_size
, 8, 64, BYTE_ORDER
);
481 if (stream
->sc
->packets_have_ts_begin
) {
483 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
484 stream
->packet_state
.beginning_cs
, 8, 64, BYTE_ORDER
);
490 if (stream
->sc
->packets_have_ts_end
) {
492 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
493 stream
->packet_state
.end_cs
, 8, 64, BYTE_ORDER
);
499 if (stream
->sc
->has_discarded_events
) {
500 /* Discarded event counter */
501 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
502 stream
->packet_state
.discarded_events_counter
, 8, 64,
509 /* Sequence number */
510 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
511 stream
->packet_state
.seq_num
, 8, 64, BYTE_ORDER
);
517 if (stream
->sc
->packet_context_fc
) {
518 const bt_field
*packet_context_field
=
519 bt_packet_borrow_context_field_const(
520 stream
->packet_state
.packet
);
522 BT_ASSERT(packet_context_field
);
523 ret
= write_struct_field(stream
,
524 (void *) stream
->sc
->packet_context_fc
,
525 packet_context_field
, false);
536 int fs_sink_stream_open_packet(struct fs_sink_stream
*stream
,
537 const bt_clock_snapshot
*cs
, const bt_packet
*packet
)
542 BT_ASSERT(!stream
->packet_state
.is_open
);
543 bt_packet_put_ref(stream
->packet_state
.packet
);
544 stream
->packet_state
.packet
= packet
;
545 bt_packet_get_ref(stream
->packet_state
.packet
);
547 stream
->packet_state
.beginning_cs
=
548 bt_clock_snapshot_get_value(cs
);
552 ret
= bt_ctfser_open_packet(&stream
->ctfser
);
554 /* bt_ctfser_open_packet() logs errors */
558 /* Packet header: magic */
559 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
560 UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER
);
562 BT_LOGE("Error writing packet header magic: stream-file-name=%s",
563 stream
->file_name
->str
);
567 /* Packet header: UUID */
568 for (i
= 0; i
< BABELTRACE_UUID_LEN
; i
++) {
569 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
570 (uint64_t) stream
->sc
->tc
->uuid
[i
], 8, 8, BYTE_ORDER
);
572 BT_LOGE("Error writing packet header UUID: stream-file-name=%s",
573 stream
->file_name
->str
);
578 /* Packet header: stream class ID */
579 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
580 bt_stream_class_get_id(stream
->sc
->ir_sc
), 8, 64, BYTE_ORDER
);
582 BT_LOGE("Error writing packet header stream class id: "
583 "stream-file-name=%s, stream-class-id=%"PRIu64
,
584 stream
->file_name
->str
,
585 bt_stream_class_get_id(stream
->sc
->ir_sc
));
589 /* Packet header: stream ID */
590 ret
= bt_ctfser_write_byte_aligned_unsigned_int(&stream
->ctfser
,
591 bt_stream_get_id(stream
->ir_stream
), 8, 64, BYTE_ORDER
);
593 BT_LOGE("Error writing packet header stream id: "
594 "stream-file-name=%s, stream-id=%"PRIu64
,
595 stream
->file_name
->str
,
596 bt_stream_get_id(stream
->ir_stream
));
600 /* Save packet context's offset to rewrite it later */
601 stream
->packet_state
.context_offset_bits
=
602 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
604 /* Write packet context just to advance to content (first event) */
605 ret
= write_packet_context(stream
);
610 stream
->packet_state
.is_open
= true;
617 int fs_sink_stream_close_packet(struct fs_sink_stream
*stream
,
618 const bt_clock_snapshot
*cs
)
622 BT_ASSERT(stream
->packet_state
.is_open
);
625 stream
->packet_state
.end_cs
= bt_clock_snapshot_get_value(cs
);
628 stream
->packet_state
.content_size
=
629 bt_ctfser_get_offset_in_current_packet_bits(&stream
->ctfser
);
630 stream
->packet_state
.total_size
=
631 (stream
->packet_state
.content_size
+ 7) & ~UINT64_C(7);
633 /* Rewrite packet context */
634 bt_ctfser_set_offset_in_current_packet_bits(&stream
->ctfser
,
635 stream
->packet_state
.context_offset_bits
);
636 ret
= write_packet_context(stream
);
642 bt_ctfser_close_current_packet(&stream
->ctfser
,
643 stream
->packet_state
.total_size
/ 8);
645 /* Partially copy current packet state to previous packet state */
646 stream
->prev_packet_state
.end_cs
= stream
->packet_state
.end_cs
;
647 stream
->prev_packet_state
.discarded_events_counter
=
648 stream
->packet_state
.discarded_events_counter
;
649 stream
->prev_packet_state
.seq_num
=
650 stream
->packet_state
.seq_num
;
652 /* Reset current packet state */
653 stream
->packet_state
.beginning_cs
= UINT64_C(-1);
654 stream
->packet_state
.end_cs
= UINT64_C(-1);
655 stream
->packet_state
.content_size
= 0;
656 stream
->packet_state
.total_size
= 0;
657 stream
->packet_state
.seq_num
+= 1;
658 stream
->packet_state
.context_offset_bits
= 0;
659 stream
->packet_state
.is_open
= false;
660 BT_PACKET_PUT_REF_AND_RESET(stream
->packet_state
.packet
);