ss: Add utility method to increment an attribute by a value
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Fri, 19 Feb 2016 16:03:55 +0000 (11:03 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Fri, 4 Mar 2016 16:21:51 +0000 (11:21 -0500)
This new utility method is useful for analysis who need to increment
values of an attribute by a value that is not 1.

This can replace ITmfStateSystemBuilder#incrementAttribute().

Change-Id: I19037dda4c417d44e8f0aacc017545f921d18ff6
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/66944
Reviewed-by: Hudson CI
analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/contextswitch/KernelContextSwitchStateProvider.java
statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/StateSystemBuilderUtilsTest.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/StateSystem.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/ITmfStateSystemBuilder.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/StateSystemBuilderUtils.java [new file with mode: 0644]
tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsEventTypesModule.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsTotalsModule.java

index c106fcf1a66eaceddbd6218cb358e84c771084e7..ea811a9a0273fcb47bb90bc678ed690763f3b604 100644 (file)
@@ -19,6 +19,7 @@ import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEven
 import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
@@ -90,7 +91,7 @@ public class KernelContextSwitchStateProvider extends AbstractTmfStateProvider {
             }
             int cpuQuark = stateSystemBuilder.getQuarkRelativeAndAdd(fCpuAttributeQuark, cpuObj.toString());
             try {
-                stateSystemBuilder.incrementAttribute(event.getTimestamp().getValue(), cpuQuark);
+                StateSystemBuilderUtils.incrementAttributeInt(stateSystemBuilder, event.getTimestamp().getValue(), cpuQuark, 1);
             } catch (StateValueTypeException | AttributeNotFoundException e) {
                 Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
             }
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/StateSystemBuilderUtilsTest.java b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/StateSystemBuilderUtilsTest.java
new file mode 100644 (file)
index 0000000..52686c2
--- /dev/null
@@ -0,0 +1,130 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * 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
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.statesystem.core.tests;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
+import org.eclipse.tracecompass.statesystem.core.StateSystemFactory;
+import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
+import org.eclipse.tracecompass.statesystem.core.backend.StateHistoryBackendFactory;
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test the {@link StateSystemBuilderUtils} class
+ *
+ * @author Geneviève Bastien
+ */
+public class StateSystemBuilderUtilsTest {
+
+    private static final long START_TIME = 1000L;
+    private static final long TIME_INCREMENT = 10;
+    private static final @NonNull String DUMMY_STRING = "test";
+
+    private ITmfStateSystemBuilder fStateSystem;
+
+    /**
+     * Build a small test state system in memory
+     */
+    @Before
+    public void setupStateSystem() {
+        try {
+            IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(DUMMY_STRING, START_TIME);
+            fStateSystem = StateSystemFactory.newStateSystem(backend);
+
+        } catch (StateValueTypeException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Test the
+     * {@link StateSystemBuilderUtils#incrementAttributeLong(ITmfStateSystemBuilder, long, int, long)}
+     * method
+     */
+    @Test
+    public void testIncrementLong() {
+        ITmfStateSystemBuilder ss = fStateSystem;
+        int quark = ss.getQuarkAbsoluteAndAdd(DUMMY_STRING);
+
+        try {
+            /* Value should be null at the beginning */
+            ITmfStateValue value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.nullValue(), value);
+
+            /* Increment by 3 */
+            long increment = 3;
+            StateSystemBuilderUtils.incrementAttributeLong(ss, START_TIME + TIME_INCREMENT, quark, increment);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueLong(increment), value);
+
+            /* Increment by 1000 */
+            Long increment2 = 1000L;
+            StateSystemBuilderUtils.incrementAttributeLong(ss, START_TIME + TIME_INCREMENT, quark, increment2);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueLong(increment + increment2), value);
+
+            /* Increment by a negative value */
+            Long increment3 = -500L;
+            StateSystemBuilderUtils.incrementAttributeLong(ss, START_TIME + TIME_INCREMENT, quark, increment3);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueLong(increment + increment2 + increment3), value);
+
+        } catch (AttributeNotFoundException e) {
+            fail(e.getMessage());
+        }
+    }
+
+    /**
+     * Test the
+     * {@link StateSystemBuilderUtils#incrementAttributeInt(ITmfStateSystemBuilder, long, int, int)}
+     * method
+     */
+    @Test
+    public void testIncrementInt() {
+        ITmfStateSystemBuilder ss = fStateSystem;
+        int quark = ss.getQuarkAbsoluteAndAdd(DUMMY_STRING);
+
+        try {
+            /* Value should be null at the beginning */
+            ITmfStateValue value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.nullValue(), value);
+
+            /* Increment by 3 */
+            int increment = 3;
+            StateSystemBuilderUtils.incrementAttributeInt(ss, START_TIME + TIME_INCREMENT, quark, increment);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueInt(increment), value);
+
+            /* Increment by 1000 */
+            int increment2 = 1000;
+            StateSystemBuilderUtils.incrementAttributeInt(ss, START_TIME + TIME_INCREMENT, quark, increment2);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueInt(increment + increment2), value);
+
+            /* Increment by a negative value */
+            int increment3 = -500;
+            StateSystemBuilderUtils.incrementAttributeInt(ss, START_TIME + TIME_INCREMENT, quark, increment3);
+            value = ss.queryOngoingState(quark);
+            assertEquals(TmfStateValue.newValueInt(increment + increment2 + increment3), value);
+
+        } catch (AttributeNotFoundException e) {
+            fail(e.getMessage());
+        }
+    }
+}
index e11bccc437f0f657d36b2073ebaf7b316abca698..e95c7d4326b6bd5867efef2b7ad880dc77856776 100644 (file)
@@ -401,6 +401,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
         transState.processStateChange(t, value, attributeQuark);
     }
 
