Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / ctf / fs-sink / fs-sink-stream.c
index a8fd554733b654f1ebf5cd11ff3079245e73a38b..1c83562a003c0e249ece3af1bebb8312d5681dd1 100644 (file)
@@ -1,28 +1,13 @@
 /*
- * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
+ * SPDX-License-Identifier: MIT
  *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
  */
 
+#define BT_COMP_LOG_SELF_COMP (stream->trace->fs_sink->self_comp)
 #define BT_LOG_OUTPUT_LEVEL (stream->log_level)
 #define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/STREAM"
-#include "logging/log.h"
+#include "logging/comp-logging.h"
 
 #include <babeltrace2/babeltrace.h>
 #include <stdio.h>
@@ -32,6 +17,7 @@
 #include "ctfser/ctfser.h"
 #include "compat/endian.h"
 
+#include "fs-sink.h"
 #include "fs-sink-trace.h"
 #include "fs-sink-stream.h"
 #include "translate-trace-ir-to-ctf-ir.h"
@@ -162,9 +148,9 @@ struct fs_sink_stream *fs_sink_stream_create(struct fs_sink_trace *trace,
        stream->prev_packet_state.end_cs = UINT64_C(-1);
        stream->prev_packet_state.discarded_events_counter = UINT64_C(-1);
        stream->prev_packet_state.seq_num = UINT64_C(-1);
-       ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->tc,
-               bt_stream_borrow_class_const(ir_stream), &stream->sc,
-               stream->log_level);
+       ret = try_translate_stream_class_trace_ir_to_ctf_ir(trace->fs_sink,
+               trace->trace, bt_stream_borrow_class_const(ir_stream),
+               &stream->sc);
        if (ret) {
                goto error;
        }
@@ -196,6 +182,34 @@ static
 int write_field(struct fs_sink_stream *stream,
                struct fs_sink_ctf_field_class *fc, const bt_field *field);
 
+static inline
+int write_bool_field(struct fs_sink_stream *stream,
+               struct fs_sink_ctf_field_class_int *fc, const bt_field *field)
+{
+       /*
+        * CTF 1.8 has no boolean field class type, so this component
+        * translates this boolean field to an 8-bit unsigned integer
+        * field which has the value 0 (false) or 1 (true).
+        */
+       return bt_ctfser_write_unsigned_int(&stream->ctfser,
+               bt_field_bool_get_value(field) ? 1 : 0,
+               fc->base.base.alignment, fc->base.size, BYTE_ORDER);
+}
+
+static inline
+int write_bit_array_field(struct fs_sink_stream *stream,
+               struct fs_sink_ctf_field_class_bit_array *fc,
+               const bt_field *field)
+{
+       /*
+        * CTF 1.8 has no bit array field class type, so this component
+        * translates this bit array field to an unsigned integer field.
+        */
+       return bt_ctfser_write_unsigned_int(&stream->ctfser,
+               bt_field_bit_array_get_value_as_integer(field),
+               fc->base.alignment, fc->size, BYTE_ORDER);
+}
+
 static inline
 int write_int_field(struct fs_sink_stream *stream,
                struct fs_sink_ctf_field_class_int *fc, const bt_field *field)
