From acec47ce7d5fbc9e7b47c6f917bfa30329e54552 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Fri, 19 Feb 2016 11:03:55 -0500 Subject: [PATCH] ss: Add utility method to increment an attribute by a value MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Reviewed-on: https://git.eclipse.org/r/66944 Reviewed-by: Hudson CI --- .../KernelContextSwitchStateProvider.java | 3 +- .../tests/StateSystemBuilderUtilsTest.java | 130 ++++++++++++++++++ .../statesystem/core/StateSystem.java | 1 + .../core/ITmfStateSystemBuilder.java | 4 + .../core/StateSystemBuilderUtils.java | 85 ++++++++++++ .../readwrite/TmfXmlReadWriteStateValue.java | 3 +- .../TmfStatisticsEventTypesModule.java | 3 +- .../statistics/TmfStatisticsTotalsModule.java | 3 +- 8 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/StateSystemBuilderUtilsTest.java create mode 100644 statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/StateSystemBuilderUtils.java diff --git a/analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/contextswitch/KernelContextSwitchStateProvider.java b/analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/contextswitch/KernelContextSwitchStateProvider.java index c106fcf1a6..ea811a9a02 100644 --- a/analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/contextswitch/KernelContextSwitchStateProvider.java +++ b/analysis/org.eclipse.tracecompass.analysis.os.linux.core/src/org/eclipse/tracecompass/analysis/os/linux/core/contextswitch/KernelContextSwitchStateProvider.java @@ -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 index 0000000000..52686c257d --- /dev/null +++ b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/StateSystemBuilderUtilsTest.java @@ -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()); + } + } +} diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/StateSystem.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/StateSystem.java index e11bccc437..e95c7d4326 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/StateSystem.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/StateSystem.java @@ -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, diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/ITmfStateSystemBuilder.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/ITmfStateSystemBuilder.java index 9868ab2534..541153362a 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/ITmfStateSystemBuilder.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/ITmfStateSystemBuilder.java @@ -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 index 0000000000..c4da3028de --- /dev/null +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/statesystem/core/StateSystemBuilderUtils.java @@ -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); + } + +} diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java index c9da497472..2f89efcfde 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.core/src/org/eclipse/tracecompass/tmf/analysis/xml/core/model/readwrite/TmfXmlReadWriteStateValue.java @@ -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); } } diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsEventTypesModule.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsEventTypesModule.java index 2966a83c9f..331fca9c3f 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsEventTypesModule.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsEventTypesModule.java @@ -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); diff --git a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsTotalsModule.java b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsTotalsModule.java index fa4f63aa0c..35811a3338 100644 --- a/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsTotalsModule.java +++ b/tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statistics/TmfStatisticsTotalsModule.java @@ -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(); -- 2.34.1