+    @Deprecated
     @Override
     public void incrementAttribute(long t, int attributeQuark)
             throws StateValueTypeException, TimeRangeException,
index 9868ab253411184f1b8d8e00fec829ff63fa645a..541153362a394f98838f9baeaf92088814c7a40b 100644 (file)
@@ -146,7 +146,11 @@ public interface ITmfStateSystemBuilder extends ITmfStateSystem {
      *             If the given timestamp is invalid
      * @throws AttributeNotFoundException
      *             If the quark is invalid
+     * @deprecated Use
+     *             {@link StateSystemBuilderUtils#incrementAttributeInt(ITmfStateSystemBuilder, long, int, Integer)}
+     *             instead
      */
+    @Deprecated
     void incrementAttribute(long t, int attributeQuark)
             throws AttributeNotFoundException, StateValueTypeException;
 
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/StateSystemBuilderUtils.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/StateSystemBuilderUtils.java
new file mode 100644 (file)
index 0000000..c4da302
--- /dev/null
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2016 École Polytechnique de Montréal
+ *
+ * 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
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.statesystem.core;
+
+import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
+import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
+import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
+
+/**
+ * Provide utility methods for building the state system
+ *
+ * @since 2.0
+ */
+public final class StateSystemBuilderUtils {
+
+    private StateSystemBuilderUtils() {
+    }
+
+    /**
+     * Increments attribute by a certain long value. Reads the current value of
+     * a given attribute as a long, and increment it by a certain increment.
+     *
+     * @param ssb
+     *            The state system builder
+     * @param t
+     *            The time at which to do the increment
+     * @param attributeQuark
+     *            The quark of the attribute to increment
+     * @param increment
+     *            The value to increment. This value can be negative.
+     * @throws StateValueTypeException
+     *             If the attribute already exists but is not of type Long
+     * @throws AttributeNotFoundException
+     *             If the quark is invalid
+     */
+    public static void incrementAttributeLong(ITmfStateSystemBuilder ssb, long t, int attributeQuark, long increment)
+            throws StateValueTypeException, AttributeNotFoundException {
+        ITmfStateValue stateValue = ssb.queryOngoingState(attributeQuark);
+
+        /* if the attribute was previously null, start counting at 0 */
+        long prevValue = 0;
+        if (!stateValue.isNull()) {
+            prevValue = stateValue.unboxLong();
+        }
+        ssb.modifyAttribute(t, TmfStateValue.newValueLong(prevValue + increment), attributeQuark);
+    }
+
+    /**
+     * Increments attribute by a certain integer value. Reads the current value
+     * of a given attribute as an int, and increment it by a certain increment.
+     *
+     * @param ssb
+     *            The state system builder
+     * @param t
+     *            The time at which to do the increment
+     * @param attributeQuark
+     *            The quark of the attribute to increment
+     * @param increment
+     *            The value to increment. This value can be negative.
+     * @throws StateValueTypeException
+     *             If the attribute already exists but is not of type Integer
+     * @throws AttributeNotFoundException
+     *             If the quark is invalid
+     */
+    public static void incrementAttributeInt(ITmfStateSystemBuilder ssb, long t, int attributeQuark, int increment)
+            throws StateValueTypeException, AttributeNotFoundException {
+        ITmfStateValue stateValue = ssb.queryOngoingState(attributeQuark);
+
+        /* if the attribute was previously null, start counting at 0 */
+        int prevValue = 0;
+        if (!stateValue.isNull()) {
+            prevValue = stateValue.unboxInt();
+        }
+        ssb.modifyAttribute(t, TmfStateValue.newValueInt(prevValue + increment), attributeQuark);
+    }
+
+}
index c9da497472fe5c807a4fc1de1f39212d4db59bc7..2f89efcfde40af695c34ecebe72b03cd5eb49d0e 100644 (file)
@@ -19,6 +19,7 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
@@ -200,7 +201,7 @@ public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
             if (ss == null) {
                 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
             }
-            ss.incrementAttribute(timestamp, quark);
+            StateSystemBuilderUtils.incrementAttributeInt(ss, timestamp, quark, 1);
         }
     }
 
