Analysis: Add unit tests for the critical path module
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.ust.core.tests / src / org / eclipse / tracecompass / lttng2 / ust / core / tests / callstack / AbstractProviderTest.java
CommitLineData
50659279 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 Ericsson
50659279
AM
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
116738ad 13package org.eclipse.tracecompass.lttng2.ust.core.tests.callstack;
50659279 14
d0c7e4ba 15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
50659279
AM
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertFalse;
18import static org.junit.Assert.assertNotNull;
19import static org.junit.Assert.assertTrue;
20import static org.junit.Assert.fail;
50659279
AM
21
22import java.io.File;
23import java.util.List;
d291a715 24import java.util.concurrent.TimeUnit;
50659279 25
116738ad 26import org.eclipse.tracecompass.internal.lttng2.ust.core.callstack.LttngUstCallStackProvider;
e894a508 27import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
1dd75589 28import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
e894a508
AM
29import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
30import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
31import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
32import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
33import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
34import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
35import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
36import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
37import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
2bdf0193 38import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
9722e5d7 39import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
50659279
AM
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Rule;
43import org.junit.Test;
44import org.junit.rules.TestRule;
45import org.junit.rules.Timeout;
46
47/**
48 * Base class for the UST callstack state provider tests.
49 *
50 * @author Alexandre Montplaisir
51 */
52public abstract class AbstractProviderTest {
53
54 /** Time-out tests after 20 seconds */
d291a715 55 @Rule public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS);
50659279
AM
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;
bd64ee73
AM
65 private TestLttngCallStackModule fModule;
66
50659279
AM
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();
50659279 101
ba27dd38
GB
102 CtfTmfTrace trace = testTrace.getTrace();
103 fTrace = trace;
bd64ee73 104 fModule = new TestLttngCallStackModule();
50659279 105 try {
f479550c 106 assertTrue(fModule.setTrace(trace));
50659279
AM
107 } catch (TmfAnalysisException e) {
108 fail();
109 }
bd64ee73
AM
110 fModule.schedule();
111 assertTrue(fModule.waitForCompletion());
50659279 112
bd64ee73 113 fSS = fModule.getStateSystem();
50659279
AM
114 assertNotNull(fSS);
115 }
116
117 /**
118 * Perform post-class clean-up.
119 */
120 @After
121 public void tearDown() {
03f0b0b1 122 fModule.dispose();
50659279
AM
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 */
95a0cba5 141 File suppDir;
0ff9e595
AM
142 CtfTmfTrace ustTrace = otherUstTrace.getTrace();
143 TestLttngCallStackModule module = null;
144 try {
145 module = new TestLttngCallStackModule();
03f0b0b1 146 try {
0ff9e595
AM
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();
bd64ee73 162 }
50659279 163 }
0ff9e595
AM
164 suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace));
165
166 ustTrace.dispose();
95a0cba5
MAL
167 deleteDirectory(suppDir);
168 assertFalse(suppDir.exists());
50659279
AM
169 }
170
171 /**
172 * Test that the callstack state system is there and contains data.
173 */
174 @Test
175 public void testConstruction() {
176 assertNotNull(fSS);
177 assertTrue(fSS.getNbAttributes() > 0);
178 }
179
180 /**
181 * Test the callstack at the beginning of the state system.
182 */
183 @Test
184 public void testCallStackBegin() {
185 long start = fSS.getStartTime();
186 String[] cs = getCallStack(fSS, getProcName(), start);
187 assertEquals(1, cs.length);
188
189 assertEquals("40472b", cs[0]);
190 }
191
192 /**
193 * Test the callstack somewhere in the trace.
194 */
195 @Test
196 public void testCallStack1() {
197 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(0));
198 assertEquals(2, cs.length);
199
200 assertEquals("40472b", cs[0]);
201 assertEquals("403d60", cs[1]);
202 }
203
204 /**
205 * Test the callstack somewhere in the trace.
206 */
207 @Test
208 public void testCallStack2() {
209 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(1));
210 assertEquals(3, cs.length);
211
212 assertEquals("40472b", cs[0]);
213 assertEquals("403b14", cs[1]);
214 assertEquals("401b23", cs[2]);
215 }
216
217 /**
218 * Test the callstack somewhere in the trace.
219 */
220 @Test
221 public void testCallStack3() {
222 String[] cs = getCallStack(fSS, getProcName(), getTestTimestamp(2));
223 assertEquals(4, cs.length);
224
225 assertEquals("40472b", cs[0]);
226 assertEquals("4045c8", cs[1]);
227 assertEquals("403760", cs[2]);
228 assertEquals("401aac", cs[3]);
229 }
230
231 /**
232 * Test the callstack at the end of the trace/state system.
233 */
234 @Test
235 public void testCallStackEnd() {
236 long end = fSS.getCurrentEndTime();
237 String[] cs = getCallStack(fSS, getProcName(), end);
238 assertEquals(3, cs.length);
239
240 assertEquals("40472b", cs[0]);
241 assertEquals("4045c8", cs[1]);
242 assertEquals("403760", cs[2]);
243 }
244
245 // ------------------------------------------------------------------------
246 // Utility methods
247 // ------------------------------------------------------------------------
248
249 /** Empty and delete a directory */
250 private static void deleteDirectory(File dir) {
251 /* Assuming the dir only contains file or empty directories */
252 for (File file : dir.listFiles()) {
253 file.delete();
254 }
255 dir.delete();
256 }
257
258 /** Get the callstack for the given timestamp, for this particular trace */
259 private static String[] getCallStack(ITmfStateSystem ss, String processName, long timestamp) {
260 try {
261 int stackAttribute = ss.getQuarkAbsolute("Threads", processName, "CallStack");
262 List<ITmfStateInterval> state = ss.queryFullState(timestamp);
263 int depth = state.get(stackAttribute).getStateValue().unboxInt();
264
265 int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth));
1dd75589
AM
266 ITmfStateValue expectedValue = state.get(stackTop).getStateValue();
267 ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute);
268 assertNotNull(interval);
269 assertEquals(expectedValue, interval.getStateValue());
50659279
AM
270
271 String[] ret = new String[depth];
272 for (int i = 0; i < depth; i++) {
273 int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1));
274 ret[i] = state.get(quark).getStateValue().unboxStr();
275 }
276 return ret;
277
278 } catch (AttributeNotFoundException e) {
279 fail(e.getMessage());
280 } catch (StateSystemDisposedException e) {
281 fail(e.getMessage());
282 }
283 fail();
284 return null;
285 }
286
287 private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule {
288
289 @Override
290 protected ITmfStateProvider createStateProvider() {
d0c7e4ba 291 return new LttngUstCallStackProvider(checkNotNull(getTrace()));
50659279 292 }
50659279
AM
293 }
294}
This page took 0.065257 seconds and 5 git commands to generate.