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