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> | |
e743c3b8 | 5 | * |
efc403bb AM |
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 | |
e743c3b8 | 10 | * |
efc403bb AM |
11 | *******************************************************************************/ |
12 | ||
13 | package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider; | |
14 | ||
6e71ce46 AM |
15 | import static org.junit.Assert.assertEquals; |
16 | import static org.junit.Assert.assertNotNull; | |
17 | import static org.junit.Assert.assertTrue; | |
efc403bb AM |
18 | |
19 | import java.io.File; | |
efc403bb AM |
20 | import java.util.List; |
21 | ||
6e71ce46 AM |
22 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes; |
23 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CtfKernelStateInput; | |
6d08acca | 24 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
96345c5a | 25 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
6d08acca AM |
26 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
27 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
1e4bb526 | 28 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
efc403bb | 29 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; |
18ab1d18 | 30 | import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput; |
f1f86dfb | 31 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; |
6e71ce46 AM |
32 | import org.eclipse.linuxtools.tmf.core.statesystem.StateSystemManager; |
33 | import org.junit.AfterClass; | |
34 | import org.junit.BeforeClass; | |
35 | import org.junit.Test; | |
efc403bb AM |
36 | |
37 | /** | |
38 | * Unit tests for the StateHistorySystem, which uses a full (non-partial) | |
39 | * history and the non-threaded CTF kernel handler. | |
e743c3b8 | 40 | * |
efc403bb | 41 | * @author alexmont |
e743c3b8 | 42 | * |
efc403bb | 43 | */ |
e743c3b8 | 44 | @SuppressWarnings({"nls", "javadoc"}) |
efc403bb AM |
45 | public class StateSystemFullHistoryTest { |
46 | ||
ebd67b34 AM |
47 | static File stateFile; |
48 | static File stateFileBenchmark; | |
efc403bb | 49 | |
ebd67b34 | 50 | static IStateChangeInput input; |
f1f86dfb | 51 | static ITmfStateSystem ssq; |
efc403bb | 52 | |
cc2292bd AM |
53 | /* Offset in the trace + start time of the trace */ |
54 | private final static long interestingTimestamp1 = 18670067372290L + 1331649577946812237L; | |
2359ecca | 55 | |
a76f1067 AM |
56 | /* ID we give to the state system we build */ |
57 | private static final String STATE_ID = "test-ss"; | |
58 | ||
efc403bb AM |
59 | protected static String getTestFileName() { |
60 | return "/tmp/statefile.ht"; //$NON-NLS-1$ | |
61 | } | |
62 | ||
63 | @BeforeClass | |
64 | public static void initialize() { | |
65 | stateFile = new File(getTestFileName()); | |
66 | stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$ | |
67 | try { | |
dc0f7bfe | 68 | input = new CtfKernelStateInput(CtfTestFiles.getTestTrace()); |
a76f1067 | 69 | ssq = StateSystemManager.loadStateHistory(stateFile, input, STATE_ID, true); |
efc403bb AM |
70 | } catch (Exception e) { |
71 | e.printStackTrace(); | |
72 | } | |
efc403bb AM |
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) ) { | |
6b78e55b AM |
81 | System.err.println("Error cleaning up during unit testing, " + //$NON-NLS-1$ |
82 | "you might have leftovers state history files in /tmp"); //$NON-NLS-1$ | |
ebd67b34 | 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... | |
e743c3b8 AM |
89 | * |
90 | * @throws IOException | |
91 | * @throws TmfTraceException | |
efc403bb AM |
92 | */ |
93 | @Test | |
51e216bd | 94 | public void testBuild() throws TmfTraceException { |
6e71ce46 | 95 | IStateChangeInput input2; |
f1f86dfb | 96 | ITmfStateSystem ssb2; |
e743c3b8 | 97 | |
6e71ce46 | 98 | input2 = new CtfKernelStateInput(CtfTestFiles.getTestTrace()); |
a76f1067 | 99 | ssb2 = StateSystemManager.loadStateHistory(stateFileBenchmark, input2, STATE_ID, true); |
1e4bb526 | 100 | |
6e71ce46 AM |
101 | assertEquals(CtfTestFiles.startTime, ssb2.getStartTime()); |
102 | assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime()); | |
efc403bb AM |
103 | } |
104 | ||
105 | @Test | |
51e216bd | 106 | public void testOpenExistingStateFile() throws TmfTraceException { |
f1f86dfb | 107 | ITmfStateSystem ssb2; |
1e4bb526 AM |
108 | |
109 | /* 'newStateFile' should have already been created */ | |
a76f1067 | 110 | ssb2 = StateSystemManager.loadStateHistory(stateFile, null, STATE_ID, true); |
1e4bb526 | 111 | |
d26f90fd | 112 | assertNotNull(ssb2); |
6e71ce46 AM |
113 | assertEquals(CtfTestFiles.startTime, ssb2.getStartTime()); |
114 | assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime()); | |
efc403bb AM |
115 | } |
116 | ||
117 | @Test | |
118 | public void testFullQuery1() throws StateValueTypeException, | |
96345c5a AM |
119 | AttributeNotFoundException, TimeRangeException, |
120 | StateSystemDisposedException { | |
efc403bb | 121 | |
dad01d27 | 122 | List<ITmfStateInterval> list; |
efc403bb | 123 | ITmfStateInterval interval; |
4b3ed6ff | 124 | int quark, valueInt; |
efc403bb AM |
125 | String valueStr; |
126 | ||
6e71ce46 | 127 | list = ssq.queryFullState(interestingTimestamp1); |
efc403bb | 128 | |
6e71ce46 | 129 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
dad01d27 | 130 | interval = list.get(quark); |
efc403bb | 131 | valueInt = interval.getStateValue().unboxInt(); |
2359ecca | 132 | assertEquals(1397, valueInt); |
efc403bb | 133 | |
6e71ce46 | 134 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); |
dad01d27 | 135 | interval = list.get(quark); |
efc403bb | 136 | valueStr = interval.getStateValue().unboxStr(); |
2359ecca | 137 | assertEquals("gdbus", valueStr); |
efc403bb | 138 | |
6e71ce46 | 139 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL); |
ab9bc51b | 140 | interval = list.get(quark); |
ae54340d AM |
141 | valueStr = interval.getStateValue().unboxStr(); |
142 | assertTrue(valueStr.equals("sys_poll")); | |
efc403bb AM |
143 | } |
144 | ||
145 | @Test | |
146 | public void testFullQuery2() { | |
147 | // | |
148 | } | |
149 | ||
150 | @Test | |
151 | public void testFullQuery3() { | |
152 | // | |
153 | } | |
154 | ||
155 | @Test | |
156 | public void testSingleQuery1() throws AttributeNotFoundException, | |
96345c5a AM |
157 | TimeRangeException, StateValueTypeException, |
158 | StateSystemDisposedException { | |
efc403bb | 159 | |
2359ecca | 160 | long timestamp = interestingTimestamp1; |
efc403bb AM |
161 | int quark; |
162 | ITmfStateInterval interval; | |
163 | String valueStr; | |
164 | ||
6e71ce46 AM |
165 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); |
166 | interval = ssq.querySingleState(timestamp, quark); | |
efc403bb | 167 | valueStr = interval.getStateValue().unboxStr(); |
2359ecca | 168 | assertEquals("gdbus", valueStr); |
efc403bb AM |
169 | } |
170 | ||
171 | @Test | |
172 | public void testSingleQuery2() { | |
173 | // | |
174 | } | |
175 | ||
176 | @Test | |
177 | public void testSingleQuery3() { | |
178 | // | |
179 | } | |
180 | ||
ab9bc51b AM |
181 | /** |
182 | * Test a range query (with no resolution parameter, so all intervals) | |
183 | */ | |
efc403bb AM |
184 | @Test |
185 | public void testRangeQuery1() throws AttributeNotFoundException, | |
96345c5a AM |
186 | TimeRangeException, StateValueTypeException, |
187 | StateSystemDisposedException { | |
efc403bb | 188 | |
2359ecca | 189 | long time1 = interestingTimestamp1; |
dc0f7bfe | 190 | long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC; |
efc403bb AM |
191 | int quark; |
192 | List<ITmfStateInterval> intervals; | |
193 | ||
6e71ce46 AM |
194 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
195 | intervals = ssq.queryHistoryRange(quark, time1, time2); | |
2359ecca AM |
196 | assertEquals(487, intervals.size()); /* Number of context switches! */ |
197 | assertEquals(1685, intervals.get(100).getStateValue().unboxInt()); | |
cc2292bd | 198 | assertEquals(1331668248427681372L, intervals.get(205).getEndTime()); |
efc403bb AM |
199 | } |
200 | ||
1d3d1293 AM |
201 | /** |
202 | * Range query, but with a t2 far off the end of the trace. | |
203 | * The result should still be valid. | |
204 | */ | |
205 | @Test | |
206 | public void testRangeQuery2() throws TimeRangeException, | |
96345c5a | 207 | AttributeNotFoundException, StateSystemDisposedException { |
1d3d1293 AM |
208 | |
209 | List<ITmfStateInterval> intervals; | |
e743c3b8 | 210 | |
6e71ce46 AM |
211 | int quark = ssq.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1"); |
212 | long ts1 = ssq.getStartTime(); /* start of the trace */ | |
1d3d1293 AM |
213 | long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid, but ignored */ |
214 | ||
6e71ce46 | 215 | intervals = ssq.queryHistoryRange(quark, ts1, ts2); |
1d3d1293 | 216 | |
06552532 AM |
217 | /* Activity of IRQ 1 over the whole trace */ |
218 | assertEquals(65, intervals.size()); | |
1d3d1293 AM |
219 | } |
220 | ||
ab9bc51b AM |
221 | /** |
222 | * Test a range query with a resolution | |
223 | */ | |
224 | @Test | |
1d3d1293 | 225 | public void testRangeQuery3() throws AttributeNotFoundException, |
96345c5a AM |
226 | TimeRangeException, StateValueTypeException, |
227 | StateSystemDisposedException { | |
ab9bc51b AM |
228 | |
229 | long time1 = interestingTimestamp1; | |
dc0f7bfe | 230 | long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC; |
ab9bc51b AM |
231 | long resolution = 1000000; /* One query every millisecond */ |
232 | int quark; | |
233 | List<ITmfStateInterval> intervals; | |
234 | ||
6e71ce46 | 235 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
b5a8d0cc | 236 | intervals = ssq.queryHistoryRange(quark, time1, time2, resolution, null); |
a9f966a2 | 237 | assertEquals(126, intervals.size()); /* Number of context switches! */ |
ab9bc51b | 238 | assertEquals(1452, intervals.get(50).getStateValue().unboxInt()); |
a9f966a2 | 239 | assertEquals(1331668248815698779L, intervals.get(100).getEndTime()); |
ab9bc51b AM |
240 | } |
241 | ||
efc403bb AM |
242 | /** |
243 | * Ask for a time range outside of the trace's range | |
efc403bb AM |
244 | */ |
245 | @Test(expected = TimeRangeException.class) | |
96345c5a AM |
246 | public void testFullQueryInvalidTime1() throws TimeRangeException, |
247 | StateSystemDisposedException { | |
dc0f7bfe | 248 | long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; |
6e71ce46 | 249 | ssq.queryFullState(ts); |
efc403bb AM |
250 | } |
251 | ||
252 | @Test(expected = TimeRangeException.class) | |
96345c5a AM |
253 | public void testFullQueryInvalidTime2() throws TimeRangeException, |
254 | StateSystemDisposedException { | |
dc0f7bfe | 255 | long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; |
6e71ce46 | 256 | ssq.queryFullState(ts); |
efc403bb AM |
257 | } |
258 | ||
259 | @Test(expected = TimeRangeException.class) | |
260 | public void testSingleQueryInvalidTime1() | |
96345c5a AM |
261 | throws AttributeNotFoundException, TimeRangeException, |
262 | StateSystemDisposedException { | |
efc403bb | 263 | |
6e71ce46 | 264 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
dc0f7bfe | 265 | long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; |
6e71ce46 | 266 | ssq.querySingleState(ts, quark); |
efc403bb AM |
267 | } |
268 | ||
269 | @Test(expected = TimeRangeException.class) | |
270 | public void testSingleQueryInvalidTime2() | |
96345c5a AM |
271 | throws AttributeNotFoundException, TimeRangeException, |
272 | StateSystemDisposedException { | |
efc403bb | 273 | |
6e71ce46 | 274 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
dc0f7bfe | 275 | long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; |
6e71ce46 | 276 | ssq.querySingleState(ts, quark); |
efc403bb AM |
277 | } |
278 | ||
279 | @Test(expected = TimeRangeException.class) | |
280 | public void testRangeQueryInvalidTime1() throws AttributeNotFoundException, | |
96345c5a | 281 | TimeRangeException, StateSystemDisposedException { |
efc403bb | 282 | |
6e71ce46 | 283 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
dc0f7bfe AM |
284 | long ts1 = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ |
285 | long ts2 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */ | |
efc403bb | 286 | |
6e71ce46 | 287 | ssq.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
288 | } |
289 | ||
290 | @Test(expected = TimeRangeException.class) | |
291 | public void testRangeQueryInvalidTime2() throws TimeRangeException, | |
96345c5a | 292 | AttributeNotFoundException, StateSystemDisposedException { |
efc403bb | 293 | |
6e71ce46 | 294 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
dc0f7bfe AM |
295 | long ts1 = CtfTestFiles.startTime - 1L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ |
296 | long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */ | |
efc403bb | 297 | |
6e71ce46 | 298 | ssq.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
299 | } |
300 | ||
301 | /** | |
302 | * Ask for a non-existing attribute | |
e743c3b8 | 303 | * |
efc403bb AM |
304 | * @throws AttributeNotFoundException |
305 | */ | |
306 | @Test(expected = AttributeNotFoundException.class) | |
307 | public void testQueryInvalidAttribute() throws AttributeNotFoundException { | |
308 | ||
6e71ce46 | 309 | ssq.getQuarkAbsolute("There", "is", "no", "cow", "level"); |
efc403bb AM |
310 | } |
311 | ||
312 | /** | |
313 | * Query but with the wrong State Value type | |
efc403bb AM |
314 | */ |
315 | @Test(expected = StateValueTypeException.class) | |
316 | public void testQueryInvalidValuetype1() throws StateValueTypeException, | |
96345c5a AM |
317 | AttributeNotFoundException, TimeRangeException, |
318 | StateSystemDisposedException { | |
dad01d27 | 319 | List<ITmfStateInterval> list; |
efc403bb AM |
320 | ITmfStateInterval interval; |
321 | int quark; | |
322 | ||
6e71ce46 AM |
323 | list = ssq.queryFullState(interestingTimestamp1); |
324 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
dad01d27 | 325 | interval = list.get(quark); |
2359ecca AM |
326 | |
327 | /* This is supposed to be an int value */ | |
328 | interval.getStateValue().unboxStr(); | |
efc403bb AM |
329 | } |
330 | ||
331 | @Test(expected = StateValueTypeException.class) | |
332 | public void testQueryInvalidValuetype2() throws StateValueTypeException, | |
96345c5a AM |
333 | AttributeNotFoundException, TimeRangeException, |
334 | StateSystemDisposedException { | |
dad01d27 | 335 | List<ITmfStateInterval> list; |
efc403bb AM |
336 | ITmfStateInterval interval; |
337 | int quark; | |
338 | ||
6e71ce46 AM |
339 | list = ssq.queryFullState(interestingTimestamp1); |
340 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); | |
dad01d27 | 341 | interval = list.get(quark); |
2359ecca AM |
342 | |
343 | /* This is supposed to be a String value */ | |
344 | interval.getStateValue().unboxInt(); | |
efc403bb AM |
345 | } |
346 | ||
347 | @Test | |
348 | public void testFullAttributeName() throws AttributeNotFoundException { | |
6e71ce46 AM |
349 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); |
350 | String name = ssq.getFullAttributePath(quark); | |
2359ecca | 351 | assertEquals(name, "CPUs/0/Current_thread"); |
efc403bb AM |
352 | } |
353 | ||
6abc2d88 AM |
354 | @Test |
355 | public void testGetQuarks_begin() { | |
6e71ce46 | 356 | List<Integer> list = ssq.getQuarks("*", "1577", Attributes.EXEC_NAME); |
6abc2d88 AM |
357 | |
358 | assertEquals(1, list.size()); | |
6abc2d88 AM |
359 | } |
360 | ||
361 | @Test | |
362 | public void testGetQuarks_middle() { | |
6e71ce46 | 363 | List<Integer> list = ssq.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME); |
6abc2d88 | 364 | |
1ba498e5 AM |
365 | /* Number of different kernel threads in the trace */ |
366 | assertEquals(168, list.size()); | |
6abc2d88 AM |
367 | } |
368 | ||
369 | @Test | |
370 | public void testGetQuarks_end() { | |
6e71ce46 | 371 | List<Integer> list = ssq.getQuarks(Attributes.THREADS, "1577", "*"); |
6abc2d88 | 372 | |
1ba498e5 AM |
373 | /* There should be 4 sub-attributes for each Thread node */ |
374 | assertEquals(4, list.size()); | |
6abc2d88 | 375 | } |
efc403bb | 376 | } |