1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.kernelanalysis
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertNull
;
19 import static org
.junit
.Assert
.assertTrue
;
20 import static org
.junit
.Assert
.fail
;
23 import java
.util
.Collection
;
24 import java
.util
.List
;
26 import org
.eclipse
.core
.runtime
.IPath
;
27 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.IStatus
;
29 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
30 import org
.eclipse
.jdt
.annotation
.NonNull
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysis
;
32 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelThreadInformationProvider
;
33 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.StateValues
;
34 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tests
.Activator
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
46 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.TmfXmlTraceStub
;
47 import org
.junit
.After
;
48 import org
.junit
.Before
;
49 import org
.junit
.Test
;
52 * Test analysis-specific methods for the {@link KernelAnalysis} class.
54 * @author Geneviève Bastien
56 public class KernelThreadInformationProviderTest
{
58 private static final @NonNull String LTTNG_KERNEL_FILE
= "testfiles/lttng_kernel_analysis.xml";
61 * The ID of the kernel analysis module for development traces
63 private static final String TEST_KERNEL_ANALYSIS_ID
= "org.eclipse.tracecompass.analysis.os.linux.tests.kernel";
65 private ITmfTrace fTrace
;
66 private KernelAnalysis fModule
;
68 private static void deleteSuppFiles(ITmfTrace trace
) {
69 /* Remove supplementary files */
70 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(trace
));
71 for (File file
: suppDir
.listFiles()) {
77 * Setup the trace for the tests
81 fTrace
= new TmfXmlTraceStub();
82 IPath filePath
= Activator
.getAbsoluteFilePath(LTTNG_KERNEL_FILE
);
83 IStatus status
= fTrace
.validate(null, filePath
.toOSString());
85 fail(status
.getException().getMessage());
88 fTrace
.initTrace(null, filePath
.toOSString(), TmfEvent
.class);
89 } catch (TmfTraceException e
) {
92 deleteSuppFiles(fTrace
);
93 ((TmfTrace
) fTrace
).traceOpened(new TmfTraceOpenedSignal(this, fTrace
, null));
94 IAnalysisModule module
= null;
95 for (IAnalysisModule mod
: TmfTraceUtils
.getAnalysisModulesOfClass(fTrace
, KernelAnalysis
.class)) {
98 assertNotNull(module
);
100 module
.waitForCompletion();
101 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(fTrace
, KernelAnalysis
.class, TEST_KERNEL_ANALYSIS_ID
);
108 public void tearDown() {
109 deleteSuppFiles(fTrace
);
115 * {@link KernelThreadInformationProvider#getThreadIds(KernelAnalysis)}
119 public void testGetThreadQuarks() {
120 KernelAnalysis module
= checkNotNull(fModule
);
121 Collection
<Integer
> threadIds
= KernelThreadInformationProvider
.getThreadIds(module
);
122 assertEquals(7, threadIds
.size());
127 * {@link KernelThreadInformationProvider#getThreadOnCpu(KernelAnalysis, long, long)}
131 public void testGetThreadOnCpu() {
132 KernelAnalysis module
= checkNotNull(fModule
);
134 /* Check with invalid timestamps */
135 Integer tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, -1);
138 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, 80);
141 /* Check with invalid cpus */
142 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 2, 20);
145 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, -1, 20);
148 /* Check valid values */
149 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, 4);
152 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, 15);
155 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 1, 15);
156 assertEquals(Integer
.valueOf(11), tid
);
158 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 1, 29);
159 assertEquals(Integer
.valueOf(20), tid
);
161 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 1, 30);
162 assertEquals(Integer
.valueOf(21), tid
);
164 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, 59);
165 assertEquals(Integer
.valueOf(11), tid
);
167 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 1, 59);
168 assertEquals(Integer
.valueOf(30), tid
);
170 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 0, 60);
171 assertEquals(Integer
.valueOf(11), tid
);
173 tid
= KernelThreadInformationProvider
.getThreadOnCpu(module
, 1, 60);
174 assertEquals(Integer
.valueOf(21), tid
);
180 * {@link KernelThreadInformationProvider#getParentPid(KernelAnalysis, Integer, long)}
184 public void testGetPpid() {
185 KernelAnalysis module
= checkNotNull(fModule
);
187 /* Check with invalid timestamps */
188 Integer ppid
= KernelThreadInformationProvider
.getParentPid(module
, 11, -1);
191 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 11, 80);
194 /* Check with invalid cpus */
195 ppid
= KernelThreadInformationProvider
.getParentPid(module
, -4, 20);
198 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 12, 20);
201 /* Check values with no parent */
202 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 10, 20);
203 assertEquals(Integer
.valueOf(0), ppid
);
205 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 30, 60);
206 assertEquals(Integer
.valueOf(0), ppid
);
208 /* Check parent determined at statedump */
209 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 11, 4);
212 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 11, 5);
213 assertEquals(Integer
.valueOf(10), ppid
);
215 /* Check parent after process fork */
216 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 21, 25);
217 assertEquals(Integer
.valueOf(20), ppid
);
219 ppid
= KernelThreadInformationProvider
.getParentPid(module
, 21, 70);
220 assertEquals(Integer
.valueOf(20), ppid
);
225 * Test the {@link KernelThreadInformationProvider#getExecutableName(KernelAnalysis, Integer)} method
228 public void testGetExecutableName() {
229 KernelAnalysis module
= checkNotNull(fModule
);
231 /* Check with invalid threads */
232 String execName
= KernelThreadInformationProvider
.getExecutableName(module
, 101);
233 assertNull(execName
);
235 execName
= KernelThreadInformationProvider
.getExecutableName(module
, -2);
236 assertNull(execName
);
238 /* Check valid value */
239 execName
= KernelThreadInformationProvider
.getExecutableName(module
, 20);
240 assertEquals("proc20", execName
);
242 /* Check valid value with process name change in history */
243 execName
= KernelThreadInformationProvider
.getExecutableName(module
, 21);
244 assertEquals("proc21", execName
);
248 private static void testIntervals(String info
, List
<ITmfStateInterval
> intervals
, ITmfStateValue
[] values
) {
249 assertEquals(info
+ " interval count", values
.length
, intervals
.size());
250 for (int i
= 0; i
< values
.length
; i
++) {
251 assertEquals(info
+ " interval " + i
, values
[i
], intervals
.get(i
).getStateValue());
257 * {@link KernelThreadInformationProvider#getStatusIntervalsForThread(KernelAnalysis, Integer, long, long, long, IProgressMonitor)}
261 public void testGetStatusIntervalsForThread() {
262 KernelAnalysis module
= checkNotNull(fModule
);
264 IProgressMonitor monitor
= new NullProgressMonitor();
265 Integer process21
= 21;
266 Integer process20
= 20;
268 /* Check invalid time ranges */
269 List
<ITmfStateInterval
> intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process21
, -15, -5, 3, monitor
);
270 assertTrue(intervals
.isEmpty());
272 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process21
, 80, 1500000000L, 50, monitor
);
273 assertTrue(intervals
.isEmpty());
275 /* Check invalid quarks */
276 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, -1, 0, 70L, 3, monitor
);
277 assertTrue(intervals
.isEmpty());
279 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, 0, 0, 70L, 3, monitor
);
280 assertTrue(intervals
.isEmpty());
282 /* Check different time ranges and resolutions */
283 ITmfStateValue
[] values
= { TmfStateValue
.nullValue(), StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
,
284 StateValues
.PROCESS_STATUS_RUN_SYSCALL_VALUE
, StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
,
285 StateValues
.PROCESS_STATUS_RUN_SYSCALL_VALUE
};
286 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process21
, 0, 70L, 3, monitor
);
287 testIntervals("tid 21 [0,70,3]", intervals
, values
);
289 ITmfStateValue
[] values2
= { TmfStateValue
.nullValue(), StateValues
.PROCESS_STATUS_RUN_SYSCALL_VALUE
,
290 StateValues
.PROCESS_STATUS_RUN_SYSCALL_VALUE
};
291 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process21
, 1, 70L, 30, monitor
);
292 testIntervals("tid 21 [0,70,30]", intervals
, values2
);
294 ITmfStateValue
[] values3
= { StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
,
295 StateValues
.PROCESS_STATUS_RUN_SYSCALL_VALUE
};
296 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process21
, 25, 50L, 3, monitor
);
297 testIntervals("tid 21 [25,50,3]", intervals
, values3
);
299 ITmfStateValue
[] values4
= { TmfStateValue
.nullValue(), StateValues
.PROCESS_STATUS_WAIT_BLOCKED_VALUE
,
300 StateValues
.PROCESS_STATUS_RUN_USERMODE_VALUE
, StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
};
301 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process20
, 0, 70L, 3, monitor
);
302 testIntervals("tid 20 [0,70,3]", intervals
, values4
);
304 ITmfStateValue
[] values5
= { TmfStateValue
.nullValue(), StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
};
305 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process20
, 1, 70L, 30, monitor
);
306 testIntervals("tid 20 [0,70,30]", intervals
, values5
);
308 ITmfStateValue
[] values6
= { StateValues
.PROCESS_STATUS_RUN_USERMODE_VALUE
,
309 StateValues
.PROCESS_STATUS_WAIT_FOR_CPU_VALUE
};
310 intervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(module
, process20
, 25, 50L, 3, monitor
);
311 testIntervals("tid 20 [25,50,3]", intervals
, values6
);