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