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 protected static File stateFile
;
46 protected static File stateFileBenchmark
;
48 protected static HistoryBuilder builder
;
49 protected static IStateChangeInput input
;
50 protected static IStateHistoryBackend hp
;
51 protected 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 stateFileBenchmark
.delete();
81 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
82 * us to @Test the @BeforeClass...
85 public void testBuild() {
86 HistoryBuilder zebuilder
;
87 IStateChangeInput zeinput
;
88 IStateHistoryBackend zehp
;
91 zeinput
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
92 zehp
= new HistoryTreeBackend(stateFileBenchmark
,
93 zeinput
.getStartTime());
94 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
96 } catch (Exception e
) {
102 public void testOpenExistingStateFile() {
103 IStateHistoryBackend hp2
= null;
104 StateHistorySystem shs2
= null;
106 /* 'newStateFile' should have already been created */
107 hp2
= new HistoryTreeBackend(stateFile
);
108 shs2
= new StateHistorySystem(hp2
, false);
109 } catch (IOException e
) {
112 assertTrue(shs2
!= null);
116 public void testFullQuery1() throws StateValueTypeException
,
117 AttributeNotFoundException
, TimeRangeException
{
119 ITmfStateInterval interval
;
120 int quark
, quark2
, valueInt
;
123 shs
.loadStateAtTime(interestingTimestamp1
);
125 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
126 interval
= shs
.queryState(quark
);
127 valueInt
= interval
.getStateValue().unboxInt();
128 assertEquals(1397, valueInt
);
130 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
131 interval
= shs
.queryState(quark
);
132 valueStr
= interval
.getStateValue().unboxStr();
133 assertEquals("gdbus", valueStr
);
135 /* Query a stack attribute, has to be done in two passes */
136 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
137 interval
= shs
.queryState(quark
);
138 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
139 quark2
= shs
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
140 interval
= shs
.queryState(quark2
);
141 valueStr
= interval
.getStateValue().unboxStr();
142 assertTrue(valueStr
.equals("sys_poll"));
146 public void testFullQuery2() {
151 public void testFullQuery3() {
156 public void testSingleQuery1() throws AttributeNotFoundException
,
157 TimeRangeException
, StateValueTypeException
{
159 long timestamp
= interestingTimestamp1
;
161 ITmfStateInterval interval
;
164 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
165 interval
= shs
.querySingleState(timestamp
, quark
);
166 valueStr
= interval
.getStateValue().unboxStr();
167 assertEquals("gdbus", valueStr
);
171 public void testSingleQuery2() {
176 public void testSingleQuery3() {
181 public void testRangeQuery1() throws AttributeNotFoundException
,
182 TimeRangeException
, StateValueTypeException
{
184 long time1
= interestingTimestamp1
;
185 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
187 List
<ITmfStateInterval
> intervals
;
189 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
190 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
191 assertEquals(487, intervals
.size()); /* Number of context switches! */
192 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
193 assertEquals(18670480869135L, intervals
.get(205).getEndTime());
197 * Ask for a time range outside of the trace's range
199 * @throws TimeRangeException
201 @Test(expected
= TimeRangeException
.class)
202 public void testFullQueryInvalidTime1() throws TimeRangeException
{
203 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
204 shs
.loadStateAtTime(ts
);
208 @Test(expected
= TimeRangeException
.class)
209 public void testFullQueryInvalidTime2() throws TimeRangeException
{
210 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
211 shs
.loadStateAtTime(ts
);
215 @Test(expected
= TimeRangeException
.class)
216 public void testSingleQueryInvalidTime1()
217 throws AttributeNotFoundException
, TimeRangeException
{
219 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
220 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
221 shs
.querySingleState(ts
, quark
);
224 @Test(expected
= TimeRangeException
.class)
225 public void testSingleQueryInvalidTime2()
226 throws AttributeNotFoundException
, TimeRangeException
{
228 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
229 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
230 shs
.querySingleState(ts
, quark
);
233 @Test(expected
= TimeRangeException
.class)
234 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
237 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
238 long ts1
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
239 long ts2
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
241 shs
.queryHistoryRange(quark
, ts1
, ts2
);
244 @Test(expected
= TimeRangeException
.class)
245 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
246 AttributeNotFoundException
{
248 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
249 long ts1
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
250 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
252 shs
.queryHistoryRange(quark
, ts1
, ts2
);
255 @Test(expected
= TimeRangeException
.class)
256 public void testRangeQueryInvalidTime3() throws TimeRangeException
,
257 AttributeNotFoundException
{
259 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
260 long ts1
= CTFTestFiles
.startTime
- 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
261 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
263 shs
.queryHistoryRange(quark
, ts1
, ts2
);
267 * Ask for a non-existing attribute
269 * @throws AttributeNotFoundException
271 @Test(expected
= AttributeNotFoundException
.class)
272 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
274 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
278 * Query but with the wrong State Value type
280 * @throws StateValueTypeException
281 * @throws AttributeNotFoundException
282 * @throws TimeRangeException
284 @Test(expected
= StateValueTypeException
.class)
285 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
286 AttributeNotFoundException
, TimeRangeException
{
287 ITmfStateInterval interval
;
290 shs
.loadStateAtTime(interestingTimestamp1
);
291 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
292 interval
= shs
.queryState(quark
);
294 /* This is supposed to be an int value */
295 interval
.getStateValue().unboxStr();
298 @Test(expected
= StateValueTypeException
.class)
299 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
300 AttributeNotFoundException
, TimeRangeException
{
301 ITmfStateInterval interval
;
304 shs
.loadStateAtTime(interestingTimestamp1
);
305 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
306 interval
= shs
.queryState(quark
);
308 /* This is supposed to be a String value */
309 interval
.getStateValue().unboxInt();
313 public void testFullAttributeName() throws AttributeNotFoundException
{
314 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
315 String name
= shs
.getFullAttributePath(quark
);
316 assertEquals(name
, "CPUs/0/Current_thread");
320 public void testGetQuarks_begin() {
321 List
<Integer
> list
= shs
.getQuarks("*", "1577", "Exec_name");
323 assertEquals(1, list
.size());
324 assertEquals(Integer
.valueOf(479), list
.get(0));
328 public void testGetQuarks_middle() {
329 List
<Integer
> list
= shs
.getQuarks("Threads", "*", "Exec_name");
331 assertEquals(Integer
.valueOf(36), list
.get(4));
332 assertEquals(Integer
.valueOf(100), list
.get(10));
333 assertEquals(Integer
.valueOf(116), list
.get(12));
337 public void testGetQuarks_end() {
338 List
<Integer
> list
= shs
.getQuarks("Threads", "1577", "*");
340 assertEquals(3, list
.size());
341 assertEquals(Integer
.valueOf(479), list
.get(1));
345 public void testDebugPrinting() throws FileNotFoundException
{
346 shs
.debugPrint(new PrintWriter(new File("/dev/null")));