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
.*;
18 import java
.io
.FileNotFoundException
;
19 import java
.io
.IOException
;
20 import java
.io
.PrintWriter
;
21 import java
.util
.List
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.AttributeNotFoundException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.StateHistorySystem
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.TimeRangeException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.backend
.historytree
.HistoryTreeBackend
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.HistoryBuilder
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.IStateChangeInput
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.IStateHistoryBackend
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.StateValueTypeException
;
33 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
34 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.trace
.Attributes
;
38 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
39 * history and the non-threaded CTF kernel handler.
44 @SuppressWarnings("nls")
45 public class StateSystemFullHistoryTest
{
47 static File stateFile
;
48 static File stateFileBenchmark
;
50 static HistoryBuilder builder
;
51 static IStateChangeInput input
;
52 static IStateHistoryBackend hp
;
53 static StateHistorySystem shs
;
55 /* Offset in the trace + start time of the trace */
56 private final static long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
58 protected static String
getTestFileName() {
59 return "/tmp/statefile.ht"; //$NON-NLS-1$
63 public static void initialize() {
64 stateFile
= new File(getTestFileName());
65 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
67 input
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
68 hp
= new HistoryTreeBackend(stateFile
, input
.getStartTime());
69 builder
= new HistoryBuilder(input
, hp
);
70 } catch (Exception e
) {
74 shs
= (StateHistorySystem
) builder
.getSS();
75 builder
.close(); /* Waits for the construction to finish */
79 public static void cleanup() {
81 ret1
= stateFile
.delete();
82 ret2
= stateFileBenchmark
.delete();
83 if ( !(ret1
&& ret2
) ) {
84 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
85 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
90 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
91 * us to @Test the @BeforeClass...
94 * @throws TmfTraceException
97 public void testBuild() throws IOException
, TmfTraceException
{
98 HistoryBuilder zebuilder
;
99 IStateChangeInput zeinput
;
100 IStateHistoryBackend zehp
= null;
102 zeinput
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
103 zehp
= new HistoryTreeBackend(stateFileBenchmark
, zeinput
.getStartTime());
104 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
108 assertEquals(CtfTestFiles
.startTime
, zehp
.getStartTime());
109 assertEquals(CtfTestFiles
.endTime
, zehp
.getEndTime());
113 public void testOpenExistingStateFile() throws IOException
{
114 IStateHistoryBackend hp2
= null;
115 StateHistorySystem shs2
= null;
117 /* 'newStateFile' should have already been created */
118 hp2
= new HistoryTreeBackend(stateFile
);
119 shs2
= new StateHistorySystem(hp2
, false);
122 assertEquals(CtfTestFiles
.startTime
, hp2
.getStartTime());
123 assertEquals(CtfTestFiles
.endTime
, hp2
.getEndTime());
127 public void testFullQuery1() throws StateValueTypeException
,
128 AttributeNotFoundException
, TimeRangeException
{
130 List
<ITmfStateInterval
> list
;
131 ITmfStateInterval interval
;
132 int quark
, quark2
, valueInt
;
135 list
= shs
.loadStateAtTime(interestingTimestamp1
);
137 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
138 interval
= list
.get(quark
);
139 valueInt
= interval
.getStateValue().unboxInt();
140 assertEquals(1397, valueInt
);
142 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
143 interval
= list
.get(quark
);
144 valueStr
= interval
.getStateValue().unboxStr();
145 assertEquals("gdbus", valueStr
);
147 /* Query a stack attribute, has to be done in two passes */
148 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_MODE_STACK
);
149 interval
= list
.get(quark
);
150 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
151 quark2
= shs
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
152 interval
= list
.get(quark2
);
153 valueStr
= interval
.getStateValue().unboxStr();
154 assertTrue(valueStr
.equals("sys_poll"));
158 public void testFullQuery2() {
163 public void testFullQuery3() {
168 public void testSingleQuery1() throws AttributeNotFoundException
,
169 TimeRangeException
, StateValueTypeException
{
171 long timestamp
= interestingTimestamp1
;
173 ITmfStateInterval interval
;
176 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
177 interval
= shs
.querySingleState(timestamp
, quark
);
178 valueStr
= interval
.getStateValue().unboxStr();
179 assertEquals("gdbus", valueStr
);
183 public void testSingleQuery2() {
188 public void testSingleQuery3() {
193 * Test a range query (with no resolution parameter, so all intervals)
196 public void testRangeQuery1() throws AttributeNotFoundException
,
197 TimeRangeException
, StateValueTypeException
{
199 long time1
= interestingTimestamp1
;
200 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
202 List
<ITmfStateInterval
> intervals
;
204 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
205 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
206 assertEquals(487, intervals
.size()); /* Number of context switches! */
207 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
208 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
212 * Range query, but with a t2 far off the end of the trace.
213 * The result should still be valid.
216 public void testRangeQuery2() throws TimeRangeException
,
217 AttributeNotFoundException
{
219 List
<ITmfStateInterval
> intervals
;
221 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.IRQ_STACK
);
222 long ts1
= shs
.getHistoryBackend().getStartTime(); /* start of the trace */
223 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
225 intervals
= shs
.queryHistoryRange(quark
, ts1
, ts2
);
227 /* Nb of IRQs on CPU 0 during the whole trace */
228 assertEquals(1653, intervals
.size());
232 * Test a range query with a resolution
235 public void testRangeQuery3() throws AttributeNotFoundException
,
236 TimeRangeException
, StateValueTypeException
{
238 long time1
= interestingTimestamp1
;
239 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
240 long resolution
= 1000000; /* One query every millisecond */
242 List
<ITmfStateInterval
> intervals
;
244 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
245 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
, resolution
);
246 assertEquals(129, intervals
.size()); /* Number of context switches! */
247 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
248 assertEquals(1331668248784789238L, intervals
.get(100).getEndTime());
252 * Ask for a time range outside of the trace's range
254 * @throws TimeRangeException
256 @Test(expected
= TimeRangeException
.class)
257 public void testFullQueryInvalidTime1() throws TimeRangeException
{
258 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
259 shs
.loadStateAtTime(ts
);
263 @Test(expected
= TimeRangeException
.class)
264 public void testFullQueryInvalidTime2() throws TimeRangeException
{
265 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
266 shs
.loadStateAtTime(ts
);
270 @Test(expected
= TimeRangeException
.class)
271 public void testSingleQueryInvalidTime1()
272 throws AttributeNotFoundException
, TimeRangeException
{
274 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
275 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
276 shs
.querySingleState(ts
, quark
);
279 @Test(expected
= TimeRangeException
.class)
280 public void testSingleQueryInvalidTime2()
281 throws AttributeNotFoundException
, TimeRangeException
{
283 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
284 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
285 shs
.querySingleState(ts
, quark
);
288 @Test(expected
= TimeRangeException
.class)
289 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
292 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
293 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
294 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
296 shs
.queryHistoryRange(quark
, ts1
, ts2
);
299 @Test(expected
= TimeRangeException
.class)
300 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
301 AttributeNotFoundException
{
303 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
304 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
305 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
307 shs
.queryHistoryRange(quark
, ts1
, ts2
);
311 * Ask for a non-existing attribute
313 * @throws AttributeNotFoundException
315 @Test(expected
= AttributeNotFoundException
.class)
316 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
318 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
322 * Query but with the wrong State Value type
324 * @throws StateValueTypeException
325 * @throws AttributeNotFoundException
326 * @throws TimeRangeException
328 @Test(expected
= StateValueTypeException
.class)
329 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
330 AttributeNotFoundException
, TimeRangeException
{
331 List
<ITmfStateInterval
> list
;
332 ITmfStateInterval interval
;
335 list
= shs
.loadStateAtTime(interestingTimestamp1
);
336 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
337 interval
= list
.get(quark
);
339 /* This is supposed to be an int value */
340 interval
.getStateValue().unboxStr();
343 @Test(expected
= StateValueTypeException
.class)
344 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
345 AttributeNotFoundException
, TimeRangeException
{
346 List
<ITmfStateInterval
> list
;
347 ITmfStateInterval interval
;
350 list
= shs
.loadStateAtTime(interestingTimestamp1
);
351 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
352 interval
= list
.get(quark
);
354 /* This is supposed to be a String value */
355 interval
.getStateValue().unboxInt();
359 public void testFullAttributeName() throws AttributeNotFoundException
{
360 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
361 String name
= shs
.getFullAttributePath(quark
);
362 assertEquals(name
, "CPUs/0/Current_thread");
366 public void testGetQuarks_begin() {
367 List
<Integer
> list
= shs
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
369 assertEquals(1, list
.size());
370 assertEquals(Integer
.valueOf(398), list
.get(0));
374 public void testGetQuarks_middle() {
375 List
<Integer
> list
= shs
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
377 assertEquals(Integer
.valueOf(18), list
.get(4));
378 assertEquals(Integer
.valueOf(54), list
.get(10));
379 assertEquals(Integer
.valueOf(64), list
.get(12));
383 public void testGetQuarks_end() {
384 List
<Integer
> list
= shs
.getQuarks(Attributes
.THREADS
, "1577", "*");
386 assertEquals(3, list
.size());
387 assertEquals(Integer
.valueOf(398), list
.get(1));
391 public void testDebugPrinting() throws FileNotFoundException
{
392 PrintWriter pw
= new PrintWriter(new File("/dev/null"));