ctf: Depend on the tracecompass-test-traces project
[deliverable/tracecompass.git] / lttng / 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, 2015 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
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
24 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
25 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
28 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
29 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
30 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
31 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
32 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
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(2, TimeUnit.MINUTES);
51
52 /** Test trace used for these tests */
53 protected static final @NonNull CtfTestTrace testTrace = CtfTestTrace.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 protected static ITmfStateSystem fixture;
68
69 /**
70 * Test set-up
71 */
72 @Before
73 public void setUp() {
74 /* Subclasses should set-up 'fixture' */
75 assertNotNull(fixture);
76 }
77
78 @Test
79 public void testFullQuery1() {
80 List<ITmfStateInterval> list;
81 ITmfStateInterval interval;
82 int quark, valueInt;
83 String valueStr;
84
85 try {
86 list = fixture.queryFullState(interestingTimestamp1);
87
88 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
89 interval = list.get(quark);
90 valueInt = interval.getStateValue().unboxInt();
91 assertEquals(1397, valueInt);
92
93 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
94 interval = list.get(quark);
95 valueStr = interval.getStateValue().unboxStr();
96 assertEquals("gdbus", valueStr);
97
98 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
99 interval = list.get(quark);
100 valueStr = interval.getStateValue().unboxStr();
101 assertEquals("sys_poll", valueStr);
102
103 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
104 fail();
105 }
106 }
107
108 @Test
109 public void testSingleQuery1() {
110 long timestamp = interestingTimestamp1;
111 int quark;
112 ITmfStateInterval interval;
113 String valueStr;
114
115 try {
116 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
117 interval = fixture.querySingleState(timestamp, quark);
118 valueStr = interval.getStateValue().unboxStr();
119 assertEquals("gdbus", valueStr);
120
121 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
122 fail();
123 }
124 }
125
126 /**
127 * Test a range query (with no resolution parameter, so all intervals)
128 */
129 @Test
130 public void testRangeQuery1() {
131 long time1 = interestingTimestamp1;
132 long time2 = time1 + 1L * NANOSECS_PER_SEC;
133 int quark;
134 List<ITmfStateInterval> intervals;
135
136 final ITmfStateSystem ss = fixture;
137 assertNotNull(ss);
138
139 try {
140 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
141 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2);
142 assertEquals(487, intervals.size()); /* Number of context switches! */
143 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
144 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
145
146 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
147 fail();
148 }
149 }
150
151 /**
152 * Range query, but with a t2 far off the end of the trace. The result
153 * should still be valid.
154 */
155 @Test
156 public void testRangeQuery2() {
157 List<ITmfStateInterval> intervals;
158
159 final ITmfStateSystem ss = fixture;
160 assertNotNull(ss);
161
162 try {
163 int quark = ss.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1");
164 long ts1 = ss.getStartTime(); /* start of the trace */
165 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */
166
167 intervals = StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
168
169 /* Activity of IRQ 1 over the whole trace */
170 assertEquals(65, intervals.size());
171
172 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
173 fail();
174 }
175 }
176
177 /**
178 * Test a range query with a resolution
179 */
180 @Test
181 public void testRangeQuery3() {
182 long time1 = interestingTimestamp1;
183 long time2 = time1 + 1L * NANOSECS_PER_SEC;
184 long resolution = 1000000; /* One query every millisecond */
185 int quark;
186 List<ITmfStateInterval> intervals;
187
188 final ITmfStateSystem ss = fixture;
189 assertNotNull(ss);
190
191 try {
192 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
193 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2, resolution, null);
194 assertEquals(126, intervals.size()); /* Number of context switches! */
195 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
196 assertEquals(1331668248815698779L, intervals.get(100).getEndTime());
197
198 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
199 fail();
200 }
201 }
202
203 /**
204 * Ask for a time range outside of the trace's range
205 */
206 @Test(expected = TimeRangeException.class)
207 public void testFullQueryInvalidTime1() throws TimeRangeException,
208 StateSystemDisposedException {
209 long ts = startTime + 20L * NANOSECS_PER_SEC;
210 fixture.queryFullState(ts);
211 }
212
213 @Test(expected = TimeRangeException.class)
214 public void testFullQueryInvalidTime2() throws TimeRangeException,
215 StateSystemDisposedException {
216 long ts = startTime - 20L * NANOSECS_PER_SEC;
217 fixture.queryFullState(ts);
218 }
219
220 @Test(expected = TimeRangeException.class)
221 public void testSingleQueryInvalidTime1() throws TimeRangeException {
222 try {
223 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
224 long ts = startTime + 20L * NANOSECS_PER_SEC;
225 fixture.querySingleState(ts, quark);
226
227 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
228 fail();
229 }
230 }
231
232 @Test(expected = TimeRangeException.class)
233 public void testSingleQueryInvalidTime2() throws TimeRangeException {
234 try {
235 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
236 long ts = startTime - 20L * NANOSECS_PER_SEC;
237 fixture.querySingleState(ts, quark);
238
239 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
240 fail();
241 }
242 }
243
244 @Test(expected = TimeRangeException.class)
245 public void testRangeQueryInvalidTime1() throws TimeRangeException {
246 final ITmfStateSystem ss = fixture;
247 assertNotNull(ss);
248
249 try {
250 int quark = ss.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 StateSystemUtils.queryHistoryRange(ss, 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 final ITmfStateSystem ss = fixture;
265 assertNotNull(ss);
266
267 try {
268 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
269 long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */
270 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */
271 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
272
273 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
274 fail();
275 }
276 }
277
278 /**
279 * Ask for a non-existing attribute
280 *
281 * @throws AttributeNotFoundException
282 */
283 @Test(expected = AttributeNotFoundException.class)
284 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
285 fixture.getQuarkAbsolute("There", "is", "no", "cow", "level");
286 }
287
288 /**
289 * Query but with the wrong State Value type
290 */
291 @Test(expected = StateValueTypeException.class)
292 public void testQueryInvalidValuetype1() throws StateValueTypeException {
293 List<ITmfStateInterval> list;
294 ITmfStateInterval interval;
295 int quark;
296
297 try {
298 list = fixture.queryFullState(interestingTimestamp1);
299 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
300 interval = list.get(quark);
301
302 /* This is supposed to be an int value */
303 interval.getStateValue().unboxStr();
304
305 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
306 fail();
307 }
308 }
309
310 @Test(expected = StateValueTypeException.class)
311 public void testQueryInvalidValuetype2() throws StateValueTypeException {
312 List<ITmfStateInterval> list;
313 ITmfStateInterval interval;
314 int quark;
315
316 try {
317 list = fixture.queryFullState(interestingTimestamp1);
318 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
319 interval = list.get(quark);
320
321 /* This is supposed to be a String value */
322 interval.getStateValue().unboxInt();
323
324 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
325 fail();
326 }
327 }
328
329 @Test
330 public void testFullAttributeName() {
331 try {
332 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
333 String name = fixture.getFullAttributePath(quark);
334 assertEquals(name, "CPUs/0/Current_thread");
335
336 } catch (AttributeNotFoundException e) {
337 fail();
338 }
339 }
340
341 @Test
342 public void testGetQuarks_begin() {
343 List<Integer> list = fixture.getQuarks("*", "1577", Attributes.EXEC_NAME);
344
345 assertEquals(1, list.size());
346 }
347
348 @Test
349 public void testGetQuarks_middle() {
350 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
351
352 /* Number of different kernel threads in the trace */
353 assertEquals(168, list.size());
354 }
355
356 @Test
357 public void testGetQuarks_end() {
358 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "1577", "*");
359
360 /* There should be 5 sub-attributes for each Thread node */
361 assertEquals(5, list.size());
362 }
363
364 // ------------------------------------------------------------------------
365 // Tests verifying the *complete* results of a full queries
366 // ------------------------------------------------------------------------
367
368 protected long getStartTimes(int idx) {
369 return TestValues.startTimes[idx];
370 }
371
372 protected long getEndTimes(int idx) {
373 return TestValues.endTimes[idx];
374 }
375
376 protected ITmfStateValue getStateValues(int idx) {
377 return TestValues.values[idx];
378 }
379
380 @Test
381 public void testFullQueryThorough() {
382 try {
383 List<ITmfStateInterval> state = fixture.queryFullState(interestingTimestamp1);
384 assertEquals(TestValues.size, state.size());
385
386 for (int i = 0; i < state.size(); i++) {
387 /* Test each component of the intervals */
388 assertEquals(getStartTimes(i), state.get(i).getStartTime());
389 assertEquals(getEndTimes(i), state.get(i).getEndTime());
390 assertEquals(i, state.get(i).getAttribute());
391 assertEquals(getStateValues(i), state.get(i).getStateValue());
392 }
393
394 } catch (StateSystemDisposedException e) {
395 fail();
396 }
397 }
398
399 @Test
400 public void testFirstIntervalIsConsidered() {
401 try {
402 int quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1397", Attributes.STATUS);
403 List<ITmfStateInterval> list = fixture.queryFullState(1331668248014135800L);
404 ITmfStateInterval interval = list.get(quark);
405 assertEquals(1331668247516664825L, interval.getStartTime());
406
407 int valueInt = interval.getStateValue().unboxInt();
408 assertEquals(1, valueInt);
409
410 } catch (StateSystemDisposedException | AttributeNotFoundException 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.056863 seconds and 5 git commands to generate.