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
.util
.List
;
22 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
23 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.stateprovider
.CtfKernelStateInput
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
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
.ITmfStateSystem
;
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", "javadoc"})
45 public class StateSystemFullHistoryTest
{
47 static File stateFile
;
48 static File stateFileBenchmark
;
50 static IStateChangeInput input
;
51 static ITmfStateSystem ssq
;
53 /* Offset in the trace + start time of the trace */
54 private final static long interestingTimestamp1
= 18670067372290L + 1331649577946812237L;
56 /* ID we give to the state system we build */
57 private static final String STATE_ID
= "test-ss";
59 protected static String
getTestFileName() {
60 return "/tmp/statefile.ht"; //$NON-NLS-1$
64 public static void initialize() {
65 stateFile
= new File(getTestFileName());
66 stateFileBenchmark
= new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
68 input
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
69 ssq
= StateSystemManager
.loadStateHistory(stateFile
, input
, STATE_ID
, true);
70 } catch (Exception e
) {
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 TmfTraceException
{
95 IStateChangeInput input2
;
98 input2
= new CtfKernelStateInput(CtfTestFiles
.getTestTrace());
99 ssb2
= StateSystemManager
.loadStateHistory(stateFileBenchmark
, input2
, STATE_ID
, true);
101 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
102 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
106 public void testOpenExistingStateFile() throws TmfTraceException
{
107 ITmfStateSystem ssb2
;
109 /* 'newStateFile' should have already been created */
110 ssb2
= StateSystemManager
.loadStateHistory(stateFile
, null, STATE_ID
, true);
113 assertEquals(CtfTestFiles
.startTime
, ssb2
.getStartTime());
114 assertEquals(CtfTestFiles
.endTime
, ssb2
.getCurrentEndTime());
118 public void testFullQuery1() throws StateValueTypeException
,
119 AttributeNotFoundException
, TimeRangeException
,
120 StateSystemDisposedException
{
122 List
<ITmfStateInterval
> list
;
123 ITmfStateInterval interval
;
127 list
= ssq
.queryFullState(interestingTimestamp1
);
129 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
130 interval
= list
.get(quark
);
131 valueInt
= interval
.getStateValue().unboxInt();
132 assertEquals(1397, valueInt
);
134 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
135 interval
= list
.get(quark
);
136 valueStr
= interval
.getStateValue().unboxStr();
137 assertEquals("gdbus", valueStr
);
139 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.SYSTEM_CALL
);
140 interval
= list
.get(quark
);
141 valueStr
= interval
.getStateValue().unboxStr();
142 assertTrue(valueStr
.equals("sys_poll"));
146 public void testFullQuery2() {
151 public void testFullQuery3() {
156 public void testSingleQuery1() throws AttributeNotFoundException
,
157 TimeRangeException
, StateValueTypeException
,
158 StateSystemDisposedException
{
160 long timestamp
= interestingTimestamp1
;
162 ITmfStateInterval interval
;
165 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
166 interval
= ssq
.querySingleState(timestamp
, quark
);
167 valueStr
= interval
.getStateValue().unboxStr();
168 assertEquals("gdbus", valueStr
);
172 public void testSingleQuery2() {
177 public void testSingleQuery3() {
182 * Test a range query (with no resolution parameter, so all intervals)
185 public void testRangeQuery1() throws AttributeNotFoundException
,
186 TimeRangeException
, StateValueTypeException
,
187 StateSystemDisposedException
{
189 long time1
= interestingTimestamp1
;
190 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
192 List
<ITmfStateInterval
> intervals
;
194 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
195 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
);
196 assertEquals(487, intervals
.size()); /* Number of context switches! */
197 assertEquals(1685, intervals
.get(100).getStateValue().unboxInt());
198 assertEquals(1331668248427681372L, intervals
.get(205).getEndTime());
202 * Range query, but with a t2 far off the end of the trace.
203 * The result should still be valid.
206 public void testRangeQuery2() throws TimeRangeException
,
207 AttributeNotFoundException
, StateSystemDisposedException
{
209 List
<ITmfStateInterval
> intervals
;
211 int quark
= ssq
.getQuarkAbsolute(Attributes
.RESOURCES
, Attributes
.IRQS
, "1");
212 long ts1
= ssq
.getStartTime(); /* start of the trace */
213 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid, but ignored */
215 intervals
= ssq
.queryHistoryRange(quark
, ts1
, ts2
);
217 /* Activity of IRQ 1 over the whole trace */
218 assertEquals(65, intervals
.size());
222 * Test a range query with a resolution
225 public void testRangeQuery3() throws AttributeNotFoundException
,
226 TimeRangeException
, StateValueTypeException
,
227 StateSystemDisposedException
{
229 long time1
= interestingTimestamp1
;
230 long time2
= time1
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
;
231 long resolution
= 1000000; /* One query every millisecond */
233 List
<ITmfStateInterval
> intervals
;
235 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
236 intervals
= ssq
.queryHistoryRange(quark
, time1
, time2
, resolution
, null);
237 assertEquals(126, intervals
.size()); /* Number of context switches! */
238 assertEquals(1452, intervals
.get(50).getStateValue().unboxInt());
239 assertEquals(1331668248815698779L, intervals
.get(100).getEndTime());
243 * Ask for a time range outside of the trace's range
245 @Test(expected
= TimeRangeException
.class)
246 public void testFullQueryInvalidTime1() throws TimeRangeException
,
247 StateSystemDisposedException
{
248 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
249 ssq
.queryFullState(ts
);
252 @Test(expected
= TimeRangeException
.class)
253 public void testFullQueryInvalidTime2() throws TimeRangeException
,
254 StateSystemDisposedException
{
255 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
256 ssq
.queryFullState(ts
);
259 @Test(expected
= TimeRangeException
.class)
260 public void testSingleQueryInvalidTime1()
261 throws AttributeNotFoundException
, TimeRangeException
,
262 StateSystemDisposedException
{
264 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
265 long ts
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
266 ssq
.querySingleState(ts
, quark
);
269 @Test(expected
= TimeRangeException
.class)
270 public void testSingleQueryInvalidTime2()
271 throws AttributeNotFoundException
, TimeRangeException
,
272 StateSystemDisposedException
{
274 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
275 long ts
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
;
276 ssq
.querySingleState(ts
, quark
);
279 @Test(expected
= TimeRangeException
.class)
280 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException
,
281 TimeRangeException
, StateSystemDisposedException
{
283 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
284 long ts1
= CtfTestFiles
.startTime
- 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
285 long ts2
= CtfTestFiles
.startTime
+ 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* valid */
287 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
290 @Test(expected
= TimeRangeException
.class)
291 public void testRangeQueryInvalidTime2() throws TimeRangeException
,
292 AttributeNotFoundException
, StateSystemDisposedException
{
294 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
295 long ts1
= CtfTestFiles
.startTime
- 1L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
296 long ts2
= CtfTestFiles
.startTime
+ 20L * CtfTestFiles
.NANOSECS_PER_SEC
; /* invalid */
298 ssq
.queryHistoryRange(quark
, ts1
, ts2
);
302 * Ask for a non-existing attribute
304 * @throws AttributeNotFoundException
306 @Test(expected
= AttributeNotFoundException
.class)
307 public void testQueryInvalidAttribute() throws AttributeNotFoundException
{
309 ssq
.getQuarkAbsolute("There", "is", "no", "cow", "level");
313 * Query but with the wrong State Value type
315 @Test(expected
= StateValueTypeException
.class)
316 public void testQueryInvalidValuetype1() throws StateValueTypeException
,
317 AttributeNotFoundException
, TimeRangeException
,
318 StateSystemDisposedException
{
319 List
<ITmfStateInterval
> list
;
320 ITmfStateInterval interval
;
323 list
= ssq
.queryFullState(interestingTimestamp1
);
324 quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
325 interval
= list
.get(quark
);
327 /* This is supposed to be an int value */
328 interval
.getStateValue().unboxStr();
331 @Test(expected
= StateValueTypeException
.class)
332 public void testQueryInvalidValuetype2() throws StateValueTypeException
,
333 AttributeNotFoundException
, TimeRangeException
,
334 StateSystemDisposedException
{
335 List
<ITmfStateInterval
> list
;
336 ITmfStateInterval interval
;
339 list
= ssq
.queryFullState(interestingTimestamp1
);
340 quark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, "1432", Attributes
.EXEC_NAME
);
341 interval
= list
.get(quark
);
343 /* This is supposed to be a String value */
344 interval
.getStateValue().unboxInt();
348 public void testFullAttributeName() throws AttributeNotFoundException
{
349 int quark
= ssq
.getQuarkAbsolute(Attributes
.CPUS
, "0", Attributes
.CURRENT_THREAD
);
350 String name
= ssq
.getFullAttributePath(quark
);
351 assertEquals(name
, "CPUs/0/Current_thread");
355 public void testGetQuarks_begin() {
356 List
<Integer
> list
= ssq
.getQuarks("*", "1577", Attributes
.EXEC_NAME
);
358 assertEquals(1, list
.size());
362 public void testGetQuarks_middle() {
363 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "*", Attributes
.EXEC_NAME
);
365 /* Number of different kernel threads in the trace */
366 assertEquals(168, list
.size());
370 public void testGetQuarks_end() {
371 List
<Integer
> list
= ssq
.getQuarks(Attributes
.THREADS
, "1577", "*");
373 /* There should be 4 sub-attributes for each Thread node */
374 assertEquals(4, list
.size());