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 CtfTmfTrace trace
= testTrace
.getTrace();
102 fModule
= new TestLttngCallStackModule();
104 fModule
.setTrace(trace
);
105 } catch (TmfAnalysisException e
) {
109 assertTrue(fModule
.waitForCompletion());
111 fSS
= fModule
.getStateSystem();
116 * Perform post-class clean-up.
119 public void tearDown() {
121 if (fTrace
!= null) {
123 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(fTrace
));
124 deleteDirectory(suppDir
);
128 // ------------------------------------------------------------------------
130 // ------------------------------------------------------------------------
133 * Test the handling of generic UST traces who do not contain the required
137 public void testOtherUstTrace() {
138 /* Initialize the trace and analysis module */
140 try (CtfTmfTrace ustTrace
= otherUstTrace
.getTrace();) {
141 TestLttngCallStackModule module
= null;
143 module
= new TestLttngCallStackModule();
145 module
.setTrace(ustTrace
);
146 } catch (TmfAnalysisException e
) {
150 assertTrue(module
.waitForCompletion());
152 /* Make sure the generated state system exists, but is empty */
153 ITmfStateSystem ss
= module
.getStateSystem();
155 assertTrue(ss
.getStartTime() >= ustTrace
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue());
156 assertEquals(0, ss
.getNbAttributes());
158 if (module
!= null) {
162 suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(ustTrace
));
164 deleteDirectory(suppDir
);
165 assertFalse(suppDir
.exists());
169 * Test that the callstack state system is there and contains data.
172 public void testConstruction() {
174 assertTrue(fSS
.getNbAttributes() > 0);
178 * Test the callstack at the beginning of the state system.
181 public void testCallStackBegin() {
182 long start
= fSS
.getStartTime();
183 String
[] cs
= getCallStack(fSS
, getProcName(), start
);
184 assertEquals(1, cs
.length
);
186 assertEquals("40472b", cs
[0]);
190 * Test the callstack somewhere in the trace.
193 public void testCallStack1() {
194 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(0));
195 assertEquals(2, cs
.length
);
197 assertEquals("40472b", cs
[0]);
198 assertEquals("403d60", cs
[1]);
202 * Test the callstack somewhere in the trace.
205 public void testCallStack2() {
206 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(1));
207 assertEquals(3, cs
.length
);
209 assertEquals("40472b", cs
[0]);
210 assertEquals("403b14", cs
[1]);
211 assertEquals("401b23", cs
[2]);
215 * Test the callstack somewhere in the trace.
218 public void testCallStack3() {
219 String
[] cs
= getCallStack(fSS
, getProcName(), getTestTimestamp(2));
220 assertEquals(4, cs
.length
);
222 assertEquals("40472b", cs
[0]);
223 assertEquals("4045c8", cs
[1]);
224 assertEquals("403760", cs
[2]);
225 assertEquals("401aac", cs
[3]);
229 * Test the callstack at the end of the trace/state system.
232 public void testCallStackEnd() {
233 long end
= fSS
.getCurrentEndTime();
234 String
[] cs
= getCallStack(fSS
, getProcName(), end
);
235 assertEquals(3, cs
.length
);
237 assertEquals("40472b", cs
[0]);
238 assertEquals("4045c8", cs
[1]);
239 assertEquals("403760", cs
[2]);
242 // ------------------------------------------------------------------------
244 // ------------------------------------------------------------------------
246 /** Empty and delete a directory */
247 private static void deleteDirectory(File dir
) {
248 /* Assuming the dir only contains file or empty directories */
249 for (File file
: dir
.listFiles()) {
255 /** Get the callstack for the given timestamp, for this particular trace */
256 private static String
[] getCallStack(ITmfStateSystem ss
, String processName
, long timestamp
) {
258 int stackAttribute
= ss
.getQuarkAbsolute("Threads", processName
, "CallStack");
259 List
<ITmfStateInterval
> state
= ss
.queryFullState(timestamp
);
260 int depth
= state
.get(stackAttribute
).getStateValue().unboxInt();
262 int stackTop
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(depth
));
263 ITmfStateValue expectedValue
= state
.get(stackTop
).getStateValue();
264 ITmfStateInterval interval
= StateSystemUtils
.querySingleStackTop(ss
, timestamp
, stackAttribute
);
265 assertNotNull(interval
);
266 assertEquals(expectedValue
, interval
.getStateValue());
268 String
[] ret
= new String
[depth
];
269 for (int i
= 0; i
< depth
; i
++) {
270 int quark
= ss
.getQuarkRelative(stackAttribute
, String
.valueOf(i
+ 1));
271 ret
[i
] = state
.get(quark
).getStateValue().unboxStr();
275 } catch (AttributeNotFoundException e
) {
276 fail(e
.getMessage());
277 } catch (StateSystemDisposedException e
) {
278 fail(e
.getMessage());
284 private class TestLttngCallStackModule
extends TmfStateSystemAnalysisModule
{
287 protected ITmfStateProvider
createStateProvider() {
288 return new LttngUstCallStackProvider(getTrace());