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
.IOException
;
19 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.HistoryBuilder
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateChangeInput
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateHistoryBackend
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemBuilder
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.backend
.historytree
.HistoryTreeBackend
;
31 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
32 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.trace
.Attributes
;
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 IStateSystemBuilder ssb
;
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 ssb
= builder
.getStateSystemBuilder();
73 builder
.close(); /* Waits for the construction to finish */
77 public static void cleanup() {
79 ret1
= stateFile
.delete();
80 ret2
= stateFileBenchmark
.delete();
81 if ( !(ret1
&& ret2
) ) {
82 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
83 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
88 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
89 * us to @Test the @BeforeClass...
92 * @throws TmfTraceException
95 public void testBuild() throws IOException
, TmfTraceException
{
96 HistoryBuilder zebuilder
;
97 IStateChangeInput zeinput
;
98 IStateHistoryBackend zehp
= null;
100 zeinput
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
101 zehp
= new HistoryTreeBackend(stateFileBenchmark
, zeinput
.getStartTime());
102 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
106 assertEquals(CtfTestFiles
.startTime
, zehp
.getStartTime());
107 assertEquals(CtfTestFiles
.endTime
, zehp
.getEndTime());
111 public void testOpenExistingStateFile() throws IOException
{
112 IStateHistoryBackend hp2
= null;
113 IStateSystemBuilder ssb2
= null;
115 /* 'newStateFile' should have already been created */
116 hp2
= new HistoryTreeBackend(stateFile
);
117 ssb2
= HistoryBuilder
.openExistingHistory(hp2
);
120 assertEquals(CtfTestFiles
.startTime
, hp2
.getStartTime());
121 assertEquals(CtfTestFiles
.endTime
, hp2
.getEndTime());
125 public void testFullQuery1() throws StateValueTypeException
,
126 AttributeNotFoundException
, TimeRangeException
{
128 List
<ITmfStateInterval
> list
;
129 ITmfStateInterval interval
;
130 int quark
, quark2
, valueInt
;
133 list
= ssb
.loadStateAtTime(interestingTimestamp1
);
135 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
136 interval
= list
.get(quark
);
137 valueInt
= interval
.getStateValue().unboxInt();
138 assertEquals(1397, valueInt
);
140 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
141 interval
= list
.get(quark
);
142 valueStr
= interval
.getStateValue().unboxStr();
143 assertEquals("gdbus", valueStr
);
145 /* Query a stack attribute, has to be done in two passes */
146 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_MODE_STACK
);
147 interval
= list
.get(quark
);
148 valueInt
= interval
.getStateValue().unboxInt(); /* The stack depth */
149 quark2
= ssb
.getQuarkRelative(quark
, Integer
.toString(valueInt
));
150 interval
= list
.get(quark2
);
151 valueStr
= interval
.getStateValue().unboxStr();
152 assertTrue(valueStr
.equals("sys_poll"));
156 public void testFullQuery2() {
161 public void testFullQuery3() {
166 public void testSingleQuery1() throws AttributeNotFoundException
,
167 TimeRangeException
, StateValueTypeException
{
169 long timestamp
= interestingTimestamp1
;
171 ITmfStateInterval interval
;
174 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
175 interval
= ssb
.querySingleState(timestamp
, quark
);
176 valueStr
= interval
.getStateValue().unboxStr();
177 assertEquals("gdbus", valueStr
);
181 public void testSingleQuery2() {
186 public void testSingleQuery3() {
191 * Test a range query (with no resolution parameter, so all intervals)
194 public void testRangeQuery1() throws AttributeNotFoundException
,
195 TimeRangeException
, StateValueTypeException
{
197 long time1
= interestingTimestamp1
;
198 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
200 List
<ITmfStateInterval
> intervals
;
202 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
203 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
);
204 assertEquals(487, intervals
.size()); /* Number of context switches! */
205 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
206 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
210 * Range query, but with a t2 far off the end of the trace.
211 * The result should still be valid.
214 public void testRangeQuery2() throws TimeRangeException
,
215 AttributeNotFoundException
{
217 List
<ITmfStateInterval
> intervals
;
219 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.IRQ_STACK
);
220 long ts1
= ssb
.getStartTime(); /* start of the trace */
221 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
223 intervals
= ssb
.queryHistoryRange(quark
, ts1
, ts2
);
225 /* Nb of IRQs on CPU 0 during the whole trace */
226 assertEquals(1653, intervals
.size());
230 * Test a range query with a resolution
233 public void testRangeQuery3() throws AttributeNotFoundException
,
234 TimeRangeException
, StateValueTypeException
{
236 long time1
= interestingTimestamp1
;
237 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
238 long resolution
= 1000000; /* One query every millisecond */
240 List
<ITmfStateInterval
> intervals
;
242 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
243 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
, resolution
);
244 assertEquals(129, intervals
.size()); /* Number of context switches! */
245 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
246 assertEquals(1331668248784789238L, intervals
.get(100).getEndTime());
250 * Ask for a time range outside of the trace's range
252 * @throws TimeRangeException
254 @Test(expected
= TimeRangeException
.class)
255 public void testFullQueryInvalidTime1() throws TimeRangeException
{
256 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
257 ssb
.loadStateAtTime(ts
);
261 @Test(expected
= TimeRangeException
.class)
262 public void testFullQueryInvalidTime2() throws TimeRangeException
{
263 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
264 ssb
.loadStateAtTime(ts
);
268 @Test(expected
= TimeRangeException
.class)
269 public void testSingleQueryInvalidTime1()
270 throws AttributeNotFoundException
, TimeRangeException
{
272 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
273 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
274 ssb
.querySingleState(ts
, quark
);
277 @Test(expected
= TimeRangeException
.class)
278 public void testSingleQueryInvalidTime2()
279 throws AttributeNotFoundException
, TimeRangeException
{
281 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
282 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
283 ssb
.querySingleState(ts
, quark
);
286 @Test(expected
= TimeRangeException
.class)
287 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
290 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
291 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
292 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
294 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
297 @Test(expected
= TimeRangeException
.class)
298 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
299 AttributeNotFoundException
{
301 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
302 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
303 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
305 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
309 * Ask for a non-existing attribute
311 * @throws AttributeNotFoundException
313 @Test(expected
= AttributeNotFoundException
.class)
314 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
316 ssb
.getQuarkAbsolute("There", "is", "no", "cow", "level");
320 * Query but with the wrong State Value type
322 * @throws StateValueTypeException
323 * @throws AttributeNotFoundException
324 * @throws TimeRangeException
326 @Test(expected
= StateValueTypeException
.class)
327 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
328 AttributeNotFoundException
, TimeRangeException
{
329 List
<ITmfStateInterval
> list
;
330 ITmfStateInterval interval
;
333 list
= ssb
.loadStateAtTime(interestingTimestamp1
);
334 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
335 interval
= list
.get(quark
);
337 /* This is supposed to be an int value */
338 interval
.getStateValue().unboxStr();
341 @Test(expected
= StateValueTypeException
.class)
342 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
343 AttributeNotFoundException
, TimeRangeException
{
344 List
<ITmfStateInterval
> list
;
345 ITmfStateInterval interval
;
348 list
= ssb
.loadStateAtTime(interestingTimestamp1
);
349 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
350 interval
= list
.get(quark
);
352 /* This is supposed to be a String value */
353 interval
.getStateValue().unboxInt();
357 public void testFullAttributeName() throws AttributeNotFoundException
{
358 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
359 String name
= ssb
.getFullAttributePath(quark
);
360 assertEquals(name
, "CPUs/0/Current_thread");
364 public void testGetQuarks_begin() {
365 List
<Integer
> list
= ssb
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
367 assertEquals(1, list
.size());
368 assertEquals(Integer
.valueOf(398), list
.get(0));
372 public void testGetQuarks_middle() {
373 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
375 assertEquals(Integer
.valueOf(18), list
.get(4));
376 assertEquals(Integer
.valueOf(54), list
.get(10));
377 assertEquals(Integer
.valueOf(64), list
.get(12));
381 public void testGetQuarks_end() {
382 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "1577", "*");
384 assertEquals(3, list
.size());
385 assertEquals(Integer
.valueOf(398), list
.get(1));