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
.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
.ITmfStateSystem
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
29 import org
.eclipse
.linuxtools
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
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 /** Test trace used for these tests */
42 protected static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.TRACE2
;
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 ITmfStateSystem ssq
;
55 /* Offset in the trace + start time of the trace */
56 static final long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
59 public void testFullQuery1() {
60 List
<ITmfStateInterval
> list
;
61 ITmfStateInterval interval
;
66 list
= ssq
.queryFullState(interestingTimestamp1
);
68 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
69 interval
= list
.get(quark
);
70 valueInt
= interval
.getStateValue().unboxInt();
71 assertEquals(1397, valueInt
);
73 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
74 interval
= list
.get(quark
);
75 valueStr
= interval
.getStateValue().unboxStr();
76 assertEquals("gdbus", valueStr
);
78 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
79 interval
= list
.get(quark
);
80 valueStr
= interval
.getStateValue().unboxStr();
81 assertTrue(valueStr
.equals("sys_poll"));
83 } catch (AttributeNotFoundException e
) {
85 } catch (TimeRangeException e
) {
87 } catch (StateSystemDisposedException e
) {
89 } catch (StateValueTypeException e
) {
95 public void testSingleQuery1() {
96 long timestamp
= interestingTimestamp1
;
98 ITmfStateInterval interval
;
102 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
103 interval
= ssq
.querySingleState(timestamp
, quark
);
104 valueStr
= interval
.getStateValue().unboxStr();
105 assertEquals("gdbus", valueStr
);
107 } catch (AttributeNotFoundException e
) {
109 } catch (TimeRangeException e
) {
111 } catch (StateSystemDisposedException e
) {
113 } catch (StateValueTypeException e
) {
119 * Test a range query (with no resolution parameter, so all intervals)
122 public void testRangeQuery1() {
123 long time1
= interestingTimestamp1
;
124 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
126 List
<ITmfStateInterval
> intervals
;
129 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
130 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
131 assertEquals(487, intervals
.size()); /* Number of context switches! */
132 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
133 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
135 } catch (AttributeNotFoundException e
) {
137 } catch (TimeRangeException e
) {
139 } catch (StateSystemDisposedException e
) {
141 } catch (StateValueTypeException e
) {
147 * Range query, but with a t2 far off the end of the trace. The result
148 * should still be valid.
151 public void testRangeQuery2() {
152 List
<ITmfStateInterval
> intervals
;
155 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
156 long ts1
= ssq
.getStartTime(); /* start of the trace */
157 long ts2
= startTime
+ 20L * NANOSECS_PER_SEC
; /* invalid, but ignored */
159 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
161 /* Activity of IRQ 1 over the whole trace */
162 assertEquals(65, intervals
.size());
164 } catch (AttributeNotFoundException e
) {
166 } catch (TimeRangeException e
) {
168 } catch (StateSystemDisposedException e
) {
174 * Test a range query with a resolution
177 public void testRangeQuery3() {
178 long time1
= interestingTimestamp1
;
179 long time2
= time1
+ 1L * NANOSECS_PER_SEC
;
180 long resolution
= 1000000; /* One query every millisecond */
182 List
<ITmfStateInterval
> intervals
;
185 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
186 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
187 assertEquals(126, intervals
.size()); /* Number of context switches! */
188 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
189 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
191 } catch (AttributeNotFoundException e
) {
193 } catch (TimeRangeException e
) {
195 } catch (StateSystemDisposedException e
) {
197 } catch (StateValueTypeException e
) {
203 * Ask for a time range outside of the trace's range
205 @Test(expected
= TimeRangeException
.class)
206 public void testFullQueryInvalidTime1() throws TimeRangeException
,
207 StateSystemDisposedException
{
208 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
209 ssq
.queryFullState(ts
);
212 @Test(expected
= TimeRangeException
.class)
213 public void testFullQueryInvalidTime2() throws TimeRangeException
,
214 StateSystemDisposedException
{
215 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
216 ssq
.queryFullState(ts
);
219 @Test(expected
= TimeRangeException
.class)
220 public void testSingleQueryInvalidTime1() throws TimeRangeException
{
222 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
223 long ts
= startTime
+ 20L * NANOSECS_PER_SEC
;
224 ssq
.querySingleState(ts
, quark
);
226 } catch (AttributeNotFoundException e
) {
228 } catch (StateSystemDisposedException e
) {
233 @Test(expected
= TimeRangeException
.class)
234 public void testSingleQueryInvalidTime2() throws TimeRangeException
{
236 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
237 long ts
= startTime
- 20L * NANOSECS_PER_SEC
;
238 ssq
.querySingleState(ts
, quark
);
240 } catch (AttributeNotFoundException e
) {
242 } catch (StateSystemDisposedException e
) {
247 @Test(expected
= TimeRangeException
.class)
248 public void testRangeQueryInvalidTime1() throws TimeRangeException
{
250 int quark
= ssq
.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 ssq
.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
= ssq
.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 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
270 } catch (AttributeNotFoundException e
) {
272 } catch (StateSystemDisposedException e
) {
278 * Ask for a non-existing attribute
280 * @throws AttributeNotFoundException
282 @Test(expected
= AttributeNotFoundException
.class)
283 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
284 ssq
.getQuarkAbsolute("There", "is", "no", "cow", "level");
288 * Query but with the wrong State Value type
290 @Test(expected
= StateValueTypeException
.class)
291 public void testQueryInvalidValuetype1() throws StateValueTypeException
{
292 List
<ITmfStateInterval
> list
;
293 ITmfStateInterval interval
;
297 list
= ssq
.queryFullState(interestingTimestamp1
);
298 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
299 interval
= list
.get(quark
);
301 /* This is supposed to be an int value */
302 interval
.getStateValue().unboxStr();
304 } catch (AttributeNotFoundException e
) {
306 } catch (TimeRangeException e
) {
308 } catch (StateSystemDisposedException e
) {
313 @Test(expected
= StateValueTypeException
.class)
314 public void testQueryInvalidValuetype2() throws StateValueTypeException
{
315 List
<ITmfStateInterval
> list
;
316 ITmfStateInterval interval
;
320 list
= ssq
.queryFullState(interestingTimestamp1
);
321 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
322 interval
= list
.get(quark
);
324 /* This is supposed to be a String value */
325 interval
.getStateValue().unboxInt();
327 } catch (AttributeNotFoundException e
) {
329 } catch (TimeRangeException e
) {
331 } catch (StateSystemDisposedException e
) {
337 public void testFullAttributeName() {
339 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
340 String name
= ssq
.getFullAttributePath(quark
);
341 assertEquals(name
, "CPUs/0/Current_thread");
343 } catch (AttributeNotFoundException e
) {
349 public void testGetQuarks_begin() {
350 List
<Integer
> list
= ssq
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
352 assertEquals(1, list
.size());
356 public void testGetQuarks_middle() {
357 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
359 /* Number of different kernel threads in the trace */
360 assertEquals(168, list
.size());
364 public void testGetQuarks_end() {
365 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "1577", "*");
367 /* There should be 4 sub-attributes for each Thread node */
368 assertEquals(4, list
.size());
371 // ------------------------------------------------------------------------
372 // Tests verifying the *complete* results of a full queries
373 // ------------------------------------------------------------------------
375 protected long getStartTimes(int idx
) {
376 return TestValues
.startTimes
[idx
];
379 protected long getEndTimes(int idx
) {
380 return TestValues
.endTimes
[idx
];
383 protected ITmfStateValue
getStateValues(int idx
) {
384 return TestValues
.values
[idx
];
388 public void testFullQueryThorough() {
390 List
<ITmfStateInterval
> state
= ssq
.queryFullState(interestingTimestamp1
);
391 assertEquals(TestValues
.size
, state
.size());
393 for (int i
= 0; i
< state
.size(); i
++) {
394 /* Test each component of the intervals */
395 assertEquals(getStartTimes(i
), state
.get(i
).getStartTime());
396 assertEquals(getEndTimes(i
), state
.get(i
).getEndTime());
397 assertEquals(i
, state
.get(i
).getAttribute());
398 assertEquals(getStateValues(i
), state
.get(i
).getStateValue());
401 } catch (TimeRangeException e
) {
403 } catch (StateSystemDisposedException e
) {
409 public void testFirstIntervalIsConsidered() {
411 List
<ITmfStateInterval
> list
= ssq
.queryFullState(1331668248014135800L);
412 ITmfStateInterval interval
= list
.get(233);
413 assertEquals(1331668247516664825L, interval
.getStartTime());
415 int valueInt
= interval
.getStateValue().unboxInt();
416 assertEquals(1, valueInt
);
417 } catch (TimeRangeException e
) {
419 } catch (StateSystemDisposedException e
) {
421 } catch (StateValueTypeException e
) {
427 public void testParentAttribute() {
428 String
[] path
= { "CPUs/0/Current_thread",
432 int q
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
433 for (int i
= 0; i
< path
.length
; i
++) {
434 String name
= ssq
.getFullAttributePath(q
);
435 assertEquals(path
[i
], name
);
436 q
= ssq
.getParentAttributeQuark(q
);
439 q
= ssq
.getParentAttributeQuark(q
);
441 } catch (AttributeNotFoundException e
) {