-
Notifications
You must be signed in to change notification settings - Fork 13
/
AbstractProviderTest.java
316 lines (273 loc) · 10.9 KB
/
AbstractProviderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*******************************************************************************
* Copyright (c) 2014, 2015 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexandre Montplaisir - Initial API and implementation
*******************************************************************************/
package org.eclipse.tracecompass.incubator.callstack.core.tests.lttng2.ust.callstack;
import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.internal.lttng2.ust.core.callstack.LttngUstCallStackProvider;
import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
import org.eclipse.tracecompass.analysis.profiling.core.callstack.CallStackStateProvider;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
/**
* Base class for the UST callstack state provider tests.
*
* @author Alexandre Montplaisir
*/
@SuppressWarnings("restriction")
public abstract class AbstractProviderTest {
/** Time-out tests after 1 minute. */
@Rule
public TestRule globalTimeout = new Timeout(1, TimeUnit.MINUTES);
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
private static final @NonNull CtfTestTrace otherUstTrace = CtfTestTrace.HELLO_LOST;
private CtfTmfTrace fTrace = null;
private ITmfStateSystem fSS = null;
private TestLttngCallStackModule fModule;
// ------------------------------------------------------------------------
// Abstract methods
// ------------------------------------------------------------------------
/**
* @return The test trace to use for this test
*/
protected abstract @NonNull CtfTestTrace getTestTrace();
/**
* @return The ID of the process the desired thread belongs to
*/
protected abstract int getProcessId();
/**
* @return The name of the executable process in that particular trace
*/
protected abstract String getThreadName();
/**
* Get the list of timestamps to query in that trace.
*
* @param index
* Which of the test timestamps?
* @return That particular timestamp
*/
protected abstract long getTestTimestamp(int index);
// ------------------------------------------------------------------------
// Maintenance
// ------------------------------------------------------------------------
/**
* Perform pre-class initialization.
*
* @throws TmfTraceException
* Exception thrown by trace
*/
@Before
public void setUp() throws TmfTraceException {
CtfTestTrace testTrace = getTestTrace();
CtfTmfTrace ctftrace = CtfTmfTestTraceUtils.getTrace(testTrace);
LttngUstTrace trace = new LttngUstTrace();
ctftrace.dispose();
trace.initTrace(null, ctftrace.getPath(), ITmfEvent.class);
fTrace = trace;
fModule = new TestLttngCallStackModule();
try {
assertTrue(fModule.setTrace(trace));
} catch (TmfAnalysisException e) {
fail();
}
fModule.schedule();
assertTrue(fModule.waitForCompletion());
fSS = fModule.getStateSystem();
assertNotNull(fSS);
}
/**
* Perform post-class clean-up.
*/
@After
public void tearDown() {
fModule.dispose();
CtfTmfTrace trace = fTrace;
if (trace != null) {
trace.dispose();
File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(trace));
deleteDirectory(suppDir);
}
}
// ------------------------------------------------------------------------
// Test methods
// ------------------------------------------------------------------------
/**
* Test the handling of generic UST traces who do not contain the required
* information.
*/
@Test
public void testOtherUstTrace() {
/* Initialize the trace and analysis module */
File suppDir;
CtfTmfTrace ustTrace = CtfTmfTestTraceUtils.getTrace(otherUstTrace);
TestLttngCallStackModule module = null;
try {
module = new TestLttngCallStackModule();
try {
assertTrue(module.setTrace(ustTrace));
} catch (TmfAnalysisException e) {
fail();
}
module.schedule();
assertTrue(module.waitForCompletion());
/* Make sure the generated state system exists, but is empty */
ITmfStateSystem ss = module.getStateSystem();
assertNotNull(ss);
assertTrue(ss.getStartTime() >= ustTrace.getStartTime().toNanos());
assertEquals(0, ss.getNbAttributes());
} finally {
if (module != null) {
module.dispose();
}
}
suppDir = new File(TmfTraceManager.getSupplementaryFileDir(ustTrace));
ustTrace.dispose();
deleteDirectory(suppDir);
assertFalse(suppDir.exists());
}
/**
* Test that the callstack state system is there and contains data.
*/
@Test
public void testConstruction() {
assertNotNull(fSS);
assertTrue(fSS.getNbAttributes() > 0);
}
/**
* Test the callstack at the beginning of the state system.
*/
@Test
public void testCallStackBegin() {
long start = fSS.getStartTime();
String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), start);
assertEquals(1, cs.length);
assertEquals("40472b", cs[0]);
}
/**
* Test the callstack somewhere in the trace.
*/
@Test
public void testCallStack1() {
String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(0));
assertEquals(2, cs.length);
assertEquals("40472b", cs[0]);
assertEquals("403d60", cs[1]);
}
/**
* Test the callstack somewhere in the trace.
*/
@Test
public void testCallStack2() {
String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(1));
assertEquals(3, cs.length);
assertEquals("40472b", cs[0]);
assertEquals("403b14", cs[1]);
assertEquals("401b23", cs[2]);
}
/**
* Test the callstack somewhere in the trace.
*/
@Test
public void testCallStack3() {
String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), getTestTimestamp(2));
assertEquals(4, cs.length);
assertEquals("40472b", cs[0]);
assertEquals("4045c8", cs[1]);
assertEquals("403760", cs[2]);
assertEquals("401aac", cs[3]);
}
/**
* Test the callstack at the end of the trace/state system.
*/
@Test
public void testCallStackEnd() {
long end = fSS.getCurrentEndTime();
String[] cs = getCallStack(fSS, getProcessId(), getThreadName(), end);
assertEquals(3, cs.length);
assertEquals("40472b", cs[0]);
assertEquals("4045c8", cs[1]);
assertEquals("403760", cs[2]);
}
// ------------------------------------------------------------------------
// Utility methods
// ------------------------------------------------------------------------
/** Empty and delete a directory */
private static void deleteDirectory(File dir) {
/* Assuming the dir only contains file or empty directories */
for (File file : dir.listFiles()) {
file.delete();
}
dir.delete();
}
/** Get the callstack for the given timestamp, for this particular trace */
private static String[] getCallStack(ITmfStateSystem ss, int pid, String threadName, long timestamp) {
try {
String processName = (pid == CallStackStateProvider.UNKNOWN_PID) ? CallStackStateProvider.UNKNOWN : Integer.toString(pid);
int stackAttribute = ss.getQuarkAbsolute("Processes", processName, threadName, "CallStack");
List<ITmfStateInterval> state = ss.queryFullState(timestamp);
int depth = state.get(stackAttribute).getStateValue().unboxInt();
int stackTop = ss.getQuarkRelative(stackAttribute, String.valueOf(depth));
ITmfStateValue expectedValue = state.get(stackTop).getStateValue();
ITmfStateInterval interval = StateSystemUtils.querySingleStackTop(ss, timestamp, stackAttribute);
assertNotNull(interval);
assertEquals(expectedValue, interval.getStateValue());
String[] ret = new String[depth];
for (int i = 0; i < depth; i++) {
int quark = ss.getQuarkRelative(stackAttribute, String.valueOf(i + 1));
ret[i] = Long.toHexString(state.get(quark).getStateValue().unboxLong());
}
return ret;
} catch (AttributeNotFoundException e) {
fail(e.getMessage());
} catch (StateSystemDisposedException e) {
fail(e.getMessage());
}
fail();
return null;
}
private class TestLttngCallStackModule extends TmfStateSystemAnalysisModule {
@Override
protected ITmfStateProvider createStateProvider() {
return new LttngUstCallStackProvider(checkNotNull(getTrace()));
}
}
}