tmf: Add a Double type for state values
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Tue, 10 Sep 2013 17:17:05 +0000 (13:17 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 11 Sep 2013 19:59:39 +0000 (15:59 -0400)
Double has a convenient Double.NaN value that cannot be attained
by "normal" means. We'll use this as the null representation of a
DoubleStateValue.

They are stored in the history tree the same way as Long state values:
by using a fixed 64-bit entry in the Strings section. If we start
using a lot of Long and Double values, eventually it might be worth
making the "valueOrOffset" field 64-bit to save space...

Change-Id: If59677565f16eaee947b9c4724fa7df7ef2239a9
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/16330
Tested-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
IP-Clean: Patrick Tasse <patrick.tasse@gmail.com>

org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/headless/GenerateTestValues.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/ITmfStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/Messages.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/TmfStateSystemExplorer.java
org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/messages.properties

index 44e71c04ad25cdf0e5d8cf157607bbca5a2498c5..90546d8dfcd137499c7b21202491e733869d5089 100644 (file)
@@ -101,6 +101,9 @@ public class GenerateTestValues {
             case LONG:
                 writer.println("TmfStateValue.newValueLong(" + val.unboxLong() +"),");
                 break;
+            case DOUBLE:
+                writer.println("TmfStateValue.newValueDouble(" + val.unboxDouble() +"),");
+                break;
             case STRING:
                 writer.println("TmfStateValue.newValueString(\"" + val.unboxStr() + "\"),");
                 break;
index 7e942d658efad4e2e93725a15cbcc2e908404362..70a27d0d0573a109104dc7af24e1edadbe47252e 100644 (file)
@@ -37,10 +37,12 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
     private static final byte TYPE_INTEGER = 0;
     private static final byte TYPE_STRING = 1;
     private static final byte TYPE_LONG = 2;
+    private static final byte TYPE_DOUBLE = 3;
 
     /* String entry sizes of different state values */
     private static final int NO_ENTRY_SIZE = 0;
     private static final int LONG_ENTRY_SIZE = 8;
+    private static final int DOUBLE_ENTRY_SIZE = 8;
     // sizes of string values depend on the string itself
 
     private final long start;
@@ -183,6 +185,21 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
              */
             buffer.reset();
             break;
+
+        case TYPE_DOUBLE:
+            /* Go read the matching entry in the Strings section of the block */
+            buffer.mark();
+            buffer.position(valueOrOffset);
+            value = TmfStateValue.newValueDouble(buffer.getDouble());
+            valueSize = DOUBLE_ENTRY_SIZE;
+
+            /*
+             * Restore the file pointer's position (so we can read the next
+             * interval)
+             */
+            buffer.reset();
+            break;
+
         default:
             /* Unknown data, better to not make anything up... */
             throw new IOException(errMsg);
@@ -279,6 +296,28 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
             buffer.reset();
             break;
 
+        case TYPE_DOUBLE:
+            /* we use the valueOffset as an offset. */
+            buffer.putInt(endPosOfStringEntry - stringsEntrySize);
+            buffer.mark();
+            buffer.position(endPosOfStringEntry - stringsEntrySize);
+
+            /* Write the Double in the Strings section */
+            try {
+                buffer.putDouble(sv.unboxDouble());
+            } catch (StateValueTypeException e) {
+                /*
+                 * This should not happen, since the value told us it was of
+                 * type Double (corrupted value?)
+                 */
+                e.printStackTrace();
+            }
+            if (buffer.position() != endPosOfStringEntry) {
+                throw new IllegalStateException();
+            }
+            buffer.reset();
+            break;
+
         default:
             break;
         }
@@ -342,6 +381,9 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
         case LONG:
             /* The value's bytes are written directly into the strings section */
             return LONG_ENTRY_SIZE;
+        case DOUBLE:
+            /* The value is also written directly into the strings section */
+            return DOUBLE_ENTRY_SIZE;
         case STRING:
             try {
                 /* String's length + 2 (1 byte for size, 1 byte for \0 at the end */
@@ -419,6 +461,8 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
             return TYPE_STRING;
         case LONG:
             return TYPE_LONG;
+        case DOUBLE:
+            return TYPE_DOUBLE;
         default:
             /* Should not happen if the switch is fully covered */
             throw new IllegalStateException();
diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java
new file mode 100644 (file)
index 0000000..926d747
--- /dev/null
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Ericsson
+ *
+ * All rights reserved. This program and the accompanying materials are
+ * made available under the terms of the Eclipse Public License v1.0 which
+ * accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+
+ * Contributors:
+ *   Alexandre Montplaisir - Initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.statevalue;
+
+/**
+ * A state value containing a double primitive.
+ *
+ * @author Alexandre Montplaisir
+ */
+final class DoubleStateValue extends TmfStateValue {
+
+    private final double valueDouble;
+
+    public DoubleStateValue(double value) {
+        valueDouble = value;
+    }
+
+    @Override
+    public Type getType() {
+        return Type.INTEGER;
+    }
+
+    @Override
+    public boolean isNull() {
+        return false;
+    }
+
+    @Override
+    public Double getValue() {
+        return valueDouble;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%3f", valueDouble); //$NON-NLS-1$
+    }
+
+    // ------------------------------------------------------------------------
+    // Unboxing methods
+    // ------------------------------------------------------------------------
+
+    @Override
+    public double unboxDouble() {
+        return valueDouble;
+    }
+}
index e49b1965c1418ed2bbbb07f167fbb0668ac6d15d..033e5042205427b241818322226bcfba85342346 100644 (file)
@@ -33,6 +33,8 @@ public interface ITmfStateValue {
         INTEGER,
         /** 64-bit integer value */
         LONG,
+        /** IEEE 754 double precision number */
+        DOUBLE,
         /** Variable-length string value */
         STRING,
     }
@@ -74,6 +76,15 @@ public interface ITmfStateValue {
      */
     long unboxLong() throws StateValueTypeException;
 
+    /**
+     * Read the contained value as a 'double' primitive
+     *
+     * @return The double contained in the state value
+     * @throws StateValueTypeException
+     *             If the contained value cannot be read as a double
+     */
+    double unboxDouble() throws StateValueTypeException;
+
     /**
      * Read the contained value as a String
      *
index 4d3aaa7f40c91bf8094b32bacce7502c332fd1bf..bd1c93d46582bc26730471c9d52ff2fc9267ecca 100644 (file)
@@ -57,6 +57,11 @@ final class NullStateValue extends TmfStateValue {
         return -1;
     }
 
+    @Override
+    public double unboxDouble() {
+        return Double.NaN;
+    }
+
     @Override
     public String unboxStr() {
         return "nullValue"; //$NON-NLS-1$
index 3fde1b72b246ce4bce56e7ebab0138903928ba95..6b2a28376034d74c0048e89fac65e09a74f9741f 100644 (file)
@@ -124,6 +124,20 @@ public abstract class TmfStateValue implements ITmfStateValue {
         return new LongStateValue(longValue);
     }
 
+    /**
+     * Factory constructor for Double state values
+     *
+     * @param value
+     *            The double value to contain
+     * @return The newly-created TmfStateValue object
+     */
+    public static TmfStateValue newValueDouble(double value) {
+        if (value == Double.NaN) {
+            return nullValue();
+        }
+        return new DoubleStateValue(value);
+    }
+
     /**
      * Factory constructor for String state values
      *
@@ -158,6 +172,11 @@ public abstract class TmfStateValue implements ITmfStateValue {
         throw new StateValueTypeException(unboxErrMsg("Long")); //$NON-NLS-1$
     }
 
+    @Override
+    public double unboxDouble() throws StateValueTypeException {
+        throw new StateValueTypeException(unboxErrMsg("Double")); //$NON-NLS-1$
+    }
+
     @Override
     public String unboxStr() throws StateValueTypeException {
         throw new StateValueTypeException(unboxErrMsg("String")); //$NON-NLS-1$
index add80d906e39b9f76f2ff6bc307c2501c3679506..f1e4307db5959b986f884ccae73e0891b8ffa0df 100644 (file)
@@ -76,6 +76,9 @@ public class Messages extends NLS {
      * @since 2.1*/
     public static String TypeLong;
 
+    /** Label for type Double */
+    public static String TypeDouble;
+
     /** Label for the type String
      * @since 2.1*/
     public static String TypeString;
index 35819243820e68d588256ee1a3cb557b7d8f8421..82fa7bcd730bb6bcfa56218ff6577d2c40e48ed7 100644 (file)
@@ -357,6 +357,10 @@ public class TmfStateSystemExplorer extends TmfView {
                 value = String.valueOf(state.unboxLong());
                 item.setText(TYPE_COL, Messages.TypeLong);
                 break;
+            case DOUBLE:
+                value = String.valueOf(state.unboxDouble());
+                item.setText(TYPE_COL, Messages.TypeDouble);
+                break;
             case STRING:
                 value = state.unboxStr();
                 item.setText(TYPE_COL, Messages.TypeString);
@@ -503,7 +507,7 @@ public class TmfStateSystemExplorer extends TmfView {
         };
         thread.start();
     }
-    
+
     /**
      * Update the display to use the updated timestamp format
      *
index 25c1f55a64e78cdbe6d7cf5c90bfde780088ed74..45e84306b55825a508484b182613cd7949ba1b5a 100644 (file)
@@ -24,4 +24,5 @@ OutOfRangeMsg=Out of range
 FilterButton=Display Only Change
 TypeInteger=Int
 TypeLong=Long
+TypeDouble=Double
 TypeString=String
\ No newline at end of file
This page took 0.109839 seconds and 5 git commands to generate.