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
;
36 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
37 * history and the non-threaded CTF kernel handler.
42 @SuppressWarnings("nls")
43 public class StateSystemFullHistoryTest
{
45 static File stateFile
;
46 static File stateFileBenchmark
;
48 static HistoryBuilder builder
;
49 static IStateChangeInput input
;
50 static IStateHistoryBackend hp
;
51 static StateHistorySystem shs
;
53 /* Offset in the trace + start time of the trace */
54 private final static long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
56 protected static String
getTestFileName() {
57 return "/tmp/statefile.ht"; //$NON-NLS-1$
61 public static void initialize() {
62 stateFile
= new File(getTestFileName());
63 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
65 input
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
66 hp
= new HistoryTreeBackend(stateFile
, input
.getStartTime());
67 builder
= new HistoryBuilder(input
, hp
);
68 } catch (Exception e
) {
72 shs
= (StateHistorySystem
) builder
.getSS();
76 public static void cleanup() {
78 ret1
= stateFile
.delete();
79 ret2
= stateFileBenchmark
.delete();
80 if ( !(ret1
&& ret2
) ) {
81 System
.err
.println("Error cleaning up during unit testing, " +
82 "you might have leftovers state history files in /tmp");
87 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
88 * us to @Test the @BeforeClass...
91 public void testBuild() {
92 HistoryBuilder zebuilder
;
93 IStateChangeInput zeinput
;
94 IStateHistoryBackend zehp
;
97 zeinput
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
98 zehp
= new HistoryTreeBackend(stateFileBenchmark
,
99 zeinput
.getStartTime());
100 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
102 } catch (Exception e
) {
108 public void testOpenExistingStateFile() {
109 IStateHistoryBackend hp2
= null;
110 StateHistorySystem shs2
= null;
112 /* 'newStateFile' should have already been created */
113 hp2
= new HistoryTreeBackend(stateFile
);
114 shs2
= new StateHistorySystem(hp2
, false);
115 } catch (IOException e
) {
118 assertTrue(shs2
!= null);
122 public void testFullQuery1() throws StateValueTypeException
,
123 AttributeNotFoundException
, TimeRangeException
{
125 List
<ITmfStateInterval
> list
;
126 ITmfStateInterval interval
;
127 int quark
, quark2
, valueInt
;
130 list
= shs
.loadStateAtTime(interestingTimestamp1
);
132 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
133 interval
= list
.get(quark
);
134 valueInt
= interval
.getStateValue().unboxInt();
135 assertEquals(1397, valueInt
);
137 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
138 interval
= list
.get(quark
);
139 valueStr
= interval
.getStateValue().unboxStr();
140 assertEquals("gdbus", valueStr
);
142 /* Query a stack attribute, has to be done in two passes */
143 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
144 interval
= list
.get(quark
);
145 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
146 quark2
= shs
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
147 interval
= list
.get(quark2
);
148 valueStr
= interval
.getStateValue().unboxStr();
149 assertTrue(valueStr
.equals("sys_poll"));
153 public void testFullQuery2() {
158 public void testFullQuery3() {
163 public void testSingleQuery1() throws AttributeNotFoundException
,
164 TimeRangeException
, StateValueTypeException
{
166 long timestamp
= interestingTimestamp1
;
168 ITmfStateInterval interval
;
171 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
172 interval
= shs
.querySingleState(timestamp
, quark
);
173 valueStr
= interval
.getStateValue().unboxStr();
174 assertEquals("gdbus", valueStr
);
178 public void testSingleQuery2() {
183 public void testSingleQuery3() {
188 * Test a range query (with no resolution parameter, so all intervals)
191 public void testRangeQuery1() throws AttributeNotFoundException
,
192 TimeRangeException
, StateValueTypeException
{
194 long time1
= interestingTimestamp1
;
195 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
197 List
<ITmfStateInterval
> intervals
;
199 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
200 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
201 assertEquals(487, intervals
.size()); /* Number of context switches! */
202 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
203 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
207 * Test a range query with a resolution
210 public void testRangeQuery2() throws AttributeNotFoundException
,
211 TimeRangeException
, StateValueTypeException
{
213 long time1
= interestingTimestamp1
;
214 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
215 long resolution
= 1000000; /* One query every millisecond */
217 List
<ITmfStateInterval
> intervals
;
219 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
220 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
, resolution
);
221 assertEquals(129, intervals
.size()); /* Number of context switches! */
222 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
223 assertEquals(1331668248784789238L, intervals
.get(100).getEndTime());
227 * Ask for a time range outside of the trace's range
229 * @throws TimeRangeException
231 @Test(expected
= TimeRangeException
.class)
232 public void testFullQueryInvalidTime1() throws TimeRangeException
{
233 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
234 shs
.loadStateAtTime(ts
);
238 @Test(expected
= TimeRangeException
.class)
239 public void testFullQueryInvalidTime2() throws TimeRangeException
{
240 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
241 shs
.loadStateAtTime(ts
);
245 @Test(expected
= TimeRangeException
.class)
246 public void testSingleQueryInvalidTime1()
247 throws AttributeNotFoundException
, TimeRangeException
{
249 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
250 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
251 shs
.querySingleState(ts
, quark
);
254 @Test(expected
= TimeRangeException
.class)
255 public void testSingleQueryInvalidTime2()
256 throws AttributeNotFoundException
, TimeRangeException
{
258 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
259 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
260 shs
.querySingleState(ts
, quark
);
263 @Test(expected
= TimeRangeException
.class)
264 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
267 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
268 long ts1
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
269 long ts2
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
271 shs
.queryHistoryRange(quark
, ts1
, ts2
);
274 @Test(expected
= TimeRangeException
.class)
275 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
276 AttributeNotFoundException
{
278 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
279 long ts1
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
280 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
282 shs
.queryHistoryRange(quark
, ts1
, ts2
);
285 @Test(expected
= TimeRangeException
.class)
286 public void testRangeQueryInvalidTime3() throws TimeRangeException
,
287 AttributeNotFoundException
{
289 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
290 long ts1
= CTFTestFiles
.startTime
- 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
291 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
293 shs
.queryHistoryRange(quark
, ts1
, ts2
);
297 * Ask for a non-existing attribute
299 * @throws AttributeNotFoundException
301 @Test(expected
= AttributeNotFoundException
.class)
302 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
304 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
308 * Query but with the wrong State Value type
310 * @throws StateValueTypeException
311 * @throws AttributeNotFoundException
312 * @throws TimeRangeException
314 @Test(expected
= StateValueTypeException
.class)
315 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
316 AttributeNotFoundException
, TimeRangeException
{
317 List
<ITmfStateInterval
> list
;
318 ITmfStateInterval interval
;
321 list
= shs
.loadStateAtTime(interestingTimestamp1
);
322 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
323 interval
= list
.get(quark
);
325 /* This is supposed to be an int value */
326 interval
.getStateValue().unboxStr();
329 @Test(expected
= StateValueTypeException
.class)
330 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
331 AttributeNotFoundException
, TimeRangeException
{
332 List
<ITmfStateInterval
> list
;
333 ITmfStateInterval interval
;
336 list
= shs
.loadStateAtTime(interestingTimestamp1
);
337 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
338 interval
= list
.get(quark
);
340 /* This is supposed to be a String value */
341 interval
.getStateValue().unboxInt();
345 public void testFullAttributeName() throws AttributeNotFoundException
{
346 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
347 String name
= shs
.getFullAttributePath(quark
);
348 assertEquals(name
, "CPUs/0/Current_thread");
352 public void testGetQuarks_begin() {
353 List
<Integer
> list
= shs
.getQuarks("*", "1577", "Exec_name");
355 assertEquals(1, list
.size());
356 assertEquals(Integer
.valueOf(479), list
.get(0));
360 public void testGetQuarks_middle() {
361 List
<Integer
> list
= shs
.getQuarks("Threads", "*", "Exec_name");
363 assertEquals(Integer
.valueOf(36), list
.get(4));
364 assertEquals(Integer
.valueOf(100), list
.get(10));
365 assertEquals(Integer
.valueOf(116), list
.get(12));
369 public void testGetQuarks_end() {
370 List
<Integer
> list
= shs
.getQuarks("Threads", "1577", "*");
372 assertEquals(3, list
.size());
373 assertEquals(Integer
.valueOf(479), list
.get(1));
377 public void testDebugPrinting() throws FileNotFoundException
{
378 shs
.debugPrint(new PrintWriter(new File("/dev/null")));