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 private final static long interestingTimestamp1
= 18670067372290L;
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 hp
= new HistoryTreeBackend(stateFile
, input
.getStartTime());
66 builder
= new HistoryBuilder(input
, hp
);
67 } catch (Exception e
) {
71 shs
= (StateHistorySystem
) builder
.getSS();
75 public static void cleanup() {
77 ret1
= stateFile
.delete();
78 ret2
= stateFileBenchmark
.delete();
79 if ( !(ret1
&& ret2
) ) {
80 System
.err
.println("Error cleaning up during unit testing, " +
81 "you might have leftovers state history files in /tmp");
86 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
87 * us to @Test the @BeforeClass...
90 public void testBuild() {
91 HistoryBuilder zebuilder
;
92 IStateChangeInput zeinput
;
93 IStateHistoryBackend zehp
;
96 zeinput
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
97 zehp
= new HistoryTreeBackend(stateFileBenchmark
,
98 zeinput
.getStartTime());
99 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
101 } catch (Exception e
) {
107 public void testOpenExistingStateFile() {
108 IStateHistoryBackend hp2
= null;
109 StateHistorySystem shs2
= null;
111 /* 'newStateFile' should have already been created */
112 hp2
= new HistoryTreeBackend(stateFile
);
113 shs2
= new StateHistorySystem(hp2
, false);
114 } catch (IOException e
) {
117 assertTrue(shs2
!= null);
121 public void testFullQuery1() throws StateValueTypeException
,
122 AttributeNotFoundException
, TimeRangeException
{
124 List
<ITmfStateInterval
> list
;
125 ITmfStateInterval interval
;
126 int quark
, quark2
, valueInt
;
129 list
= shs
.loadStateAtTime(interestingTimestamp1
);
131 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
132 interval
= list
.get(quark
);
133 valueInt
= interval
.getStateValue().unboxInt();
134 assertEquals(1397, valueInt
);
136 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
137 interval
= list
.get(quark
);
138 valueStr
= interval
.getStateValue().unboxStr();
139 assertEquals("gdbus", valueStr
);
141 /* Query a stack attribute, has to be done in two passes */
142 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
143 interval
= shs
.queryState(quark
);
144 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
145 quark2
= shs
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
146 interval
= shs
.queryState(quark2
);
147 valueStr
= interval
.getStateValue().unboxStr();
148 assertTrue(valueStr
.equals("sys_poll"));
152 public void testFullQuery2() {
157 public void testFullQuery3() {
162 public void testSingleQuery1() throws AttributeNotFoundException
,
163 TimeRangeException
, StateValueTypeException
{
165 long timestamp
= interestingTimestamp1
;
167 ITmfStateInterval interval
;
170 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
171 interval
= shs
.querySingleState(timestamp
, quark
);
172 valueStr
= interval
.getStateValue().unboxStr();
173 assertEquals("gdbus", valueStr
);
177 public void testSingleQuery2() {
182 public void testSingleQuery3() {
187 public void testRangeQuery1() throws AttributeNotFoundException
,
188 TimeRangeException
, StateValueTypeException
{
190 long time1
= interestingTimestamp1
;
191 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
193 List
<ITmfStateInterval
> intervals
;
195 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
196 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
197 assertEquals(487, intervals
.size()); /* Number of context switches! */
198 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
199 assertEquals(18670480869135L, intervals
.get(205).getEndTime());
203 * Ask for a time range outside of the trace's range
205 * @throws TimeRangeException
207 @Test(expected
= TimeRangeException
.class)
208 public void testFullQueryInvalidTime1() throws TimeRangeException
{
209 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
210 shs
.loadStateAtTime(ts
);
214 @Test(expected
= TimeRangeException
.class)
215 public void testFullQueryInvalidTime2() throws TimeRangeException
{
216 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
217 shs
.loadStateAtTime(ts
);
221 @Test(expected
= TimeRangeException
.class)
222 public void testSingleQueryInvalidTime1()
223 throws AttributeNotFoundException
, TimeRangeException
{
225 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
226 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
227 shs
.querySingleState(ts
, quark
);
230 @Test(expected
= TimeRangeException
.class)
231 public void testSingleQueryInvalidTime2()
232 throws AttributeNotFoundException
, TimeRangeException
{
234 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
235 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
236 shs
.querySingleState(ts
, quark
);
239 @Test(expected
= TimeRangeException
.class)
240 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
243 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
244 long ts1
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
245 long ts2
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
247 shs
.queryHistoryRange(quark
, ts1
, ts2
);
250 @Test(expected
= TimeRangeException
.class)
251 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
252 AttributeNotFoundException
{
254 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
255 long ts1
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
256 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
258 shs
.queryHistoryRange(quark
, ts1
, ts2
);
261 @Test(expected
= TimeRangeException
.class)
262 public void testRangeQueryInvalidTime3() throws TimeRangeException
,
263 AttributeNotFoundException
{
265 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
266 long ts1
= CTFTestFiles
.startTime
- 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
267 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
269 shs
.queryHistoryRange(quark
, ts1
, ts2
);
273 * Ask for a non-existing attribute
275 * @throws AttributeNotFoundException
277 @Test(expected
= AttributeNotFoundException
.class)
278 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
280 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
284 * Query but with the wrong State Value type
286 * @throws StateValueTypeException
287 * @throws AttributeNotFoundException
288 * @throws TimeRangeException
290 @Test(expected
= StateValueTypeException
.class)
291 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
292 AttributeNotFoundException
, TimeRangeException
{
293 List
<ITmfStateInterval
> list
;
294 ITmfStateInterval interval
;
297 list
= shs
.loadStateAtTime(interestingTimestamp1
);
298 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
299 interval
= list
.get(quark
);
301 /* This is supposed to be an int value */
302 interval
.getStateValue().unboxStr();
305 @Test(expected
= StateValueTypeException
.class)
306 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
307 AttributeNotFoundException
, TimeRangeException
{
308 List
<ITmfStateInterval
> list
;
309 ITmfStateInterval interval
;
312 list
= shs
.loadStateAtTime(interestingTimestamp1
);
313 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
314 interval
= list
.get(quark
);
316 /* This is supposed to be a String value */
317 interval
.getStateValue().unboxInt();
321 public void testFullAttributeName() throws AttributeNotFoundException
{
322 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
323 String name
= shs
.getFullAttributePath(quark
);
324 assertEquals(name
, "CPUs/0/Current_thread");
328 public void testGetQuarks_begin() {
329 List
<Integer
> list
= shs
.getQuarks("*", "1577", "Exec_name");
331 assertEquals(1, list
.size());
332 assertEquals(Integer
.valueOf(479), list
.get(0));
336 public void testGetQuarks_middle() {
337 List
<Integer
> list
= shs
.getQuarks("Threads", "*", "Exec_name");
339 assertEquals(Integer
.valueOf(36), list
.get(4));
340 assertEquals(Integer
.valueOf(100), list
.get(10));
341 assertEquals(Integer
.valueOf(116), list
.get(12));
345 public void testGetQuarks_end() {
346 List
<Integer
> list
= shs
.getQuarks("Threads", "1577", "*");
348 assertEquals(3, list
.size());
349 assertEquals(Integer
.valueOf(479), list
.get(1));
353 public void testDebugPrinting() throws FileNotFoundException
{
354 shs
.debugPrint(new PrintWriter(new File("/dev/null")));