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