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
;
22 import java
.util
.List
;
24 import org
.eclipse
.linuxtools
.internal
.lttng2
.ust
.core
.trace
.callstack
.LttngUstCallStackProvider
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
37 import org
.junit
.After
;
38 import org
.junit
.Before
;
39 import org
.junit
.Rule
;
40 import org
.junit
.Test
;
41 import org
.junit
.rules
.TestRule
;
42 import org
.junit
.rules
.Timeout
;
45 * Base class for the UST callstack state provider tests.
47 * @author Alexandre Montplaisir
49 public abstract class AbstractProviderTest
{
51 /** Time-out tests after 20 seconds */
52 @Rule public TestRule globalTimeout
= new Timeout(20000);
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 private static final CtfTmfTestTrace otherUstTrace
= CtfTmfTestTrace
.HELLO_LOST
;
60 private CtfTmfTrace fTrace
= null;
61 private ITmfStateSystem fSS
= null;
62 private TestLttngCallStackModule fModule
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * @return The test trace to use for this test
72 protected abstract CtfTmfTestTrace
getTestTrace();
75 * @return The name of the executable process in that particular trace
77 protected abstract String
getProcName();
80 * Get the list of timestamps to query in that trace.
83 * Which of the test timestamps?
84 * @return That particular timestamp
86 protected abstract long getTestTimestamp(int index
);
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * Perform pre-class initialization.
97 CtfTmfTestTrace testTrace
= getTestTrace();
99 fTrace
= testTrace
.getTrace();
100 fModule
= new TestLttngCallStackModule();
102 fModule
.setTrace(fTrace
);
103 } catch (TmfAnalysisException e
) {
107 assertTrue(fModule
.waitForCompletion());
109 fSS
= fModule
.getStateSystem();
114 * Perform post-class clean-up.
117 public void tearDown() {
119 if (fTrace
!= null) {
121 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
122 deleteDirectory(suppDir
);
126 // ------------------------------------------------------------------------
128 // ------------------------------------------------------------------------
131 * Test the handling of generic UST traces who do not contain the required
135 public void testOtherUstTrace() {
136 /* Initialize the trace and analysis module */
138 try (CtfTmfTrace ustTrace
= otherUstTrace
.getTrace();) {
139 try (TestLttngCallStackModule module
= new TestLttngCallStackModule();) {
141 module
.setTrace(ustTrace
);
142 } catch (TmfAnalysisException e
) {
146 assertTrue(module
.waitForCompletion());
148 /* Make sure the generated state system exists, but is empty */
149 ITmfStateSystem ss
= module
.getStateSystem();
151 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
152 assertEquals(0, ss
.getNbAttributes());
154 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
156 deleteDirectory(suppDir
);
157 assertFalse(suppDir
.exists());
161 * Test that the callstack state system is there and contains data.
164 public void testConstruction() {
166 assertTrue(fSS
.getNbAttributes() > 0);
170 * Test the callstack at the beginning of the state system.
173 public void testCallStackBegin() {
174 long start
= fSS
.getStartTime();
175 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
176 assertEquals(1, cs
.length
);
178 assertEquals("40472b", cs
[0]);
182 * Test the callstack somewhere in the trace.
185 public void testCallStack1() {
186 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
187 assertEquals(2, cs
.length
);
189 assertEquals("40472b", cs
[0]);
190 assertEquals("403d60", cs
[1]);
194 * Test the callstack somewhere in the trace.
197 public void testCallStack2() {
198 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
199 assertEquals(3, cs
.length
);
201 assertEquals("40472b", cs
[0]);
202 assertEquals("403b14", cs
[1]);
203 assertEquals("401b23", cs
[2]);
207 * Test the callstack somewhere in the trace.
210 public void testCallStack3() {
211 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
212 assertEquals(4, cs
.length
);
214 assertEquals("40472b", cs
[0]);
215 assertEquals("4045c8", cs
[1]);
216 assertEquals("403760", cs
[2]);
217 assertEquals("401aac", cs
[3]);
221 * Test the callstack at the end of the trace/state system.
224 public void testCallStackEnd() {
225 long end
= fSS
.getCurrentEndTime();
226 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
227 assertEquals(3, cs
.length
);
229 assertEquals("40472b", cs
[0]);
230 assertEquals("4045c8", cs
[1]);
231 assertEquals("403760", cs
[2]);
234 // ------------------------------------------------------------------------
236 // ------------------------------------------------------------------------
238 /** Empty and delete a directory */
239 private static void deleteDirectory(File dir
) {
240 /* Assuming the dir only contains file or empty directories */
241 for (File file
: dir
.listFiles()) {
247 /** Get the callstack for the given timestamp, for this particular trace */
248 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
250 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
251 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
252 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
254 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
255 ITmfStateValue top
= state
.get(stackTop
).getStateValue();
256 assertEquals(top
, ss
.querySingleStackTop(timestamp
, stackAttribute
).getStateValue());
258 String
[] ret
= new String
[depth
];
259 for (int i
= 0; i
< depth
; i
++) {
260 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
261 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
265 } catch (AttributeNotFoundException e
) {
266 fail(e
.getMessage());
267 } catch (StateSystemDisposedException e
) {
268 fail(e
.getMessage());
274 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
277 protected ITmfStateProvider
createStateProvider() {
278 return new LttngUstCallStackProvider(getTrace());