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