1 /*******************************************************************************
2 * Copyright (c) 2014 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
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
22 import java
.util
.List
;
24 import org
.eclipse
.tracecompass
.internal
.lttng2
.ust
.core
.trace
.callstack
.LttngUstCallStackProvider
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
36 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.Rule
;
41 import org
.junit
.Test
;
42 import org
.junit
.rules
.TestRule
;
43 import org
.junit
.rules
.Timeout
;
46 * Base class for the UST callstack state provider tests.
48 * @author Alexandre Montplaisir
50 public abstract class AbstractProviderTest
{
52 /** Time-out tests after 20 seconds */
53 @Rule public TestRule globalTimeout
= new Timeout(20000);
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 private static final CtfTmfTestTrace otherUstTrace
= CtfTmfTestTrace
.HELLO_LOST
;
61 private CtfTmfTrace fTrace
= null;
62 private ITmfStateSystem fSS
= null;
63 private TestLttngCallStackModule fModule
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * @return The test trace to use for this test
73 protected abstract CtfTmfTestTrace
getTestTrace();
76 * @return The name of the executable process in that particular trace
78 protected abstract String
getProcName();
81 * Get the list of timestamps to query in that trace.
84 * Which of the test timestamps?
85 * @return That particular timestamp
87 protected abstract long getTestTimestamp(int index
);
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 * Perform pre-class initialization.
98 CtfTmfTestTrace testTrace
= getTestTrace();
100 fTrace
= testTrace
.getTrace();
101 fModule
= new TestLttngCallStackModule();
103 fModule
.setTrace(fTrace
);
104 } catch (TmfAnalysisException e
) {
108 assertTrue(fModule
.waitForCompletion());
110 fSS
= fModule
.getStateSystem();
115 * Perform post-class clean-up.
118 public void tearDown() {
120 if (fTrace
!= null) {
122 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
123 deleteDirectory(suppDir
);
127 // ------------------------------------------------------------------------
129 // ------------------------------------------------------------------------
132 * Test the handling of generic UST traces who do not contain the required
136 public void testOtherUstTrace() {
137 /* Initialize the trace and analysis module */
139 try (CtfTmfTrace ustTrace
= otherUstTrace
.getTrace();) {
140 TestLttngCallStackModule module
= null;
142 module
= new TestLttngCallStackModule();
144 module
.setTrace(ustTrace
);
145 } catch (TmfAnalysisException e
) {
149 assertTrue(module
.waitForCompletion());
151 /* Make sure the generated state system exists, but is empty */
152 ITmfStateSystem ss
= module
.getStateSystem();
154 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
155 assertEquals(0, ss
.getNbAttributes());
157 if (module
!= null) {
161 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
163 deleteDirectory(suppDir
);
164 assertFalse(suppDir
.exists());
168 * Test that the callstack state system is there and contains data.
171 public void testConstruction() {
173 assertTrue(fSS
.getNbAttributes() > 0);
177 * Test the callstack at the beginning of the state system.
180 public void testCallStackBegin() {
181 long start
= fSS
.getStartTime();
182 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
183 assertEquals(1, cs
.length
);
185 assertEquals("40472b", cs
[0]);
189 * Test the callstack somewhere in the trace.
192 public void testCallStack1() {
193 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
194 assertEquals(2, cs
.length
);
196 assertEquals("40472b", cs
[0]);
197 assertEquals("403d60", cs
[1]);
201 * Test the callstack somewhere in the trace.
204 public void testCallStack2() {
205 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
206 assertEquals(3, cs
.length
);
208 assertEquals("40472b", cs
[0]);
209 assertEquals("403b14", cs
[1]);
210 assertEquals("401b23", cs
[2]);
214 * Test the callstack somewhere in the trace.
217 public void testCallStack3() {
218 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
219 assertEquals(4, cs
.length
);
221 assertEquals("40472b", cs
[0]);
222 assertEquals("4045c8", cs
[1]);
223 assertEquals("403760", cs
[2]);
224 assertEquals("401aac", cs
[3]);
228 * Test the callstack at the end of the trace/state system.
231 public void testCallStackEnd() {
232 long end
= fSS
.getCurrentEndTime();
233 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
234 assertEquals(3, cs
.length
);
236 assertEquals("40472b", cs
[0]);
237 assertEquals("4045c8", cs
[1]);
238 assertEquals("403760", cs
[2]);
241 // ------------------------------------------------------------------------
243 // ------------------------------------------------------------------------
245 /** Empty and delete a directory */
246 private static void deleteDirectory(File dir
) {
247 /* Assuming the dir only contains file or empty directories */
248 for (File file
: dir
.listFiles()) {
254 /** Get the callstack for the given timestamp, for this particular trace */
255 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
257 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
258 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
259 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
261 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
262 ITmfStateValue expectedValue
= state
.get(stackTop
).getStateValue();
263 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss
, timestamp
, stackAttribute
);
264 assertNotNull(interval
);
265 assertEquals(expectedValue
, interval
.getStateValue());
267 String
[] ret
= new String
[depth
];
268 for (int i
= 0; i
< depth
; i
++) {
269 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
270 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
274 } catch (AttributeNotFoundException e
) {
275 fail(e
.getMessage());
276 } catch (StateSystemDisposedException e
) {
277 fail(e
.getMessage());
283 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
286 protected ITmfStateProvider
createStateProvider() {
287 return new LttngUstCallStackProvider(getTrace());