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
.junit
.Test
;
33 * Base unit tests for the StateHistorySystem. Extension can be made to test
34 * different state back-end types or configurations.
36 * @author Alexandre Montplaisir
38 @SuppressWarnings("javadoc")
39 public abstract class StateSystemTest
{
41 /** Index of the test trace used for these tests */
42 protected static final int TRACE_INDEX
= 1;
44 /** Expected start time of the test trace/state history */
45 protected static final long startTime
= 1331668247314038062L;
47 /** Expected end time of the state history built from the test trace */
48 protected static final long endTime
= 1331668259054285979L;
50 /** Number of nanoseconds in one second */
51 private static final long NANOSECS_PER_SEC
= 1000000000L;
53 protected static ITmfStateProvider input
;
54 protected static ITmfStateSystem ssq
;
56 /* Offset in the trace + start time of the trace */
57 static final long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
60 public void testFullQuery1() {
61 List
<ITmfStateInterval
> list
;
62 ITmfStateInterval interval
;
67 list
= ssq
.queryFullState(interestingTimestamp1
);
69 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
70 interval
= list
.get(quark
);
71 valueInt
= interval
.getStateValue().unboxInt();
72 assertEquals(1397, valueInt
);
74 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
75 interval
= list
.get(quark
);
76 valueStr
= interval
.getStateValue().unboxStr();
77 assertEquals("gdbus", valueStr
);
79 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
80 interval
= list
.get(quark
);
81 valueStr
= interval
.getStateValue().unboxStr();
82 assertTrue(valueStr
.equals("sys_poll"));
84 } catch (AttributeNotFoundException e
) {
86 } catch (TimeRangeException e
) {
88 } catch (StateSystemDisposedException e
) {
90 } catch (StateValueTypeException e
) {
96 public void testSingleQuery1() {
97 long timestamp
= interestingTimestamp1
;
99 ITmfStateInterval interval
;
103 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
104 interval
= ssq
.querySingleState(timestamp
, quark
);
105 valueStr
= interval
.getStateValue().unboxStr();
106 assertEquals("gdbus", valueStr
);
108 } catch (AttributeNotFoundException e
) {
110 } catch (TimeRangeException e
) {
112 } catch (StateSystemDisposedException e
) {
114 } catch (StateValueTypeException e
) {
120 * Test a range query (with no resolution parameter, so all intervals)
123 public void testRangeQuery1() {
124 long time1
= interestingTimestamp1
;
125 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
127 List
<ITmfStateInterval
> intervals
;
130 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
131 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
132 assertEquals(487, intervals
.size()); /* Number of context switches! */
133 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
134 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
136 } catch (AttributeNotFoundException e
) {
138 } catch (TimeRangeException e
) {
140 } catch (StateSystemDisposedException e
) {
142 } catch (StateValueTypeException e
) {
148 * Range query, but with a t2 far off the end of the trace.
149 * The result should still be valid.
152 public void testRangeQuery2() {
153 List
<ITmfStateInterval
> intervals
;
156 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
157 long ts1
= ssq
.getStartTime(); /* start of the trace */
158 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
160 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
162 /* Activity of IRQ 1 over the whole trace */
163 assertEquals(65, intervals
.size());
165 } catch (AttributeNotFoundException e
) {
167 } catch (TimeRangeException e
) {
169 } catch (StateSystemDisposedException e
) {
175 * Test a range query with a resolution
178 public void testRangeQuery3() {
179 long time1
= interestingTimestamp1
;
180 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
181 long resolution
= 1000000; /* One query every millisecond */
183 List
<ITmfStateInterval
> intervals
;
186 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
187 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
188 assertEquals(126, intervals
.size()); /* Number of context switches! */
189 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
190 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
192 } catch (AttributeNotFoundException e
) {
194 } catch (TimeRangeException e
) {
196 } catch (StateSystemDisposedException e
) {
198 } catch (StateValueTypeException 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 ssq
.queryFullState(ts
);
213 @Test(expected
= TimeRangeException
.class)
214 public void testFullQueryInvalidTime2() throws TimeRangeException
,
215 StateSystemDisposedException
{
216 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
217 ssq
.queryFullState(ts
);
220 @Test(expected
= TimeRangeException
.class)
221 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
223 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
224 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
225 ssq
.querySingleState(ts
, quark
);
227 } catch (AttributeNotFoundException e
) {
229 } catch (StateSystemDisposedException e
) {
234 @Test(expected
= TimeRangeException
.class)
235 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
237 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
238 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
239 ssq
.querySingleState(ts
, quark
);
241 } catch (AttributeNotFoundException e
) {
243 } catch (StateSystemDisposedException e
) {
248 @Test(expected
= TimeRangeException
.class)
249 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
251 int quark
= ssq
.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 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
256 } catch (AttributeNotFoundException e
) {
258 } catch (StateSystemDisposedException e
) {
263 @Test(expected
= TimeRangeException
.class)
264 public void testRangeQueryInvalidTime2() throws TimeRangeException
{
266 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
267 long ts1
= startTime
- 1L * NANOSECS_PER_SEC
; /* invalid */
268 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid */
269 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
271 } catch (AttributeNotFoundException e
) {
273 } catch (StateSystemDisposedException e
) {
279 * Ask for a non-existing attribute
281 * @throws AttributeNotFoundException
283 @Test(expected
= AttributeNotFoundException
.class)
284 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
285 ssq
.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
= ssq
.queryFullState(interestingTimestamp1
);
299 quark
= ssq
.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 e
) {
307 } catch (TimeRangeException e
) {
309 } catch (StateSystemDisposedException e
) {
314 @Test(expected
= StateValueTypeException
.class)
315 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
316 List
<ITmfStateInterval
> list
;
317 ITmfStateInterval interval
;
321 list
= ssq
.queryFullState(interestingTimestamp1
);
322 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
323 interval
= list
.get(quark
);
325 /* This is supposed to be a String value */
326 interval
.getStateValue().unboxInt();
328 } catch (AttributeNotFoundException e
) {
330 } catch (TimeRangeException e
) {
332 } catch (StateSystemDisposedException e
) {
338 public void testFullAttributeName() {
340 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
341 String name
= ssq
.getFullAttributePath(quark
);
342 assertEquals(name
, "CPUs/0/Current_thread");
344 } catch (AttributeNotFoundException e
) {
350 public void testGetQuarks_begin() {
351 List
<Integer
> list
= ssq
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
353 assertEquals(1, list
.size());
357 public void testGetQuarks_middle() {
358 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
360 /* Number of different kernel threads in the trace */
361 assertEquals(168, list
.size());
365 public void testGetQuarks_end() {
366 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "1577", "*");
368 /* There should be 4 sub-attributes for each Thread node */
369 assertEquals(4, list
.size());
372 // ------------------------------------------------------------------------
373 // Tests verifying the *complete* results of a full queries
374 // ------------------------------------------------------------------------
376 protected long getStartTimes(int idx
) {
377 return TestValues
.startTimes
[idx
];
380 protected long getEndTimes(int idx
) {
381 return TestValues
.endTimes
[idx
];
384 protected ITmfStateValue
getStateValues(int idx
) {
385 return TestValues
.values
[idx
];
389 public void testFullQueryThorough() {
391 List
<ITmfStateInterval
> state
= ssq
.queryFullState(interestingTimestamp1
);
392 assertEquals(TestValues
.size
, state
.size());
394 for (int i
= 0; i
< state
.size(); i
++) {
395 /* Test each component of the intervals */
396 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
397 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
398 assertEquals(i
, state
.get(i
).getAttribute());
399 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
402 } catch (TimeRangeException e
) {
404 } catch (StateSystemDisposedException e
) {