e9870dbbf856b0f7316cec815e111724835f9591
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core.tests / src / org / eclipse / tracecompass / analysis / os / linux / core / tests / contextswitch / ContextSwitchProviderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 package org.eclipse.tracecompass.analysis.os.linux.core.tests.contextswitch;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.File;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.tracecompass.analysis.os.linux.core.contextswitch.KernelContextSwitchAnalysis;
27 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
28 import org.eclipse.tracecompass.analysis.os.linux.core.tests.Activator;
29 import org.eclipse.tracecompass.analysis.os.linux.core.tests.stubs.trace.TmfXmlKernelTraceStub;
30 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
31 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
33 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
34 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
35 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
36 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
37 import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestHelper;
38 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
39 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
40 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
41 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45
46 /**
47 * Test suite for the {@link KernelContextSwitchAnalysis} class
48 *
49 * @author Matthew Khouzam
50 */
51 public class ContextSwitchProviderTest {
52
53 private static final String CPU_USAGE_FILE = "testfiles/cpu_analysis.xml";
54
55 private ITmfTrace fTrace;
56 private KernelContextSwitchAnalysis fModule;
57
58 private static void deleteSuppFiles(ITmfTrace trace) {
59 /* Remove supplementary files */
60 File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(trace));
61 for (File file : suppDir.listFiles()) {
62 file.delete();
63 }
64 }
65
66 /**
67 * Setup the trace for the tests
68 */
69 @Before
70 public void setUp() {
71 ITmfTrace trace = new TmfXmlKernelTraceStub();
72 IPath filePath = Activator.getAbsoluteFilePath(CPU_USAGE_FILE);
73 IStatus status = trace.validate(null, filePath.toOSString());
74 if (!status.isOK()) {
75 fail(status.getException().getMessage());
76 }
77 try {
78 trace.initTrace(null, filePath.toOSString(), TmfEvent.class);
79 } catch (TmfTraceException e) {
80 fail(e.getMessage());
81 }
82 deleteSuppFiles(trace);
83 ((TmfTrace) trace).traceOpened(new TmfTraceOpenedSignal(this, trace, null));
84
85 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelContextSwitchAnalysis.class, KernelContextSwitchAnalysis.ID);
86 assertNotNull(fModule);
87 fTrace = trace;
88 }
89
90 /**
91 * Clean up
92 */
93 @After
94 public void tearDown() {
95 deleteSuppFiles(fTrace);
96 fTrace.dispose();
97 }
98
99 /**
100 * Test that the analysis executes without problems
101 */
102 @Test
103 public void testAnalysisExecution() {
104 /* Make sure the analysis hasn't run yet */
105 assertNull(fModule.getStateSystem());
106
107 /* Execute the analysis */
108 assertTrue(TmfTestHelper.executeAnalysis(fModule));
109 assertNotNull(fModule.getStateSystem());
110 }
111
112 /**
113 * Test that the state system is returned with the expected results
114 */
115 @Test
116 public void testReturnedStateSystem() {
117 fModule.schedule();
118 fModule.waitForCompletion();
119 ITmfStateSystem ss = fModule.getStateSystem();
120 assertNotNull(ss);
121 assertEquals(1L, ss.getStartTime());
122 assertEquals(25L, ss.getCurrentEndTime());
123
124 try {
125 int cpusQuark = ss.getQuarkAbsolute(Attributes.CPUS);
126
127 /*
128 * There should be 2 CPU entries: 0 and 1 and 3 process entries
129 * under each
130 */
131 List<Integer> cpuQuarks = ss.getSubAttributes(cpusQuark, false);
132 assertEquals(2, cpuQuarks.size());
133
134 /* Proc 2 on CPU 0 should run from 1 to 20 seconds */
135 int proc2Quark = ss.getQuarkAbsolute(Attributes.CPUS, "0");
136 ITmfStateInterval interval = ss.querySingleState(2L, proc2Quark);
137 assertEquals(1L, interval.getStartTime());
138 assertEquals(19L, interval.getEndTime());
139
140 /*
141 * Query at the end and make sure all processes on all CPU have the
142 * expected values
143 */
144 List<ITmfStateInterval> state = ss.queryFullState(25L);
145
146 int quark = ss.getQuarkAbsolute("CPUs", "0");
147 assertEquals(3L, state.get(quark).getStateValue().unboxLong());
148 quark = ss.getQuarkAbsolute("CPUs", "1");
149 assertEquals(5L, state.get(quark).getStateValue().unboxLong());
150
151 state = ss.queryFullState(19L);
152
153 quark = ss.getQuarkAbsolute("CPUs", "0");
154 assertEquals(1L, state.get(quark).getStateValue().unboxLong());
155 quark = ss.getQuarkAbsolute("CPUs", "1");
156 assertEquals(4L, state.get(quark).getStateValue().unboxLong());
157
158 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
159 fail(e.getMessage());
160 }
161 }
162
163 /**
164 * Test the
165 * {@link KernelContextSwitchAnalysis#getContextSwitchesRange(long, long)}
166 * method.
167 */
168 @Test
169 public void testUsageInRange() {
170 fModule.schedule();
171 fModule.waitForCompletion();
172
173 /* This range should query the total range */
174 Map<Integer, Long> expected = new HashMap<>();
175 expected.put(0, 2L);
176 expected.put(1, 6L);
177 expected.put(-1, 8L);
178 Map<Integer, Long> resultMap = fModule.getContextSwitchesRange(0L, 30L);
179 assertEquals(expected, resultMap);
180
181 /* Verify a range when a process runs at the start */
182 expected.clear();
183 expected.put(0, 1L);
184 expected.put(1, 0L);
185 expected.put(-1, 1L);
186 resultMap = fModule.getContextSwitchesRange(22L, 25L);
187 assertEquals(expected, resultMap);
188
189 /* Verify a range when a process runs at the end */
190 expected.clear();
191 expected.put(0, 0L);
192 expected.put(1, 2L);
193 expected.put(-1, 2L);
194 resultMap = fModule.getContextSwitchesRange(1L, 4L);
195 assertEquals(expected, resultMap);
196
197 /* Verify a range when a process runs at start and at the end */
198 expected.clear();
199 expected.put(0, 0L);
200 expected.put(1, 2L);
201 expected.put(-1, 2L);
202 resultMap = fModule.getContextSwitchesRange(4L, 13L);
203 assertEquals(expected, resultMap);
204 }
205
206 /**
207 * Test the usage in invalid circumstances
208 */
209 @Test
210 public void testInvalid() {
211 Map<Integer, Long> resultMap = fModule.getContextSwitchesRange(0L, 30L);
212 assertEquals(Collections.EMPTY_MAP, resultMap);
213
214 fModule.schedule();
215 fModule.waitForCompletion();
216
217 resultMap = fModule.getContextSwitchesRange(30L, 0L);
218 assertEquals(Collections.EMPTY_MAP, resultMap);
219
220 resultMap = fModule.getContextSwitchesRange(0L, 0L);
221 assertEquals(Collections.EMPTY_MAP, resultMap);
222
223 resultMap = fModule.getContextSwitchesRange(-30L, 0L);
224 assertEquals(Collections.EMPTY_MAP, resultMap);
225 }
226 }
This page took 0.037368 seconds and 4 git commands to generate.