Commit | Line | Data |
---|---|---|
50659279 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 Ericsson |
50659279 AM |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
116738ad | 13 | package org.eclipse.tracecompass.lttng2.ust.core.tests.callstack; |
50659279 | 14 | |
d0c7e4ba | 15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
50659279 AM |
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; | |
50659279 AM |
21 | |
22 | import java.io.File; | |
23 | import java.util.List; | |
d291a715 | 24 | import java.util.concurrent.TimeUnit; |
50659279 | 25 | |
c4d57ac1 | 26 | import org.eclipse.jdt.annotation.NonNull; |
116738ad | 27 | import org.eclipse.tracecompass.internal.lttng2.ust.core.callstack.LttngUstCallStackProvider; |
e894a508 | 28 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
1dd75589 | 29 | import org.eclipse.tracecompass.statesystem.core.StateSystemUtils; |
e894a508 AM |
30 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; | |
32 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
33 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
c4d57ac1 | 34 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException; |
36 | import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider; | |
37 | import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule; | |
2bdf0193 | 38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; |
c4d57ac1 | 39 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
9722e5d7 | 40 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
50659279 AM |
41 | import org.junit.After; |
42 | import org.junit.Before; | |
43 | import org.junit.Rule; | |
44 | import org.junit.Test; | |
45 | import org.junit.rules.TestRule; | |
46 | import org.junit.rules.Timeout; | |
47 | ||
48 | /** | |
49 | * Base class for the UST callstack state provider tests. | |
50 | * | |
51 | * @author Alexandre Montplaisir | |
52 | */ | |
53 | public abstract class AbstractProviderTest { | |
54 | ||
55 | /** Time-out tests after 20 seconds */ | |
d291a715 | 56 | @Rule public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS); |
50659279 AM |
57 | |
58 | // ------------------------------------------------------------------------ | |
59 | // Attributes | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
c4d57ac1 | 62 | private static final @NonNull CtfTestTrace otherUstTrace = CtfTestTrace.HELLO_LOST; |
50659279 AM |
63 | |
64 | private CtfTmfTrace fTrace = null; | |
65 | private ITmfStateSystem fSS = null; | |
bd64ee73 AM |
66 | private TestLttngCallStackModule fModule; |
67 | ||
50659279 AM |
68 | |
69 | // ------------------------------------------------------------------------ | |
70 | // Abstract methods | |
71 | // ------------------------------------------------------------------------ | |
72 | ||
73 | /** | |
74 | * @return The test trace to use for this test | |
75 | */ | |
c4d57ac1 | 76 | protected abstract @NonNull CtfTestTrace getTestTrace(); |
50659279 AM |
77 | |
78 | /** | |
79 | * @return The name of the executable process in that particular trace | |
80 | */ | |
81 | protected abstract String getProcName(); | |
82 | ||
83 | /** | |
84 | * Get the list of timestamps to query in that trace. | |
85 | * | |
86 | * @param index | |
87 | * Which of the test timestamps? | |
88 | * @return That particular timestamp | |
89 | */ | |
90 | protected abstract long getTestTimestamp(int index); | |
91 | ||
92 | // ------------------------------------------------------------------------ | |
93 | // Maintenance | |
94 | // ------------------------------------------------------------------------ | |
95 | ||
96 | /** | |
97 | * Perform pre-class initialization. | |
98 | */ | |
99 | @Before | |
100 | public void setUp() { | |
c4d57ac1 | 101 | CtfTestTrace testTrace = getTestTrace(); |
50659279 | 102 | |
c4d57ac1 | 103 | CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(testTrace); |
ba27dd38 | 104 | fTrace = trace; |
bd64ee73 | 105 | fModule = new TestLttngCallStackModule(); |
50659279 | 106 | try { |
f479550c | 107 | assertTrue(fModule.setTrace(trace)); |
50659279 AM |
108 | } catch (TmfAnalysisException e) { |
109 | fail(); | |
110 | } | |
bd64ee73 AM |
111 | fModule.schedule(); |
112 | assertTrue(fModule.waitForCompletion()); | |
50659279 | 113 | |
bd64ee73 | 114 | fSS = fModule.getStateSystem(); |
50659279 AM |
115 | assertNotNull(fSS); |
116 | } | |
117 | ||
118 | /** | |
119 | * Perform post-class clean-up. | |
120 | */ | |
121 | @After | |
122 | public void tearDown() { | |
03f0b0b1 | 123 | fModule.dispose(); |
50659279 AM |
124 | if (fTrace != null) { |
125 | fTrace.dispose(); | |
126 | File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(fTrace)); | |
127 | deleteDirectory(suppDir); | |
128 | } | |
129 | } | |
130 | ||
131 | // ------------------------------------------------------------------------ | |
132 | // Test methods | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
135 | /** | |
136 | * Test the handling of generic UST traces who do not contain the required | |
137 | * information. | |
138 | */ | |
139 | @Test | |
140 | public void testOtherUstTrace() { | |
141 | /* Initialize the trace and analysis module */ | |
95a0cba5 | 142 | File suppDir; |
c4d57ac1 | 143 | CtfTmfTrace ustTrace = CtfTmfTestTraceUtils.getTrace(otherUstTrace); |
0ff9e595 AM |
144 | TestLttngCallStackModule module = null; |
145 | try { | |
146 | module = new TestLttngCallStackModule(); | |
03f0b0b1 | 147 | try { |
0ff9e595 AM |
148 | assertTrue(module.setTrace(ustTrace)); |
149 | } catch (TmfAnalysisException e) { | |
150 | fail(); | |
151 | } | |
152 | module.schedule(); | |
153 | assertTrue(module.waitForCompletion()); | |
154 | ||
155 | /* Make sure the generated state system exists, but is empty */ | |
156 | ITmfStateSystem ss = module.getStateSystem(); | |
157 | assertNotNull(ss); | |
16801c72 | 158 | assertTrue(ss.getStartTime() >= ustTrace.getStartTime().toNanos()); |
0ff9e595 AM |
159 | assertEquals(0, ss.getNbAttributes()); |
160 | } finally { | |
161 | if (module != null) { | |
162 | module.dispose(); | |
bd64ee73 | 163 | } |
50659279 | 164 | } |
0ff9e595 AM |
165 | suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace)); |
166 | ||
167 | ustTrace.dispose(); | |
95a0cba5 MAL |
168 | deleteDirectory(suppDir); |
169 | assertFalse(suppDir.exists()); | |
50659279 AM |
170 | } |
171 | ||
172 | /** | |
173 | * Test that the callstack state system is there and contains data. | |
174 | */ | |
175 | @Test | |
176 | public void testConstruction() { | |
177 | assertNotNull(fSS); | |
178 | assertTrue(fSS.getNbAttributes() > 0); | |
179 | } | |
180 | ||
181 | /** | |
182 | * Test the callstack at the beginning of the state system. | |
183 | */ | |
184 | @Test | |
185 | public void testCallStackBegin() { | |
186 | long start = fSS.getStartTime(); | |
187 | String[] cs = getCallStack(fSS, getProcName(), start); | |
188 | assertEquals(1, cs.length); | |
189 | ||
190 | assertEquals("40472b", cs[0]); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Test the callstack somewhere in the trace. | |
195 | */ | |
196 | @Test | |
197 | public void testCallStack1() { | |
198 | String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(0)); | |
199 | assertEquals(2, cs.length); | |
200 | ||
201 | assertEquals("40472b", cs[0]); | |
202 | assertEquals("403d60", cs[1]); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Test the callstack somewhere in the trace. | |
207 | */ | |
208 | @Test | |
209 | public void testCallStack2() { | |
210 | String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(1)); | |
211 | assertEquals(3, cs.length); | |
212 | ||
213 | assertEquals("40472b", cs[0]); | |
214 | assertEquals("403b14", cs[1]); | |
215 | assertEquals("401b23", cs[2]); | |
216 | } | |
217 | ||
218 | /** | |
219 | * Test the callstack somewhere in the trace. | |
220 | */ | |
221 | @Test | |
222 | public void testCallStack3() { | |
223 | String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(2)); | |
224 | assertEquals(4, cs.length); | |
225 | ||
226 | assertEquals("40472b", cs[0]); | |
227 | assertEquals("4045c8", cs[1]); | |
228 | assertEquals("403760", cs[2]); | |
229 | assertEquals("401aac", cs[3]); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Test the callstack at the end of the trace/state system. | |
234 | */ | |
235 | @Test | |
236 | public void testCallStackEnd() { | |
237 | long end = fSS.getCurrentEndTime(); | |
238 | String[] cs = getCallStack(fSS, getProcName(), end); | |
239 | assertEquals(3, cs.length); | |
240 | ||
241 | assertEquals("40472b", cs[0]); | |
242 | assertEquals("4045c8", cs[1]); | |
243 | assertEquals("403760", cs[2]); | |
244 | } | |
245 | ||
246 | // ------------------------------------------------------------------------ | |
247 | // Utility methods | |
248 | // ------------------------------------------------------------------------ | |
249 | ||
250 | /** Empty and delete a directory */ | |
251 | private static void deleteDirectory(File dir) { | |
252 | /* Assuming the dir only contains file or empty directories */ | |
253 | for (File file : dir.listFiles()) { | |
254 | file.delete(); | |
255 | } | |
256 | dir.delete(); | |
257 | } | |
258 | ||
259 | /** Get the callstack for the given timestamp, for this particular trace */ | |
260 | private static String[] getCallStack(ITmfStateSystem ss, String processName, long timestamp) { | |
261 | try { | |
262 | int stackAttribute = ss.getQuarkAbsolute("Threads", processName, "CallStack"); | |
263 | List<ITmfStateInterval> state = ss.queryFullState(timestamp); | |
264 | int depth = state.get(stackAttribute).getStateValue().unboxInt(); | |
265 | ||
266 | int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth)); | |
1dd75589 AM |
267 | ITmfStateValue expectedValue = state.get(stackTop).getStateValue(); |
268 | ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute); | |
269 | assertNotNull(interval); | |
270 | assertEquals(expectedValue, interval.getStateValue()); | |
50659279 AM |
271 | |
272 | String[] ret = new String[depth]; | |
273 | for (int i = 0; i < depth; i++) { | |
274 | int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1)); | |
275 | ret[i] = state.get(quark).getStateValue().unboxStr(); | |
276 | } | |
277 | return ret; | |
278 | ||
279 | } catch (AttributeNotFoundException e) { | |
280 | fail(e.getMessage()); | |
281 | } catch (StateSystemDisposedException e) { | |
282 | fail(e.getMessage()); | |
283 | } | |
284 | fail(); | |
285 | return null; | |
286 | } | |
287 | ||
288 | private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule { | |
289 | ||
290 | @Override | |
291 | protected ITmfStateProvider createStateProvider() { | |
d0c7e4ba | 292 | return new LttngUstCallStackProvider(checkNotNull(getTrace())); |
50659279 | 293 | } |
50659279 AM |
294 | } |
295 | } |