Commit | Line | Data |
---|---|---|
96811390 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
96811390 GB |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
e363eae1 | 13 | package org.eclipse.tracecompass.analysis.os.linux.core.tests.cpuusage; |
96811390 GB |
14 | |
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; | |
20 | ||
21 | import java.io.File; | |
22 | import java.util.HashMap; | |
23 | import java.util.List; | |
24 | import java.util.Map; | |
25 | ||
26 | import org.eclipse.core.runtime.IPath; | |
27 | import org.eclipse.core.runtime.IStatus; | |
e363eae1 AM |
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.KernelAnalysis; | |
31 | import org.eclipse.tracecompass.analysis.os.linux.core.tests.Activator; | |
96811390 GB |
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; | |
49 | ||
50 | /** | |
e363eae1 | 51 | * Test suite for the {@link KernelCpuUsageAnalysis} class |
96811390 GB |
52 | * |
53 | * @author Geneviève Bastien | |
54 | */ | |
55 | public class CpuUsageStateProviderTest { | |
56 | ||
57 | private static final String CPU_USAGE_FILE = "testfiles/cpu_analysis.xml"; | |
e363eae1 | 58 | |
96811390 | 59 | private ITmfTrace fTrace; |
e363eae1 | 60 | private KernelCpuUsageAnalysis fModule; |
96811390 GB |
61 | |
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()) { | |
66 | file.delete(); | |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Setup the trace for the tests | |
72 | */ | |
73 | @Before | |
74 | public void setUp() { | |
1d83ed07 | 75 | ITmfTrace trace = new TmfXmlTraceStub(); |
96811390 | 76 | IPath filePath = Activator.getAbsoluteFilePath(CPU_USAGE_FILE); |
1d83ed07 | 77 | IStatus status = trace.validate(null, filePath.toOSString()); |
96811390 GB |
78 | if (!status.isOK()) { |
79 | fail(status.getException().getMessage()); | |
80 | } | |
81 | try { | |
1d83ed07 | 82 | trace.initTrace(null, filePath.toOSString(), TmfEvent.class); |
96811390 GB |
83 | } catch (TmfTraceException e) { |
84 | fail(e.getMessage()); | |
85 | } | |
1d83ed07 AM |
86 | deleteSuppFiles(trace); |
87 | ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null)); | |
96811390 GB |
88 | /* |
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 | |
92 | */ | |
93 | IAnalysisModule module = null; | |
1d83ed07 | 94 | for (IAnalysisModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, KernelAnalysis.class)) { |
96811390 GB |
95 | module = mod; |
96 | } | |
97 | assertNotNull(module); | |
98 | module.schedule(); | |
99 | module.waitForCompletion(); | |
100 | /* End of the FIXME block */ | |
e363eae1 | 101 | |
1d83ed07 | 102 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID); |
96811390 | 103 | assertNotNull(fModule); |
1d83ed07 | 104 | fTrace = trace; |
96811390 GB |
105 | } |
106 | ||
107 | /** | |
108 | * Clean up | |
109 | */ | |
110 | @After | |
111 | public void tearDown() { | |
112 | deleteSuppFiles(fTrace); | |
113 | fTrace.dispose(); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Test that the analysis executes without problems | |
118 | */ | |
119 | @Test | |
120 | public void testAnalysisExecution() { | |
121 | /* Make sure the analysis hasn't run yet */ | |
122 | assertNull(fModule.getStateSystem()); | |
123 | ||
124 | /* Execute the analysis */ | |
125 | assertTrue(TmfTestHelper.executeAnalysis(fModule)); | |
126 | assertNotNull(fModule.getStateSystem()); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Test that the state system is returned with the expected results | |
131 | */ | |
132 | @Test | |
133 | public void testReturnedStateSystem() { | |
134 | fModule.schedule(); | |
135 | fModule.waitForCompletion(); | |
136 | ITmfStateSystem ss = fModule.getStateSystem(); | |
137 | assertNotNull(ss); | |
138 | assertEquals(1L, ss.getStartTime()); | |
139 | assertEquals(25L, ss.getCurrentEndTime()); | |
140 | ||
141 | try { | |
142 | int cpusQuark = ss.getQuarkAbsolute(Attributes.CPUS); | |
143 | ||
144 | /* | |
145 | * There should be 2 CPU entries: 0 and 1 and 3 process entries | |
146 | * under each | |
147 | */ | |
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()); | |
152 | } | |
153 | ||
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()); | |
159 | ||
160 | /* | |
161 | * Query at the end and make sure all processes on all CPU have the | |
162 | * expected values | |
163 | */ | |
8e413c2d AM |
164 | List<ITmfStateInterval> state = ss.queryFullState(25L); |
165 | ||
166 | int quark = ss.getQuarkAbsolute("CPUs", "0", "1"); | |
167 | assertEquals(0L, state.get(quark).getStateValue().unboxLong()); | |
168 | ||
169 | quark = ss.getQuarkAbsolute("CPUs", "0", "2"); | |
170 | assertEquals(19L, state.get(quark).getStateValue().unboxLong()); | |
171 | ||
172 | quark = ss.getQuarkAbsolute("CPUs", "0", "3"); | |
173 | assertEquals(5L, state.get(quark).getStateValue().unboxLong()); | |
174 | ||
175 | quark = ss.getQuarkAbsolute("CPUs", "1", "1"); | |
176 | assertEquals(5L, state.get(quark).getStateValue().unboxLong()); | |
177 | ||
178 | quark = ss.getQuarkAbsolute("CPUs", "1", "3"); | |
179 | assertEquals(6L, state.get(quark).getStateValue().unboxLong()); | |
180 | ||
181 | quark = ss.getQuarkAbsolute("CPUs", "1", "4"); | |
182 | assertEquals(8L, state.get(quark).getStateValue().unboxLong()); | |
96811390 GB |
183 | |
184 | } catch (AttributeNotFoundException | StateSystemDisposedException e) { | |
185 | fail(e.getMessage()); | |
186 | } | |
187 | } | |
188 | ||
189 | /** | |
190 | * Test the | |
e363eae1 | 191 | * {@link KernelCpuUsageAnalysis#getCpuUsageInRange(long, long)} |
96811390 GB |
192 | * method. |
193 | */ | |
194 | @Test | |
195 | public void testUsageInRange() { | |
196 | fModule.schedule(); | |
197 | fModule.waitForCompletion(); | |
198 | ||
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); | |
216 | ||
217 | /* Verify a range when a process runs at the start */ | |
218 | expected.clear(); | |
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); | |
234 | ||
235 | /* Verify a range when a process runs at the end */ | |
236 | expected.clear(); | |
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); | |
252 | ||
253 | /* Verify a range when a process runs at start and at the end */ | |
254 | expected.clear(); | |
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); | |
270 | ||
271 | } | |
272 | } |