Commit | Line | Data |
---|---|---|
96811390 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
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 GB |
59 | /** |
60 | * The ID of the cpu usage analysis module for development traces | |
61 | */ | |
e363eae1 | 62 | private static final String TEST_CPU_USAGE_ANALYSIS_ID = "org.eclipse.tracecompass.analysis.os.linux.tests.cpuusage"; |
96811390 GB |
63 | |
64 | private ITmfTrace fTrace; | |
e363eae1 | 65 | private KernelCpuUsageAnalysis fModule; |
96811390 GB |
66 | |
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()) { | |
71 | file.delete(); | |
72 | } | |
73 | } | |
74 | ||
75 | /** | |
76 | * Setup the trace for the tests | |
77 | */ | |
78 | @Before | |
79 | public void setUp() { | |
80 | fTrace = new TmfXmlTraceStub(); | |
81 | IPath filePath = Activator.getAbsoluteFilePath(CPU_USAGE_FILE); | |
82 | IStatus status = fTrace.validate(null, filePath.toOSString()); | |
83 | if (!status.isOK()) { | |
84 | fail(status.getException().getMessage()); | |
85 | } | |
86 | try { | |
87 | fTrace.initTrace(null, filePath.toOSString(), TmfEvent.class); | |
88 | } catch (TmfTraceException e) { | |
89 | fail(e.getMessage()); | |
90 | } | |
91 | deleteSuppFiles(fTrace); | |
92 | ((TmfTrace) fTrace).traceOpened(new TmfTraceOpenedSignal(this, fTrace, null)); | |
93 | /* | |
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 | |
97 | */ | |
98 | IAnalysisModule module = null; | |
e363eae1 | 99 | for (IAnalysisModule mod : TmfTraceUtils.getAnalysisModulesOfClass(fTrace, KernelAnalysis.class)) { |
96811390 GB |
100 | module = mod; |
101 | } | |
102 | assertNotNull(module); | |
103 | module.schedule(); | |
104 | module.waitForCompletion(); | |
105 | /* End of the FIXME block */ | |
e363eae1 AM |
106 | |
107 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(fTrace, KernelCpuUsageAnalysis.class, TEST_CPU_USAGE_ANALYSIS_ID); | |
96811390 GB |
108 | assertNotNull(fModule); |
109 | } | |
110 | ||
111 | /** | |
112 | * Clean up | |
113 | */ | |
114 | @After | |
115 | public void tearDown() { | |
116 | deleteSuppFiles(fTrace); | |
117 | fTrace.dispose(); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Test that the analysis executes without problems | |
122 | */ | |
123 | @Test | |
124 | public void testAnalysisExecution() { | |
125 | /* Make sure the analysis hasn't run yet */ | |
126 | assertNull(fModule.getStateSystem()); | |
127 | ||
128 | /* Execute the analysis */ | |
129 | assertTrue(TmfTestHelper.executeAnalysis(fModule)); | |
130 | assertNotNull(fModule.getStateSystem()); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Test that the state system is returned with the expected results | |
135 | */ | |
136 | @Test | |
137 | public void testReturnedStateSystem() { | |
138 | fModule.schedule(); | |
139 | fModule.waitForCompletion(); | |
140 | ITmfStateSystem ss = fModule.getStateSystem(); | |
141 | assertNotNull(ss); | |
142 | assertEquals(1L, ss.getStartTime()); | |
143 | assertEquals(25L, ss.getCurrentEndTime()); | |
144 | ||
145 | try { | |
146 | int cpusQuark = ss.getQuarkAbsolute(Attributes.CPUS); | |
147 | ||
148 | /* | |
149 | * There should be 2 CPU entries: 0 and 1 and 3 process entries | |
150 | * under each | |
151 | */ | |
152 | List<Integer> cpuQuarks = ss.getSubAttributes(cpusQuark, false); | |
153 | assertEquals(2, cpuQuarks.size()); | |
154 | for (Integer cpuQuark : cpuQuarks) { | |
155 | assertEquals(3, ss.getSubAttributes(cpuQuark, false).size()); | |
156 | } | |
157 | ||
158 | /* Proc 2 on CPU 0 should run from 1 to 20 seconds */ | |
159 | int proc2Quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", "2"); | |
160 | ITmfStateInterval interval = ss.querySingleState(2L, proc2Quark); | |
161 | assertEquals(1L, interval.getStartTime()); | |
162 | assertEquals(19L, interval.getEndTime()); | |
163 | ||
164 | /* | |
165 | * Query at the end and make sure all processes on all CPU have the | |
166 | * expected values | |
167 | */ | |
168 | Map<String, Long> expected = new HashMap<>(); | |
169 | expected.put("CPUs/0/1", 0L); | |
170 | expected.put("CPUs/0/2", 19L); | |
171 | expected.put("CPUs/0/3", 5L); | |
172 | expected.put("CPUs/1/1", 5L); | |
173 | expected.put("CPUs/1/3", 6L); | |
174 | expected.put("CPUs/1/4", 8L); | |
175 | List<ITmfStateInterval> intervals = ss.queryFullState(25L); | |
176 | Map<String, Long> intervalMap = new HashMap<>(); | |
177 | for (ITmfStateInterval oneInterval : intervals) { | |
178 | if (!oneInterval.getStateValue().isNull()) { | |
179 | intervalMap.put(ss.getFullAttributePath(oneInterval.getAttribute()), oneInterval.getStateValue().unboxLong()); | |
180 | } | |
181 | } | |
182 | assertEquals(expected, intervalMap); | |
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 | } |