1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
19 import java
.util
.List
;
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
.ITmfStateProvider
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.shared
.CtfTmfTestTrace
;
31 import org
.junit
.Test
;
34 * Base unit tests for the StateHistorySystem. Extension can be made to test
35 * different state back-end types or configurations.
37 * @author Alexandre Montplaisir
39 @SuppressWarnings("javadoc")
40 public abstract class StateSystemTest
{
42 /** Test trace used for these tests */
43 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.TRACE2
;
45 /** Expected start time of the test trace/state history */
46 protected static final long startTime
= 1331668247314038062L;
48 /** Expected end time of the state history built from the test trace */
49 protected static final long endTime
= 1331668259054285979L;
51 /** Number of nanoseconds in one second */
52 private static final long NANOSECS_PER_SEC
= 1000000000L;
54 protected static ITmfStateProvider input
;
55 protected static ITmfStateSystem ssq
;
57 /* Offset in the trace + start time of the trace */
58 static final long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
61 public void testFullQuery1() {
62 List
<ITmfStateInterval
> list
;
63 ITmfStateInterval interval
;
68 list
= ssq
.queryFullState(interestingTimestamp1
);
70 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
71 interval
= list
.get(quark
);
72 valueInt
= interval
.getStateValue().unboxInt();
73 assertEquals(1397, valueInt
);
75 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
76 interval
= list
.get(quark
);
77 valueStr
= interval
.getStateValue().unboxStr();
78 assertEquals("gdbus", valueStr
);
80 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
81 interval
= list
.get(quark
);
82 valueStr
= interval
.getStateValue().unboxStr();
83 assertTrue(valueStr
.equals("sys_poll"));
85 } catch (AttributeNotFoundException e
) {
87 } catch (TimeRangeException e
) {
89 } catch (StateSystemDisposedException e
) {
91 } catch (StateValueTypeException e
) {
97 public void testSingleQuery1() {
98 long timestamp
= interestingTimestamp1
;
100 ITmfStateInterval interval
;
104 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
105 interval
= ssq
.querySingleState(timestamp
, quark
);
106 valueStr
= interval
.getStateValue().unboxStr();
107 assertEquals("gdbus", valueStr
);
109 } catch (AttributeNotFoundException e
) {
111 } catch (TimeRangeException e
) {
113 } catch (StateSystemDisposedException e
) {
115 } catch (StateValueTypeException e
) {
121 * Test a range query (with no resolution parameter, so all intervals)
124 public void testRangeQuery1() {
125 long time1
= interestingTimestamp1
;
126 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
128 List
<ITmfStateInterval
> intervals
;
131 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
132 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
133 assertEquals(487, intervals
.size()); /* Number of context switches! */
134 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
135 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
137 } catch (AttributeNotFoundException e
) {
139 } catch (TimeRangeException e
) {
141 } catch (StateSystemDisposedException e
) {
143 } catch (StateValueTypeException e
) {
149 * Range query, but with a t2 far off the end of the trace. The result
150 * should still be valid.
153 public void testRangeQuery2() {
154 List
<ITmfStateInterval
> intervals
;
157 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
158 long ts1
= ssq
.getStartTime(); /* start of the trace */
159 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
161 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
163 /* Activity of IRQ 1 over the whole trace */
164 assertEquals(65, intervals
.size());
166 } catch (AttributeNotFoundException e
) {
168 } catch (TimeRangeException e
) {
170 } catch (StateSystemDisposedException e
) {
176 * Test a range query with a resolution
179 public void testRangeQuery3() {
180 long time1
= interestingTimestamp1
;
181 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
182 long resolution
= 1000000; /* One query every millisecond */
184 List
<ITmfStateInterval
> intervals
;
187 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
188 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
189 assertEquals(126, intervals
.size()); /* Number of context switches! */
190 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
191 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
193 } catch (AttributeNotFoundException e
) {
195 } catch (TimeRangeException e
) {
197 } catch (StateSystemDisposedException e
) {
199 } catch (StateValueTypeException 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 ssq
.queryFullState(ts
);
214 @Test(expected
= TimeRangeException
.class)
215 public void testFullQueryInvalidTime2() throws TimeRangeException
,
216 StateSystemDisposedException
{
217 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
218 ssq
.queryFullState(ts
);
221 @Test(expected
= TimeRangeException
.class)
222 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
224 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
225 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
226 ssq
.querySingleState(ts
, quark
);
228 } catch (AttributeNotFoundException e
) {
230 } catch (StateSystemDisposedException e
) {
235 @Test(expected
= TimeRangeException
.class)
236 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
238 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
239 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
240 ssq
.querySingleState(ts
, quark
);
242 } catch (AttributeNotFoundException e
) {
244 } catch (StateSystemDisposedException e
) {
249 @Test(expected
= TimeRangeException
.class)
250 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
252 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
253 long ts1
= startTime
- 20L * NANOSECS_PER_SEC
; /* invalid */
254 long ts2
= startTime
+ 1L * NANOSECS_PER_SEC
; /* valid */
255 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
257 } catch (AttributeNotFoundException e
) {
259 } catch (StateSystemDisposedException e
) {
264 @Test(expected
= TimeRangeException
.class)
265 public void testRangeQueryInvalidTime2() throws TimeRangeException
{
267 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
268 long ts1
= startTime
- 1L * NANOSECS_PER_SEC
; /* invalid */
269 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid */
270 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
272 } catch (AttributeNotFoundException e
) {
274 } catch (StateSystemDisposedException e
) {
280 * Ask for a non-existing attribute
282 * @throws AttributeNotFoundException
284 @Test(expected
= AttributeNotFoundException
.class)
285 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
286 ssq
.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
= ssq
.queryFullState(interestingTimestamp1
);
300 quark
= ssq
.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 e
) {
308 } catch (TimeRangeException e
) {
310 } catch (StateSystemDisposedException e
) {
315 @Test(expected
= StateValueTypeException
.class)
316 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
317 List
<ITmfStateInterval
> list
;
318 ITmfStateInterval interval
;
322 list
= ssq
.queryFullState(interestingTimestamp1
);
323 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
324 interval
= list
.get(quark
);
326 /* This is supposed to be a String value */
327 interval
.getStateValue().unboxInt();
329 } catch (AttributeNotFoundException e
) {
331 } catch (TimeRangeException e
) {
333 } catch (StateSystemDisposedException e
) {
339 public void testFullAttributeName() {
341 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
342 String name
= ssq
.getFullAttributePath(quark
);
343 assertEquals(name
, "CPUs/0/Current_thread");
345 } catch (AttributeNotFoundException e
) {
351 public void testGetQuarks_begin() {
352 List
<Integer
> list
= ssq
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
354 assertEquals(1, list
.size());
358 public void testGetQuarks_middle() {
359 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
361 /* Number of different kernel threads in the trace */
362 assertEquals(168, list
.size());
366 public void testGetQuarks_end() {
367 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "1577", "*");
369 /* There should be 4 sub-attributes for each Thread node */
370 assertEquals(4, list
.size());
373 // ------------------------------------------------------------------------
374 // Tests verifying the *complete* results of a full queries
375 // ------------------------------------------------------------------------
377 protected long getStartTimes(int idx
) {
378 return TestValues
.startTimes
[idx
];
381 protected long getEndTimes(int idx
) {
382 return TestValues
.endTimes
[idx
];
385 protected ITmfStateValue
getStateValues(int idx
) {
386 return TestValues
.values
[idx
];
390 public void testFullQueryThorough() {
392 List
<ITmfStateInterval
> state
= ssq
.queryFullState(interestingTimestamp1
);
393 assertEquals(TestValues
.size
, state
.size());
395 for (int i
= 0; i
< state
.size(); i
++) {
396 /* Test each component of the intervals */
397 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
398 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
399 assertEquals(i
, state
.get(i
).getAttribute());
400 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
403 } catch (TimeRangeException e
) {
405 } catch (StateSystemDisposedException e
) {
411 public void testFirstIntervalIsConsidered() {
413 List
<ITmfStateInterval
> list
= ssq
.queryFullState(1331668248014135800L);
414 ITmfStateInterval interval
= list
.get(233);
415 assertEquals(1331668247516664825L, interval
.getStartTime());
417 int valueInt
= interval
.getStateValue().unboxInt();
418 assertEquals(1, valueInt
);
419 } catch (TimeRangeException e
) {
421 } catch (StateSystemDisposedException e
) {
423 } catch (StateValueTypeException e
) {