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;
}
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);
}
--- /dev/null
+/*******************************************************************************
+ * 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());
+ }
+ }
+}
transState.processStateChange(t, value, attributeQuark);
}
+ @Deprecated
@Override
public void incrementAttribute(long t, int attributeQuark)
throws StateValueTypeException, TimeRangeException,
* 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;
--- /dev/null
+/*******************************************************************************
+ * 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);
+ }
+
+}
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;
if (ss == null) {
throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
}
- ss.incrementAttribute(timestamp, quark);
+ StateSystemBuilderUtils.incrementAttributeInt(ss, timestamp, quark, 1);
}
}
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;
/* 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);
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;
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();