import java.io.File;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
+import com.google.common.collect.ImmutableSet;
+
/**
* Test suite for the {@link KernelCpuUsageAnalysis} class
*
}
/**
- * Test the {@link KernelCpuUsageAnalysis#getCpuUsageInRange(long, long)}
+ * Test the
+ * {@link KernelCpuUsageAnalysis#getCpuUsageInRange(java.util.Set, long, long)}
* method.
+ * <p>
+ * TODO: extend!
*/
@Test
public void testUsageInRange() {
expected.put("total/4", 13L);
expected.put("0", 24L);
expected.put("1", 24L);
- Map<String, Long> resultMap = fModule.getCpuUsageInRange(0L, 30L);
+ Map<String, Long> resultMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, 0L, 30L);
assertEquals(expected, resultMap);
/* Verify a range when a process runs at the start */
expected.put("total/4", 3L);
expected.put("0", 3L);
expected.put("1", 3L);
- resultMap = fModule.getCpuUsageInRange(22L, 25L);
+ resultMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, 22L, 25L);
assertEquals(expected, resultMap);
/* Verify a range when a process runs at the end */
expected.put("total/4", 2L);
expected.put("0", 3L);
expected.put("1", 3L);
- resultMap = fModule.getCpuUsageInRange(1L, 4L);
+ resultMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, 1L, 4L);
assertEquals(expected, resultMap);
/* Verify a range when a process runs at start and at the end */
expected.put("total/4", 4L);
expected.put("0", 9L);
expected.put("1", 9L);
- resultMap = fModule.getCpuUsageInRange(4L, 13L);
+ resultMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, 4L, 13L);
+ assertEquals(expected, resultMap);
+ }
+
+ /**
+ * Tests the cpu usage for a cpu subset within a range
+ */
+ @Test
+ public void testInRangeWithCpuSubset() {
+
+ fModule.schedule();
+ fModule.waitForCompletion();
+
+ /* Verify a range when a process runs at start and at the end */
+ Map<String, Long> expected = new HashMap<>();
+ expected.put("0/1", 0L);
+ expected.put("0/2", 9L);
+ expected.put("0/3", 0L);
+ expected.put("total/1", 0L);
+ expected.put("total/2", 9L);
+ expected.put("total/3", 0L);
+ expected.put("0", 9L);
+ expected.put("total", 9L);
+ Map<String, Long> resultMap = fModule.getCpuUsageInRange(Collections.<@NonNull Integer> singleton(0), 4L, 13L);
+ assertEquals(expected, resultMap);
+
+ /* Verify a range when a process runs at start and at the end */
+ expected.clear();
+ expected.put("1/1", 0L);
+ expected.put("1/3", 5L);
+ expected.put("1/4", 4L);
+ expected.put("total/1", 0L);
+ expected.put("total/3", 5L);
+ expected.put("total/4", 4L);
+ expected.put("1", 9L);
+ expected.put("total", 9L);
+ resultMap = fModule.getCpuUsageInRange(ImmutableSet.of(1,2), 4L, 13L);
assertEquals(expected, resultMap);
}
import java.util.Map.Entry;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelAnalysisModule;
import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
return modules;
}
+ /**
+ * Gets the maximum number of cores detected
+ *
+ * @return the number of cores
+ * @since 2.0
+ */
+ public int getNumberOfCores() {
+
+ ITmfStateSystem cpuSs = getStateSystem();
+ if (cpuSs != null) {
+ try {
+ int cpusNode = cpuSs.getQuarkAbsolute(Attributes.CPUS);
+ final @NonNull List<@NonNull Integer> subAttributes = cpuSs.getSubAttributes(cpusNode, false);
+ int cpus = Integer.MIN_VALUE;
+ for (Integer quark : subAttributes) {
+ cpus = Math.max(Integer.parseInt(cpuSs.getAttributeName(quark)), cpus);
+ }
+ return Math.max(subAttributes.size(), cpus);
+ } catch (AttributeNotFoundException e) {
+ Activator.getDefault().logError(e.getMessage(), e);
+ }
+ }
+ return -1;
+
+ }
+
/**
* Get a map of time spent on CPU by various threads during a time range.
*
+ * @param cpus
+ * A set of the desired CPUs to get. An empty set gets all the
+ * cores
* @param start
* Start time of requested range
* @param end
* End time of requested range
* @return A map of TID -> time spent on CPU in the [start, end] interval
+ * @since 2.0
*/
- public Map<String, Long> getCpuUsageInRange(long start, long end) {
+ public Map<String, Long> getCpuUsageInRange(Set<@NonNull Integer> cpus, long start, long end) {
Map<String, Long> map = new HashMap<>();
Map<String, Long> totalMap = new HashMap<>();
int cpusNode = cpuSs.getQuarkAbsolute(Attributes.CPUS);
Map<Integer, List<Integer>> tidsPerCpu = new HashMap<>();
for (int cpuNode : cpuSs.getSubAttributes(cpusNode, false)) {
- tidsPerCpu.put(cpuNode, cpuSs.getSubAttributes(cpuNode, false));
+ final @NonNull List<@NonNull Integer> cpuSubAttributes = cpuSs.getSubAttributes(cpuNode, false);
+ if (cpus.isEmpty() || cpus.contains(Integer.parseInt(cpuSs.getAttributeName(cpuNode)))) {
+ tidsPerCpu.put(cpuNode, cpuSubAttributes);
+ }
}
/* Query full states at start and end times */
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
currentEnd = ss.getCurrentEndTime();
/* Initialize the data */
- Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(Math.max(start, traceStart), Math.min(end, traceEnd));
+ Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, Math.max(start, traceStart), Math.min(end, traceEnd));
Map<String, String> totalEntries = new HashMap<>();
fYValues.clear();
fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length));
prevTime = time - 1;
}
- cpuUsageMap = fModule.getCpuUsageInRange(prevTime, time);
+ cpuUsageMap = fModule.getCpuUsageInRange(Collections.EMPTY_SET, prevTime, time);
/*
* Calculate the sum of all total entries, and add a data