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