Commit | Line | Data |
---|---|---|
efc403bb AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal | |
4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> | |
5 | * | |
6 | * All rights reserved. This program and the accompanying materials are | |
7 | * made available under the terms of the Eclipse Public License v1.0 which | |
8 | * accompanies this distribution, and is available at | |
9 | * http://www.eclipse.org/legal/epl-v10.html | |
10 | * | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider; | |
14 | ||
15 | import static org.junit.Assert.*; | |
16 | ||
17 | import java.io.File; | |
18 | import java.io.FileNotFoundException; | |
19 | import java.io.IOException; | |
20 | import java.io.PrintWriter; | |
21 | import java.util.List; | |
22 | ||
23 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; | |
24 | import org.eclipse.linuxtools.tmf.core.statesystem.AttributeNotFoundException; | |
25 | import org.eclipse.linuxtools.tmf.core.statesystem.StateHistorySystem; | |
26 | import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException; | |
27 | import org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree.HistoryTreeBackend; | |
28 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.HistoryBuilder; | |
29 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateChangeInput; | |
30 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend; | |
31 | import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException; | |
dc0f7bfe | 32 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CtfKernelStateInput; |
efc403bb AM |
33 | import org.junit.*; |
34 | ||
35 | /** | |
36 | * Unit tests for the StateHistorySystem, which uses a full (non-partial) | |
37 | * history and the non-threaded CTF kernel handler. | |
38 | * | |
39 | * @author alexmont | |
40 | * | |
41 | */ | |
42 | @SuppressWarnings("nls") | |
43 | public class StateSystemFullHistoryTest { | |
44 | ||
ebd67b34 AM |
45 | static File stateFile; |
46 | static File stateFileBenchmark; | |
efc403bb | 47 | |
ebd67b34 AM |
48 | static HistoryBuilder builder; |
49 | static IStateChangeInput input; | |
50 | static IStateHistoryBackend hp; | |
51 | static StateHistorySystem shs; | |
efc403bb | 52 | |
cc2292bd AM |
53 | /* Offset in the trace + start time of the trace */ |
54 | private final static long interestingTimestamp1 = 18670067372290L + 1331649577946812237L; | |
2359ecca | 55 | |
efc403bb AM |
56 | protected static String getTestFileName() { |
57 | return "/tmp/statefile.ht"; //$NON-NLS-1$ | |
58 | } | |
59 | ||
60 | @BeforeClass | |
61 | public static void initialize() { | |
62 | stateFile = new File(getTestFileName()); | |
63 | stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$ | |
64 | try { | |
dc0f7bfe | 65 | input = new CtfKernelStateInput(CtfTestFiles.getTestTrace()); |
efc403bb AM |
66 | hp = new HistoryTreeBackend(stateFile, input.getStartTime()); |
67 | builder = new HistoryBuilder(input, hp); | |
68 | } catch (Exception e) { | |
69 | e.printStackTrace(); | |
70 | } | |
71 | builder.run(); | |
72 | shs = (StateHistorySystem) builder.getSS(); | |
73 | } | |
74 | ||
75 | @AfterClass | |
76 | public static void cleanup() { | |
ebd67b34 AM |
77 | boolean ret1, ret2; |
78 | ret1 = stateFile.delete(); | |
79 | ret2 = stateFileBenchmark.delete(); | |
80 | if ( !(ret1 && ret2) ) { | |
81 | System.err.println("Error cleaning up during unit testing, " + | |
82 | "you might have leftovers state history files in /tmp"); | |
83 | } | |
efc403bb AM |
84 | } |
85 | ||
86 | /** | |
87 | * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow | |
88 | * us to @Test the @BeforeClass... | |
89 | */ | |
90 | @Test | |
91 | public void testBuild() { | |
92 | HistoryBuilder zebuilder; | |
93 | IStateChangeInput zeinput; | |
94 | IStateHistoryBackend zehp; | |
95 | ||
96 | try { | |
dc0f7bfe | 97 | zeinput = new CtfKernelStateInput(CtfTestFiles.getTestTrace()); |
efc403bb AM |
98 | zehp = new HistoryTreeBackend(stateFileBenchmark, |
99 | zeinput.getStartTime()); | |
100 | zebuilder = new HistoryBuilder(zeinput, zehp); | |
101 | zebuilder.run(); | |
102 | } catch (Exception e) { | |
103 | e.printStackTrace(); | |
104 | } | |
105 | } | |
106 | ||
107 | @Test | |
108 | public void testOpenExistingStateFile() { | |
109 | IStateHistoryBackend hp2 = null; | |
110 | StateHistorySystem shs2 = null; | |
111 | try { | |
112 | /* 'newStateFile' should have already been created */ | |
113 | hp2 = new HistoryTreeBackend(stateFile); | |
114 | shs2 = new StateHistorySystem(hp2, false); | |
115 | } catch (IOException e) { | |
116 | e.printStackTrace(); | |
117 | } | |
118 | assertTrue(shs2 != null); | |
119 | } | |
120 | ||
121 | @Test | |
122 | public void testFullQuery1() throws StateValueTypeException, | |
123 | AttributeNotFoundException, TimeRangeException { | |
124 | ||
dad01d27 | 125 | List<ITmfStateInterval> list; |
efc403bb | 126 | ITmfStateInterval interval; |
ae54340d | 127 | int quark, quark2, valueInt; |
efc403bb AM |
128 | String valueStr; |
129 | ||
dad01d27 | 130 | list = shs.loadStateAtTime(interestingTimestamp1); |
efc403bb AM |
131 | |
132 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dad01d27 | 133 | interval = list.get(quark); |
efc403bb | 134 | valueInt = interval.getStateValue().unboxInt(); |
2359ecca | 135 | assertEquals(1397, valueInt); |
efc403bb | 136 | |
2359ecca | 137 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); |
dad01d27 | 138 | interval = list.get(quark); |
efc403bb | 139 | valueStr = interval.getStateValue().unboxStr(); |
2359ecca | 140 | assertEquals("gdbus", valueStr); |
efc403bb | 141 | |
ae54340d AM |
142 | /* Query a stack attribute, has to be done in two passes */ |
143 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack"); | |
ab9bc51b | 144 | interval = list.get(quark); |
ae54340d AM |
145 | valueInt = interval.getStateValue().unboxInt(); /* The stack depth */ |
146 | quark2 = shs.getQuarkRelative(quark, Integer.toString(valueInt)); | |
ab9bc51b | 147 | interval = list.get(quark2); |
ae54340d AM |
148 | valueStr = interval.getStateValue().unboxStr(); |
149 | assertTrue(valueStr.equals("sys_poll")); | |
efc403bb AM |
150 | } |
151 | ||
152 | @Test | |
153 | public void testFullQuery2() { | |
154 | // | |
155 | } | |
156 | ||
157 | @Test | |
158 | public void testFullQuery3() { | |
159 | // | |
160 | } | |
161 | ||
162 | @Test | |
163 | public void testSingleQuery1() throws AttributeNotFoundException, | |
164 | TimeRangeException, StateValueTypeException { | |
165 | ||
2359ecca | 166 | long timestamp = interestingTimestamp1; |
efc403bb AM |
167 | int quark; |
168 | ITmfStateInterval interval; | |
169 | String valueStr; | |
170 | ||
2359ecca | 171 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); |
efc403bb AM |
172 | interval = shs.querySingleState(timestamp, quark); |
173 | valueStr = interval.getStateValue().unboxStr(); | |
2359ecca | 174 | assertEquals("gdbus", valueStr); |
efc403bb AM |
175 | } |
176 | ||
177 | @Test | |
178 | public void testSingleQuery2() { | |
179 | // | |
180 | } | |
181 | ||
182 | @Test | |
183 | public void testSingleQuery3() { | |
184 | // | |
185 | } | |
186 | ||
ab9bc51b AM |
187 | /** |
188 | * Test a range query (with no resolution parameter, so all intervals) | |
189 | */ | |
efc403bb AM |
190 | @Test |
191 | public void testRangeQuery1() throws AttributeNotFoundException, | |
192 | TimeRangeException, StateValueTypeException { | |
193 | ||
2359ecca | 194 | long time1 = interestingTimestamp1; |
dc0f7bfe | 195 | long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC; |
efc403bb AM |
196 | int quark; |
197 | List<ITmfStateInterval> intervals; | |
198 | ||
199 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
200 | intervals = shs.queryHistoryRange(quark, time1, time2); | |
2359ecca AM |
201 | assertEquals(487, intervals.size()); /* Number of context switches! */ |
202 | assertEquals(1685, intervals.get(100).getStateValue().unboxInt()); | |
cc2292bd | 203 | assertEquals(1331668248427681372L, intervals.get(205).getEndTime()); |
efc403bb AM |
204 | } |
205 | ||
ab9bc51b AM |
206 | /** |
207 | * Test a range query with a resolution | |
208 | */ | |
209 | @Test | |
210 | public void testRangeQuery2() throws AttributeNotFoundException, | |
211 | TimeRangeException, StateValueTypeException { | |
212 | ||
213 | long time1 = interestingTimestamp1; | |
dc0f7bfe | 214 | long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC; |
ab9bc51b AM |
215 | long resolution = 1000000; /* One query every millisecond */ |
216 | int quark; | |
217 | List<ITmfStateInterval> intervals; | |
218 | ||
219 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
220 | intervals = shs.queryHistoryRange(quark, time1, time2, resolution); | |
221 | assertEquals(129, intervals.size()); /* Number of context switches! */ | |
222 | assertEquals(1452, intervals.get(50).getStateValue().unboxInt()); | |
cc2292bd | 223 | assertEquals(1331668248784789238L, intervals.get(100).getEndTime()); |
ab9bc51b AM |
224 | } |
225 | ||
efc403bb AM |
226 | /** |
227 | * Ask for a time range outside of the trace's range | |
228 | * | |
229 | * @throws TimeRangeException | |
230 | */ | |
231 | @Test(expected = TimeRangeException.class) | |
232 | public void testFullQueryInvalidTime1() throws TimeRangeException { | |
dc0f7bfe | 233 | long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; |
2359ecca | 234 | shs.loadStateAtTime(ts); |
efc403bb AM |
235 | |
236 | } | |
237 | ||
238 | @Test(expected = TimeRangeException.class) | |
239 | public void testFullQueryInvalidTime2() throws TimeRangeException { | |
dc0f7bfe | 240 | long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; |
2359ecca | 241 | shs.loadStateAtTime(ts); |
efc403bb AM |
242 | |
243 | } | |
244 | ||
245 | @Test(expected = TimeRangeException.class) | |
246 | public void testSingleQueryInvalidTime1() | |
247 | throws AttributeNotFoundException, TimeRangeException { | |
248 | ||
249 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dc0f7bfe | 250 | long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; |
2359ecca | 251 | shs.querySingleState(ts, quark); |
efc403bb AM |
252 | } |
253 | ||
254 | @Test(expected = TimeRangeException.class) | |
255 | public void testSingleQueryInvalidTime2() | |
256 | throws AttributeNotFoundException, TimeRangeException { | |
257 | ||
258 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dc0f7bfe | 259 | long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; |
2359ecca | 260 | shs.querySingleState(ts, quark); |
efc403bb AM |
261 | } |
262 | ||
263 | @Test(expected = TimeRangeException.class) | |
264 | public void testRangeQueryInvalidTime1() throws AttributeNotFoundException, | |
265 | TimeRangeException { | |
266 | ||
267 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dc0f7bfe AM |
268 | long ts1 = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ |
269 | long ts2 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */ | |
efc403bb | 270 | |
2359ecca | 271 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
272 | } |
273 | ||
274 | @Test(expected = TimeRangeException.class) | |
275 | public void testRangeQueryInvalidTime2() throws TimeRangeException, | |
276 | AttributeNotFoundException { | |
277 | ||
278 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dc0f7bfe AM |
279 | long ts1 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */ |
280 | long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ | |
efc403bb | 281 | |
2359ecca | 282 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
283 | } |
284 | ||
285 | @Test(expected = TimeRangeException.class) | |
286 | public void testRangeQueryInvalidTime3() throws TimeRangeException, | |
287 | AttributeNotFoundException { | |
288 | ||
289 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
dc0f7bfe AM |
290 | long ts1 = CtfTestFiles.startTime - 1L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ |
291 | long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ | |
efc403bb | 292 | |
2359ecca | 293 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
294 | } |
295 | ||
296 | /** | |
297 | * Ask for a non-existing attribute | |
298 | * | |
299 | * @throws AttributeNotFoundException | |
300 | */ | |
301 | @Test(expected = AttributeNotFoundException.class) | |
302 | public void testQueryInvalidAttribute() throws AttributeNotFoundException { | |
303 | ||
304 | shs.getQuarkAbsolute("There", "is", "no", "cow", "level"); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Query but with the wrong State Value type | |
309 | * | |
310 | * @throws StateValueTypeException | |
311 | * @throws AttributeNotFoundException | |
312 | * @throws TimeRangeException | |
313 | */ | |
314 | @Test(expected = StateValueTypeException.class) | |
315 | public void testQueryInvalidValuetype1() throws StateValueTypeException, | |
316 | AttributeNotFoundException, TimeRangeException { | |
dad01d27 | 317 | List<ITmfStateInterval> list; |
efc403bb AM |
318 | ITmfStateInterval interval; |
319 | int quark; | |
320 | ||
dad01d27 | 321 | list = shs.loadStateAtTime(interestingTimestamp1); |
efc403bb | 322 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); |
dad01d27 | 323 | interval = list.get(quark); |
2359ecca AM |
324 | |
325 | /* This is supposed to be an int value */ | |
326 | interval.getStateValue().unboxStr(); | |
efc403bb AM |
327 | } |
328 | ||
329 | @Test(expected = StateValueTypeException.class) | |
330 | public void testQueryInvalidValuetype2() throws StateValueTypeException, | |
331 | AttributeNotFoundException, TimeRangeException { | |
dad01d27 | 332 | List<ITmfStateInterval> list; |
efc403bb AM |
333 | ITmfStateInterval interval; |
334 | int quark; | |
335 | ||
dad01d27 | 336 | list = shs.loadStateAtTime(interestingTimestamp1); |
2359ecca | 337 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); |
dad01d27 | 338 | interval = list.get(quark); |
2359ecca AM |
339 | |
340 | /* This is supposed to be a String value */ | |
341 | interval.getStateValue().unboxInt(); | |
efc403bb AM |
342 | } |
343 | ||
344 | @Test | |
345 | public void testFullAttributeName() throws AttributeNotFoundException { | |
346 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
347 | String name = shs.getFullAttributePath(quark); | |
2359ecca | 348 | assertEquals(name, "CPUs/0/Current_thread"); |
efc403bb AM |
349 | } |
350 | ||
6abc2d88 AM |
351 | @Test |
352 | public void testGetQuarks_begin() { | |
353 | List<Integer> list = shs.getQuarks("*", "1577", "Exec_name"); | |
354 | ||
355 | assertEquals(1, list.size()); | |
356 | assertEquals(Integer.valueOf(479), list.get(0)); | |
357 | } | |
358 | ||
359 | @Test | |
360 | public void testGetQuarks_middle() { | |
361 | List<Integer> list = shs.getQuarks("Threads", "*", "Exec_name"); | |
362 | ||
363 | assertEquals(Integer.valueOf(36), list.get(4)); | |
364 | assertEquals(Integer.valueOf(100), list.get(10)); | |
365 | assertEquals(Integer.valueOf(116), list.get(12)); | |
366 | } | |
367 | ||
368 | @Test | |
369 | public void testGetQuarks_end() { | |
370 | List<Integer> list = shs.getQuarks("Threads", "1577", "*"); | |
371 | ||
372 | assertEquals(3, list.size()); | |
373 | assertEquals(Integer.valueOf(479), list.get(1)); | |
374 | } | |
375 | ||
efc403bb AM |
376 | @Test |
377 | public void testDebugPrinting() throws FileNotFoundException { | |
6f04204e AM |
378 | PrintWriter pw = new PrintWriter(new File("/dev/null")); |
379 | shs.debugPrint(pw); | |
380 | pw.close(); | |
efc403bb AM |
381 | } |
382 | } |