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