1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.tests
.stateprovider
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
18 import static org
.junit
.Assert
.fail
;
19 import static org
.junit
.Assume
.assumeTrue
;
21 import java
.util
.List
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
24 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
26 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
27 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
28 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
29 import org
.eclipse
.linuxtools
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Rule
;
35 import org
.junit
.Test
;
36 import org
.junit
.rules
.TestRule
;
37 import org
.junit
.rules
.Timeout
;
40 * Base unit tests for the StateHistorySystem. Extension can be made to test
41 * different state back-end types or configurations.
43 * @author Alexandre Montplaisir
45 @SuppressWarnings("javadoc")
46 public abstract class StateSystemTest
{
48 /** Timeout the tests after 2 minutes */
50 public TestRule timeoutRule
= new Timeout(120000);
52 /** Test trace used for these tests */
53 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.TRACE2
;
55 /** Expected start time of the test trace/state history */
56 protected static final long startTime
= 1331668247314038062L;
58 /** Expected end time of the state history built from the test trace */
59 protected static final long endTime
= 1331668259054285979L;
61 /** Offset in the trace + start time of the trace */
62 protected static final long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
64 /** Number of nanoseconds in one second */
65 private static final long NANOSECS_PER_SEC
= 1000000000L;
67 private ITmfStateSystem fixture
;
75 assumeTrue(testTrace
.exists());
76 fixture
= this.initialize();
77 assertNotNull(fixture
);
80 protected abstract ITmfStateSystem
initialize();
83 public void tearDown() {
84 if (fixture
!= null) {
91 public void testFullQuery1() {
92 List
<ITmfStateInterval
> list
;
93 ITmfStateInterval interval
;
98 list
= fixture
.queryFullState(interestingTimestamp1
);
100 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
101 interval
= list
.get(quark
);
102 valueInt
= interval
.getStateValue().unboxInt();
103 assertEquals(1397, valueInt
);
105 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
106 interval
= list
.get(quark
);
107 valueStr
= interval
.getStateValue().unboxStr();
108 assertEquals("gdbus", valueStr
);
110 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
111 interval
= list
.get(quark
);
112 valueStr
= interval
.getStateValue().unboxStr();
113 assertTrue(valueStr
.equals("sys_poll"));
115 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
121 public void testSingleQuery1() {
122 long timestamp
= interestingTimestamp1
;
124 ITmfStateInterval interval
;
128 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
129 interval
= fixture
.querySingleState(timestamp
, quark
);
130 valueStr
= interval
.getStateValue().unboxStr();
131 assertEquals("gdbus", valueStr
);
133 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
139 * Test a range query (with no resolution parameter, so all intervals)
142 public void testRangeQuery1() {
143 long time1
= interestingTimestamp1
;
144 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
146 List
<ITmfStateInterval
> intervals
;
149 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
150 intervals
= fixture
.queryHistoryRange(quark
, time1
, time2
);
151 assertEquals(487, intervals
.size()); /* Number of context switches! */
152 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
153 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
155 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
161 * Range query, but with a t2 far off the end of the trace. The result
162 * should still be valid.
165 public void testRangeQuery2() {
166 List
<ITmfStateInterval
> intervals
;
169 int quark
= fixture
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
170 long ts1
= fixture
.getStartTime(); /* start of the trace */
171 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
173 intervals
= fixture
.queryHistoryRange(quark
, ts1
, ts2
);
175 /* Activity of IRQ 1 over the whole trace */
176 assertEquals(65, intervals
.size());
178 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
184 * Test a range query with a resolution
187 public void testRangeQuery3() {
188 long time1
= interestingTimestamp1
;
189 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
190 long resolution
= 1000000; /* One query every millisecond */
192 List
<ITmfStateInterval
> intervals
;
195 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
196 intervals
= fixture
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
197 assertEquals(126, intervals
.size()); /* Number of context switches! */
198 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
199 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
201 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
207 * Ask for a time range outside of the trace's range
209 @Test(expected
= TimeRangeException
.class)
210 public void testFullQueryInvalidTime1() throws TimeRangeException
,
211 StateSystemDisposedException
{
212 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
213 fixture
.queryFullState(ts
);
216 @Test(expected
= TimeRangeException
.class)
217 public void testFullQueryInvalidTime2() throws TimeRangeException
,
218 StateSystemDisposedException
{
219 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
220 fixture
.queryFullState(ts
);
223 @Test(expected
= TimeRangeException
.class)
224 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
226 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
227 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
228 fixture
.querySingleState(ts
, quark
);
230 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
235 @Test(expected
= TimeRangeException
.class)
236 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
238 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
239 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
240 fixture
.querySingleState(ts
, quark
);
242 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
247 @Test(expected
= TimeRangeException
.class)
248 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
250 int quark
= fixture
.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 fixture
.queryHistoryRange(quark
, ts1
, ts2
);
255 } catch (AttributeNotFoundException e
) {
257 } catch (StateSystemDisposedException e
) {
262 @Test(expected
= TimeRangeException
.class)
263 public void testRangeQueryInvalidTime2() throws TimeRangeException
{
265 int quark
= fixture
.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 fixture
.queryHistoryRange(quark
, ts1
, ts2
);
270 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
276 * Ask for a non-existing attribute
278 * @throws AttributeNotFoundException
280 @Test(expected
= AttributeNotFoundException
.class)
281 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
282 fixture
.getQuarkAbsolute("There", "is", "no", "cow", "level");
286 * Query but with the wrong State Value type
288 @Test(expected
= StateValueTypeException
.class)
289 public void testQueryInvalidValuetype1() throws StateValueTypeException
{
290 List
<ITmfStateInterval
> list
;
291 ITmfStateInterval interval
;
295 list
= fixture
.queryFullState(interestingTimestamp1
);
296 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
297 interval
= list
.get(quark
);
299 /* This is supposed to be an int value */
300 interval
.getStateValue().unboxStr();
302 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
307 @Test(expected
= StateValueTypeException
.class)
308 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
309 List
<ITmfStateInterval
> list
;
310 ITmfStateInterval interval
;
314 list
= fixture
.queryFullState(interestingTimestamp1
);
315 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
316 interval
= list
.get(quark
);
318 /* This is supposed to be a String value */
319 interval
.getStateValue().unboxInt();
321 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
327 public void testFullAttributeName() {
329 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
330 String name
= fixture
.getFullAttributePath(quark
);
331 assertEquals(name
, "CPUs/0/Current_thread");
333 } catch (AttributeNotFoundException e
) {
339 public void testGetQuarks_begin() {
340 List
<Integer
> list
= fixture
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
342 assertEquals(1, list
.size());
346 public void testGetQuarks_middle() {
347 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
349 /* Number of different kernel threads in the trace */
350 assertEquals(168, list
.size());
354 public void testGetQuarks_end() {
355 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "1577", "*");
357 /* There should be 4 sub-attributes for each Thread node */
358 assertEquals(4, list
.size());
361 // ------------------------------------------------------------------------
362 // Tests verifying the *complete* results of a full queries
363 // ------------------------------------------------------------------------
365 protected long getStartTimes(int idx
) {
366 return TestValues
.startTimes
[idx
];
369 protected long getEndTimes(int idx
) {
370 return TestValues
.endTimes
[idx
];
373 protected ITmfStateValue
getStateValues(int idx
) {
374 return TestValues
.values
[idx
];
378 public void testFullQueryThorough() {
380 List
<ITmfStateInterval
> state
= fixture
.queryFullState(interestingTimestamp1
);
381 assertEquals(TestValues
.size
, state
.size());
383 for (int i
= 0; i
< state
.size(); i
++) {
384 /* Test each component of the intervals */
385 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
386 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
387 assertEquals(i
, state
.get(i
).getAttribute());
388 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
391 } catch (StateSystemDisposedException e
) {
397 public void testFirstIntervalIsConsidered() {
399 List
<ITmfStateInterval
> list
= fixture
.queryFullState(1331668248014135800L);
400 ITmfStateInterval interval
= list
.get(233);
401 assertEquals(1331668247516664825L, interval
.getStartTime());
403 int valueInt
= interval
.getStateValue().unboxInt();
404 assertEquals(1, valueInt
);
406 } catch (StateSystemDisposedException e
) {
412 public void testParentAttribute() {
413 String
[] path
= { "CPUs/0/Current_thread",
417 int q
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
418 for (int i
= 0; i
< path
.length
; i
++) {
419 String name
= fixture
.getFullAttributePath(q
);
420 assertEquals(path
[i
], name
);
421 q
= fixture
.getParentAttributeQuark(q
);
424 q
= fixture
.getParentAttributeQuark(q
);
426 } catch (AttributeNotFoundException e
) {