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