index 2966a83c9f4241b6e2febe0ffd92e23f8ecc4c62..331fca9c3f1d74b0ea52a4bb9e9eea7a40952efa 100644 (file)
@@ -17,6 +17,7 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
@@ -162,7 +163,7 @@ public class TmfStatisticsEventTypesModule extends TmfStateSystemAnalysisModule
 
                 /* Number of events of each type, globally */
                 quark = ss.getQuarkAbsoluteAndAdd(Attributes.EVENT_TYPES, eventName);
-                ss.incrementAttribute(ts, quark);
+                StateSystemBuilderUtils.incrementAttributeInt(ss, ts, quark, 1);
 
 //                /* Number of events per CPU */
 //                quark = ss.getQuarkRelativeAndAdd(currentCPUNode, Attributes.STATISTICS, Attributes.EVENT_TYPES, eventName);
index fa4f63aa0c9d8190a364978ccc2412b67ae14de6..35811a3338b7d6576ec8708cff34c463d2603b7e 100644 (file)
@@ -16,6 +16,7 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
+import org.eclipse.tracecompass.statesystem.core.StateSystemBuilderUtils;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
@@ -123,7 +124,7 @@ public class TmfStatisticsTotalsModule extends TmfStateSystemAnalysisModule {
             try {
                 /* Total number of events */
                 int quark = ss.getQuarkAbsoluteAndAdd(Attributes.TOTAL);
-                ss.incrementAttribute(ts, quark);
+                StateSystemBuilderUtils.incrementAttributeInt(ss, ts, quark, 1);
 
             } catch (StateValueTypeException | TimeRangeException | AttributeNotFoundException e) {
                 e.printStackTrace();
This page took 0.031453 seconds and 5 git commands to generate.