1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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
;
19 import java
.util
.List
;
20 import java
.util
.concurrent
.TimeUnit
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
32 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
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(2, TimeUnit
.MINUTES
);
52 /** Test trace used for these tests */
53 protected static final @NonNull CtfTestTrace testTrace
= CtfTestTrace
.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 protected static ITmfStateSystem fixture
;
74 /* Subclasses should set-up 'fixture' */
75 assertNotNull(fixture
);
79 public void testFullQuery1() {
80 List
<ITmfStateInterval
> list
;
81 ITmfStateInterval interval
;
86 list
= fixture
.queryFullState(interestingTimestamp1
);
88 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
89 interval
= list
.get(quark
);
90 valueInt
= interval
.getStateValue().unboxInt();
91 assertEquals(1397, valueInt
);
93 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
94 interval
= list
.get(quark
);
95 valueStr
= interval
.getStateValue().unboxStr();
96 assertEquals("gdbus", valueStr
);
98 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
99 interval
= list
.get(quark
);
100 valueStr
= interval
.getStateValue().unboxStr();
101 assertEquals("sys_poll", valueStr
);
103 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
109 public void testSingleQuery1() {
110 long timestamp
= interestingTimestamp1
;
112 ITmfStateInterval interval
;
116 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
117 interval
= fixture
.querySingleState(timestamp
, quark
);
118 valueStr
= interval
.getStateValue().unboxStr();
119 assertEquals("gdbus", valueStr
);
121 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
127 * Test a range query (with no resolution parameter, so all intervals)
130 public void testRangeQuery1() {
131 long time1
= interestingTimestamp1
;
132 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
134 List
<ITmfStateInterval
> intervals
;
136 final ITmfStateSystem ss
= fixture
;
140 quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
141 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, time1
, time2
);
142 assertEquals(487, intervals
.size()); /* Number of context switches! */
143 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
144 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
146 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
152 * Range query, but with a t2 far off the end of the trace. The result
153 * should still be valid.
156 public void testRangeQuery2() {
157 List
<ITmfStateInterval
> intervals
;
159 final ITmfStateSystem ss
= fixture
;
163 int quark
= ss
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
164 long ts1
= ss
.getStartTime(); /* start of the trace */
165 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
167 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
169 /* Activity of IRQ 1 over the whole trace */
170 assertEquals(65, intervals
.size());
172 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
178 * Test a range query with a resolution
181 public void testRangeQuery3() {
182 long time1
= interestingTimestamp1
;
183 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
184 long resolution
= 1000000; /* One query every millisecond */
186 List
<ITmfStateInterval
> intervals
;
188 final ITmfStateSystem ss
= fixture
;
192 quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
193 intervals
= StateSystemUtils
.queryHistoryRange(ss
, quark
, time1
, time2
, resolution
, null);
194 assertEquals(126, intervals
.size()); /* Number of context switches! */
195 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
196 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
198 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
204 * Ask for a time range outside of the trace's range
206 @Test(expected
= TimeRangeException
.class)
207 public void testFullQueryInvalidTime1() throws TimeRangeException
,
208 StateSystemDisposedException
{
209 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
210 fixture
.queryFullState(ts
);
213 @Test(expected
= TimeRangeException
.class)
214 public void testFullQueryInvalidTime2() throws TimeRangeException
,
215 StateSystemDisposedException
{
216 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
217 fixture
.queryFullState(ts
);
220 @Test(expected
= TimeRangeException
.class)
221 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
223 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
224 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
225 fixture
.querySingleState(ts
, quark
);
227 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
232 @Test(expected
= TimeRangeException
.class)
233 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
235 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
236 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
237 fixture
.querySingleState(ts
, quark
);
239 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
244 @Test(expected
= TimeRangeException
.class)
245 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
246 final ITmfStateSystem ss
= fixture
;
250 int quark
= ss
.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 StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
255 } catch (AttributeNotFoundException e
) {
257 } catch (StateSystemDisposedException e
) {
262 @Test(expected
= TimeRangeException
.class)
263 public void testRangeQueryInvalidTime2() throws TimeRangeException
{
264 final ITmfStateSystem ss
= fixture
;
268 int quark
= ss
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
269 long ts1
= startTime
- 1L * NANOSECS_PER_SEC
; /* invalid */
270 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid */
271 StateSystemUtils
.queryHistoryRange(ss
, quark
, ts1
, ts2
);
273 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
279 * Ask for a non-existing attribute
281 * @throws AttributeNotFoundException
283 @Test(expected
= AttributeNotFoundException
.class)
284 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
285 fixture
.getQuarkAbsolute("There", "is", "no", "cow", "level");
289 * Query but with the wrong State Value type
291 @Test(expected
= StateValueTypeException
.class)
292 public void testQueryInvalidValuetype1() throws StateValueTypeException
{
293 List
<ITmfStateInterval
> list
;
294 ITmfStateInterval interval
;
298 list
= fixture
.queryFullState(interestingTimestamp1
);
299 quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
300 interval
= list
.get(quark
);
302 /* This is supposed to be an int value */
303 interval
.getStateValue().unboxStr();
305 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
310 @Test(expected
= StateValueTypeException
.class)
311 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
312 List
<ITmfStateInterval
> list
;
313 ITmfStateInterval interval
;
317 list
= fixture
.queryFullState(interestingTimestamp1
);
318 quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
319 interval
= list
.get(quark
);
321 /* This is supposed to be a String value */
322 interval
.getStateValue().unboxInt();
324 } catch (AttributeNotFoundException
| StateSystemDisposedException e
) {
330 public void testFullAttributeName() {
332 int quark
= fixture
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
333 String name
= fixture
.getFullAttributePath(quark
);
334 assertEquals(name
, "CPUs/0/Current_thread");
336 } catch (AttributeNotFoundException e
) {
342 public void testGetQuarks_begin() {
343 List
<Integer
> list
= fixture
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
345 assertEquals(1, list
.size());
349 public void testGetQuarks_middle() {
350 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
352 /* Number of different kernel threads in the trace */
353 assertEquals(168, list
.size());
357 public void testGetQuarks_end() {
358 List
<Integer
> list
= fixture
.getQuarks(Attributes
.THREADS
, "1577", "*");
360 /* There should be 5 sub-attributes for each Thread node */
361 assertEquals(5, list
.size());
364 // ------------------------------------------------------------------------
365 // Tests verifying the *complete* results of a full queries
366 // ------------------------------------------------------------------------
368 protected long getStartTimes(int idx
) {
369 return TestValues
.startTimes
[idx
];
372 protected long getEndTimes(int idx
) {
373 return TestValues
.endTimes
[idx
];
376 protected ITmfStateValue
getStateValues(int idx
) {
377 return TestValues
.values
[idx
];
381 public void testFullQueryThorough() {
383 List
<ITmfStateInterval
> state
= fixture
.queryFullState(interestingTimestamp1
);
384 assertEquals(TestValues
.size
, state
.size());
386 for (int i
= 0; i
< state
.size(); i
++) {
387 /* Test each component of the intervals */
388 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
389 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
390 assertEquals(i
, state
.get(i
).getAttribute());
391 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
394 } catch (StateSystemDisposedException e
) {
400 public void testFirstIntervalIsConsidered() {
402 int quark
= fixture
.getQuarkAbsolute(Attributes
.THREADS
, "1397", Attributes
.STATUS
);
403 List
<ITmfStateInterval
> list
= fixture
.queryFullState(1331668248014135800L);
404 ITmfStateInterval interval
= list
.get(quark
);
405 assertEquals(1331668247516664825L, interval
.getStartTime());
407 int valueInt
= interval
.getStateValue().unboxInt();
408 assertEquals(1, valueInt
);
410 } catch (StateSystemDisposedException
| AttributeNotFoundException 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
) {