lttng: Add unit tests for the CPU usage analysis
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core.tests / src / org / eclipse / tracecompass / lttng2 / kernel / core / tests / analysis / cpuusage / CpuUsageStateProviderTest.java
CommitLineData
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
13package org.eclipse.tracecompass.lttng2.kernel.core.tests.analysis.cpuusage;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
20
21import java.io.File;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26import org.eclipse.core.runtime.IPath;
27import org.eclipse.core.runtime.IStatus;
28import org.eclipse.tracecompass.internal.lttng2.kernel.core.Attributes;
29import org.eclipse.tracecompass.lttng2.kernel.core.analysis.cpuusage.LttngKernelCpuUsageAnalysis;
30import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelAnalysis;
31import org.eclipse.tracecompass.lttng2.kernel.core.tests.Activator;
32import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
33import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
34import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
35import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
36import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
37import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
38import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
39import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
40import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestHelper;
41import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
42import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
43import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
44import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
45import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
46import org.junit.After;
47import org.junit.Before;
48import org.junit.Test;
49
50/**
51 * Test suite for the {@link LttngKernelCpuUsageAnalysis} class
52 *
53 * @author Geneviève Bastien
54 */
55public class CpuUsageStateProviderTest {
56
57 private static final String CPU_USAGE_FILE = "testfiles/cpu_analysis.xml";
58 /**
59 * The ID of the cpu usage analysis module for development traces
60 */
61 public static final String CPU_USAGE_ANALYSIS_ID = "lttng2.kernel.core.tests.cpuusage";
62
63 private ITmfTrace fTrace;
64 private LttngKernelCpuUsageAnalysis fModule;
65
66 private static void deleteSuppFiles(ITmfTrace trace) {
67 /* Remove supplementary files */
68 File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(trace));
69 for (File file : suppDir.listFiles()) {
70 file.delete();
71 }
72 }
73
74 /**
75 * Setup the trace for the tests
76 */
77 @Before
78 public void setUp() {
79 fTrace = new TmfXmlTraceStub();
80 IPath filePath = Activator.getAbsoluteFilePath(CPU_USAGE_FILE);
81 IStatus status = fTrace.validate(null, filePath.toOSString());
82 if (!status.isOK()) {
83 fail(status.getException().getMessage());
84 }
85 try {
86 fTrace.initTrace(null, filePath.toOSString(), TmfEvent.class);
87 } catch (TmfTraceException e) {
88 fail(e.getMessage());
89 }
90 deleteSuppFiles(fTrace);
91 ((TmfTrace) fTrace).traceOpened(new TmfTraceOpenedSignal(this, fTrace, null));
92 /*
93 * FIXME: Make sure this analysis is finished before running the CPU
94 * analysis. This block can be removed once analysis dependency and
95 * request precedence is implemented
96 */
97 IAnalysisModule module = null;
98 for (IAnalysisModule mod : TmfTraceUtils.getAnalysisModulesOfClass(fTrace, LttngKernelAnalysis.class)) {
99 module = mod;
100 }
101 assertNotNull(module);
102 module.schedule();
103 module.waitForCompletion();
104 /* End of the FIXME block */
105 fModule = TmfTraceUtils.getAnalysisModuleOfClass(fTrace, LttngKernelCpuUsageAnalysis.class, CPU_USAGE_ANALYSIS_ID);
106 assertNotNull(fModule);
107 }
108
109 /**
110 * Clean up
111 */
112 @After
113 public void tearDown() {
114 deleteSuppFiles(fTrace);
115 fTrace.dispose();
116 }
117
118 /**
119 * Test that the analysis executes without problems
120 */
121 @Test
122 public void testAnalysisExecution() {
123 /* Make sure the analysis hasn't run yet */
124 assertNull(fModule.getStateSystem());
125
126 /* Execute the analysis */
127 assertTrue(TmfTestHelper.executeAnalysis(fModule));
128 assertNotNull(fModule.getStateSystem());
129 }
130
131 /**
132 * Test that the state system is returned with the expected results
133 */
134 @Test
135 public void testReturnedStateSystem() {
136 fModule.schedule();
137 fModule.waitForCompletion();
138 ITmfStateSystem ss = fModule.getStateSystem();
139 assertNotNull(ss);
140 assertEquals(1L, ss.getStartTime());
141 assertEquals(25L, ss.getCurrentEndTime());
142
143 try {
144 int cpusQuark = ss.getQuarkAbsolute(Attributes.CPUS);
145
146 /*
147 * There should be 2 CPU entries: 0 and 1 and 3 process entries
148 * under each
149 */
150 List<Integer> cpuQuarks = ss.getSubAttributes(cpusQuark, false);
151 assertEquals(2, cpuQuarks.size());
152 for (Integer cpuQuark : cpuQuarks) {
153 assertEquals(3, ss.getSubAttributes(cpuQuark, false).size());
154 }
155
156 /* Proc 2 on CPU 0 should run from 1 to 20 seconds */
157 int proc2Quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", "2");
158 ITmfStateInterval interval = ss.querySingleState(2L, proc2Quark);
159 assertEquals(1L, interval.getStartTime());
160 assertEquals(19L, interval.getEndTime());
161
162 /*
163 * Query at the end and make sure all processes on all CPU have the
164 * expected values
165 */
166 Map<String, Long> expected = new HashMap<>();
167 expected.put("CPUs/0/1", 0L);
168 expected.put("CPUs/0/2", 19L);
169 expected.put("CPUs/0/3", 5L);
170 expected.put("CPUs/1/1", 5L);
171 expected.put("CPUs/1/3", 6L);
172 expected.put("CPUs/1/4", 8L);
173 List<ITmfStateInterval> intervals = ss.queryFullState(25L);
174 Map<String, Long> intervalMap = new HashMap<>();
175 for (ITmfStateInterval oneInterval : intervals) {
176 if (!oneInterval.getStateValue().isNull()) {
177 intervalMap.put(ss.getFullAttributePath(oneInterval.getAttribute()), oneInterval.getStateValue().unboxLong());
178 }
179 }
180 assertEquals(expected, intervalMap);
181
182 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
183 fail(e.getMessage());
184 }
185 }
186
187 /**
188 * Test the
189 * {@link LttngKernelCpuUsageAnalysis#getCpuUsageInRange(long, long)}
190 * method.
191 */
192 @Test
193 public void testUsageInRange() {
194 fModule.schedule();
195 fModule.waitForCompletion();
196
197 /* This range should query the total range */
198 Map<String, Long> expected = new HashMap<>();
199 expected.put("0/1", 0L);
200 expected.put("0/2", 19L);
201 expected.put("0/3", 5L);
202 expected.put("1/1", 5L);
203 expected.put("1/3", 6L);
204 expected.put("1/4", 13L);
205 expected.put("total", 48L);
206 expected.put("total/1", 5L);
207 expected.put("total/2", 19L);
208 expected.put("total/3", 11L);
209 expected.put("total/4", 13L);
210 expected.put("0", 24L);
211 expected.put("1", 24L);
212 Map<String, Long> resultMap = fModule.getCpuUsageInRange(0L, 30L);
213 assertEquals(expected, resultMap);
214
215 /* Verify a range when a process runs at the start */
216 expected.clear();
217 expected.put("0/1", 0L);
218 expected.put("0/2", 0L);
219 expected.put("0/3", 3L);
220 expected.put("1/1", 0L);
221 expected.put("1/3", 0L);
222 expected.put("1/4", 3L);
223 expected.put("total", 6L);
224 expected.put("total/1", 0L);
225 expected.put("total/2", 0L);
226 expected.put("total/3", 3L);
227 expected.put("total/4", 3L);
228 expected.put("0", 3L);
229 expected.put("1", 3L);
230 resultMap = fModule.getCpuUsageInRange(22L, 25L);
231 assertEquals(expected, resultMap);
232
233 /* Verify a range when a process runs at the end */
234 expected.clear();
235 expected.put("0/1", 0L);
236 expected.put("0/2", 3L);
237 expected.put("0/3", 0L);
238 expected.put("1/1", 0L);
239 expected.put("1/3", 1L);
240 expected.put("1/4", 2L);
241 expected.put("total", 6L);
242 expected.put("total/1", 0L);
243 expected.put("total/2", 3L);
244 expected.put("total/3", 1L);
245 expected.put("total/4", 2L);
246 expected.put("0", 3L);
247 expected.put("1", 3L);
248 resultMap = fModule.getCpuUsageInRange(1L, 4L);
249 assertEquals(expected, resultMap);
250
251 /* Verify a range when a process runs at start and at the end */
252 expected.clear();
253 expected.put("0/1", 0L);
254 expected.put("0/2", 9L);
255 expected.put("0/3", 0L);
256 expected.put("1/1", 0L);
257 expected.put("1/3", 5L);
258 expected.put("1/4", 4L);
259 expected.put("total", 18L);
260 expected.put("total/1", 0L);
261 expected.put("total/2", 9L);
262 expected.put("total/3", 5L);
263 expected.put("total/4", 4L);
264 expected.put("0", 9L);
265 expected.put("1", 9L);
266 resultMap = fModule.getCpuUsageInRange(4L, 13L);
267 assertEquals(expected, resultMap);
268
269 }
270}
This page took 0.035887 seconds and 5 git commands to generate.