1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.contextswitch
;
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
;
19 import java
.util
.Collections
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
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
;
47 * Test suite for the {@link KernelContextSwitchAnalysis} class
49 * @author Matthew Khouzam
51 public class ContextSwitchProviderTest
{
53 private static final String CPU_USAGE_FILE
= "testfiles/cpu_analysis.xml";
55 private ITmfTrace fTrace
;
56 private KernelContextSwitchAnalysis fModule
;
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()) {
67 * Setup the trace for the tests
71 ITmfTrace trace
= new TmfXmlKernelTraceStub();
72 IPath filePath
= Activator
.getAbsoluteFilePath(CPU_USAGE_FILE
);
73 IStatus status
= trace
.validate(null, filePath
.toOSString());
75 fail(status
.getException().getMessage());
78 trace
.initTrace(null, filePath
.toOSString(), TmfEvent
.class);
79 } catch (TmfTraceException e
) {
82 deleteSuppFiles(trace
);
83 ((TmfTrace
) trace
).traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
85 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelContextSwitchAnalysis
.class, KernelContextSwitchAnalysis
.ID
);
86 assertNotNull(fModule
);
94 public void tearDown() {
95 deleteSuppFiles(fTrace
);
100 * Test that the analysis executes without problems
103 public void testAnalysisExecution() {
104 /* Make sure the analysis hasn't run yet */
105 assertNull(fModule
.getStateSystem());
107 /* Execute the analysis */
108 assertTrue(TmfTestHelper
.executeAnalysis(fModule
));
109 assertNotNull(fModule
.getStateSystem());
113 * Test that the state system is returned with the expected results
116 public void testReturnedStateSystem() {
118 fModule
.waitForCompletion();
119 ITmfStateSystem ss
= fModule
.getStateSystem();
121 assertEquals(1L, ss
.getStartTime());
122 assertEquals(25L, ss
.getCurrentEndTime());
125 int cpusQuark
= ss
.getQuarkAbsolute(Attributes
.CPUS
);
128 * There should be 2 CPU entries: 0 and 1 and 3 process entries
131 List
<Integer
> cpuQuarks
= ss
.getSubAttributes(cpusQuark
, false);
132 assertEquals(2, cpuQuarks
.size());
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());
141 * Query at the end and make sure all processes on all CPU have the
144 List
<ITmfStateInterval
> state
= ss
.queryFullState(25L);
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());
151 state
= ss
.queryFullState(19L);
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());
158 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
159 fail(e
.getMessage());
165 * {@link KernelContextSwitchAnalysis#getContextSwitchesRange(long, long)}
169 public void testUsageInRange() {
171 fModule
.waitForCompletion();
173 /* This range should query the total range */
174 Map
<Integer
, Long
> expected
= new HashMap
<>();
177 expected
.put(-1, 8L);
178 Map
<Integer
, Long
> resultMap
= fModule
.getContextSwitchesRange(0L, 30L);
179 assertEquals(expected
, resultMap
);
181 /* Verify a range when a process runs at the start */
185 expected
.put(-1, 1L);
186 resultMap
= fModule
.getContextSwitchesRange(22L, 25L);
187 assertEquals(expected
, resultMap
);
189 /* Verify a range when a process runs at the end */
193 expected
.put(-1, 2L);
194 resultMap
= fModule
.getContextSwitchesRange(1L, 4L);
195 assertEquals(expected
, resultMap
);
197 /* Verify a range when a process runs at start and at the end */
201 expected
.put(-1, 2L);
202 resultMap
= fModule
.getContextSwitchesRange(4L, 13L);
203 assertEquals(expected
, resultMap
);
207 * Test the usage in invalid circumstances
210 public void testInvalid() {
211 Map
<Integer
, Long
> resultMap
= fModule
.getContextSwitchesRange(0L, 30L);
212 assertEquals(Collections
.EMPTY_MAP
, resultMap
);
215 fModule
.waitForCompletion();
217 resultMap
= fModule
.getContextSwitchesRange(30L, 0L);
218 assertEquals(Collections
.EMPTY_MAP
, resultMap
);
220 resultMap
= fModule
.getContextSwitchesRange(0L, 0L);
221 assertEquals(Collections
.EMPTY_MAP
, resultMap
);
223 resultMap
= fModule
.getContextSwitchesRange(-30L, 0L);
224 assertEquals(Collections
.EMPTY_MAP
, resultMap
);