1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.cpuusage
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertNull
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
22 import java
.util
.ArrayList
;
23 import java
.util
.HashMap
;
24 import java
.util
.List
;
27 import org
.eclipse
.core
.runtime
.IPath
;
28 import org
.eclipse
.core
.runtime
.IStatus
;
29 import org
.eclipse
.jdt
.annotation
.NonNull
;
30 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.cpuusage
.KernelCpuUsageAnalysis
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
32 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysisModule
;
33 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.Activator
;
34 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.stubs
.trace
.TmfXmlKernelTraceStub
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.tests
.shared
.utils
.StateIntervalStub
;
41 import org
.eclipse
.tracecompass
.statesystem
.core
.tests
.shared
.utils
.StateSystemTestUtils
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestHelper
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
51 import org
.junit
.After
;
52 import org
.junit
.Before
;
53 import org
.junit
.Test
;
56 * Test suite for the {@link KernelCpuUsageAnalysis} class
58 * @author Geneviève Bastien
60 public class CpuUsageStateProviderTest
{
62 private static final String CPU_USAGE_FILE
= "testfiles/cpu_analysis.xml";
64 private ITmfTrace fTrace
;
65 private KernelCpuUsageAnalysis fModule
;
67 private static void deleteSuppFiles(ITmfTrace trace
) {
68 /* Remove supplementary files */
69 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(trace
));
70 for (File file
: suppDir
.listFiles()) {
76 * Setup the trace for the tests
80 ITmfTrace trace
= new TmfXmlKernelTraceStub();
81 IPath filePath
= Activator
.getAbsoluteFilePath(CPU_USAGE_FILE
);
82 IStatus status
= trace
.validate(null, filePath
.toOSString());
84 fail(status
.getException().getMessage());
87 trace
.initTrace(null, filePath
.toOSString(), TmfEvent
.class);
88 } catch (TmfTraceException e
) {
91 deleteSuppFiles(trace
);
92 ((TmfTrace
) trace
).traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
94 * FIXME: Make sure this analysis is finished before running the CPU
95 * analysis. This block can be removed once analysis dependency and
96 * request precedence is implemented
98 IAnalysisModule module
= null;
99 for (IAnalysisModule mod
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, KernelAnalysisModule
.class)) {
102 assertNotNull(module
);
104 module
.waitForCompletion();
105 /* End of the FIXME block */
107 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelCpuUsageAnalysis
.class, KernelCpuUsageAnalysis
.ID
);
108 assertNotNull(fModule
);
116 public void tearDown() {
117 deleteSuppFiles(fTrace
);
122 * Test that the analysis executes without problems
125 public void testAnalysisExecution() {
126 /* Make sure the analysis hasn't run yet */
127 assertNull(fModule
.getStateSystem());
129 /* Execute the analysis */
130 assertTrue(TmfTestHelper
.executeAnalysis(fModule
));
131 assertNotNull(fModule
.getStateSystem());
135 * Test that the state system is returned with the expected results
138 public void testReturnedStateSystem() {
140 fModule
.waitForCompletion();
141 ITmfStateSystem ss
= fModule
.getStateSystem();
143 assertEquals(1L, ss
.getStartTime());
144 assertEquals(25L, ss
.getCurrentEndTime());
147 int cpusQuark
= ss
.getQuarkAbsolute(Attributes
.CPUS
);
150 * There should be 2 CPU entries: 0 and 1 and 3 process entries
153 List
<Integer
> cpuQuarks
= ss
.getSubAttributes(cpusQuark
, false);
154 assertEquals(2, cpuQuarks
.size());
155 for (Integer cpuQuark
: cpuQuarks
) {
156 assertEquals(3, ss
.getSubAttributes(cpuQuark
, false).size());
159 /* Test the intervals of proc2 on CPU 0 */
160 List
<@NonNull ITmfStateInterval
> intervals
= new ArrayList
<>();
161 intervals
.add(new StateIntervalStub(1, 19, TmfStateValue
.nullValue()));
162 intervals
.add(new StateIntervalStub(20, 25, TmfStateValue
.newValueLong(19L)));
163 StateSystemTestUtils
.testIntervalForAttributes(ss
, intervals
, Attributes
.CPUS
, "0", "2");
165 /* Test the intervals of proc 4 CPU 1 */
167 intervals
.add(new StateIntervalStub(1, 4, TmfStateValue
.nullValue()));
168 intervals
.add(new StateIntervalStub(5, 14, TmfStateValue
.newValueLong(3L)));
169 intervals
.add(new StateIntervalStub(15, 25, TmfStateValue
.newValueLong(8L)));
170 StateSystemTestUtils
.testIntervalForAttributes(ss
, intervals
, Attributes
.CPUS
, "1", "4");
172 /* Test the intervals of proc 3 on both CPUs */
174 intervals
.add(new StateIntervalStub(1, 24, TmfStateValue
.nullValue()));
175 intervals
.add(new StateIntervalStub(25, 25, TmfStateValue
.newValueLong(5L)));
176 StateSystemTestUtils
.testIntervalForAttributes(ss
, intervals
, Attributes
.CPUS
, "0", "3");
179 intervals
.add(new StateIntervalStub(1, 1, TmfStateValue
.nullValue()));
180 intervals
.add(new StateIntervalStub(2, 9, TmfStateValue
.newValueLong(1L)));
181 intervals
.add(new StateIntervalStub(10, 25, TmfStateValue
.newValueLong(6L)));
182 StateSystemTestUtils
.testIntervalForAttributes(ss
, intervals
, Attributes
.CPUS
, "1", "3");
185 * Query at the end and make sure all processes on all CPU have the
188 Map
<@NonNull String
@NonNull [], @NonNull ITmfStateValue
> map
= new HashMap
<>();
189 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "0", "1"), TmfStateValue
.newValueLong(0L));
190 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "0", "2"), TmfStateValue
.newValueLong(19L));
191 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "0", "3"), TmfStateValue
.newValueLong(5L));
192 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "1", "1"), TmfStateValue
.newValueLong(5L));
193 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "1", "3"), TmfStateValue
.newValueLong(6L));
194 map
.put(StateSystemTestUtils
.makeAttribute(Attributes
.CPUS
, "1", "4"), TmfStateValue
.newValueLong(8L));
195 StateSystemTestUtils
.testValuesAtTime(ss
, 25L, map
);
197 } catch (AttributeNotFoundException e
) {
198 fail(e
.getMessage());
203 * Test the {@link KernelCpuUsageAnalysis#getCpuUsageInRange(long, long)}
207 public void testUsageInRange() {
209 fModule
.waitForCompletion();
211 /* This range should query the total range */
212 Map
<String
, Long
> expected
= new HashMap
<>();
213 expected
.put("0/1", 0L);
214 expected
.put("0/2", 19L);
215 expected
.put("0/3", 5L);
216 expected
.put("1/1", 5L);
217 expected
.put("1/3", 6L);
218 expected
.put("1/4", 13L);
219 expected
.put("total", 48L);
220 expected
.put("total/1", 5L);
221 expected
.put("total/2", 19L);
222 expected
.put("total/3", 11L);
223 expected
.put("total/4", 13L);
224 expected
.put("0", 24L);
225 expected
.put("1", 24L);
226 Map
<String
, Long
> resultMap
= fModule
.getCpuUsageInRange(0L, 30L);
227 assertEquals(expected
, resultMap
);
229 /* Verify a range when a process runs at the start */
231 expected
.put("0/1", 0L);
232 expected
.put("0/2", 0L);
233 expected
.put("0/3", 3L);
234 expected
.put("1/1", 0L);
235 expected
.put("1/3", 0L);
236 expected
.put("1/4", 3L);
237 expected
.put("total", 6L);
238 expected
.put("total/1", 0L);
239 expected
.put("total/2", 0L);
240 expected
.put("total/3", 3L);
241 expected
.put("total/4", 3L);
242 expected
.put("0", 3L);
243 expected
.put("1", 3L);
244 resultMap
= fModule
.getCpuUsageInRange(22L, 25L);
245 assertEquals(expected
, resultMap
);
247 /* Verify a range when a process runs at the end */
249 expected
.put("0/1", 0L);
250 expected
.put("0/2", 3L);
251 expected
.put("0/3", 0L);
252 expected
.put("1/1", 0L);
253 expected
.put("1/3", 1L);
254 expected
.put("1/4", 2L);
255 expected
.put("total", 6L);
256 expected
.put("total/1", 0L);
257 expected
.put("total/2", 3L);
258 expected
.put("total/3", 1L);
259 expected
.put("total/4", 2L);
260 expected
.put("0", 3L);
261 expected
.put("1", 3L);
262 resultMap
= fModule
.getCpuUsageInRange(1L, 4L);
263 assertEquals(expected
, resultMap
);
265 /* Verify a range when a process runs at start and at the end */
267 expected
.put("0/1", 0L);
268 expected
.put("0/2", 9L);
269 expected
.put("0/3", 0L);
270 expected
.put("1/1", 0L);
271 expected
.put("1/3", 5L);
272 expected
.put("1/4", 4L);
273 expected
.put("total", 18L);
274 expected
.put("total/1", 0L);
275 expected
.put("total/2", 9L);
276 expected
.put("total/3", 5L);
277 expected
.put("total/4", 4L);
278 expected
.put("0", 9L);
279 expected
.put("1", 9L);
280 resultMap
= fModule
.getCpuUsageInRange(4L, 13L);
281 assertEquals(expected
, resultMap
);