From a3c22e8eb720864bdd595f7a0bd6451417afc94a Mon Sep 17 00:00:00 2001 From: Alexandre Montplaisir Date: Tue, 10 Sep 2013 13:17:05 -0400 Subject: [PATCH] tmf: Add a Double type for state values 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 Reviewed-on: https://git.eclipse.org/r/16330 Tested-by: Hudson CI Reviewed-by: Patrick Tasse IP-Clean: Patrick Tasse --- .../tests/headless/GenerateTestValues.java | 3 + .../backends/historytree/HTInterval.java | 44 +++++++++++++++ .../tmf/core/statevalue/DoubleStateValue.java | 56 +++++++++++++++++++ .../tmf/core/statevalue/ITmfStateValue.java | 11 ++++ .../tmf/core/statevalue/NullStateValue.java | 5 ++ .../tmf/core/statevalue/TmfStateValue.java | 19 +++++++ .../tmf/ui/views/statesystem/Messages.java | 3 + .../statesystem/TmfStateSystemExplorer.java | 6 +- .../ui/views/statesystem/messages.properties | 1 + 9 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java diff --git a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/headless/GenerateTestValues.java b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/headless/GenerateTestValues.java index 44e71c04ad..90546d8dfc 100644 --- a/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/headless/GenerateTestValues.java +++ b/org.eclipse.linuxtools.lttng2.kernel.core.tests/src/org/eclipse/linuxtools/lttng2/kernel/core/tests/headless/GenerateTestValues.java @@ -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; diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java index 7e942d658e..70a27d0d05 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java @@ -37,10 +37,12 @@ final class HTInterval implements ITmfStateInterval, Comparable { 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 { */ 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 { 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 { 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 { 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 index 0000000000..926d7474cf --- /dev/null +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/DoubleStateValue.java @@ -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; + } +} diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/ITmfStateValue.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/ITmfStateValue.java index e49b1965c1..033e504220 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/ITmfStateValue.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/ITmfStateValue.java @@ -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 * diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java index 4d3aaa7f40..bd1c93d465 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java @@ -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$ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java index 3fde1b72b2..6b2a283760 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java @@ -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$ diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/Messages.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/Messages.java index add80d906e..f1e4307db5 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/Messages.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/Messages.java @@ -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; diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/TmfStateSystemExplorer.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/TmfStateSystemExplorer.java index 3581924382..82fa7bcd73 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/TmfStateSystemExplorer.java +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/TmfStateSystemExplorer.java @@ -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 * diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/messages.properties b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/messages.properties index 25c1f55a64..45e84306b5 100644 --- a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/messages.properties +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statesystem/messages.properties @@ -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 -- 2.34.1