1 /*******************************************************************************
2 * Copyright (c) 2014, 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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.lttng2
.ust
.core
.tests
.callstack
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertNotNull
;
19 import static org
.junit
.Assert
.assertTrue
;
20 import static org
.junit
.Assert
.fail
;
23 import java
.util
.List
;
24 import java
.util
.concurrent
.TimeUnit
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.callstack
.LttngUstCallStackProvider
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
34 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
39 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
40 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
41 import org
.junit
.After
;
42 import org
.junit
.Before
;
43 import org
.junit
.Rule
;
44 import org
.junit
.Test
;
45 import org
.junit
.rules
.TestRule
;
46 import org
.junit
.rules
.Timeout
;
49 * Base class for the UST callstack state provider tests.
51 * @author Alexandre Montplaisir
53 public abstract class AbstractProviderTest
{
55 /** Time-out tests after 1 minute. */
57 public TestRule globalTimeout
= new Timeout(1, TimeUnit
.MINUTES
);
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private static final @NonNull CtfTestTrace otherUstTrace
= CtfTestTrace
.HELLO_LOST
;
65 private CtfTmfTrace fTrace
= null;
66 private ITmfStateSystem fSS
= null;
67 private TestLttngCallStackModule fModule
;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * @return The test trace to use for this test
77 protected abstract @NonNull CtfTestTrace
getTestTrace();
80 * @return The name of the executable process in that particular trace
82 protected abstract String
getProcName();
85 * Get the list of timestamps to query in that trace.
88 * Which of the test timestamps?
89 * @return That particular timestamp
91 protected abstract long getTestTimestamp(int index
);
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
98 * Perform pre-class initialization.
101 public void setUp() {
102 CtfTestTrace testTrace
= getTestTrace();
104 CtfTmfTrace trace
= CtfTmfTestTraceUtils
.getTrace(testTrace
);
106 fModule
= new TestLttngCallStackModule();
108 assertTrue(fModule
.setTrace(trace
));
109 } catch (TmfAnalysisException e
) {
113 assertTrue(fModule
.waitForCompletion());
115 fSS
= fModule
.getStateSystem();
120 * Perform post-class clean-up.
123 public void tearDown() {
125 if (fTrace
!= null) {
127 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
128 deleteDirectory(suppDir
);
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 * Test the handling of generic UST traces who do not contain the required
141 public void testOtherUstTrace() {
142 /* Initialize the trace and analysis module */
144 CtfTmfTrace ustTrace
= CtfTmfTestTraceUtils
.getTrace(otherUstTrace
);
145 TestLttngCallStackModule module
= null;
147 module
= new TestLttngCallStackModule();
149 assertTrue(module
.setTrace(ustTrace
));
150 } catch (TmfAnalysisException e
) {
154 assertTrue(module
.waitForCompletion());
156 /* Make sure the generated state system exists, but is empty */
157 ITmfStateSystem ss
= module
.getStateSystem();
159 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().toNanos());
160 assertEquals(0, ss
.getNbAttributes());
162 if (module
!= null) {
166 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
169 deleteDirectory(suppDir
);
170 assertFalse(suppDir
.exists());
174 * Test that the callstack state system is there and contains data.
177 public void testConstruction() {
179 assertTrue(fSS
.getNbAttributes() > 0);
183 * Test the callstack at the beginning of the state system.
186 public void testCallStackBegin() {
187 long start
= fSS
.getStartTime();
188 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
189 assertEquals(1, cs
.length
);
191 assertEquals("40472b", cs
[0]);
195 * Test the callstack somewhere in the trace.
198 public void testCallStack1() {
199 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
200 assertEquals(2, cs
.length
);
202 assertEquals("40472b", cs
[0]);
203 assertEquals("403d60", cs
[1]);
207 * Test the callstack somewhere in the trace.
210 public void testCallStack2() {
211 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
212 assertEquals(3, cs
.length
);
214 assertEquals("40472b", cs
[0]);
215 assertEquals("403b14", cs
[1]);
216 assertEquals("401b23", cs
[2]);
220 * Test the callstack somewhere in the trace.
223 public void testCallStack3() {
224 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
225 assertEquals(4, cs
.length
);
227 assertEquals("40472b", cs
[0]);
228 assertEquals("4045c8", cs
[1]);
229 assertEquals("403760", cs
[2]);
230 assertEquals("401aac", cs
[3]);
234 * Test the callstack at the end of the trace/state system.
237 public void testCallStackEnd() {
238 long end
= fSS
.getCurrentEndTime();
239 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
240 assertEquals(3, cs
.length
);
242 assertEquals("40472b", cs
[0]);
243 assertEquals("4045c8", cs
[1]);
244 assertEquals("403760", cs
[2]);
247 // ------------------------------------------------------------------------
249 // ------------------------------------------------------------------------
251 /** Empty and delete a directory */
252 private static void deleteDirectory(File dir
) {
253 /* Assuming the dir only contains file or empty directories */
254 for (File file
: dir
.listFiles()) {
260 /** Get the callstack for the given timestamp, for this particular trace */
261 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
263 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
264 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
265 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
267 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
268 ITmfStateValue expectedValue
= state
.get(stackTop
).getStateValue();
269 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss
, timestamp
, stackAttribute
);
270 assertNotNull(interval
);
271 assertEquals(expectedValue
, interval
.getStateValue());
273 String
[] ret
= new String
[depth
];
274 for (int i
= 0; i
< depth
; i
++) {
275 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
276 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
280 } catch (AttributeNotFoundException e
) {
281 fail(e
.getMessage());
282 } catch (StateSystemDisposedException e
) {
283 fail(e
.getMessage());
289 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
292 protected ITmfStateProvider
createStateProvider() {
293 return new LttngUstCallStackProvider(checkNotNull(getTrace()));