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 | ||
9e8daee1 MAL |
55 | /** Time-out tests after 1 minute. */ |
56 | @Rule | |
57 | public TestRule globalTimeout = new Timeout(1, TimeUnit.MINUTES); | |
50659279 AM |
58 | |
59 | // ------------------------------------------------------------------------ | |
60 | // Attributes | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
c4d57ac1 | 63 | private static final @NonNull CtfTestTrace otherUstTrace = CtfTestTrace.HELLO_LOST; |
50659279 AM |
64 | |
65 | private CtfTmfTrace fTrace = null; | |
66 | private ITmfStateSystem fSS = null; | |
bd64ee73 AM |
67 | private TestLttngCallStackModule fModule; |
68 | ||
50659279 AM |
69 | |
70 | // ------------------------------------------------------------------------ | |
71 | // Abstract methods | |
72 | // ------------------------------------------------------------------------ | |
73 | ||
74 | /** | |
75 | * @return The test trace to use for this test | |
76 | */ | |
c4d57ac1 | 77 | protected abstract @NonNull CtfTestTrace getTestTrace(); |
50659279 | 78 | |
7f86b721 AM |
79 | /** |
80 | * @return The ID of the process the desired thread belongs to | |
81 | */ | |
82 | protected abstract int getProcessId(); | |
83 | ||
50659279 AM |
84 | /** |
85 | * @return The name of the executable process in that particular trace | |
86 | */ | |
7f86b721 | 87 | protected abstract String getThreadName(); |
50659279 AM |
88 | |
89 | /** | |
90 | * Get the list of timestamps to query in that trace. | |
91 | * | |
92 | * @param index | |
93 | * Which of the test timestamps? | |
94 | * @return That particular timestamp | |
95 | */ | |
96 | protected abstract long getTestTimestamp(int index); | |
97 | ||
98 | // ------------------------------------------------------------------------ | |
99 | // Maintenance | |
100 | // ------------------------------------------------------------------------ | |
101 | ||
102 | /** | |
103 | * Perform pre-class initialization. | |
104 | */ | |
105 | @Before | |
106 | public void setUp() { | |
c4d57ac1 | 107 | CtfTestTrace testTrace = getTestTrace(); |
50659279 | 108 | |
c4d57ac1 | 109 | CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(testTrace); |
ba27dd38 | 110 | fTrace = trace; |
bd64ee73 | 111 | fModule = new TestLttngCallStackModule(); |
50659279 | 112 | try { |
f479550c | 113 | assertTrue(fModule.setTrace(trace)); |
50659279 AM |
114 | } catch (TmfAnalysisException e) { |
115 | fail(); | |
116 | } | |
bd64ee73 AM |
117 | fModule.schedule(); |
118 | assertTrue(fModule.waitForCompletion()); | |
50659279 | 119 | |
bd64ee73 | 120 | fSS = fModule.getStateSystem(); |
50659279 AM |
121 | assertNotNull(fSS); |
122 | } | |
123 | ||
124 | /** | |
125 | * Perform post-class clean-up. | |
126 | */ | |
127 | @After | |
128 | public void tearDown() { | |
03f0b0b1 | 129 | fModule.dispose(); |
50659279 AM |
130 | if (fTrace != null) { |
131 | fTrace.dispose(); | |
132 | File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(fTrace)); | |
133 | deleteDirectory(suppDir); | |
134 | } | |
135 | } | |
136 | ||
137 | // ------------------------------------------------------------------------ | |
138 | // Test methods | |
139 | // ------------------------------------------------------------------------ | |
140 | ||
141 | /** | |
142 | * Test the handling of generic UST traces who do not contain the required | |
143 | * information. | |
144 | */ | |
145 | @Test | |
146 | public void testOtherUstTrace() { | |
147 | /* Initialize the trace and analysis module */ | |
95a0cba5 | 148 | File suppDir; |
c4d57ac1 | 149 | CtfTmfTrace ustTrace = CtfTmfTestTraceUtils.getTrace(otherUstTrace); |
0ff9e595 AM |
150 | TestLttngCallStackModule module = null; |
151 | try { | |
152 | module = new TestLttngCallStackModule(); | |
03f0b0b1 | 153 | try { |
0ff9e595 AM |
154 | assertTrue(module.setTrace(ustTrace)); |
155 | } catch (TmfAnalysisException e) { | |
156 | fail(); | |
157 | } | |
158 | module.schedule(); | |
159 | assertTrue(module.waitForCompletion()); | |
160 | ||
161 | /* Make sure the generated state system exists, but is empty */ | |
162 | ITmfStateSystem ss = module.getStateSystem(); | |
163 | assertNotNull(ss); | |
16801c72 | 164 | assertTrue(ss.getStartTime() >= ustTrace.getStartTime().toNanos()); |
0ff9e595 AM |
165 | assertEquals(0, ss.getNbAttributes()); |
166 | } finally { | |
167 | if (module != null) { | |
168 | module.dispose(); | |
bd64ee73 | 169 | } |
50659279 | 170 | } |
0ff9e595 AM |
171 | suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace)); |
172 | ||
173 | ustTrace.dispose(); | |
95a0cba5 MAL |
174 | deleteDirectory(suppDir); |
175 | assertFalse(suppDir.exists()); | |
50659279 AM |
176 | } |
177 | ||
178 | /** | |
179 | * Test that the callstack state system is there and contains data. | |
180 | */ | |
181 | @Test | |
182 | public void testConstruction() { | |
183 | assertNotNull(fSS); | |
184 | assertTrue(fSS.getNbAttributes() > 0); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Test the callstack at the beginning of the state system. | |
189 | */ | |
190 | @Test | |
191 | public void testCallStackBegin() { | |
192 | long start = fSS.getStartTime(); | |
7f86b721 | 193 | String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), start); |
50659279 AM |
194 | assertEquals(1, cs.length); |
195 | ||
196 | assertEquals("40472b", cs[0]); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Test the callstack somewhere in the trace. | |
201 | */ | |
202 | @Test | |
203 | public void testCallStack1() { | |
7f86b721 | 204 | String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(0)); |
50659279 AM |
205 | assertEquals(2, cs.length); |
206 | ||
207 | assertEquals("40472b", cs[0]); | |
208 | assertEquals("403d60", cs[1]); | |
209 | } | |
210 | ||
211 | /** | |
212 | * Test the callstack somewhere in the trace. | |
213 | */ | |
214 | @Test | |
215 | public void testCallStack2() { | |
7f86b721 | 216 | String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(1)); |
50659279 AM |
217 | assertEquals(3, cs.length); |
218 | ||
219 | assertEquals("40472b", cs[0]); | |
220 | assertEquals("403b14", cs[1]); | |
221 | assertEquals("401b23", cs[2]); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Test the callstack somewhere in the trace. | |
226 | */ | |
227 | @Test | |
228 | public void testCallStack3() { | |
7f86b721 | 229 | String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(2)); |
50659279 AM |
230 | assertEquals(4, cs.length); |
231 | ||
232 | assertEquals("40472b", cs[0]); | |
233 | assertEquals("4045c8", cs[1]); | |
234 | assertEquals("403760", cs[2]); | |
235 | assertEquals("401aac", cs[3]); | |
236 | } | |
237 | ||
238 | /** | |
239 | * Test the callstack at the end of the trace/state system. | |
240 | */ | |
241 | @Test | |
242 | public void testCallStackEnd() { | |
243 | long end = fSS.getCurrentEndTime(); | |
7f86b721 | 244 | String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), end); |
50659279 AM |
245 | assertEquals(3, cs.length); |
246 | ||
247 | assertEquals("40472b", cs[0]); | |
248 | assertEquals("4045c8", cs[1]); | |
249 | assertEquals("403760", cs[2]); | |
250 | } | |
251 | ||
252 | // ------------------------------------------------------------------------ | |
253 | // Utility methods | |
254 | // ------------------------------------------------------------------------ | |
255 | ||
256 | /** Empty and delete a directory */ | |
257 | private static void deleteDirectory(File dir) { | |
258 | /* Assuming the dir only contains file or empty directories */ | |
259 | for (File file : dir.listFiles()) { | |
260 | file.delete(); | |
261 | } | |
262 | dir.delete(); | |
263 | } | |
264 | ||
265 | /** Get the callstack for the given timestamp, for this particular trace */ | |
7f86b721 | 266 | private static String[] getCallStack(ITmfStateSystem ss, int pid, String threadName, long timestamp) { |
50659279 | 267 | try { |
7f86b721 | 268 | int stackAttribute = ss.getQuarkAbsolute("Processes", Integer.toString(pid), threadName, "CallStack"); |
50659279 AM |
269 | List<ITmfStateInterval> state = ss.queryFullState(timestamp); |
270 | int depth = state.get(stackAttribute).getStateValue().unboxInt(); | |
271 | ||
272 | int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth)); | |
1dd75589 AM |
273 | ITmfStateValue expectedValue = state.get(stackTop).getStateValue(); |
274 | ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute); | |
275 | assertNotNull(interval); | |
276 | assertEquals(expectedValue, interval.getStateValue()); | |
50659279 AM |
277 | |
278 | String[] ret = new String[depth]; | |
279 | for (int i = 0; i < depth; i++) { | |
280 | int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1)); | |
c3777c23 | 281 | ret[i] = Long.toHexString(state.get(quark).getStateValue().unboxLong()); |
50659279 AM |
282 | } |
283 | return ret; | |
284 | ||
285 | } catch (AttributeNotFoundException e) { | |
286 | fail(e.getMessage()); | |
287 | } catch (StateSystemDisposedException e) { | |
288 | fail(e.getMessage()); | |
289 | } | |
290 | fail(); | |
291 | return null; | |
292 | } | |
293 | ||
294 | private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule { | |
295 | ||
296 | @Override | |
297 | protected ITmfStateProvider createStateProvider() { | |
d0c7e4ba | 298 | return new LttngUstCallStackProvider(checkNotNull(getTrace())); |
50659279 | 299 | } |
50659279 AM |
300 | } |
301 | } |