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
.tracecompass
.internal
.lttng2
.ust
.core
.callstack
.LttngUstCallStackProvider
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.Rule
;
43 import org
.junit
.Test
;
44 import org
.junit
.rules
.TestRule
;
45 import org
.junit
.rules
.Timeout
;
48 * Base class for the UST callstack state provider tests.
50 * @author Alexandre Montplaisir
52 public abstract class AbstractProviderTest
{
54 /** Time-out tests after 20 seconds */
55 @Rule public TestRule globalTimeout
= new Timeout(20, TimeUnit
.SECONDS
);
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private static final CtfTmfTestTrace otherUstTrace
= CtfTmfTestTrace
.HELLO_LOST
;
63 private CtfTmfTrace fTrace
= null;
64 private ITmfStateSystem fSS
= null;
65 private TestLttngCallStackModule fModule
;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 * @return The test trace to use for this test
75 protected abstract CtfTmfTestTrace
getTestTrace();
78 * @return The name of the executable process in that particular trace
80 protected abstract String
getProcName();
83 * Get the list of timestamps to query in that trace.
86 * Which of the test timestamps?
87 * @return That particular timestamp
89 protected abstract long getTestTimestamp(int index
);
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 * Perform pre-class initialization.
100 CtfTmfTestTrace testTrace
= getTestTrace();
102 CtfTmfTrace trace
= testTrace
.getTrace();
104 fModule
= new TestLttngCallStackModule();
106 assertTrue(fModule
.setTrace(trace
));
107 } catch (TmfAnalysisException e
) {
111 assertTrue(fModule
.waitForCompletion());
113 fSS
= fModule
.getStateSystem();
118 * Perform post-class clean-up.
121 public void tearDown() {
123 if (fTrace
!= null) {
125 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
126 deleteDirectory(suppDir
);
130 // ------------------------------------------------------------------------
132 // ------------------------------------------------------------------------
135 * Test the handling of generic UST traces who do not contain the required
139 public void testOtherUstTrace() {
140 /* Initialize the trace and analysis module */
142 CtfTmfTrace ustTrace
= otherUstTrace
.getTrace();
143 TestLttngCallStackModule module
= null;
145 module
= new TestLttngCallStackModule();
147 assertTrue(module
.setTrace(ustTrace
));
148 } catch (TmfAnalysisException e
) {
152 assertTrue(module
.waitForCompletion());
154 /* Make sure the generated state system exists, but is empty */
155 ITmfStateSystem ss
= module
.getStateSystem();
157 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
158 assertEquals(0, ss
.getNbAttributes());
160 if (module
!= null) {
164 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
167 deleteDirectory(suppDir
);
168 assertFalse(suppDir
.exists());
172 * Test that the callstack state system is there and contains data.
175 public void testConstruction() {
177 assertTrue(fSS
.getNbAttributes() > 0);
181 * Test the callstack at the beginning of the state system.
184 public void testCallStackBegin() {
185 long start
= fSS
.getStartTime();
186 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
187 assertEquals(1, cs
.length
);
189 assertEquals("40472b", cs
[0]);
193 * Test the callstack somewhere in the trace.
196 public void testCallStack1() {
197 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
198 assertEquals(2, cs
.length
);
200 assertEquals("40472b", cs
[0]);
201 assertEquals("403d60", cs
[1]);
205 * Test the callstack somewhere in the trace.
208 public void testCallStack2() {
209 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
210 assertEquals(3, cs
.length
);
212 assertEquals("40472b", cs
[0]);
213 assertEquals("403b14", cs
[1]);
214 assertEquals("401b23", cs
[2]);
218 * Test the callstack somewhere in the trace.
221 public void testCallStack3() {
222 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
223 assertEquals(4, cs
.length
);
225 assertEquals("40472b", cs
[0]);
226 assertEquals("4045c8", cs
[1]);
227 assertEquals("403760", cs
[2]);
228 assertEquals("401aac", cs
[3]);
232 * Test the callstack at the end of the trace/state system.
235 public void testCallStackEnd() {
236 long end
= fSS
.getCurrentEndTime();
237 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
238 assertEquals(3, cs
.length
);
240 assertEquals("40472b", cs
[0]);
241 assertEquals("4045c8", cs
[1]);
242 assertEquals("403760", cs
[2]);
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
249 /** Empty and delete a directory */
250 private static void deleteDirectory(File dir
) {
251 /* Assuming the dir only contains file or empty directories */
252 for (File file
: dir
.listFiles()) {
258 /** Get the callstack for the given timestamp, for this particular trace */
259 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
261 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
262 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
263 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
265 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
266 ITmfStateValue expectedValue
= state
.get(stackTop
).getStateValue();
267 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss
, timestamp
, stackAttribute
);
268 assertNotNull(interval
);
269 assertEquals(expectedValue
, interval
.getStateValue());
271 String
[] ret
= new String
[depth
];
272 for (int i
= 0; i
< depth
; i
++) {
273 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
274 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
278 } catch (AttributeNotFoundException e
) {
279 fail(e
.getMessage());
280 } catch (StateSystemDisposedException e
) {
281 fail(e
.getMessage());
287 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
290 protected ITmfStateProvider
createStateProvider() {
291 return new LttngUstCallStackProvider(checkNotNull(getTrace()));