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
.tracecompass
.lttng2
.kernel
.core
.tests
.analysis
.kernel
.statesystem
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.fail
;
18 import static org
.junit
.Assume
.assumeTrue
;
20 import java
.util
.List
;
22 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
32 import org
.junit
.Before
;
33 import org
.junit
.Rule
;
34 import org
.junit
.Test
;
35 import org
.junit
.rules
.TestRule
;
36 import org
.junit
.rules
.Timeout
;
39 * Base unit tests for the StateHistorySystem. Extension can be made to test
40 * different state back-end types or configurations.
42 * @author Alexandre Montplaisir
44 @SuppressWarnings("javadoc")
45 public abstract class StateSystemTest
{
47 /** Timeout the tests after 2 minutes */
49 public TestRule timeoutRule
= new Timeout(120000);
51 /** Test trace used for these tests */
52 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.TRACE2
;
54 /** Expected start time of the test trace/state history */
55 protected static final long startTime
= 1331668247314038062L;
57 /** Expected end time of the state history built from the test trace */
58 protected static final long endTime
= 1331668259054285979L;
60 /** Offset in the trace + start time of the trace */
61 protected static final long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
63 /** Number of nanoseconds in one second */
64 private static final long NANOSECS_PER_SEC
= 1000000000L;
66 protected static ITmfStateSystem fixture
;
67 protected static boolean traceIsPresent
= false;
74 assumeTrue(traceIsPresent
);
75 /* Subclasses should set-up 'fixture' */
76 assertNotNull(fixture
);
80 public void testFullQuery1() {
81 List
<ITmfStateInterval
> list
;
82 ITmfStateInterval interval
;
87 list
= fixture
.queryFullState(interestingTimestamp1
);
89 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
90 interval
= list
.get(quark
);
91 valueInt
= interval
.getStateValue().unboxInt();
92 assertEquals(1397, valueInt
);
94 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
95 interval
= list
.get(quark
);
96 valueStr
= interval
.getStateValue().unboxStr();
97 assertEquals("gdbus", valueStr
);
99 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
100 interval
= list
.get(quark
);
101 valueStr
= interval
.getStateValue().unboxStr();
102 assertEquals("sys_poll", valueStr
);
104 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
110 public void testSingleQuery1() {
111 long timestamp
= interestingTimestamp1
;
113 ITmfStateInterval interval
;
117 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
118 interval
= fixture
.querySingleState(timestamp
, quark
);
119 valueStr
= interval
.getStateValue().unboxStr();
120 assertEquals("gdbus", valueStr
);
122 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
128 * Test a range query (with no resolution parameter, so all intervals)
131 public void testRangeQuery1() {
132 long time1
= interestingTimestamp1
;
133 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
135 List
<ITmfStateInterval
> intervals
;
137 final ITmfStateSystem ss
= fixture
;
141 quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
142 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, time1
, time2
);
143 assertEquals(487, intervals
.size()); /* Number of context switches! */
144 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
145 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
147 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
153 * Range query, but with a t2 far off the end of the trace. The result
154 * should still be valid.
157 public void testRangeQuery2() {
158 List
<ITmfStateInterval
> intervals
;
160 final ITmfStateSystem ss
= fixture
;
164 int quark
= ss
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
165 long ts1
= ss
.getStartTime(); /* start of the trace */
166 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
168 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
170 /* Activity of IRQ 1 over the whole trace */
171 assertEquals(65, intervals
.size());
173 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
179 * Test a range query with a resolution
182 public void testRangeQuery3() {
183 long time1
= interestingTimestamp1
;
184 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
185 long resolution
= 1000000; /* One query every millisecond */
187 List
<ITmfStateInterval
> intervals
;
189 final ITmfStateSystem ss
= fixture
;
193 quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
194 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, time1
, time2
, resolution
, null);
195 assertEquals(126, intervals
.size()); /* Number of context switches! */
196 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
197 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
199 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
205 * Ask for a time range outside of the trace's range
207 @Test(expected
= TimeRangeException
.class)
208 public void testFullQueryInvalidTime1() throws TimeRangeException
,
209 StateSystemDisposedException
{
210 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
211 fixture
.queryFullState(ts
);
214 @Test(expected
= TimeRangeException
.class)
215 public void testFullQueryInvalidTime2() throws TimeRangeException
,
216 StateSystemDisposedException
{
217 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
218 fixture
.queryFullState(ts
);
221 @Test(expected
= TimeRangeException
.class)
222 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
224 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
225 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
226 fixture
.querySingleState(ts
, quark
);
228 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
233 @Test(expected
= TimeRangeException
.class)
234 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
236 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
237 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
238 fixture
.querySingleState(ts
, quark
);
240 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
245 @Test(expected
= TimeRangeException
.class)
246 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
247 final ITmfStateSystem ss
= fixture
;
251 int quark
= ss
.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 StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
256 } catch (AttributeNotFoundException e
) {
258 } catch (StateSystemDisposedException e
) {
263 @Test(expected
= TimeRangeException
.class)
264 public void testRangeQueryInvalidTime2() throws TimeRangeException
{
265 final ITmfStateSystem ss
= fixture
;
269 int quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
270 long ts1
= startTime
- 1L * NANOSECS_PER_SEC
; /* invalid */
271 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid */
272 StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
274 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
280 * Ask for a non-existing attribute
282 * @throws AttributeNotFoundException
284 @Test(expected
= AttributeNotFoundException
.class)
285 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
286 fixture
.getQuarkAbsolute("There", "is", "no", "cow", "level");
290 * Query but with the wrong State Value type
292 @Test(expected
= StateValueTypeException
.class)
293 public void testQueryInvalidValuetype1() throws StateValueTypeException
{
294 List
<ITmfStateInterval
> list
;
295 ITmfStateInterval interval
;
299 list
= fixture
.queryFullState(interestingTimestamp1
);
300 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
301 interval
= list
.get(quark
);
303 /* This is supposed to be an int value */
304 interval
.getStateValue().unboxStr();
306 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
311 @Test(expected
= StateValueTypeException
.class)
312 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
313 List
<ITmfStateInterval
> list
;
314 ITmfStateInterval interval
;
318 list
= fixture
.queryFullState(interestingTimestamp1
);
319 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
320 interval
= list
.get(quark
);
322 /* This is supposed to be a String value */
323 interval
.getStateValue().unboxInt();
325 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
331 public void testFullAttributeName() {
333 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
334 String name
= fixture
.getFullAttributePath(quark
);
335 assertEquals(name
, "CPUs/0/Current_thread");
337 } catch (AttributeNotFoundException e
) {
343 public void testGetQuarks_begin() {
344 List
<Integer
> list
= fixture
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
346 assertEquals(1, list
.size());
350 public void testGetQuarks_middle() {
351 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
353 /* Number of different kernel threads in the trace */
354 assertEquals(168, list
.size());
358 public void testGetQuarks_end() {
359 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "1577", "*");
361 /* There should be 4 sub-attributes for each Thread node */
362 assertEquals(4, list
.size());
365 // ------------------------------------------------------------------------
366 // Tests verifying the *complete* results of a full queries
367 // ------------------------------------------------------------------------
369 protected long getStartTimes(int idx
) {
370 return TestValues
.startTimes
[idx
];
373 protected long getEndTimes(int idx
) {
374 return TestValues
.endTimes
[idx
];
377 protected ITmfStateValue
getStateValues(int idx
) {
378 return TestValues
.values
[idx
];
382 public void testFullQueryThorough() {
384 List
<ITmfStateInterval
> state
= fixture
.queryFullState(interestingTimestamp1
);
385 assertEquals(TestValues
.size
, state
.size());
387 for (int i
= 0; i
< state
.size(); i
++) {
388 /* Test each component of the intervals */
389 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
390 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
391 assertEquals(i
, state
.get(i
).getAttribute());
392 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
395 } catch (StateSystemDisposedException e
) {
401 public void testFirstIntervalIsConsidered() {
403 List
<ITmfStateInterval
> list
= fixture
.queryFullState(1331668248014135800L);
404 ITmfStateInterval interval
= list
.get(233);
405 assertEquals(1331668247516664825L, interval
.getStartTime());
407 int valueInt
= interval
.getStateValue().unboxInt();
408 assertEquals(1, valueInt
);
410 } catch (StateSystemDisposedException e
) {
416 public void testParentAttribute() {
417 String
[] path
= { "CPUs/0/Current_thread",
421 int q
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
422 for (int i
= 0; i
< path
.length
; i
++) {
423 String name
= fixture
.getFullAttributePath(q
);
424 assertEquals(path
[i
], name
);
425 q
= fixture
.getParentAttributeQuark(q
);
428 q
= fixture
.getParentAttributeQuark(q
);
430 } catch (AttributeNotFoundException e
) {