d5b979b5cf42e945bef2b6bddb3de947ab89a5b4
[deliverable/tracecompass.git] / 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
25 import org.eclipse.tracecompass.internal.lttng2.ust.core.callstack.LttngUstCallStackProvider;
26 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
27 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
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;
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;
37 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
38 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
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
101 CtfTmfTrace trace = testTrace.getTrace();
102 fTrace = trace;
103 fModule = new TestLttngCallStackModule();
104 try {
105 assertTrue(fModule.setTrace(trace));
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.dispose();
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 File suppDir;
141 try (CtfTmfTrace ustTrace = otherUstTrace.getTrace();) {
142 TestLttngCallStackModule module = null;
143 try {
144 module = new TestLttngCallStackModule();
145 try {
146 assertTrue(module.setTrace(ustTrace));
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());
158 } finally {
159 if (module != null) {
160 module.dispose();
161 }
162 }
163 suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace));
164 }
165 deleteDirectory(suppDir);
166 assertFalse(suppDir.exists());
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));
264 ITmfStateValue expectedValue = state.get(stackTop).getStateValue();
265 ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute);
266 assertNotNull(interval);
267 assertEquals(expectedValue, interval.getStateValue());
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() {
289 return new LttngUstCallStackProvider(checkNotNull(getTrace()));
290 }
291 }
292 }
This page took 0.03725 seconds and 4 git commands to generate.