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
.linuxtools
.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
;
20 import static org
.junit
.Assume
.assumeTrue
;
23 import java
.util
.List
;
25 import org
.eclipse
.linuxtools
.internal
.lttng2
.ust
.core
.trace
.callstack
.LttngUstCallStackProvider
;
26 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
27 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
29 import org
.eclipse
.linuxtools
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateProvider
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
37 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.CtfTmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
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();
100 assumeTrue(testTrace
.exists());
102 fTrace
= testTrace
.getTrace();
103 fModule
= new TestLttngCallStackModule();
105 fModule
.setTrace(fTrace
);
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 */
140 final ITmfTrace ustTrace
= otherUstTrace
.getTrace();
141 try (TestLttngCallStackModule module
= new TestLttngCallStackModule();) {
143 module
.setTrace(ustTrace
);
144 } catch (TmfAnalysisException e
) {
148 assertTrue(module
.waitForCompletion());
150 /* Make sure the generated state system exists, but is empty */
151 ITmfStateSystem ss
= module
.getStateSystem();
153 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
154 assertEquals(0, ss
.getNbAttributes());
156 /* Dispose the trace */
158 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
159 deleteDirectory(suppDir
);
160 assertFalse(suppDir
.exists());
164 * Test that the callstack state system is there and contains data.
167 public void testConstruction() {
169 assertTrue(fSS
.getNbAttributes() > 0);
173 * Test the callstack at the beginning of the state system.
176 public void testCallStackBegin() {
177 long start
= fSS
.getStartTime();
178 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
179 assertEquals(1, cs
.length
);
181 assertEquals("40472b", cs
[0]);
185 * Test the callstack somewhere in the trace.
188 public void testCallStack1() {
189 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
190 assertEquals(2, cs
.length
);
192 assertEquals("40472b", cs
[0]);
193 assertEquals("403d60", cs
[1]);
197 * Test the callstack somewhere in the trace.
200 public void testCallStack2() {
201 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
202 assertEquals(3, cs
.length
);
204 assertEquals("40472b", cs
[0]);
205 assertEquals("403b14", cs
[1]);
206 assertEquals("401b23", cs
[2]);
210 * Test the callstack somewhere in the trace.
213 public void testCallStack3() {
214 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
215 assertEquals(4, cs
.length
);
217 assertEquals("40472b", cs
[0]);
218 assertEquals("4045c8", cs
[1]);
219 assertEquals("403760", cs
[2]);
220 assertEquals("401aac", cs
[3]);
224 * Test the callstack at the end of the trace/state system.
227 public void testCallStackEnd() {
228 long end
= fSS
.getCurrentEndTime();
229 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
230 assertEquals(3, cs
.length
);
232 assertEquals("40472b", cs
[0]);
233 assertEquals("4045c8", cs
[1]);
234 assertEquals("403760", cs
[2]);
237 // ------------------------------------------------------------------------
239 // ------------------------------------------------------------------------
241 /** Empty and delete a directory */
242 private static void deleteDirectory(File dir
) {
243 /* Assuming the dir only contains file or empty directories */
244 for (File file
: dir
.listFiles()) {
250 /** Get the callstack for the given timestamp, for this particular trace */
251 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
253 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
254 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
255 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
257 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
258 ITmfStateValue top
= state
.get(stackTop
).getStateValue();
259 assertEquals(top
, ss
.querySingleStackTop(timestamp
, stackAttribute
).getStateValue());
261 String
[] ret
= new String
[depth
];
262 for (int i
= 0; i
< depth
; i
++) {
263 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
264 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
268 } catch (AttributeNotFoundException e
) {
269 fail(e
.getMessage());
270 } catch (StateSystemDisposedException e
) {
271 fail(e
.getMessage());
277 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
280 protected ITmfStateProvider
createStateProvider() {
281 return new LttngUstCallStackProvider(getTrace());