Commit | Line | Data |
---|---|---|
f9a76cac | 1 | /******************************************************************************* |
94cce698 | 2 | * Copyright (c) 2012, 2013 Ericsson |
f9a76cac AM |
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.assertEquals; | |
16 | import static org.junit.Assert.assertTrue; | |
c4d139aa | 17 | import static org.junit.Assert.fail; |
f9a76cac AM |
18 | |
19 | import java.util.List; | |
20 | ||
21 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes; | |
22 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; | |
23 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; | |
24 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; | |
25 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
26 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; | |
27 | import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput; | |
28 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem; | |
b33f7554 | 29 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
f9a76cac AM |
30 | import org.junit.Test; |
31 | ||
32 | /** | |
33 | * Base unit tests for the StateHistorySystem. Extension can be made to test | |
34 | * different state back-end types or configurations. | |
35 | * | |
36 | * @author Alexandre Montplaisir | |
f9a76cac AM |
37 | */ |
38 | @SuppressWarnings({"nls", "javadoc"}) | |
39 | public abstract class StateSystemTest { | |
40 | ||
92ba8466 AM |
41 | /** Index of the test trace used for these tests */ |
42 | protected static final int TRACE_INDEX = 1; | |
43 | ||
44 | /** Expected start time of the test trace/state history */ | |
45 | protected static final long startTime = 1331668247314038062L; | |
46 | ||
47 | /** Expected end time of the state history built from the test trace */ | |
48 | protected static final long endTime = 1331668259054285979L; | |
49 | ||
50 | /** Number of nanoseconds in one second */ | |
51 | private static final long NANOSECS_PER_SEC = 1000000000L; | |
52 | ||
f9a76cac AM |
53 | protected static IStateChangeInput input; |
54 | protected static ITmfStateSystem ssq; | |
55 | ||
56 | /* Offset in the trace + start time of the trace */ | |
b33f7554 | 57 | static final long interestingTimestamp1 = 18670067372290L + 1331649577946812237L; |
f9a76cac AM |
58 | |
59 | @Test | |
c4d139aa | 60 | public void testFullQuery1() { |
f9a76cac AM |
61 | List<ITmfStateInterval> list; |
62 | ITmfStateInterval interval; | |
63 | int quark, valueInt; | |
64 | String valueStr; | |
65 | ||
c4d139aa AM |
66 | try { |
67 | list = ssq.queryFullState(interestingTimestamp1); | |
68 | ||
69 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
70 | interval = list.get(quark); | |
71 | valueInt = interval.getStateValue().unboxInt(); | |
72 | assertEquals(1397, valueInt); | |
73 | ||
74 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); | |
75 | interval = list.get(quark); | |
76 | valueStr = interval.getStateValue().unboxStr(); | |
77 | assertEquals("gdbus", valueStr); | |
78 | ||
79 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL); | |
80 | interval = list.get(quark); | |
81 | valueStr = interval.getStateValue().unboxStr(); | |
82 | assertTrue(valueStr.equals("sys_poll")); | |
83 | ||
84 | } catch (AttributeNotFoundException e) { | |
85 | fail(); | |
86 | } catch (TimeRangeException e) { | |
87 | fail(); | |
88 | } catch (StateSystemDisposedException e) { | |
89 | fail(); | |
90 | } catch (StateValueTypeException e) { | |
91 | fail(); | |
92 | } | |
f9a76cac AM |
93 | } |
94 | ||
95 | @Test | |
c4d139aa | 96 | public void testSingleQuery1() { |
f9a76cac AM |
97 | long timestamp = interestingTimestamp1; |
98 | int quark; | |
99 | ITmfStateInterval interval; | |
100 | String valueStr; | |
101 | ||
c4d139aa AM |
102 | try { |
103 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); | |
104 | interval = ssq.querySingleState(timestamp, quark); | |
105 | valueStr = interval.getStateValue().unboxStr(); | |
106 | assertEquals("gdbus", valueStr); | |
107 | ||
108 | } catch (AttributeNotFoundException e) { | |
109 | fail(); | |
110 | } catch (TimeRangeException e) { | |
111 | fail(); | |
112 | } catch (StateSystemDisposedException e) { | |
113 | fail(); | |
114 | } catch (StateValueTypeException e) { | |
115 | fail(); | |
116 | } | |
f9a76cac AM |
117 | } |
118 | ||
119 | /** | |
120 | * Test a range query (with no resolution parameter, so all intervals) | |
121 | */ | |
122 | @Test | |
c4d139aa | 123 | public void testRangeQuery1() { |
f9a76cac | 124 | long time1 = interestingTimestamp1; |
92ba8466 | 125 | long time2 = time1 + 1L * NANOSECS_PER_SEC; |
f9a76cac AM |
126 | int quark; |
127 | List<ITmfStateInterval> intervals; | |
128 | ||
c4d139aa AM |
129 | try { |
130 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
131 | intervals = ssq.queryHistoryRange(quark, time1, time2); | |
132 | assertEquals(487, intervals.size()); /* Number of context switches! */ | |
133 | assertEquals(1685, intervals.get(100).getStateValue().unboxInt()); | |
134 | assertEquals(1331668248427681372L, intervals.get(205).getEndTime()); | |
135 | ||
136 | } catch (AttributeNotFoundException e) { | |
137 | fail(); | |
138 | } catch (TimeRangeException e) { | |
139 | fail(); | |
140 | } catch (StateSystemDisposedException e) { | |
141 | fail(); | |
142 | } catch (StateValueTypeException e) { | |
143 | fail(); | |
144 | } | |
f9a76cac AM |
145 | } |
146 | ||
147 | /** | |
148 | * Range query, but with a t2 far off the end of the trace. | |
149 | * The result should still be valid. | |
150 | */ | |
151 | @Test | |
c4d139aa | 152 | public void testRangeQuery2() { |
f9a76cac AM |
153 | List<ITmfStateInterval> intervals; |
154 | ||
c4d139aa AM |
155 | try { |
156 | int quark = ssq.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1"); | |
157 | long ts1 = ssq.getStartTime(); /* start of the trace */ | |
158 | long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */ | |
159 | ||
160 | intervals = ssq.queryHistoryRange(quark, ts1, ts2); | |
f9a76cac | 161 | |
c4d139aa AM |
162 | /* Activity of IRQ 1 over the whole trace */ |
163 | assertEquals(65, intervals.size()); | |
f9a76cac | 164 | |
c4d139aa AM |
165 | } catch (AttributeNotFoundException e) { |
166 | fail(); | |
167 | } catch (TimeRangeException e) { | |
168 | fail(); | |
169 | } catch (StateSystemDisposedException e) { | |
170 | fail(); | |
171 | } | |
f9a76cac AM |
172 | } |
173 | ||
174 | /** | |
175 | * Test a range query with a resolution | |
176 | */ | |
177 | @Test | |
c4d139aa | 178 | public void testRangeQuery3() { |
f9a76cac | 179 | long time1 = interestingTimestamp1; |
92ba8466 | 180 | long time2 = time1 + 1L * NANOSECS_PER_SEC; |
f9a76cac AM |
181 | long resolution = 1000000; /* One query every millisecond */ |
182 | int quark; | |
183 | List<ITmfStateInterval> intervals; | |
184 | ||
c4d139aa AM |
185 | try { |
186 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
187 | intervals = ssq.queryHistoryRange(quark, time1, time2, resolution, null); | |
188 | assertEquals(126, intervals.size()); /* Number of context switches! */ | |
189 | assertEquals(1452, intervals.get(50).getStateValue().unboxInt()); | |
190 | assertEquals(1331668248815698779L, intervals.get(100).getEndTime()); | |
191 | ||
192 | } catch (AttributeNotFoundException e) { | |
193 | fail(); | |
194 | } catch (TimeRangeException e) { | |
195 | fail(); | |
196 | } catch (StateSystemDisposedException e) { | |
197 | fail(); | |
198 | } catch (StateValueTypeException e) { | |
199 | fail(); | |
200 | } | |
f9a76cac AM |
201 | } |
202 | ||
203 | /** | |
204 | * Ask for a time range outside of the trace's range | |
205 | */ | |
206 | @Test(expected = TimeRangeException.class) | |
207 | public void testFullQueryInvalidTime1() throws TimeRangeException, | |
208 | StateSystemDisposedException { | |
92ba8466 | 209 | long ts = startTime + 20L * NANOSECS_PER_SEC; |
f9a76cac AM |
210 | ssq.queryFullState(ts); |
211 | } | |
212 | ||
213 | @Test(expected = TimeRangeException.class) | |
214 | public void testFullQueryInvalidTime2() throws TimeRangeException, | |
215 | StateSystemDisposedException { | |
92ba8466 | 216 | long ts = startTime - 20L * NANOSECS_PER_SEC; |
f9a76cac AM |
217 | ssq.queryFullState(ts); |
218 | } | |
219 | ||
220 | @Test(expected = TimeRangeException.class) | |
c4d139aa AM |
221 | public void testSingleQueryInvalidTime1() throws TimeRangeException { |
222 | try { | |
223 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
224 | long ts = startTime + 20L * NANOSECS_PER_SEC; | |
225 | ssq.querySingleState(ts, quark); | |
226 | ||
227 | } catch (AttributeNotFoundException e) { | |
228 | fail(); | |
229 | } catch (StateSystemDisposedException e) { | |
230 | fail(); | |
231 | } | |
f9a76cac AM |
232 | } |
233 | ||
234 | @Test(expected = TimeRangeException.class) | |
c4d139aa AM |
235 | public void testSingleQueryInvalidTime2() throws TimeRangeException { |
236 | try { | |
237 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
238 | long ts = startTime - 20L * NANOSECS_PER_SEC; | |
239 | ssq.querySingleState(ts, quark); | |
240 | ||
241 | } catch (AttributeNotFoundException e) { | |
242 | fail(); | |
243 | } catch (StateSystemDisposedException e) { | |
244 | fail(); | |
245 | } | |
f9a76cac AM |
246 | } |
247 | ||
248 | @Test(expected = TimeRangeException.class) | |
c4d139aa AM |
249 | public void testRangeQueryInvalidTime1() throws TimeRangeException { |
250 | try { | |
251 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
252 | long ts1 = startTime - 20L * NANOSECS_PER_SEC; /* invalid */ | |
253 | long ts2 = startTime + 1L * NANOSECS_PER_SEC; /* valid */ | |
254 | ssq.queryHistoryRange(quark, ts1, ts2); | |
255 | ||
256 | } catch (AttributeNotFoundException e) { | |
257 | fail(); | |
258 | } catch (StateSystemDisposedException e) { | |
259 | fail(); | |
260 | } | |
f9a76cac AM |
261 | } |
262 | ||
263 | @Test(expected = TimeRangeException.class) | |
c4d139aa AM |
264 | public void testRangeQueryInvalidTime2() throws TimeRangeException { |
265 | try { | |
266 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
267 | long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */ | |
268 | long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */ | |
269 | ssq.queryHistoryRange(quark, ts1, ts2); | |
270 | ||
271 | } catch (AttributeNotFoundException e) { | |
272 | fail(); | |
273 | } catch (StateSystemDisposedException e) { | |
274 | fail(); | |
275 | } | |
f9a76cac AM |
276 | } |
277 | ||
278 | /** | |
279 | * Ask for a non-existing attribute | |
280 | * | |
281 | * @throws AttributeNotFoundException | |
282 | */ | |
283 | @Test(expected = AttributeNotFoundException.class) | |
284 | public void testQueryInvalidAttribute() throws AttributeNotFoundException { | |
f9a76cac AM |
285 | ssq.getQuarkAbsolute("There", "is", "no", "cow", "level"); |
286 | } | |
287 | ||
288 | /** | |
289 | * Query but with the wrong State Value type | |
290 | */ | |
291 | @Test(expected = StateValueTypeException.class) | |
c4d139aa | 292 | public void testQueryInvalidValuetype1() throws StateValueTypeException { |
f9a76cac AM |
293 | List<ITmfStateInterval> list; |
294 | ITmfStateInterval interval; | |
295 | int quark; | |
296 | ||
c4d139aa AM |
297 | try { |
298 | list = ssq.queryFullState(interestingTimestamp1); | |
299 | quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
300 | interval = list.get(quark); | |
301 | ||
302 | /* This is supposed to be an int value */ | |
303 | interval.getStateValue().unboxStr(); | |
304 | ||
305 | } catch (AttributeNotFoundException e) { | |
306 | fail(); | |
307 | } catch (TimeRangeException e) { | |
308 | fail(); | |
309 | } catch (StateSystemDisposedException e) { | |
310 | fail(); | |
311 | } | |
f9a76cac AM |
312 | } |
313 | ||
314 | @Test(expected = StateValueTypeException.class) | |
c4d139aa | 315 | public void testQueryInvalidValuetype2() throws StateValueTypeException { |
f9a76cac AM |
316 | List<ITmfStateInterval> list; |
317 | ITmfStateInterval interval; | |
318 | int quark; | |
319 | ||
c4d139aa AM |
320 | try { |
321 | list = ssq.queryFullState(interestingTimestamp1); | |
322 | quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME); | |
323 | interval = list.get(quark); | |
324 | ||
325 | /* This is supposed to be a String value */ | |
326 | interval.getStateValue().unboxInt(); | |
327 | ||
328 | } catch (AttributeNotFoundException e) { | |
329 | fail(); | |
330 | } catch (TimeRangeException e) { | |
331 | fail(); | |
332 | } catch (StateSystemDisposedException e) { | |
333 | fail(); | |
334 | } | |
f9a76cac AM |
335 | } |
336 | ||
337 | @Test | |
c4d139aa AM |
338 | public void testFullAttributeName() { |
339 | try { | |
340 | int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD); | |
341 | String name = ssq.getFullAttributePath(quark); | |
342 | assertEquals(name, "CPUs/0/Current_thread"); | |
343 | ||
344 | } catch (AttributeNotFoundException e) { | |
345 | fail(); | |
346 | } | |
f9a76cac AM |
347 | } |
348 | ||
349 | @Test | |
350 | public void testGetQuarks_begin() { | |
351 | List<Integer> list = ssq.getQuarks("*", "1577", Attributes.EXEC_NAME); | |
352 | ||
353 | assertEquals(1, list.size()); | |
354 | } | |
355 | ||
356 | @Test | |
357 | public void testGetQuarks_middle() { | |
358 | List<Integer> list = ssq.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME); | |
359 | ||
360 | /* Number of different kernel threads in the trace */ | |
361 | assertEquals(168, list.size()); | |
362 | } | |
363 | ||
364 | @Test | |
365 | public void testGetQuarks_end() { | |
366 | List<Integer> list = ssq.getQuarks(Attributes.THREADS, "1577", "*"); | |
367 | ||
368 | /* There should be 4 sub-attributes for each Thread node */ | |
369 | assertEquals(4, list.size()); | |
370 | } | |
b33f7554 AM |
371 | |
372 | // ------------------------------------------------------------------------ | |
373 | // Tests verifying the *complete* results of a full queries | |
374 | // ------------------------------------------------------------------------ | |
375 | ||
376 | protected long getStartTimes(int idx) { | |
377 | return TestValues.startTimes[idx]; | |
378 | } | |
379 | ||
380 | protected long getEndTimes(int idx) { | |
381 | return TestValues.endTimes[idx]; | |
382 | } | |
383 | ||
384 | protected ITmfStateValue getStateValues(int idx) { | |
385 | return TestValues.values[idx]; | |
386 | } | |
387 | ||
388 | @Test | |
389 | public void testFullQueryThorough() { | |
390 | try { | |
391 | List<ITmfStateInterval> state = ssq.queryFullState(interestingTimestamp1); | |
392 | assertEquals(TestValues.size, state.size()); | |
393 | ||
394 | for (int i = 0; i < state.size(); i++) { | |
395 | /* Test each component of the intervals */ | |
396 | assertEquals(getStartTimes(i), state.get(i).getStartTime()); | |
397 | assertEquals(getEndTimes(i), state.get(i).getEndTime()); | |
398 | assertEquals(i, state.get(i).getAttribute()); | |
399 | assertEquals(getStateValues(i), state.get(i).getStateValue()); | |
400 | } | |
401 | ||
402 | } catch (TimeRangeException e) { | |
403 | fail(); | |
404 | } catch (StateSystemDisposedException e) { | |
405 | fail(); | |
406 | } | |
407 | } | |
f9a76cac | 408 | } |