1 /*******************************************************************************
2 * Copyright (c) 2012 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
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.util
.List
;
22 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateChangeInput
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.StateSystemManager
;
32 import org
.junit
.AfterClass
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Test
;
37 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
38 * history and the non-threaded CTF kernel handler.
43 @SuppressWarnings({"nls", "javadoc"})
44 public class StateSystemFullHistoryTest
{
46 static File stateFile
;
47 static File stateFileBenchmark
;
49 static IStateChangeInput input
;
50 static IStateSystemQuerier ssq
;
52 /* Offset in the trace + start time of the trace */
53 private final static long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
55 protected static String
getTestFileName() {
56 return "/tmp/statefile.ht"; //$NON-NLS-1$
60 public static void initialize() {
61 stateFile
= new File(getTestFileName());
62 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
64 input
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
65 ssq
= StateSystemManager
.loadStateHistory(stateFile
, input
, true);
66 } catch (Exception e
) {
72 public static void cleanup() {
74 ret1
= stateFile
.delete();
75 ret2
= stateFileBenchmark
.delete();
76 if ( !(ret1
&& ret2
) ) {
77 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
78 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
83 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
84 * us to @Test the @BeforeClass...
87 * @throws TmfTraceException
90 public void testBuild() throws TmfTraceException
{
91 IStateChangeInput input2
;
92 IStateSystemQuerier ssb2
;
94 input2
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
95 ssb2
= StateSystemManager
.loadStateHistory(stateFileBenchmark
, input2
, true);
97 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
98 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
102 public void testOpenExistingStateFile() throws TmfTraceException
{
103 IStateSystemQuerier ssb2
;
105 /* 'newStateFile' should have already been created */
106 ssb2
= StateSystemManager
.loadStateHistory(stateFile
, null, true);
109 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
110 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
114 public void testFullQuery1() throws StateValueTypeException
,
115 AttributeNotFoundException
, TimeRangeException
{
117 List
<ITmfStateInterval
> list
;
118 ITmfStateInterval interval
;
122 list
= ssq
.queryFullState(interestingTimestamp1
);
124 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
125 interval
= list
.get(quark
);
126 valueInt
= interval
.getStateValue().unboxInt();
127 assertEquals(1397, valueInt
);
129 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
130 interval
= list
.get(quark
);
131 valueStr
= interval
.getStateValue().unboxStr();
132 assertEquals("gdbus", valueStr
);
134 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
135 interval
= list
.get(quark
);
136 valueStr
= interval
.getStateValue().unboxStr();
137 assertTrue(valueStr
.equals("sys_poll"));
141 public void testFullQuery2() {
146 public void testFullQuery3() {
151 public void testSingleQuery1() throws AttributeNotFoundException
,
152 TimeRangeException
, StateValueTypeException
{
154 long timestamp
= interestingTimestamp1
;
156 ITmfStateInterval interval
;
159 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
160 interval
= ssq
.querySingleState(timestamp
, quark
);
161 valueStr
= interval
.getStateValue().unboxStr();
162 assertEquals("gdbus", valueStr
);
166 public void testSingleQuery2() {
171 public void testSingleQuery3() {
176 * Test a range query (with no resolution parameter, so all intervals)
179 public void testRangeQuery1() throws AttributeNotFoundException
,
180 TimeRangeException
, StateValueTypeException
{
182 long time1
= interestingTimestamp1
;
183 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
185 List
<ITmfStateInterval
> intervals
;
187 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
188 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
189 assertEquals(487, intervals
.size()); /* Number of context switches! */
190 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
191 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
195 * Range query, but with a t2 far off the end of the trace.
196 * The result should still be valid.
199 public void testRangeQuery2() throws TimeRangeException
,
200 AttributeNotFoundException
{
202 List
<ITmfStateInterval
> intervals
;
204 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
205 long ts1
= ssq
.getStartTime(); /* start of the trace */
206 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
208 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
210 /* Activity of IRQ 1 over the whole trace */
211 assertEquals(65, intervals
.size());
215 * Test a range query with a resolution
218 public void testRangeQuery3() throws AttributeNotFoundException
,
219 TimeRangeException
, StateValueTypeException
{
221 long time1
= interestingTimestamp1
;
222 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
223 long resolution
= 1000000; /* One query every millisecond */
225 List
<ITmfStateInterval
> intervals
;
227 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
228 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
229 assertEquals(126, intervals
.size()); /* Number of context switches! */
230 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
231 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
235 * Ask for a time range outside of the trace's range
237 * @throws TimeRangeException
239 @Test(expected
= TimeRangeException
.class)
240 public void testFullQueryInvalidTime1() throws TimeRangeException
{
241 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
242 ssq
.queryFullState(ts
);
246 @Test(expected
= TimeRangeException
.class)
247 public void testFullQueryInvalidTime2() throws TimeRangeException
{
248 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
249 ssq
.queryFullState(ts
);
253 @Test(expected
= TimeRangeException
.class)
254 public void testSingleQueryInvalidTime1()
255 throws AttributeNotFoundException
, TimeRangeException
{
257 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
258 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
259 ssq
.querySingleState(ts
, quark
);
262 @Test(expected
= TimeRangeException
.class)
263 public void testSingleQueryInvalidTime2()
264 throws AttributeNotFoundException
, TimeRangeException
{
266 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
267 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
268 ssq
.querySingleState(ts
, quark
);
271 @Test(expected
= TimeRangeException
.class)
272 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
275 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
276 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
277 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
279 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
282 @Test(expected
= TimeRangeException
.class)
283 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
284 AttributeNotFoundException
{
286 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
287 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
288 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
290 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
294 * Ask for a non-existing attribute
296 * @throws AttributeNotFoundException
298 @Test(expected
= AttributeNotFoundException
.class)
299 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
301 ssq
.getQuarkAbsolute("There", "is", "no", "cow", "level");
305 * Query but with the wrong State Value type
307 * @throws StateValueTypeException
308 * @throws AttributeNotFoundException
309 * @throws TimeRangeException
311 @Test(expected
= StateValueTypeException
.class)
312 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
313 AttributeNotFoundException
, TimeRangeException
{
314 List
<ITmfStateInterval
> list
;
315 ITmfStateInterval interval
;
318 list
= ssq
.queryFullState(interestingTimestamp1
);
319 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
320 interval
= list
.get(quark
);
322 /* This is supposed to be an int value */
323 interval
.getStateValue().unboxStr();
326 @Test(expected
= StateValueTypeException
.class)
327 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
328 AttributeNotFoundException
, TimeRangeException
{
329 List
<ITmfStateInterval
> list
;
330 ITmfStateInterval interval
;
333 list
= ssq
.queryFullState(interestingTimestamp1
);
334 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
335 interval
= list
.get(quark
);
337 /* This is supposed to be a String value */
338 interval
.getStateValue().unboxInt();
342 public void testFullAttributeName() throws AttributeNotFoundException
{
343 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
344 String name
= ssq
.getFullAttributePath(quark
);
345 assertEquals(name
, "CPUs/0/Current_thread");
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());