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
;
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 // FIXME fails at the moment (attribute type is int, and = 3129??), I'll
136 // figure it out later
137 // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack");
138 // interval = shs.getState(quark);
139 // valueStr = interval.getStateValue().unboxStr();
140 // assertTrue( valueStr.equals("bloup") );
144 public void testFullQuery2() {
149 public void testFullQuery3() {
154 public void testSingleQuery1() throws AttributeNotFoundException
,
155 TimeRangeException
, StateValueTypeException
{
157 long timestamp
= interestingTimestamp1
;
159 ITmfStateInterval interval
;
162 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
163 interval
= shs
.querySingleState(timestamp
, quark
);
164 valueStr
= interval
.getStateValue().unboxStr();
165 assertEquals("gdbus", valueStr
);
169 public void testSingleQuery2() {
174 public void testSingleQuery3() {
179 public void testRangeQuery1() throws AttributeNotFoundException
,
180 TimeRangeException
, StateValueTypeException
{
182 long time1
= interestingTimestamp1
;
183 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
185 List
<ITmfStateInterval
> intervals
;
187 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
188 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
189 assertEquals(487, intervals
.size()); /* Number of context switches! */
190 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
191 assertEquals(18670480869135L, intervals
.get(205).getEndTime());
195 * Ask for a time range outside of the trace's range
197 * @throws TimeRangeException
199 @Test(expected
= TimeRangeException
.class)
200 public void testFullQueryInvalidTime1() throws TimeRangeException
{
201 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
202 shs
.loadStateAtTime(ts
);
206 @Test(expected
= TimeRangeException
.class)
207 public void testFullQueryInvalidTime2() throws TimeRangeException
{
208 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
209 shs
.loadStateAtTime(ts
);
213 @Test(expected
= TimeRangeException
.class)
214 public void testSingleQueryInvalidTime1()
215 throws AttributeNotFoundException
, TimeRangeException
{
217 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
218 long ts
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
219 shs
.querySingleState(ts
, quark
);
222 @Test(expected
= TimeRangeException
.class)
223 public void testSingleQueryInvalidTime2()
224 throws AttributeNotFoundException
, TimeRangeException
{
226 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
227 long ts
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
;
228 shs
.querySingleState(ts
, quark
);
231 @Test(expected
= TimeRangeException
.class)
232 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
235 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
236 long ts1
= CTFTestFiles
.startTime
- 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
237 long ts2
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
239 shs
.queryHistoryRange(quark
, ts1
, ts2
);
242 @Test(expected
= TimeRangeException
.class)
243 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
244 AttributeNotFoundException
{
246 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
247 long ts1
= CTFTestFiles
.startTime
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
248 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
250 shs
.queryHistoryRange(quark
, ts1
, ts2
);
253 @Test(expected
= TimeRangeException
.class)
254 public void testRangeQueryInvalidTime3() throws TimeRangeException
,
255 AttributeNotFoundException
{
257 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
258 long ts1
= CTFTestFiles
.startTime
- 1L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
259 long ts2
= CTFTestFiles
.startTime
+ 20L * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
261 shs
.queryHistoryRange(quark
, ts1
, ts2
);
265 * Ask for a non-existing attribute
267 * @throws AttributeNotFoundException
269 @Test(expected
= AttributeNotFoundException
.class)
270 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
272 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
276 * Query but with the wrong State Value type
278 * @throws StateValueTypeException
279 * @throws AttributeNotFoundException
280 * @throws TimeRangeException
282 @Test(expected
= StateValueTypeException
.class)
283 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
284 AttributeNotFoundException
, TimeRangeException
{
285 ITmfStateInterval interval
;
288 shs
.loadStateAtTime(interestingTimestamp1
);
289 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
290 interval
= shs
.queryState(quark
);
292 /* This is supposed to be an int value */
293 interval
.getStateValue().unboxStr();
296 @Test(expected
= StateValueTypeException
.class)
297 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
298 AttributeNotFoundException
, TimeRangeException
{
299 ITmfStateInterval interval
;
302 shs
.loadStateAtTime(interestingTimestamp1
);
303 quark
= shs
.getQuarkAbsolute("Threads", "1432", "Exec_name");
304 interval
= shs
.queryState(quark
);
306 /* This is supposed to be a String value */
307 interval
.getStateValue().unboxInt();
311 public void testFullAttributeName() throws AttributeNotFoundException
{
312 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
313 String name
= shs
.getFullAttributePath(quark
);
314 assertEquals(name
, "CPUs/0/Current_thread");
318 public void testGetQuarks_begin() {
319 List
<Integer
> list
= shs
.getQuarks("*", "1577", "Exec_name");
321 assertEquals(1, list
.size());
322 assertEquals(Integer
.valueOf(479), list
.get(0));
326 public void testGetQuarks_middle() {
327 List
<Integer
> list
= shs
.getQuarks("Threads", "*", "Exec_name");
329 assertEquals(Integer
.valueOf(36), list
.get(4));
330 assertEquals(Integer
.valueOf(100), list
.get(10));
331 assertEquals(Integer
.valueOf(116), list
.get(12));
335 public void testGetQuarks_end() {
336 List
<Integer
> list
= shs
.getQuarks("Threads", "1577", "*");
338 assertEquals(3, list
.size());
339 assertEquals(Integer
.valueOf(479), list
.get(1));
343 public void testDebugPrinting() throws FileNotFoundException
{
344 shs
.debugPrint(new PrintWriter(new File("/dev/null")));