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
.Attributes
;
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 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
) {
71 builder
.startBuilding(null);
72 ssb
= builder
.getStateSystemBuilder();
76 public static void cleanup() {
78 ret1
= stateFile
.delete();
79 ret2
= stateFileBenchmark
.delete();
80 if ( !(ret1
&& ret2
) ) {
81 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
82 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
87 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
88 * us to @Test the @BeforeClass...
91 * @throws TmfTraceException
94 public void testBuild() throws IOException
, TmfTraceException
{
95 HistoryBuilder zebuilder
;
96 IStateChangeInput zeinput
;
97 IStateHistoryBackend zehp
= null;
99 zeinput
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
100 zehp
= new HistoryTreeBackend(stateFileBenchmark
, zeinput
.getStartTime());
101 zebuilder
= new HistoryBuilder(zeinput
, zehp
);
102 zebuilder
.startBuilding(null);
104 assertEquals(CtfTestFiles
.startTime
, zehp
.getStartTime());
105 assertEquals(CtfTestFiles
.endTime
, zehp
.getEndTime());
109 public void testOpenExistingStateFile() throws IOException
{
110 IStateHistoryBackend hp2
= null;
111 IStateSystemBuilder ssb2
= null;
113 /* 'newStateFile' should have already been created */
114 hp2
= new HistoryTreeBackend(stateFile
);
115 ssb2
= HistoryBuilder
.openExistingHistory(hp2
);
118 assertEquals(CtfTestFiles
.startTime
, hp2
.getStartTime());
119 assertEquals(CtfTestFiles
.endTime
, hp2
.getEndTime());
123 public void testFullQuery1() throws StateValueTypeException
,
124 AttributeNotFoundException
, TimeRangeException
{
126 List
<ITmfStateInterval
> list
;
127 ITmfStateInterval interval
;
131 list
= ssb
.queryFullState(interestingTimestamp1
);
133 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
134 interval
= list
.get(quark
);
135 valueInt
= interval
.getStateValue().unboxInt();
136 assertEquals(1397, valueInt
);
138 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
139 interval
= list
.get(quark
);
140 valueStr
= interval
.getStateValue().unboxStr();
141 assertEquals("gdbus", valueStr
);
143 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
144 interval
= list
.get(quark
);
145 valueStr
= interval
.getStateValue().unboxStr();
146 assertTrue(valueStr
.equals("sys_poll"));
150 public void testFullQuery2() {
155 public void testFullQuery3() {
160 public void testSingleQuery1() throws AttributeNotFoundException
,
161 TimeRangeException
, StateValueTypeException
{
163 long timestamp
= interestingTimestamp1
;
165 ITmfStateInterval interval
;
168 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
169 interval
= ssb
.querySingleState(timestamp
, quark
);
170 valueStr
= interval
.getStateValue().unboxStr();
171 assertEquals("gdbus", valueStr
);
175 public void testSingleQuery2() {
180 public void testSingleQuery3() {
185 * Test a range query (with no resolution parameter, so all intervals)
188 public void testRangeQuery1() throws AttributeNotFoundException
,
189 TimeRangeException
, StateValueTypeException
{
191 long time1
= interestingTimestamp1
;
192 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
194 List
<ITmfStateInterval
> intervals
;
196 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
197 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
);
198 assertEquals(487, intervals
.size()); /* Number of context switches! */
199 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
200 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
204 * Range query, but with a t2 far off the end of the trace.
205 * The result should still be valid.
208 public void testRangeQuery2() throws TimeRangeException
,
209 AttributeNotFoundException
{
211 List
<ITmfStateInterval
> intervals
;
213 int quark
= ssb
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
214 long ts1
= ssb
.getStartTime(); /* start of the trace */
215 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
217 intervals
= ssb
.queryHistoryRange(quark
, ts1
, ts2
);
219 /* Activity of IRQ 1 over the whole trace */
220 assertEquals(65, intervals
.size());
224 * Test a range query with a resolution
227 public void testRangeQuery3() throws AttributeNotFoundException
,
228 TimeRangeException
, StateValueTypeException
{
230 long time1
= interestingTimestamp1
;
231 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
232 long resolution
= 1000000; /* One query every millisecond */
234 List
<ITmfStateInterval
> intervals
;
236 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
237 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
, resolution
);
238 assertEquals(126, intervals
.size()); /* Number of context switches! */
239 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
240 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
244 * Ask for a time range outside of the trace's range
246 * @throws TimeRangeException
248 @Test(expected
= TimeRangeException
.class)
249 public void testFullQueryInvalidTime1() throws TimeRangeException
{
250 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
251 ssb
.queryFullState(ts
);
255 @Test(expected
= TimeRangeException
.class)
256 public void testFullQueryInvalidTime2() throws TimeRangeException
{
257 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
258 ssb
.queryFullState(ts
);
262 @Test(expected
= TimeRangeException
.class)
263 public void testSingleQueryInvalidTime1()
264 throws AttributeNotFoundException
, TimeRangeException
{
266 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
267 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
268 ssb
.querySingleState(ts
, quark
);
271 @Test(expected
= TimeRangeException
.class)
272 public void testSingleQueryInvalidTime2()
273 throws AttributeNotFoundException
, TimeRangeException
{
275 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
276 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
277 ssb
.querySingleState(ts
, quark
);
280 @Test(expected
= TimeRangeException
.class)
281 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
284 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
285 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
286 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
288 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
291 @Test(expected
= TimeRangeException
.class)
292 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
293 AttributeNotFoundException
{
295 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
296 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
297 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
299 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
303 * Ask for a non-existing attribute
305 * @throws AttributeNotFoundException
307 @Test(expected
= AttributeNotFoundException
.class)
308 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
310 ssb
.getQuarkAbsolute("There", "is", "no", "cow", "level");
314 * Query but with the wrong State Value type
316 * @throws StateValueTypeException
317 * @throws AttributeNotFoundException
318 * @throws TimeRangeException
320 @Test(expected
= StateValueTypeException
.class)
321 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
322 AttributeNotFoundException
, TimeRangeException
{
323 List
<ITmfStateInterval
> list
;
324 ITmfStateInterval interval
;
327 list
= ssb
.queryFullState(interestingTimestamp1
);
328 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
329 interval
= list
.get(quark
);
331 /* This is supposed to be an int value */
332 interval
.getStateValue().unboxStr();
335 @Test(expected
= StateValueTypeException
.class)
336 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
337 AttributeNotFoundException
, TimeRangeException
{
338 List
<ITmfStateInterval
> list
;
339 ITmfStateInterval interval
;
342 list
= ssb
.queryFullState(interestingTimestamp1
);
343 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
344 interval
= list
.get(quark
);
346 /* This is supposed to be a String value */
347 interval
.getStateValue().unboxInt();
351 public void testFullAttributeName() throws AttributeNotFoundException
{
352 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
353 String name
= ssb
.getFullAttributePath(quark
);
354 assertEquals(name
, "CPUs/0/Current_thread");
358 public void testGetQuarks_begin() {
359 List
<Integer
> list
= ssb
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
361 assertEquals(1, list
.size());
365 public void testGetQuarks_middle() {
366 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
368 /* Number of different kernel threads in the trace */
369 assertEquals(168, list
.size());
373 public void testGetQuarks_end() {
374 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "1577", "*");
376 /* There should be 4 sub-attributes for each Thread node */
377 assertEquals(4, list
.size());