tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
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
10 *
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 import static org.junit.Assume.assumeTrue;
20
21 import java.util.List;
22
23 import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
24 import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
25 import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
26 import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
27 import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
28 import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
29 import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
30 import org.eclipse.linuxtools.statesystem.core.statevalue.ITmfStateValue;
31 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.TestRule;
37 import org.junit.rules.Timeout;
38
39 /**
40 * Base unit tests for the StateHistorySystem. Extension can be made to test
41 * different state back-end types or configurations.
42 *
43 * @author Alexandre Montplaisir
44 */
45 @SuppressWarnings("javadoc")
46 public abstract class StateSystemTest {
47
48 /** Timeout the tests after 2 minutes */
49 @Rule
50 public TestRule timeoutRule = new Timeout(120000);
51
52 /** Test trace used for these tests */
53 protected static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.TRACE2;
54
55 /** Expected start time of the test trace/state history */
56 protected static final long startTime = 1331668247314038062L;
57
58 /** Expected end time of the state history built from the test trace */
59 protected static final long endTime = 1331668259054285979L;
60
61 /** Offset in the trace + start time of the trace */
62 protected static final long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
63
64 /** Number of nanoseconds in one second */
65 private static final long NANOSECS_PER_SEC = 1000000000L;
66
67 private ITmfStateSystem fixture;
68
69
70 /**
71 * Class set-up
72 */
73 @Before
74 public void setUp() {
75 assumeTrue(testTrace.exists());
76 fixture = this.initialize();
77 assertNotNull(fixture);
78 }
79
80 protected abstract ITmfStateSystem initialize();
81
82 @After
83 public void tearDown() {
84 if (fixture != null) {
85 fixture.dispose();
86 }
87 fixture = null;
88 }
89
90 @Test
91 public void testFullQuery1() {
92 List<ITmfStateInterval> list;
93 ITmfStateInterval interval;
94 int quark, valueInt;
95 String valueStr;
96
97 try {
98 list = fixture.queryFullState(interestingTimestamp1);
99
100 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
101 interval = list.get(quark);
102 valueInt = interval.getStateValue().unboxInt();
103 assertEquals(1397, valueInt);
104
105 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
106 interval = list.get(quark);
107 valueStr = interval.getStateValue().unboxStr();
108 assertEquals("gdbus", valueStr);
109
110 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
111 interval = list.get(quark);
112 valueStr = interval.getStateValue().unboxStr();
113 assertTrue(valueStr.equals("sys_poll"));
114
115 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
116 fail();
117 }
118 }
119
120 @Test
121 public void testSingleQuery1() {
122 long timestamp = interestingTimestamp1;
123 int quark;
124 ITmfStateInterval interval;
125 String valueStr;
126
127 try {
128 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
129 interval = fixture.querySingleState(timestamp, quark);
130 valueStr = interval.getStateValue().unboxStr();
131 assertEquals("gdbus", valueStr);
132
133 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
134 fail();
135 }
136 }
137
138 /**
139 * Test a range query (with no resolution parameter, so all intervals)
140 */
141 @Test
142 public void testRangeQuery1() {
143 long time1 = interestingTimestamp1;
144 long time2 = time1 + 1L * NANOSECS_PER_SEC;
145 int quark;
146 List<ITmfStateInterval> intervals;
147
148 try {
149 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
150 intervals = fixture.queryHistoryRange(quark, time1, time2);
151 assertEquals(487, intervals.size()); /* Number of context switches! */
152 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
153 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
154
155 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
156 fail();
157 }
158 }
159
160 /**
161 * Range query, but with a t2 far off the end of the trace. The result
162 * should still be valid.
163 */
164 @Test
165 public void testRangeQuery2() {
166 List<ITmfStateInterval> intervals;
167
168 try {
169 int quark = fixture.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1");
170 long ts1 = fixture.getStartTime(); /* start of the trace */
171 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */
172
173 intervals = fixture.queryHistoryRange(quark, ts1, ts2);
174
175 /* Activity of IRQ 1 over the whole trace */
176 assertEquals(65, intervals.size());
177
178 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
179 fail();
180 }
181 }
182
183 /**
184 * Test a range query with a resolution
185 */
186 @Test
187 public void testRangeQuery3() {
188 long time1 = interestingTimestamp1;
189 long time2 = time1 + 1L * NANOSECS_PER_SEC;
190 long resolution = 1000000; /* One query every millisecond */
191 int quark;
192 List<ITmfStateInterval> intervals;
193
194 try {
195 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
196 intervals = fixture.queryHistoryRange(quark, time1, time2, resolution, null);
197 assertEquals(126, intervals.size()); /* Number of context switches! */
198 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
199 assertEquals(1331668248815698779L, intervals.get(100).getEndTime());
200
201 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
202 fail();
203 }
204 }
205
206 /**
207 * Ask for a time range outside of the trace's range
208 */
209 @Test(expected = TimeRangeException.class)
210 public void testFullQueryInvalidTime1() throws TimeRangeException,
211 StateSystemDisposedException {
212 long ts = startTime + 20L * NANOSECS_PER_SEC;
213 fixture.queryFullState(ts);
214 }
215
216 @Test(expected = TimeRangeException.class)
217 public void testFullQueryInvalidTime2() throws TimeRangeException,
218 StateSystemDisposedException {
219 long ts = startTime - 20L * NANOSECS_PER_SEC;
220 fixture.queryFullState(ts);
221 }
222
223 @Test(expected = TimeRangeException.class)
224 public void testSingleQueryInvalidTime1() throws TimeRangeException {
225 try {
226 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
227 long ts = startTime + 20L * NANOSECS_PER_SEC;
228 fixture.querySingleState(ts, quark);
229
230 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
231 fail();
232 }
233 }
234
235 @Test(expected = TimeRangeException.class)
236 public void testSingleQueryInvalidTime2() throws TimeRangeException {
237 try {
238 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
239 long ts = startTime - 20L * NANOSECS_PER_SEC;
240 fixture.querySingleState(ts, quark);
241
242 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
243 fail();
244 }
245 }
246
247 @Test(expected = TimeRangeException.class)
248 public void testRangeQueryInvalidTime1() throws TimeRangeException {
249 try {
250 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
251 long ts1 = startTime - 20L * NANOSECS_PER_SEC; /* invalid */
252 long ts2 = startTime + 1L * NANOSECS_PER_SEC; /* valid */
253 fixture.queryHistoryRange(quark, ts1, ts2);
254
255 } catch (AttributeNotFoundException e) {
256 fail();
257 } catch (StateSystemDisposedException e) {
258 fail();
259 }
260 }
261
262 @Test(expected = TimeRangeException.class)
263 public void testRangeQueryInvalidTime2() throws TimeRangeException {
264 try {
265 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
266 long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */
267 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */
268 fixture.queryHistoryRange(quark, ts1, ts2);
269
270 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
271 fail();
272 }
273 }
274
275 /**
276 * Ask for a non-existing attribute
277 *
278 * @throws AttributeNotFoundException
279 */
280 @Test(expected = AttributeNotFoundException.class)
281 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
282 fixture.getQuarkAbsolute("There", "is", "no", "cow", "level");
283 }
284
285 /**
286 * Query but with the wrong State Value type
287 */
288 @Test(expected = StateValueTypeException.class)
289 public void testQueryInvalidValuetype1() throws StateValueTypeException {
290 List<ITmfStateInterval> list;
291 ITmfStateInterval interval;
292 int quark;
293
294 try {
295 list = fixture.queryFullState(interestingTimestamp1);
296 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
297 interval = list.get(quark);
298
299 /* This is supposed to be an int value */
300 interval.getStateValue().unboxStr();
301
302 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
303 fail();
304 }
305 }
306
307 @Test(expected = StateValueTypeException.class)
308 public void testQueryInvalidValuetype2() throws StateValueTypeException {
309 List<ITmfStateInterval> list;
310 ITmfStateInterval interval;
311 int quark;
312
313 try {
314 list = fixture.queryFullState(interestingTimestamp1);
315 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
316 interval = list.get(quark);
317
318 /* This is supposed to be a String value */
319 interval.getStateValue().unboxInt();
320
321 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
322 fail();
323 }
324 }
325
326 @Test
327 public void testFullAttributeName() {
328 try {
329 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
330 String name = fixture.getFullAttributePath(quark);
331 assertEquals(name, "CPUs/0/Current_thread");
332
333 } catch (AttributeNotFoundException e) {
334 fail();
335 }
336 }
337
338 @Test
339 public void testGetQuarks_begin() {
340 List<Integer> list = fixture.getQuarks("*", "1577", Attributes.EXEC_NAME);
341
342 assertEquals(1, list.size());
343 }
344
345 @Test
346 public void testGetQuarks_middle() {
347 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
348
349 /* Number of different kernel threads in the trace */
350 assertEquals(168, list.size());
351 }
352
353 @Test
354 public void testGetQuarks_end() {
355 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "1577", "*");
356
357 /* There should be 4 sub-attributes for each Thread node */
358 assertEquals(4, list.size());
359 }
360
361 // ------------------------------------------------------------------------
362 // Tests verifying the *complete* results of a full queries
363 // ------------------------------------------------------------------------
364
365 protected long getStartTimes(int idx) {
366 return TestValues.startTimes[idx];
367 }
368
369 protected long getEndTimes(int idx) {
370 return TestValues.endTimes[idx];
371 }
372
373 protected ITmfStateValue getStateValues(int idx) {
374 return TestValues.values[idx];
375 }
376
377 @Test
378 public void testFullQueryThorough() {
379 try {
380 List<ITmfStateInterval> state = fixture.queryFullState(interestingTimestamp1);
381 assertEquals(TestValues.size, state.size());
382
383 for (int i = 0; i < state.size(); i++) {
384 /* Test each component of the intervals */
385 assertEquals(getStartTimes(i), state.get(i).getStartTime());
386 assertEquals(getEndTimes(i), state.get(i).getEndTime());
387 assertEquals(i, state.get(i).getAttribute());
388 assertEquals(getStateValues(i), state.get(i).getStateValue());
389 }
390
391 } catch (StateSystemDisposedException e) {
392 fail();
393 }
394 }
395
396 @Test
397 public void testFirstIntervalIsConsidered() {
398 try {
399 List<ITmfStateInterval> list = fixture.queryFullState(1331668248014135800L);
400 ITmfStateInterval interval = list.get(233);
401 assertEquals(1331668247516664825L, interval.getStartTime());
402
403 int valueInt = interval.getStateValue().unboxInt();
404 assertEquals(1, valueInt);
405
406 } catch (StateSystemDisposedException e) {
407 fail();
408 }
409 }
410
411 @Test
412 public void testParentAttribute() {
413 String[] path = { "CPUs/0/Current_thread",
414 "CPUs/0",
415 "CPUs" };
416 try {
417 int q = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
418 for (int i = 0; i < path.length; i++) {
419 String name = fixture.getFullAttributePath(q);
420 assertEquals(path[i], name);
421 q = fixture.getParentAttributeQuark(q);
422 }
423 assertEquals(-1, q);
424 q = fixture.getParentAttributeQuark(q);
425 assertEquals(-1, q);
426 } catch (AttributeNotFoundException e) {
427 fail();
428 }
429 }
430
431 }
This page took 0.041223 seconds and 5 git commands to generate.