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