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
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.io
.IOException
;
21 import java
.util
.List
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
24 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateChangeInput
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.StateSystemManager
;
33 import org
.junit
.AfterClass
;
34 import org
.junit
.BeforeClass
;
35 import org
.junit
.Test
;
38 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
39 * history and the non-threaded CTF kernel handler.
44 @SuppressWarnings("nls")
45 public class StateSystemFullHistoryTest
{
47 static File stateFile
;
48 static File stateFileBenchmark
;
50 static IStateChangeInput input
;
51 static IStateSystemQuerier ssq
;
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 ssq
= StateSystemManager
.loadStateHistory(stateFile
, input
);
67 } catch (Exception e
) {
73 public static void cleanup() {
75 ret1
= stateFile
.delete();
76 ret2
= stateFileBenchmark
.delete();
77 if ( !(ret1
&& ret2
) ) {
78 System
.err
.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
79 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
84 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
85 * us to @Test the @BeforeClass...
88 * @throws TmfTraceException
91 public void testBuild() throws IOException
, TmfTraceException
{
92 IStateChangeInput input2
;
93 IStateSystemQuerier ssb2
;
95 input2
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
96 ssb2
= StateSystemManager
.loadStateHistory(stateFileBenchmark
, input2
);
98 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
99 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
103 public void testOpenExistingStateFile() throws IOException
, TmfTraceException
{
104 IStateSystemQuerier ssb2
;
106 /* 'newStateFile' should have already been created */
107 ssb2
= StateSystemManager
.loadStateHistory(stateFile
, null);
110 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
111 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
115 public void testFullQuery1() throws StateValueTypeException
,
116 AttributeNotFoundException
, TimeRangeException
{
118 List
<ITmfStateInterval
> list
;
119 ITmfStateInterval interval
;
123 list
= ssq
.queryFullState(interestingTimestamp1
);
125 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
126 interval
= list
.get(quark
);
127 valueInt
= interval
.getStateValue().unboxInt();
128 assertEquals(1397, valueInt
);
130 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
131 interval
= list
.get(quark
);
132 valueStr
= interval
.getStateValue().unboxStr();
133 assertEquals("gdbus", valueStr
);
135 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
136 interval
= list
.get(quark
);
137 valueStr
= interval
.getStateValue().unboxStr();
138 assertTrue(valueStr
.equals("sys_poll"));
142 public void testFullQuery2() {
147 public void testFullQuery3() {
152 public void testSingleQuery1() throws AttributeNotFoundException
,
153 TimeRangeException
, StateValueTypeException
{
155 long timestamp
= interestingTimestamp1
;
157 ITmfStateInterval interval
;
160 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
161 interval
= ssq
.querySingleState(timestamp
, quark
);
162 valueStr
= interval
.getStateValue().unboxStr();
163 assertEquals("gdbus", valueStr
);
167 public void testSingleQuery2() {
172 public void testSingleQuery3() {
177 * Test a range query (with no resolution parameter, so all intervals)
180 public void testRangeQuery1() throws AttributeNotFoundException
,
181 TimeRangeException
, StateValueTypeException
{
183 long time1
= interestingTimestamp1
;
184 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
186 List
<ITmfStateInterval
> intervals
;
188 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
189 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
190 assertEquals(487, intervals
.size()); /* Number of context switches! */
191 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
192 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
196 * Range query, but with a t2 far off the end of the trace.
197 * The result should still be valid.
200 public void testRangeQuery2() throws TimeRangeException
,
201 AttributeNotFoundException
{
203 List
<ITmfStateInterval
> intervals
;
205 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
206 long ts1
= ssq
.getStartTime(); /* start of the trace */
207 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
209 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
211 /* Activity of IRQ 1 over the whole trace */
212 assertEquals(65, intervals
.size());
216 * Test a range query with a resolution
219 public void testRangeQuery3() throws AttributeNotFoundException
,
220 TimeRangeException
, StateValueTypeException
{
222 long time1
= interestingTimestamp1
;
223 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
224 long resolution
= 1000000; /* One query every millisecond */
226 List
<ITmfStateInterval
> intervals
;
228 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
229 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
);
230 assertEquals(126, intervals
.size()); /* Number of context switches! */
231 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
232 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
236 * Ask for a time range outside of the trace's range
238 * @throws TimeRangeException
240 @Test(expected
= TimeRangeException
.class)
241 public void testFullQueryInvalidTime1() throws TimeRangeException
{
242 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
243 ssq
.queryFullState(ts
);
247 @Test(expected
= TimeRangeException
.class)
248 public void testFullQueryInvalidTime2() throws TimeRangeException
{
249 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
250 ssq
.queryFullState(ts
);
254 @Test(expected
= TimeRangeException
.class)
255 public void testSingleQueryInvalidTime1()
256 throws AttributeNotFoundException
, TimeRangeException
{
258 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
259 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
260 ssq
.querySingleState(ts
, quark
);
263 @Test(expected
= TimeRangeException
.class)
264 public void testSingleQueryInvalidTime2()
265 throws AttributeNotFoundException
, TimeRangeException
{
267 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
268 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
269 ssq
.querySingleState(ts
, quark
);
272 @Test(expected
= TimeRangeException
.class)
273 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
276 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
277 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
278 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
280 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
283 @Test(expected
= TimeRangeException
.class)
284 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
285 AttributeNotFoundException
{
287 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
288 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
289 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
291 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
295 * Ask for a non-existing attribute
297 * @throws AttributeNotFoundException
299 @Test(expected
= AttributeNotFoundException
.class)
300 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
302 ssq
.getQuarkAbsolute("There", "is", "no", "cow", "level");
306 * Query but with the wrong State Value type
308 * @throws StateValueTypeException
309 * @throws AttributeNotFoundException
310 * @throws TimeRangeException
312 @Test(expected
= StateValueTypeException
.class)
313 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
314 AttributeNotFoundException
, TimeRangeException
{
315 List
<ITmfStateInterval
> list
;
316 ITmfStateInterval interval
;
319 list
= ssq
.queryFullState(interestingTimestamp1
);
320 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
321 interval
= list
.get(quark
);
323 /* This is supposed to be an int value */
324 interval
.getStateValue().unboxStr();
327 @Test(expected
= StateValueTypeException
.class)
328 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
329 AttributeNotFoundException
, TimeRangeException
{
330 List
<ITmfStateInterval
> list
;
331 ITmfStateInterval interval
;
334 list
= ssq
.queryFullState(interestingTimestamp1
);
335 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
336 interval
= list
.get(quark
);
338 /* This is supposed to be a String value */
339 interval
.getStateValue().unboxInt();
343 public void testFullAttributeName() throws AttributeNotFoundException
{
344 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
345 String name
= ssq
.getFullAttributePath(quark
);
346 assertEquals(name
, "CPUs/0/Current_thread");
350 public void testGetQuarks_begin() {
351 List
<Integer
> list
= ssq
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
353 assertEquals(1, list
.size());
357 public void testGetQuarks_middle() {
358 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
360 /* Number of different kernel threads in the trace */
361 assertEquals(168, list
.size());
365 public void testGetQuarks_end() {
366 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "1577", "*");
368 /* There should be 4 sub-attributes for each Thread node */
369 assertEquals(4, list
.size());