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 protected static String
getTestFileName() {
54 return "/tmp/statefile.ht"; //$NON-NLS-1$
58 public static void initialize() {
59 stateFile
= new File(getTestFileName());
60 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
62 input
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
63 hp
= new HistoryTreeBackend(stateFile
, input
.getStartTime());
64 builder
= new HistoryBuilder(input
, hp
);
65 } catch (Exception e
) {
69 shs
= (StateHistorySystem
) builder
.getSS();
73 public static void cleanup() {
75 stateFileBenchmark
.delete();
79 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
80 * us to @Test the @BeforeClass...
83 public void testBuild() {
84 HistoryBuilder zebuilder
;
85 IStateChangeInput zeinput
;
86 IStateHistoryBackend zehp
;
89 zeinput
= new CTFKernelStateInput(CTFTestFiles
.getTestTrace());
90 zehp
= new HistoryTreeBackend(stateFileBenchmark
,
91 zeinput
.getStartTime());
92 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
94 } catch (Exception e
) {
100 public void testOpenExistingStateFile() {
101 IStateHistoryBackend hp2
= null;
102 StateHistorySystem shs2
= null;
104 /* 'newStateFile' should have already been created */
105 hp2
= new HistoryTreeBackend(stateFile
);
106 shs2
= new StateHistorySystem(hp2
, false);
107 } catch (IOException e
) {
110 assertTrue(shs2
!= null);
114 public void testFullQuery1() throws StateValueTypeException
,
115 AttributeNotFoundException
, TimeRangeException
{
117 ITmfStateInterval interval
;
121 shs
.loadStateAtTime(17622841472359L);
123 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
124 interval
= shs
.queryState(quark
);
125 valueInt
= interval
.getStateValue().unboxInt();
126 assertTrue(valueInt
== 1929);
128 quark
= shs
.getQuarkAbsolute("Threads", "1197", "Exec_name");
129 interval
= shs
.queryState(quark
);
130 valueStr
= interval
.getStateValue().unboxStr();
131 assertTrue(valueStr
.equals("apache2"));
133 // FIXME fails at the moment (attribute type is int, and = 3129??), I'll
134 // figure it out later
135 // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack");
136 // interval = shs.getState(quark);
137 // valueStr = interval.getStateValue().unboxStr();
138 // assertTrue( valueStr.equals("bloup") );
142 public void testFullQuery2() {
147 public void testFullQuery3() {
152 public void testSingleQuery1() throws AttributeNotFoundException
,
153 TimeRangeException
, StateValueTypeException
{
155 long timestamp
= 17622841472359L;
157 ITmfStateInterval interval
;
160 quark
= shs
.getQuarkAbsolute("Threads", "1197", "Exec_name");
161 interval
= shs
.querySingleState(timestamp
, quark
);
162 valueStr
= interval
.getStateValue().unboxStr();
163 assertTrue(valueStr
.equals("apache2"));
167 public void testSingleQuery2() {
172 public void testSingleQuery3() {
177 public void testRangeQuery1() throws AttributeNotFoundException
,
178 TimeRangeException
, StateValueTypeException
{
180 long time1
= 17622841472359L;
181 long time2
= time1
+ 1L * CTFTestFiles
.NANOSECS_PER_SEC
;
183 List
<ITmfStateInterval
> intervals
;
185 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
186 intervals
= shs
.queryHistoryRange(quark
, time1
, time2
);
187 assertTrue(intervals
.size() == 1018); /* Number of context switches! */
188 assertTrue(intervals
.get(100).getStateValue().unboxInt() == 2974);
189 assertTrue(intervals
.get(205).getEndTime() == 17622977386059L);
193 * Ask for a time range outside of the trace's range
195 * @throws TimeRangeException
197 @Test(expected
= TimeRangeException
.class)
198 public void testFullQueryInvalidTime1() throws TimeRangeException
{
199 shs
.loadStateAtTime(CTFTestFiles
.startTime1
+ 20L
200 * CTFTestFiles
.NANOSECS_PER_SEC
);
204 @Test(expected
= TimeRangeException
.class)
205 public void testFullQueryInvalidTime2() throws TimeRangeException
{
206 shs
.loadStateAtTime(CTFTestFiles
.startTime1
- 20L
207 * CTFTestFiles
.NANOSECS_PER_SEC
);
211 @Test(expected
= TimeRangeException
.class)
212 public void testSingleQueryInvalidTime1()
213 throws AttributeNotFoundException
, TimeRangeException
{
215 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
216 long time
= CTFTestFiles
.startTime1
+ 20L
217 * CTFTestFiles
.NANOSECS_PER_SEC
;
218 shs
.querySingleState(time
, quark
);
221 @Test(expected
= TimeRangeException
.class)
222 public void testSingleQueryInvalidTime2()
223 throws AttributeNotFoundException
, TimeRangeException
{
225 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
226 long time
= CTFTestFiles
.startTime1
- 20L
227 * CTFTestFiles
.NANOSECS_PER_SEC
;
228 shs
.querySingleState(time
, quark
);
231 @Test(expected
= TimeRangeException
.class)
232 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
235 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
236 long time1
= CTFTestFiles
.startTime1
- 20L
237 * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
238 long time2
= CTFTestFiles
.startTime1
+ 1L
239 * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
241 shs
.queryHistoryRange(quark
, time1
, time2
);
244 @Test(expected
= TimeRangeException
.class)
245 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
246 AttributeNotFoundException
{
248 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
249 long time1
= CTFTestFiles
.startTime1
+ 1L
250 * CTFTestFiles
.NANOSECS_PER_SEC
; /* valid */
251 long time2
= CTFTestFiles
.startTime1
+ 20L
252 * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
254 shs
.queryHistoryRange(quark
, time1
, time2
);
257 @Test(expected
= TimeRangeException
.class)
258 public void testRangeQueryInvalidTime3() throws TimeRangeException
,
259 AttributeNotFoundException
{
261 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
262 long time1
= CTFTestFiles
.startTime1
- 1L
263 * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
264 long time2
= CTFTestFiles
.startTime1
+ 20L
265 * CTFTestFiles
.NANOSECS_PER_SEC
; /* invalid */
267 shs
.queryHistoryRange(quark
, time1
, time2
);
271 * Ask for a non-existing attribute
273 * @throws AttributeNotFoundException
275 @Test(expected
= AttributeNotFoundException
.class)
276 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
278 shs
.getQuarkAbsolute("There", "is", "no", "cow", "level");
282 * Query but with the wrong State Value type
284 * @throws StateValueTypeException
285 * @throws AttributeNotFoundException
286 * @throws TimeRangeException
288 @Test(expected
= StateValueTypeException
.class)
289 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
290 AttributeNotFoundException
, TimeRangeException
{
292 ITmfStateInterval interval
;
295 shs
.loadStateAtTime(17622841472359L);
297 quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
298 interval
= shs
.queryState(quark
);
299 interval
.getStateValue().unboxStr(); /*
300 * This is supposed to be a int
305 @Test(expected
= StateValueTypeException
.class)
306 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
307 AttributeNotFoundException
, TimeRangeException
{
309 ITmfStateInterval interval
;
312 shs
.loadStateAtTime(17622841472359L);
314 quark
= shs
.getQuarkAbsolute("Threads", "1197", "Exec_name");
315 interval
= shs
.queryState(quark
);
316 interval
.getStateValue().unboxInt(); /*
317 * This is supposed to be a String
323 public void testFullAttributeName() throws AttributeNotFoundException
{
324 int quark
= shs
.getQuarkAbsolute("CPUs", "0", "Current_thread");
325 String name
= shs
.getFullAttributePath(quark
);
326 assertTrue(name
.equals("CPUs/0/Current_thread"));
330 public void testDebugPrinting() throws FileNotFoundException
{
331 shs
.debugPrint(new PrintWriter(new File("/dev/null")));