linux: introduce execution contexts in resources view
[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, 2016 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.eclipse.tracecompass.statesystem.core.ITmfStateSystem.INVALID_ATTRIBUTE;
16 import static org.eclipse.tracecompass.statesystem.core.ITmfStateSystem.ROOT_ATTRIBUTE;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertNotEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.fail;
21
22 import java.util.List;
23 import java.util.concurrent.TimeUnit;
24
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
27 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
28 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
29 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
30 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
31 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
33 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
34 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
35 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.TestRule;
40 import org.junit.rules.Timeout;
41
42 /**
43 * Base unit tests for the StateHistorySystem. Extension can be made to test
44 * different state back-end types or configurations.
45 *
46 * @author Alexandre Montplaisir
47 */
48 @SuppressWarnings("javadoc")
49 public abstract class StateSystemTest {
50
51 /** Timeout the tests after 2 minutes */
52 @Rule
53 public TestRule timeoutRule = new Timeout(2, TimeUnit.MINUTES);
54
55 /** Test trace used for these tests */
56 protected static final @NonNull CtfTestTrace testTrace = CtfTestTrace.TRACE2;
57
58 /** Expected start time of the test trace/state history */
59 protected static final long startTime = 1331668247314038062L;
60
61 /** Expected end time of the state history built from the test trace */
62 protected static final long endTime = 1331668259054285979L;
63
64 /** Offset in the trace + start time of the trace */
65 protected static final long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
66
67 /** Number of nanoseconds in one second */
68 private static final long NANOSECS_PER_SEC = 1000000000L;
69
70 protected static ITmfStateSystem fixture;
71
72 /**
73 * Test set-up
74 */
75 @Before
76 public void setUp() {
77 /* Subclasses should set-up 'fixture' */
78 assertNotNull(fixture);
79 }
80
81 @Test
82 public void testFullQuery1() {
83 List<ITmfStateInterval> list;
84 ITmfStateInterval interval;
85 int quark, valueInt;
86 String valueStr;
87
88 try {
89 list = fixture.queryFullState(interestingTimestamp1);
90
91 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
92 interval = list.get(quark);
93 valueInt = interval.getStateValue().unboxInt();
94 assertEquals(1397, valueInt);
95
96 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
97 interval = list.get(quark);
98 valueStr = interval.getStateValue().unboxStr();
99 assertEquals("gdbus", valueStr);
100
101 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
102 interval = list.get(quark);
103 valueStr = interval.getStateValue().unboxStr();
104 assertEquals("sys_poll", valueStr);
105
106 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
107 fail();
108 }
109 }
110
111 @Test
112 public void testSingleQuery1() {
113 long timestamp = interestingTimestamp1;
114 int quark;
115 ITmfStateInterval interval;
116 String valueStr;
117
118 try {
119 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
120 interval = fixture.querySingleState(timestamp, quark);
121 valueStr = interval.getStateValue().unboxStr();
122 assertEquals("gdbus", valueStr);
123
124 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
125 fail();
126 }
127 }
128
129 /**
130 * Test a range query (with no resolution parameter, so all intervals)
131 */
132 @Test
133 public void testRangeQuery1() {
134 long time1 = interestingTimestamp1;
135 long time2 = time1 + 1L * NANOSECS_PER_SEC;
136 int quark;
137 List<ITmfStateInterval> intervals;
138
139 final ITmfStateSystem ss = fixture;
140 assertNotNull(ss);
141
142 try {
143 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
144 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2);
145 assertEquals(487, intervals.size()); /* Number of context switches! */
146 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
147 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
148
149 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
150 fail();
151 }
152 }
153
154 /**
155 * Range query, but with a t2 far off the end of the trace. The result
156 * should still be valid.
157 */
158 @Test
159 public void testRangeQuery2() {
160 List<ITmfStateInterval> intervals;
161
162 final ITmfStateSystem ss = fixture;
163 assertNotNull(ss);
164
165 try {
166 int quark = ss.getQuarkAbsolute(Attributes.CPUS, Integer.toString(0), Attributes.IRQS, "1");
167 long ts1 = ss.getStartTime(); /* start of the trace */
168 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */
169
170 intervals = StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
171
172 /* Activity of IRQ 1 over the whole trace */
173 assertEquals(65, intervals.size());
174
175 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
176 fail();
177 }
178 }
179
180 /**
181 * Test a range query with a resolution
182 */
183 @Test
184 public void testRangeQuery3() {
185 long time1 = interestingTimestamp1;
186 long time2 = time1 + 1L * NANOSECS_PER_SEC;
187 long resolution = 1000000; /* One query every millisecond */
188 int quark;
189 List<ITmfStateInterval> intervals;
190
191 final ITmfStateSystem ss = fixture;
192 assertNotNull(ss);
193
194 try {
195 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
196 intervals = StateSystemUtils.queryHistoryRange(ss, 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 final ITmfStateSystem ss = fixture;
250 assertNotNull(ss);
251
252 try {
253 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
254 long ts1 = startTime - 20L * NANOSECS_PER_SEC; /* invalid */
255 long ts2 = startTime + 1L * NANOSECS_PER_SEC; /* valid */
256 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
257
258 } catch (AttributeNotFoundException e) {
259 fail();
260 } catch (StateSystemDisposedException e) {
261 fail();
262 }
263 }
264
265 @Test(expected = TimeRangeException.class)
266 public void testRangeQueryInvalidTime2() throws TimeRangeException {
267 final ITmfStateSystem ss = fixture;
268 assertNotNull(ss);
269
270 try {
271 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
272 long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */
273 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */
274 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
275
276 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
277 fail();
278 }
279 }
280
281 /**
282 * Ask for a non-existing attribute
283 *
284 * @throws AttributeNotFoundException
285 */
286 @Test(expected = AttributeNotFoundException.class)
287 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
288 fixture.getQuarkAbsolute("There", "is", "no", "cow", "level");
289 }
290
291 /**
292 * Query but with the wrong State Value type
293 */
294 @Test(expected = StateValueTypeException.class)
295 public void testQueryInvalidValuetype1() throws StateValueTypeException {
296 List<ITmfStateInterval> list;
297 ITmfStateInterval interval;
298 int quark;
299
300 try {
301 list = fixture.queryFullState(interestingTimestamp1);
302 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
303 interval = list.get(quark);
304
305 /* This is supposed to be an int value */
306 interval.getStateValue().unboxStr();
307
308 } catch (AttributeNotFoundException | 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 = fixture.queryFullState(interestingTimestamp1);
321 quark = fixture.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 | StateSystemDisposedException e) {
328 fail();
329 }
330 }
331
332 @Test
333 public void testOptQuarkAbsolute() {
334 int quark = fixture.optQuarkAbsolute();
335 assertEquals(ROOT_ATTRIBUTE, quark);
336
337 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
338 assertNotEquals(INVALID_ATTRIBUTE, quark);
339 assertEquals(Attributes.EXEC_NAME, fixture.getAttributeName(quark));
340
341 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "1432", "absent");
342 assertEquals(INVALID_ATTRIBUTE, quark);
343
344 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "absent", Attributes.EXEC_NAME);
345 assertEquals(INVALID_ATTRIBUTE, quark);
346
347 quark = fixture.optQuarkAbsolute("absent", "1432", Attributes.EXEC_NAME);
348 assertEquals(INVALID_ATTRIBUTE, quark);
349 }
350
351 @Test
352 public void testOptQuarkRelative() {
353 int threadsQuark = INVALID_ATTRIBUTE;
354 try {
355 threadsQuark = fixture.getQuarkAbsolute(Attributes.THREADS);
356 } catch (AttributeNotFoundException e) {
357 fail();
358 }
359 assertNotEquals(INVALID_ATTRIBUTE, threadsQuark);
360
361 int quark = fixture.optQuarkRelative(threadsQuark);
362 assertEquals(threadsQuark, quark);
363
364 quark = fixture.optQuarkRelative(threadsQuark, "1432", Attributes.EXEC_NAME);
365 assertNotEquals(INVALID_ATTRIBUTE, quark);
366 assertEquals(Attributes.EXEC_NAME, fixture.getAttributeName(quark));
367
368 quark = fixture.optQuarkRelative(threadsQuark, "1432", "absent");
369 assertEquals(INVALID_ATTRIBUTE, quark);
370
371 quark = fixture.optQuarkRelative(threadsQuark, "absent", Attributes.EXEC_NAME);
372 assertEquals(INVALID_ATTRIBUTE, quark);
373 }
374
375 @Test
376 public void testFullAttributeName() {
377 try {
378 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
379 String name = fixture.getFullAttributePath(quark);
380 assertEquals(name, "CPUs/0/Current_thread");
381
382 } catch (AttributeNotFoundException e) {
383 fail();
384 }
385 }
386
387 @Test
388 public void testGetQuarks_begin() {
389 List<Integer> list = fixture.getQuarks("*", "1577", Attributes.EXEC_NAME);
390
391 assertEquals(1, list.size());
392 }
393
394 @Test
395 public void testGetQuarks_middle() {
396 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
397
398 /* Number of different kernel threads in the trace */
399 assertEquals(168, list.size());
400 }
401
402 @Test
403 public void testGetQuarks_end() {
404 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "1577", "*");
405
406 /* There should be 5 sub-attributes for each Thread node */
407 assertEquals(5, list.size());
408 }
409
410 @Test
411 public void testGetQuarksNoMatch() {
412 List<Integer> list = fixture.getQuarks("invalid");
413 assertEquals(0, list.size());
414
415 list = fixture.getQuarks("*", "invalid");
416 assertEquals(0, list.size());
417
418 list = fixture.getQuarks("invalid", "*");
419 assertEquals(0, list.size());
420
421 list = fixture.getQuarks(Attributes.THREADS, "*", "invalid");
422 assertEquals(0, list.size());
423 }
424
425 // ------------------------------------------------------------------------
426 // Tests verifying the *complete* results of a full queries
427 // ------------------------------------------------------------------------
428
429 protected long getStartTimes(int idx) {
430 return TestValues.startTimes[idx];
431 }
432
433 protected long getEndTimes(int idx) {
434 return TestValues.endTimes[idx];
435 }
436
437 protected ITmfStateValue getStateValues(int idx) {
438 return TestValues.values[idx];
439 }
440
441 @Test
442 public void testFullQueryThorough() {
443 try {
444 List<ITmfStateInterval> state = fixture.queryFullState(interestingTimestamp1);
445 assertEquals(TestValues.size, state.size());
446
447 for (int i = 0; i < state.size(); i++) {
448 /* Test each component of the intervals */
449 assertEquals(getStartTimes(i), state.get(i).getStartTime());
450 assertEquals(getEndTimes(i), state.get(i).getEndTime());
451 assertEquals(i, state.get(i).getAttribute());
452 assertEquals(getStateValues(i), state.get(i).getStateValue());
453 }
454
455 } catch (StateSystemDisposedException e) {
456 fail();
457 }
458 }
459
460 @Test
461 public void testFirstIntervalIsConsidered() {
462 try {
463 int quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1397", Attributes.STATUS);
464 List<ITmfStateInterval> list = fixture.queryFullState(1331668248014135800L);
465 ITmfStateInterval interval = list.get(quark);
466 assertEquals(1331668247516664825L, interval.getStartTime());
467
468 int valueInt = interval.getStateValue().unboxInt();
469 assertEquals(1, valueInt);
470
471 } catch (StateSystemDisposedException | AttributeNotFoundException e) {
472 fail();
473 }
474 }
475
476 @Test
477 public void testParentAttribute() {
478 String[] path = { "CPUs/0/Current_thread",
479 "CPUs/0",
480 "CPUs" };
481 try {
482 int q = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
483 for (int i = 0; i < path.length; i++) {
484 String name = fixture.getFullAttributePath(q);
485 assertEquals(path[i], name);
486 q = fixture.getParentAttributeQuark(q);
487 }
488 assertEquals(ROOT_ATTRIBUTE, q);
489 q = fixture.getParentAttributeQuark(q);
490 assertEquals(ROOT_ATTRIBUTE, q);
491 } catch (AttributeNotFoundException e) {
492 fail();
493 }
494 }
495
496 }
This page took 0.051493 seconds and 5 git commands to generate.