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
;
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 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
146 interval
= list
.get(quark
);
147 valueStr
= interval
.getStateValue().unboxStr();
148 assertTrue(valueStr
.equals("sys_poll"));
152 public void testFullQuery2() {
157 public void testFullQuery3() {
162 public void testSingleQuery1() throws AttributeNotFoundException
,
163 TimeRangeException
, StateValueTypeException
{
165 long timestamp
= interestingTimestamp1
;
167 ITmfStateInterval interval
;
170 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
171 interval
= ssb
.querySingleState(timestamp
, quark
);
172 valueStr
= interval
.getStateValue().unboxStr();
173 assertEquals("gdbus", valueStr
);
177 public void testSingleQuery2() {
182 public void testSingleQuery3() {
187 * Test a range query (with no resolution parameter, so all intervals)
190 public void testRangeQuery1() throws AttributeNotFoundException
,
191 TimeRangeException
, StateValueTypeException
{
193 long time1
= interestingTimestamp1
;
194 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
196 List
<ITmfStateInterval
> intervals
;
198 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
199 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
);
200 assertEquals(487, intervals
.size()); /* Number of context switches! */
201 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
202 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
206 * Range query, but with a t2 far off the end of the trace.
207 * The result should still be valid.
210 public void testRangeQuery2() throws TimeRangeException
,
211 AttributeNotFoundException
{
213 List
<ITmfStateInterval
> intervals
;
215 int quark
= ssb
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
216 long ts1
= ssb
.getStartTime(); /* start of the trace */
217 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
219 intervals
= ssb
.queryHistoryRange(quark
, ts1
, ts2
);
221 /* Activity of IRQ 1 over the whole trace */
222 assertEquals(65, intervals
.size());
226 * Test a range query with a resolution
229 public void testRangeQuery3() throws AttributeNotFoundException
,
230 TimeRangeException
, StateValueTypeException
{
232 long time1
= interestingTimestamp1
;
233 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
234 long resolution
= 1000000; /* One query every millisecond */
236 List
<ITmfStateInterval
> intervals
;
238 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
239 intervals
= ssb
.queryHistoryRange(quark
, time1
, time2
, resolution
);
240 assertEquals(129, intervals
.size()); /* Number of context switches! */
241 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
242 assertEquals(1331668248784789238L, intervals
.get(100).getEndTime());
246 * Ask for a time range outside of the trace's range
248 * @throws TimeRangeException
250 @Test(expected
= TimeRangeException
.class)
251 public void testFullQueryInvalidTime1() throws TimeRangeException
{
252 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
253 ssb
.loadStateAtTime(ts
);
257 @Test(expected
= TimeRangeException
.class)
258 public void testFullQueryInvalidTime2() throws TimeRangeException
{
259 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
260 ssb
.loadStateAtTime(ts
);
264 @Test(expected
= TimeRangeException
.class)
265 public void testSingleQueryInvalidTime1()
266 throws AttributeNotFoundException
, TimeRangeException
{
268 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
269 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
270 ssb
.querySingleState(ts
, quark
);
273 @Test(expected
= TimeRangeException
.class)
274 public void testSingleQueryInvalidTime2()
275 throws AttributeNotFoundException
, TimeRangeException
{
277 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
278 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
279 ssb
.querySingleState(ts
, quark
);
282 @Test(expected
= TimeRangeException
.class)
283 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
286 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
287 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
288 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
290 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
293 @Test(expected
= TimeRangeException
.class)
294 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
295 AttributeNotFoundException
{
297 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
298 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
299 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
301 ssb
.queryHistoryRange(quark
, ts1
, ts2
);
305 * Ask for a non-existing attribute
307 * @throws AttributeNotFoundException
309 @Test(expected
= AttributeNotFoundException
.class)
310 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
312 ssb
.getQuarkAbsolute("There", "is", "no", "cow", "level");
316 * Query but with the wrong State Value type
318 * @throws StateValueTypeException
319 * @throws AttributeNotFoundException
320 * @throws TimeRangeException
322 @Test(expected
= StateValueTypeException
.class)
323 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
324 AttributeNotFoundException
, TimeRangeException
{
325 List
<ITmfStateInterval
> list
;
326 ITmfStateInterval interval
;
329 list
= ssb
.loadStateAtTime(interestingTimestamp1
);
330 quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
331 interval
= list
.get(quark
);
333 /* This is supposed to be an int value */
334 interval
.getStateValue().unboxStr();
337 @Test(expected
= StateValueTypeException
.class)
338 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
339 AttributeNotFoundException
, TimeRangeException
{
340 List
<ITmfStateInterval
> list
;
341 ITmfStateInterval interval
;
344 list
= ssb
.loadStateAtTime(interestingTimestamp1
);
345 quark
= ssb
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
346 interval
= list
.get(quark
);
348 /* This is supposed to be a String value */
349 interval
.getStateValue().unboxInt();
353 public void testFullAttributeName() throws AttributeNotFoundException
{
354 int quark
= ssb
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
355 String name
= ssb
.getFullAttributePath(quark
);
356 assertEquals(name
, "CPUs/0/Current_thread");
360 public void testGetQuarks_begin() {
361 List
<Integer
> list
= ssb
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
363 assertEquals(1, list
.size());
367 public void testGetQuarks_middle() {
368 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
370 /* Number of different kernel threads in the trace */
371 assertEquals(168, list
.size());
375 public void testGetQuarks_end() {
376 List
<Integer
> list
= ssb
.getQuarks(Attributes
.THREADS
, "1577", "*");
378 /* There should be 4 sub-attributes for each Thread node */
379 assertEquals(4, list
.size());