@@ -204,11 +218,11 @@ int write_int_field(struct fs_sink_stream *stream,
 
        if (fc->is_signed) {
                ret = bt_ctfser_write_signed_int(&stream->ctfser,
-                       bt_field_signed_integer_get_value(field),
+                       bt_field_integer_signed_get_value(field),
                        fc->base.base.alignment, fc->base.size, BYTE_ORDER);
        } else {
                ret = bt_ctfser_write_unsigned_int(&stream->ctfser,
-                       bt_field_unsigned_integer_get_value(field),
+                       bt_field_integer_unsigned_get_value(field),
                        fc->base.base.alignment, fc->base.size, BYTE_ORDER);
        }
 
@@ -220,12 +234,14 @@ int write_float_field(struct fs_sink_stream *stream,
                struct fs_sink_ctf_field_class_float *fc, const bt_field *field)
 {
        int ret;
-       double val = bt_field_real_get_value(field);
+       double val;
 
        if (fc->base.size == 32) {
+               val = (double) bt_field_real_single_precision_get_value(field);
                ret = bt_ctfser_write_float32(&stream->ctfser, val,
                        fc->base.base.alignment, BYTE_ORDER);
        } else {
+               val = bt_field_real_double_precision_get_value(field);
                ret = bt_ctfser_write_float64(&stream->ctfser, val,
                        fc->base.base.alignment, BYTE_ORDER);
        }
@@ -319,13 +335,47 @@ end:
        return ret;
 }
 
+static inline
+int write_option_field(struct fs_sink_stream *stream,
+               struct fs_sink_ctf_field_class_option *fc,
+               const bt_field *field)
+{
+       int ret;
+       const bt_field *content_field =
+               bt_field_option_borrow_field_const(field);
+
+       ret = bt_ctfser_write_unsigned_int(&stream->ctfser,
+               content_field ? 1 : 0, 8, 8, BYTE_ORDER);
+       if (G_UNLIKELY(ret)) {
+               goto end;
+       }
+
+       /*
+        * CTF 1.8 has no option field class type, so this component
+        * translates the option field class to a variant field class
+        * where the options are:
+        *
+        * * An empty structure field class (field occupies 0 bits).
+        * * The optional field class itself.
+        *
+        * If `content_field` is `NULL`, do not write anything (empty
+        * structure).
+        */
+       if (content_field) {
+               ret = write_field(stream, fc->content_fc, content_field);
+       }
+
+end:
+       return ret;
+}
+
 static inline
 int write_variant_field(struct fs_sink_stream *stream,
                struct fs_sink_ctf_field_class_variant *fc,
                const bt_field *field)
 {
        uint64_t opt_index =
-               bt_field_variant_get_selected_option_field_index(field);
+               bt_field_variant_get_selected_option_index(field);
        int ret;
 
        if (fc->tag_is_before) {
@@ -352,6 +402,12 @@ int write_field(struct fs_sink_stream *stream,
        int ret;
 
        switch (fc->type) {
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_BOOL:
+               ret = write_bool_field(stream, (void *) fc, field);
+               break;
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_BIT_ARRAY:
+               ret = write_bit_array_field(stream, (void *) fc, field);
+               break;
        case FS_SINK_CTF_FIELD_CLASS_TYPE_INT:
                ret = write_int_field(stream, (void *) fc, field);
                break;
@@ -370,11 +426,14 @@ int write_field(struct fs_sink_stream *stream,
        case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE:
                ret = write_sequence_field(stream, (void *) fc, field);
                break;
+       case FS_SINK_CTF_FIELD_CLASS_TYPE_OPTION:
+               ret = write_option_field(stream, (void *) fc, field);
+               break;
        case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT:
                ret = write_variant_field(stream, (void *) fc, field);
                break;
        default:
-               abort();
+               bt_common_abort();
        }
 
        return ret;
@@ -395,7 +454,7 @@ int write_event_header(struct fs_sink_stream *stream,
 
        /* Time */
        if (stream->sc->default_clock_class) {
-               BT_ASSERT(cs);
+               BT_ASSERT_DBG(cs);
                ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser,
                        bt_clock_snapshot_get_value(cs), 8, 64, BYTE_ORDER);
                if (G_UNLIKELY(ret)) {
@@ -424,7 +483,7 @@ int fs_sink_stream_write_event(struct fs_sink_stream *stream,
        /* Common context */
        if (stream->sc->event_common_context_fc) {
                field = bt_event_borrow_common_context_field_const(event);
-               BT_ASSERT(field);
+               BT_ASSERT_DBG(field);
                ret = write_struct_field(stream,
                        (void *) stream->sc->event_common_context_fc,
                        field, true);
@@ -436,7 +495,7 @@ int fs_sink_stream_write_event(struct fs_sink_stream *stream,
        /* Specific context */
        if (ec->spec_context_fc) {
                field = bt_event_borrow_specific_context_field_const(event);
-               BT_ASSERT(field);
+               BT_ASSERT_DBG(field);
                ret = write_struct_field(stream, (void *) ec->spec_context_fc,
                        field, true);
                if (G_UNLIKELY(ret)) {
@@ -447,7 +506,7 @@ int fs_sink_stream_write_event(struct fs_sink_stream *stream,
        /* Specific context */
        if (ec->payload_fc) {
                field = bt_event_borrow_payload_field_const(event);
-               BT_ASSERT(field);
+               BT_ASSERT_DBG(field);
                ret = write_struct_field(stream, (void *) ec->payload_fc,
                        field, true);
                if (G_UNLIKELY(ret)) {
@@ -515,10 +574,11 @@ int write_packet_context(struct fs_sink_stream *stream)
 
        /* Other members */
        if (stream->sc->packet_context_fc) {
-               const bt_field *packet_context_field =
-                       bt_packet_borrow_context_field_const(
-                               stream->packet_state.packet);
+               const bt_field *packet_context_field;
 
+               BT_ASSERT(stream->packet_state.packet);
+               packet_context_field = bt_packet_borrow_context_field_const(
+                       stream->packet_state.packet);
                BT_ASSERT(packet_context_field);
                ret = write_struct_field(stream,
                        (void *) stream->sc->packet_context_fc,
@@ -559,17 +619,17 @@ int fs_sink_stream_open_packet(struct fs_sink_stream *stream,
        ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser,
                UINT64_C(0xc1fc1fc1), 8, 32, BYTE_ORDER);
        if (ret) {
-               BT_LOGE("Error writing packet header magic: stream-file-name=%s",
+               BT_COMP_LOGE("Error writing packet header magic: stream-file-name=%s",
                        stream->file_name->str);
                goto end;
        }
 
        /* Packet header: UUID */
-       for (i = 0; i < BABELTRACE_UUID_LEN; i++) {
+       for (i = 0; i < BT_UUID_LEN; i++) {
                ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser,
-                       (uint64_t) stream->sc->tc->uuid[i], 8, 8, BYTE_ORDER);
+                       (uint64_t) stream->sc->trace->uuid[i], 8, 8, BYTE_ORDER);
                if (ret) {
-                       BT_LOGE("Error writing packet header UUID: stream-file-name=%s",
+                       BT_COMP_LOGE("Error writing packet header UUID: stream-file-name=%s",
                                stream->file_name->str);
                        goto end;
                }
@@ -579,7 +639,7 @@ int fs_sink_stream_open_packet(struct fs_sink_stream *stream,
        ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser,
                bt_stream_class_get_id(stream->sc->ir_sc), 8, 64, BYTE_ORDER);
        if (ret) {
-               BT_LOGE("Error writing packet header stream class id: "
+               BT_COMP_LOGE("Error writing packet header stream class id: "
                        "stream-file-name=%s, stream-class-id=%"PRIu64,
                        stream->file_name->str,
                        bt_stream_class_get_id(stream->sc->ir_sc));
@@ -590,7 +650,7 @@ int fs_sink_stream_open_packet(struct fs_sink_stream *stream,
        ret = bt_ctfser_write_byte_aligned_unsigned_int(&stream->ctfser,
                bt_stream_get_id(stream->ir_stream), 8, 64, BYTE_ORDER);
        if (ret) {
-               BT_LOGE("Error writing packet header stream id: "
+               BT_COMP_LOGE("Error writing packet header stream id: "
                        "stream-file-name=%s, stream-id=%"PRIu64,
                        stream->file_name->str,
                        bt_stream_get_id(stream->ir_stream));
This page took 0.028342 seconds and 4 git commands to generate.