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