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
.trace
.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
;
25 import org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.trace
.callstack
.LttngUstCallStackProvider
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
39 import org
.junit
.After
;
40 import org
.junit
.Before
;
41 import org
.junit
.Rule
;
42 import org
.junit
.Test
;
43 import org
.junit
.rules
.TestRule
;
44 import org
.junit
.rules
.Timeout
;
47 * Base class for the UST callstack state provider tests.
49 * @author Alexandre Montplaisir
51 public abstract class AbstractProviderTest
{
53 /** Time-out tests after 20 seconds */
54 @Rule public TestRule globalTimeout
= new Timeout(20000);
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private static final CtfTmfTestTrace otherUstTrace
= CtfTmfTestTrace
.HELLO_LOST
;
62 private CtfTmfTrace fTrace
= null;
63 private ITmfStateSystem fSS
= null;
64 private TestLttngCallStackModule fModule
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * @return The test trace to use for this test
74 protected abstract CtfTmfTestTrace
getTestTrace();
77 * @return The name of the executable process in that particular trace
79 protected abstract String
getProcName();
82 * Get the list of timestamps to query in that trace.
85 * Which of the test timestamps?
86 * @return That particular timestamp
88 protected abstract long getTestTimestamp(int index
);
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 * Perform pre-class initialization.
99 CtfTmfTestTrace testTrace
= getTestTrace();
101 CtfTmfTrace trace
= testTrace
.getTrace();
103 fModule
= new TestLttngCallStackModule();
105 fModule
.setTrace(trace
);
106 } catch (TmfAnalysisException e
) {
110 assertTrue(fModule
.waitForCompletion());
112 fSS
= fModule
.getStateSystem();
117 * Perform post-class clean-up.
120 public void tearDown() {
122 if (fTrace
!= null) {
124 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
125 deleteDirectory(suppDir
);
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
134 * Test the handling of generic UST traces who do not contain the required
138 public void testOtherUstTrace() {
139 /* Initialize the trace and analysis module */
141 try (CtfTmfTrace ustTrace
= otherUstTrace
.getTrace();) {
142 TestLttngCallStackModule module
= null;
144 module
= new TestLttngCallStackModule();
146 module
.setTrace(ustTrace
);
147 } catch (TmfAnalysisException e
) {
151 assertTrue(module
.waitForCompletion());
153 /* Make sure the generated state system exists, but is empty */
154 ITmfStateSystem ss
= module
.getStateSystem();
156 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
157 assertEquals(0, ss
.getNbAttributes());
159 if (module
!= null) {
163 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
165 deleteDirectory(suppDir
);
166 assertFalse(suppDir
.exists());
170 * Test that the callstack state system is there and contains data.
173 public void testConstruction() {
175 assertTrue(fSS
.getNbAttributes() > 0);
179 * Test the callstack at the beginning of the state system.
182 public void testCallStackBegin() {
183 long start
= fSS
.getStartTime();
184 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
185 assertEquals(1, cs
.length
);
187 assertEquals("40472b", cs
[0]);
191 * Test the callstack somewhere in the trace.
194 public void testCallStack1() {
195 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
196 assertEquals(2, cs
.length
);
198 assertEquals("40472b", cs
[0]);
199 assertEquals("403d60", cs
[1]);
203 * Test the callstack somewhere in the trace.
206 public void testCallStack2() {
207 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
208 assertEquals(3, cs
.length
);
210 assertEquals("40472b", cs
[0]);
211 assertEquals("403b14", cs
[1]);
212 assertEquals("401b23", cs
[2]);
216 * Test the callstack somewhere in the trace.
219 public void testCallStack3() {
220 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
221 assertEquals(4, cs
.length
);
223 assertEquals("40472b", cs
[0]);
224 assertEquals("4045c8", cs
[1]);
225 assertEquals("403760", cs
[2]);
226 assertEquals("401aac", cs
[3]);
230 * Test the callstack at the end of the trace/state system.
233 public void testCallStackEnd() {
234 long end
= fSS
.getCurrentEndTime();
235 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
236 assertEquals(3, cs
.length
);
238 assertEquals("40472b", cs
[0]);
239 assertEquals("4045c8", cs
[1]);
240 assertEquals("403760", cs
[2]);
243 // ------------------------------------------------------------------------
245 // ------------------------------------------------------------------------
247 /** Empty and delete a directory */
248 private static void deleteDirectory(File dir
) {
249 /* Assuming the dir only contains file or empty directories */
250 for (File file
: dir
.listFiles()) {
256 /** Get the callstack for the given timestamp, for this particular trace */
257 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
259 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
260 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
261 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
263 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
264 ITmfStateValue expectedValue
= state
.get(stackTop
).getStateValue();
265 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss
, timestamp
, stackAttribute
);
266 assertNotNull(interval
);
267 assertEquals(expectedValue
, interval
.getStateValue());
269 String
[] ret
= new String
[depth
];
270 for (int i
= 0; i
< depth
; i
++) {
271 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
272 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
276 } catch (AttributeNotFoundException e
) {
277 fail(e
.getMessage());
278 } catch (StateSystemDisposedException e
) {
279 fail(e
.getMessage());
285 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
288 protected ITmfStateProvider
createStateProvider() {
289 return new LttngUstCallStackProvider(checkNotNull(getTrace()));