tmf: Make IAnalysisModule AutoCloseable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ust.core.tests / src / org / eclipse / linuxtools / lttng2 / ust / core / tests / trace / callstack / AbstractProviderTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 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.linuxtools.lttng2.ust.core.tests.trace.callstack;
14
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;
20 import static org.junit.Assume.assumeTrue;
21
22 import java.io.File;
23 import java.util.List;
24
25 import org.eclipse.linuxtools.internal.lttng2.ust.core.trace.callstack.LttngUstCallStackProvider;
26 import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
27 import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
28 import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
29 import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
30 import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue;
31 import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
32 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider;
33 import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
34 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
36 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
37 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
38 import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
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;
64 private TestLttngCallStackModule fModule;
65
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();
100 assumeTrue(testTrace.exists());
101
102 fTrace = testTrace.getTrace();
103 fModule = new TestLttngCallStackModule();
104 try {
105 fModule.setTrace(fTrace);
106 } catch (TmfAnalysisException e) {
107 fail();
108 }
109 fModule.schedule();
110 assertTrue(fModule.waitForCompletion());
111
112 fSS = fModule.getStateSystem();
113 assertNotNull(fSS);
114 }
115
116 /**
117 * Perform post-class clean-up.
118 */
119 @After
120 public void tearDown() {
121 fModule.close();
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 */
140 final ITmfTrace ustTrace = otherUstTrace.getTrace();
141 try (TestLttngCallStackModule module = new TestLttngCallStackModule();) {
142 try {
143 module.setTrace(ustTrace);
144 } catch (TmfAnalysisException e) {
145 fail();
146 }
147 module.schedule();
148 assertTrue(module.waitForCompletion());
149
150 /* Make sure the generated state system exists, but is empty */
151 ITmfStateSystem ss = module.getStateSystem();
152 assertNotNull(ss);
153 assertTrue(ss.getStartTime() >= ustTrace.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue());
154 assertEquals(0, ss.getNbAttributes());
155 }
156 /* Dispose the trace */
157 ustTrace.dispose();
158 File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace));
159 deleteDirectory(suppDir);
160 assertFalse(suppDir.exists());
161 }
162
163 /**
164 * Test that the callstack state system is there and contains data.
165 */
166 @Test
167 public void testConstruction() {
168 assertNotNull(fSS);
169 assertTrue(fSS.getNbAttributes() > 0);
170 }
171
172 /**
173 * Test the callstack at the beginning of the state system.
174 */
175 @Test
176 public void testCallStackBegin() {
177 long start = fSS.getStartTime();
178 String[] cs = getCallStack(fSS, getProcName(), start);
179 assertEquals(1, cs.length);
180
181 assertEquals("40472b", cs[0]);
182 }
183
184 /**
185 * Test the callstack somewhere in the trace.
186 */
187 @Test
188 public void testCallStack1() {
189 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(0));
190 assertEquals(2, cs.length);
191
192 assertEquals("40472b", cs[0]);
193 assertEquals("403d60", cs[1]);
194 }
195
196 /**
197 * Test the callstack somewhere in the trace.
198 */
199 @Test
200 public void testCallStack2() {
201 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(1));
202 assertEquals(3, cs.length);
203
204 assertEquals("40472b", cs[0]);
205 assertEquals("403b14", cs[1]);
206 assertEquals("401b23", cs[2]);
207 }
208
209 /**
210 * Test the callstack somewhere in the trace.
211 */
212 @Test
213 public void testCallStack3() {
214 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(2));
215 assertEquals(4, cs.length);
216
217 assertEquals("40472b", cs[0]);
218 assertEquals("4045c8", cs[1]);
219 assertEquals("403760", cs[2]);
220 assertEquals("401aac", cs[3]);
221 }
222
223 /**
224 * Test the callstack at the end of the trace/state system.
225 */
226 @Test
227 public void testCallStackEnd() {
228 long end = fSS.getCurrentEndTime();
229 String[] cs = getCallStack(fSS, getProcName(), end);
230 assertEquals(3, cs.length);
231
232 assertEquals("40472b", cs[0]);
233 assertEquals("4045c8", cs[1]);
234 assertEquals("403760", cs[2]);
235 }
236
237 // ------------------------------------------------------------------------
238 // Utility methods
239 // ------------------------------------------------------------------------
240
241 /** Empty and delete a directory */
242 private static void deleteDirectory(File dir) {
243 /* Assuming the dir only contains file or empty directories */
244 for (File file : dir.listFiles()) {
245 file.delete();
246 }
247 dir.delete();
248 }
249
250 /** Get the callstack for the given timestamp, for this particular trace */
251 private static String[] getCallStack(ITmfStateSystem ss, String processName, long timestamp) {
252 try {
253 int stackAttribute = ss.getQuarkAbsolute("Threads", processName, "CallStack");
254 List<ITmfStateInterval> state = ss.queryFullState(timestamp);
255 int depth = state.get(stackAttribute).getStateValue().unboxInt();
256
257 int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth));
258 ITmfStateValue top = state.get(stackTop).getStateValue();
259 assertEquals(top, ss.querySingleStackTop(timestamp, stackAttribute).getStateValue());
260
261 String[] ret = new String[depth];
262 for (int i = 0; i < depth; i++) {
263 int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1));
264 ret[i] = state.get(quark).getStateValue().unboxStr();
265 }
266 return ret;
267
268 } catch (AttributeNotFoundException e) {
269 fail(e.getMessage());
270 } catch (StateSystemDisposedException e) {
271 fail(e.getMessage());
272 }
273 fail();
274 return null;
275 }
276
277 private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule {
278
279 @Override
280 protected ITmfStateProvider createStateProvider() {
281 return new LttngUstCallStackProvider(getTrace());
282 }
283 }
284 }
This page took 0.042094 seconds and 6 git commands to generate.