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