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
.interval
.ITmfStateInterval
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.AttributeNotFoundException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.StateHistorySystem
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.TimeRangeException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.backend
.historytree
.HistoryTreeBackend
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.HistoryBuilder
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.IStateChangeInput
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.helpers
.IStateHistoryBackend
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.StateValueTypeException
;
32 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
33 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.trace
.Attributes
;
37 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
38 * history and the non-threaded CTF kernel handler.
43 @SuppressWarnings("nls")
44 public class StateSystemFullHistoryTest
{
46 static File stateFile
;
47 static File stateFileBenchmark
;
49 static HistoryBuilder builder
;
50 static IStateChangeInput input
;
51 static IStateHistoryBackend hp
;
52 static StateHistorySystem shs
;
54 /* Offset in the trace + start time of the trace */
55 private final static long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
57 protected static String
getTestFileName() {
58 return "/tmp/statefile.ht"; //$NON-NLS-1$
62 public static void initialize() {
63 stateFile
= new File(getTestFileName());
64 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
66 input
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
67 hp
= new HistoryTreeBackend(stateFile
, input
.getStartTime());
68 builder
= new HistoryBuilder(input
, hp
);
69 } catch (Exception e
) {
73 shs
= (StateHistorySystem
) builder
.getSS();
74 builder
.close(); /* Waits for the construction to finish */
78 public static void cleanup() {
80 ret1
= stateFile
.delete();
81 ret2
= stateFileBenchmark
.delete();
82 if ( !(ret1
&& ret2
) ) {
83 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
84 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
89 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
90 * us to @Test the @BeforeClass...
93 public void testBuild() {
94 HistoryBuilder zebuilder
;
95 IStateChangeInput zeinput
;
96 IStateHistoryBackend zehp
;
99 zeinput
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
100 zehp
= new HistoryTreeBackend(stateFileBenchmark
,
101 zeinput
.getStartTime());
102 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
104 } catch (Exception e
) {
110 public void testOpenExistingStateFile() {
111 IStateHistoryBackend hp2
= null;
112 StateHistorySystem shs2
= null;
114 /* 'newStateFile' should have already been created */
115 hp2
= new HistoryTreeBackend(stateFile
);
116 shs2
= new StateHistorySystem(hp2
, false);
117 } catch (IOException e
) {
120 assertTrue(shs2
!= null);
124 public void testFullQuery1() throws StateValueTypeException
,
125 AttributeNotFoundException
, TimeRangeException
{
127 List
<ITmfStateInterval
> list
;
128 ITmfStateInterval interval
;
129 int quark
, quark2
, valueInt
;
132 list
= shs
.loadStateAtTime(interestingTimestamp1
);
134 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
135 interval
= list
.get(quark
);
136 valueInt
= interval
.getStateValue().unboxInt();
137 assertEquals(1397, valueInt
);
139 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
140 interval
= list
.get(quark
);
141 valueStr
= interval
.getStateValue().unboxStr();
142 assertEquals("gdbus", valueStr
);
144 /* Query a stack attribute, has to be done in two passes */
145 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_MODE_STACK
);
146 interval
= list
.get(quark
);
147 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
148 quark2
= shs
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
149 interval
= list
.get(quark2
);
150 valueStr
= interval
.getStateValue().unboxStr();
151 assertTrue(valueStr
.equals("sys_poll"));
155 public void testFullQuery2() {
160 public void testFullQuery3() {
165 public void testSingleQuery1() throws AttributeNotFoundException
,
166 TimeRangeException
, StateValueTypeException
{
168 long timestamp
= interestingTimestamp1
;
170 ITmfStateInterval interval
;
173 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
174 interval
= shs
.querySingleState(timestamp
, quark
);
175 valueStr
= interval
.getStateValue().unboxStr();
176 assertEquals("gdbus", valueStr
);
180 public void testSingleQuery2() {
185 public void testSingleQuery3() {
190 * Test a range query (with no resolution parameter, so all intervals)
193 public void testRangeQuery1() throws AttributeNotFoundException
,
194 TimeRangeException
, StateValueTypeException
{
196 long time1
= interestingTimestamp1
;
197 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
199 List
<ITmfStateInterval
> intervals
;
201 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
202 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
203 assertEquals(487, intervals
.size()); /* Number of context switches! */
204 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
205 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
209 * Range query, but with a t2 far off the end of the trace.
210 * The result should still be valid.
213 public void testRangeQuery2() throws TimeRangeException
,
214 AttributeNotFoundException
{
216 List
<ITmfStateInterval
> intervals
;
218 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.IRQ_STACK
);
219 long ts1
= shs
.getHistoryBackend().getStartTime(); /* start of the trace */
220 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
222 intervals
= shs
.queryHistoryRange(quark
, ts1
, ts2
);
224 /* Nb of IRQs on CPU 0 during the whole trace */
225 assertEquals(1653, intervals
.size());
229 * Test a range query with a resolution
232 public void testRangeQuery3() throws AttributeNotFoundException
,
233 TimeRangeException
, StateValueTypeException
{
235 long time1
= interestingTimestamp1
;
236 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
237 long resolution
= 1000000; /* One query every millisecond */
239 List
<ITmfStateInterval
> intervals
;
241 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
242 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
, resolution
);
243 assertEquals(129, intervals
.size()); /* Number of context switches! */
244 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
245 assertEquals(1331668248784789238L, intervals
.get(100).getEndTime());
249 * Ask for a time range outside of the trace's range
251 * @throws TimeRangeException
253 @Test(expected
= TimeRangeException
.class)
254 public void testFullQueryInvalidTime1() throws TimeRangeException
{
255 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
256 shs
.loadStateAtTime(ts
);
260 @Test(expected
= TimeRangeException
.class)
261 public void testFullQueryInvalidTime2() throws TimeRangeException
{
262 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
263 shs
.loadStateAtTime(ts
);
267 @Test(expected
= TimeRangeException
.class)
268 public void testSingleQueryInvalidTime1()
269 throws AttributeNotFoundException
, TimeRangeException
{
271 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
272 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
273 shs
.querySingleState(ts
, quark
);
276 @Test(expected
= TimeRangeException
.class)
277 public void testSingleQueryInvalidTime2()
278 throws AttributeNotFoundException
, TimeRangeException
{
280 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
281 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
282 shs
.querySingleState(ts
, quark
);
285 @Test(expected
= TimeRangeException
.class)
286 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
289 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
290 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
291 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
293 shs
.queryHistoryRange(quark
, ts1
, ts2
);
296 @Test(expected
= TimeRangeException
.class)
297 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
298 AttributeNotFoundException
{
300 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
301 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
302 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
304 shs
.queryHistoryRange(quark
, ts1
, ts2
);
308 * Ask for a non-existing attribute
310 * @throws AttributeNotFoundException
312 @Test(expected
= AttributeNotFoundException
.class)
313 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
315 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
319 * Query but with the wrong State Value type
321 * @throws StateValueTypeException
322 * @throws AttributeNotFoundException
323 * @throws TimeRangeException
325 @Test(expected
= StateValueTypeException
.class)
326 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
327 AttributeNotFoundException
, TimeRangeException
{
328 List
<ITmfStateInterval
> list
;
329 ITmfStateInterval interval
;
332 list
= shs
.loadStateAtTime(interestingTimestamp1
);
333 quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
334 interval
= list
.get(quark
);
336 /* This is supposed to be an int value */
337 interval
.getStateValue().unboxStr();
340 @Test(expected
= StateValueTypeException
.class)
341 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
342 AttributeNotFoundException
, TimeRangeException
{
343 List
<ITmfStateInterval
> list
;
344 ITmfStateInterval interval
;
347 list
= shs
.loadStateAtTime(interestingTimestamp1
);
348 quark
= shs
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
349 interval
= list
.get(quark
);
351 /* This is supposed to be a String value */
352 interval
.getStateValue().unboxInt();
356 public void testFullAttributeName() throws AttributeNotFoundException
{
357 int quark
= shs
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
358 String name
= shs
.getFullAttributePath(quark
);
359 assertEquals(name
, "CPUs/0/Current_thread");
363 public void testGetQuarks_begin() {
364 List
<Integer
> list
= shs
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
366 assertEquals(1, list
.size());
367 assertEquals(Integer
.valueOf(398), list
.get(0));
371 public void testGetQuarks_middle() {
372 List
<Integer
> list
= shs
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
374 assertEquals(Integer
.valueOf(18), list
.get(4));
375 assertEquals(Integer
.valueOf(54), list
.get(10));
376 assertEquals(Integer
.valueOf(64), list
.get(12));
380 public void testGetQuarks_end() {
381 List
<Integer
> list
= shs
.getQuarks(Attributes
.THREADS
, "1577", "*");
383 assertEquals(3, list
.size());
384 assertEquals(Integer
.valueOf(398), list
.get(1));
388 public void testDebugPrinting() throws FileNotFoundException
{
389 PrintWriter pw
= new PrintWriter(new File("/dev/null"));