os.tests: Add a kernel stub trace class and use that in unit tests
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core.tests / src / org / eclipse / tracecompass / analysis / os / linux / core / tests / cpuusage / CpuUsageStateProviderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 É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
13 package org.eclipse.tracecompass.analysis.os.linux.core.tests.cpuusage;
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.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
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;
54
55 /**
56 * Test suite for the {@link KernelCpuUsageAnalysis} class
57 *
58 * @author Geneviève Bastien
59 */
60 public class CpuUsageStateProviderTest {
61
62 private static final String CPU_USAGE_FILE = "testfiles/cpu_analysis.xml";
63
64 private ITmfTrace fTrace;
65 private KernelCpuUsageAnalysis fModule;
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 ITmfTrace trace = new TmfXmlKernelTraceStub();
81 IPath filePath = Activator.getAbsoluteFilePath(CPU_USAGE_FILE);
82 IStatus status = trace.validate(null, filePath.toOSString());
83 if (!status.isOK()) {
84 fail(status.getException().getMessage());
85 }
86 try {
87 trace.initTrace(null, filePath.toOSString(), TmfEvent.class);
88 } catch (TmfTraceException e) {
89 fail(e.getMessage());
90 }
91 deleteSuppFiles(trace);
92 ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, 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;
99 for (IAnalysisModule mod : TmfTraceUtils.getAnalysisModulesOfClass(trace, KernelAnalysisModule.class)) {
100 module = mod;
101 }
102 assertNotNull(module);
103 module.schedule();
104 module.waitForCompletion();
105 /* End of the FIXME block */
106
107 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
108 assertNotNull(fModule);
109 fTrace = trace;
110 }
111
112 /**
113 * Clean up
114 */
115 @After
116 public void tearDown() {
117 deleteSuppFiles(fTrace);
118 fTrace.dispose();
119 }
120
121 /**
122 * Test that the analysis executes without problems
123 */
124 @Test
125 public void testAnalysisExecution() {
126 /* Make sure the analysis hasn't run yet */
127 assertNull(fModule.getStateSystem());
128
129 /* Execute the analysis */
130 assertTrue(TmfTestHelper.executeAnalysis(fModule));
131 assertNotNull(fModule.getStateSystem());
132 }
133
134 /**
135 * Test that the state system is returned with the expected results
136 */
137 @Test
138 public void testReturnedStateSystem() {
139 fModule.schedule();
140 fModule.waitForCompletion();
141 ITmfStateSystem ss = fModule.getStateSystem();
142 assertNotNull(ss);
143 assertEquals(1L, ss.getStartTime());
144 assertEquals(25L, ss.getCurrentEndTime());
145
146 try {
147 int cpusQuark = ss.getQuarkAbsolute(Attributes.CPUS);
148
149 /*
150 * There should be 2 CPU entries: 0 and 1 and 3 process entries
151 * under each
152 */
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());
157 }
158
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");
164
165 /* Test the intervals of proc 4 CPU 1 */
166 intervals.clear();
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");
171
172 /* Test the intervals of proc 3 on both CPUs */
173 intervals.clear();
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");
177
178 intervals.clear();
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");
183
184 /*
185 * Query at the end and make sure all processes on all CPU have the
186 * expected values
187 */
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);
196
197 } catch (AttributeNotFoundException e) {
198 fail(e.getMessage());
199 }
200 }
201
202 /**
203 * Test the {@link KernelCpuUsageAnalysis#getCpuUsageInRange(long, long)}
204 * method.
205 */
206 @Test
207 public void testUsageInRange() {
208 fModule.schedule();
209 fModule.waitForCompletion();
210
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);
228
229 /* Verify a range when a process runs at the start */
230 expected.clear();
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);
246
247 /* Verify a range when a process runs at the end */
248 expected.clear();
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);
264
265 /* Verify a range when a process runs at start and at the end */
266 expected.clear();
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);
282
283 }
284 }
This page took 0.039667 seconds and 5 git commands to generate.