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