Use o.e.test and jdt.annotation from Eclipse 4.5
[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 try (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 deleteDirectory(suppDir);
167 assertFalse(suppDir.exists());
168 }
169
170 /**
171 * Test that the callstack state system is there and contains data.
172 */
173 @Test
174 public void testConstruction() {
175 assertNotNull(fSS);
176 assertTrue(fSS.getNbAttributes() > 0);
177 }
178
179 /**
180 * Test the callstack at the beginning of the state system.
181 */
182 @Test
183 public void testCallStackBegin() {
184 long start = fSS.getStartTime();
185 String[] cs = getCallStack(fSS, getProcName(), start);
186 assertEquals(1, cs.length);
187
188 assertEquals("40472b", cs[0]);
189 }
190
191 /**
192 * Test the callstack somewhere in the trace.
193 */
194 @Test
195 public void testCallStack1() {
196 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(0));
197 assertEquals(2, cs.length);
198
199 assertEquals("40472b", cs[0]);
200 assertEquals("403d60", cs[1]);
201 }
202
203 /**
204 * Test the callstack somewhere in the trace.
205 */
206 @Test
207 public void testCallStack2() {
208 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(1));
209 assertEquals(3, cs.length);
210
211 assertEquals("40472b", cs[0]);
212 assertEquals("403b14", cs[1]);
213 assertEquals("401b23", cs[2]);
214 }
215
216 /**
217 * Test the callstack somewhere in the trace.
218 */
219 @Test
220 public void testCallStack3() {
221 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(2));
222 assertEquals(4, cs.length);
223
224 assertEquals("40472b", cs[0]);
225 assertEquals("4045c8", cs[1]);
226 assertEquals("403760", cs[2]);
227 assertEquals("401aac", cs[3]);
228 }
229
230 /**
231 * Test the callstack at the end of the trace/state system.
232 */
233 @Test
234 public void testCallStackEnd() {
235 long end = fSS.getCurrentEndTime();
236 String[] cs = getCallStack(fSS, getProcName(), end);
237 assertEquals(3, cs.length);
238
239 assertEquals("40472b", cs[0]);
240 assertEquals("4045c8", cs[1]);
241 assertEquals("403760", cs[2]);
242 }
243
244 // ------------------------------------------------------------------------
245 // Utility methods
246 // ------------------------------------------------------------------------
247
248 /** Empty and delete a directory */
249 private static void deleteDirectory(File dir) {
250 /* Assuming the dir only contains file or empty directories */
251 for (File file : dir.listFiles()) {
252 file.delete();
253 }
254 dir.delete();
255 }
256
257 /** Get the callstack for the given timestamp, for this particular trace */
258 private static String[] getCallStack(ITmfStateSystem ss, String processName, long timestamp) {
259 try {
260 int stackAttribute = ss.getQuarkAbsolute("Threads", processName, "CallStack");
261 List<ITmfStateInterval> state = ss.queryFullState(timestamp);
262 int depth = state.get(stackAttribute).getStateValue().unboxInt();
263
264 int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth));
265 ITmfStateValue expectedValue = state.get(stackTop).getStateValue();
266 ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute);
267 assertNotNull(interval);
268 assertEquals(expectedValue, interval.getStateValue());
269
270 String[] ret = new String[depth];
271 for (int i = 0; i < depth; i++) {
272 int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1));
273 ret[i] = state.get(quark).getStateValue().unboxStr();
274 }
275 return ret;
276
277 } catch (AttributeNotFoundException e) {
278 fail(e.getMessage());
279 } catch (StateSystemDisposedException e) {
280 fail(e.getMessage());
281 }
282 fail();
283 return null;
284 }
285
286 private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule {
287
288 @Override
289 protected ITmfStateProvider createStateProvider() {
290 return new LttngUstCallStackProvider(checkNotNull(getTrace()));
291 }
292 }
293 }
This page took 0.043996 seconds and 5 git commands to generate.