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
.HashMap
;
23 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.IStatus
;
28 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.cpuusage
.KernelCpuUsageAnalysis
;
29 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
30 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysisModule
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.Activator
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestHelper
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
45 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.TmfXmlTraceStub
;
46 import org
.junit
.After
;
47 import org
.junit
.Before
;
48 import org
.junit
.Test
;
51 * Test suite for the {@link KernelCpuUsageAnalysis} class
53 * @author Geneviève Bastien
55 public class CpuUsageStateProviderTest
{
57 private static final String CPU_USAGE_FILE
= "testfiles/cpu_analysis.xml";
59 private ITmfTrace fTrace
;
60 private KernelCpuUsageAnalysis fModule
;
62 private static void deleteSuppFiles(ITmfTrace trace
) {
63 /* Remove supplementary files */
64 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(trace
));
65 for (File file
: suppDir
.listFiles()) {
71 * Setup the trace for the tests
75 ITmfTrace trace
= new TmfXmlTraceStub();
76 IPath filePath
= Activator
.getAbsoluteFilePath(CPU_USAGE_FILE
);
77 IStatus status
= trace
.validate(null, filePath
.toOSString());
79 fail(status
.getException().getMessage());
82 trace
.initTrace(null, filePath
.toOSString(), TmfEvent
.class);
83 } catch (TmfTraceException e
) {
86 deleteSuppFiles(trace
);
87 ((TmfTrace
) trace
).traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
89 * FIXME: Make sure this analysis is finished before running the CPU
90 * analysis. This block can be removed once analysis dependency and
91 * request precedence is implemented
93 IAnalysisModule module
= null;
94 for (IAnalysisModule mod
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, KernelAnalysisModule
.class)) {
97 assertNotNull(module
);
99 module
.waitForCompletion();
100 /* End of the FIXME block */
102 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelCpuUsageAnalysis
.class, KernelCpuUsageAnalysis
.ID
);
103 assertNotNull(fModule
);
111 public void tearDown() {
112 deleteSuppFiles(fTrace
);
117 * Test that the analysis executes without problems
120 public void testAnalysisExecution() {
121 /* Make sure the analysis hasn't run yet */
122 assertNull(fModule
.getStateSystem());
124 /* Execute the analysis */
125 assertTrue(TmfTestHelper
.executeAnalysis(fModule
));
126 assertNotNull(fModule
.getStateSystem());
130 * Test that the state system is returned with the expected results
133 public void testReturnedStateSystem() {
135 fModule
.waitForCompletion();
136 ITmfStateSystem ss
= fModule
.getStateSystem();
138 assertEquals(1L, ss
.getStartTime());
139 assertEquals(25L, ss
.getCurrentEndTime());
142 int cpusQuark
= ss
.getQuarkAbsolute(Attributes
.CPUS
);
145 * There should be 2 CPU entries: 0 and 1 and 3 process entries
148 List
<Integer
> cpuQuarks
= ss
.getSubAttributes(cpusQuark
, false);
149 assertEquals(2, cpuQuarks
.size());
150 for (Integer cpuQuark
: cpuQuarks
) {
151 assertEquals(3, ss
.getSubAttributes(cpuQuark
, false).size());
154 /* Proc 2 on CPU 0 should run from 1 to 20 seconds */
155 int proc2Quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", "2");
156 ITmfStateInterval interval
= ss
.querySingleState(2L, proc2Quark
);
157 assertEquals(1L, interval
.getStartTime());
158 assertEquals(19L, interval
.getEndTime());
161 * Query at the end and make sure all processes on all CPU have the
164 List
<ITmfStateInterval
> state
= ss
.queryFullState(25L);
166 int quark
= ss
.getQuarkAbsolute("CPUs", "0", "1");
167 assertEquals(0L, state
.get(quark
).getStateValue().unboxLong());
169 quark
= ss
.getQuarkAbsolute("CPUs", "0", "2");
170 assertEquals(19L, state
.get(quark
).getStateValue().unboxLong());
172 quark
= ss
.getQuarkAbsolute("CPUs", "0", "3");
173 assertEquals(5L, state
.get(quark
).getStateValue().unboxLong());
175 quark
= ss
.getQuarkAbsolute("CPUs", "1", "1");
176 assertEquals(5L, state
.get(quark
).getStateValue().unboxLong());
178 quark
= ss
.getQuarkAbsolute("CPUs", "1", "3");
179 assertEquals(6L, state
.get(quark
).getStateValue().unboxLong());
181 quark
= ss
.getQuarkAbsolute("CPUs", "1", "4");
182 assertEquals(8L, state
.get(quark
).getStateValue().unboxLong());
184 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
185 fail(e
.getMessage());
191 * {@link KernelCpuUsageAnalysis#getCpuUsageInRange(long, long)}
195 public void testUsageInRange() {
197 fModule
.waitForCompletion();
199 /* This range should query the total range */
200 Map
<String
, Long
> expected
= new HashMap
<>();
201 expected
.put("0/1", 0L);
202 expected
.put("0/2", 19L);
203 expected
.put("0/3", 5L);
204 expected
.put("1/1", 5L);
205 expected
.put("1/3", 6L);
206 expected
.put("1/4", 13L);
207 expected
.put("total", 48L);
208 expected
.put("total/1", 5L);
209 expected
.put("total/2", 19L);
210 expected
.put("total/3", 11L);
211 expected
.put("total/4", 13L);
212 expected
.put("0", 24L);
213 expected
.put("1", 24L);
214 Map
<String
, Long
> resultMap
= fModule
.getCpuUsageInRange(0L, 30L);
215 assertEquals(expected
, resultMap
);
217 /* Verify a range when a process runs at the start */
219 expected
.put("0/1", 0L);
220 expected
.put("0/2", 0L);
221 expected
.put("0/3", 3L);
222 expected
.put("1/1", 0L);
223 expected
.put("1/3", 0L);
224 expected
.put("1/4", 3L);
225 expected
.put("total", 6L);
226 expected
.put("total/1", 0L);
227 expected
.put("total/2", 0L);
228 expected
.put("total/3", 3L);
229 expected
.put("total/4", 3L);
230 expected
.put("0", 3L);
231 expected
.put("1", 3L);
232 resultMap
= fModule
.getCpuUsageInRange(22L, 25L);
233 assertEquals(expected
, resultMap
);
235 /* Verify a range when a process runs at the end */
237 expected
.put("0/1", 0L);
238 expected
.put("0/2", 3L);
239 expected
.put("0/3", 0L);
240 expected
.put("1/1", 0L);
241 expected
.put("1/3", 1L);
242 expected
.put("1/4", 2L);
243 expected
.put("total", 6L);
244 expected
.put("total/1", 0L);
245 expected
.put("total/2", 3L);
246 expected
.put("total/3", 1L);
247 expected
.put("total/4", 2L);
248 expected
.put("0", 3L);
249 expected
.put("1", 3L);
250 resultMap
= fModule
.getCpuUsageInRange(1L, 4L);
251 assertEquals(expected
, resultMap
);
253 /* Verify a range when a process runs at start and at the end */
255 expected
.put("0/1", 0L);
256 expected
.put("0/2", 9L);
257 expected
.put("0/3", 0L);
258 expected
.put("1/1", 0L);
259 expected
.put("1/3", 5L);
260 expected
.put("1/4", 4L);
261 expected
.put("total", 18L);
262 expected
.put("total/1", 0L);
263 expected
.put("total/2", 9L);
264 expected
.put("total/3", 5L);
265 expected
.put("total/4", 4L);
266 expected
.put("0", 9L);
267 expected
.put("1", 9L);
268 resultMap
= fModule
.getCpuUsageInRange(4L, 13L);
269 assertEquals(expected
, resultMap
);