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
CommitLineData
f9a76cac 1/*******************************************************************************
c44f0a0c 2 * Copyright (c) 2012, 2016 Ericsson
f9a76cac
AM
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
42d5b5f2 13package org.eclipse.tracecompass.lttng2.kernel.core.tests.analysis.kernel.statesystem;
f9a76cac 14
c44f0a0c
PT
15import static org.eclipse.tracecompass.statesystem.core.ITmfStateSystem.INVALID_ATTRIBUTE;
16import static org.eclipse.tracecompass.statesystem.core.ITmfStateSystem.ROOT_ATTRIBUTE;
f9a76cac 17import static org.junit.Assert.assertEquals;
c44f0a0c 18import static org.junit.Assert.assertNotEquals;
edbdb7ea 19import static org.junit.Assert.assertNotNull;
c4d139aa 20import static org.junit.Assert.fail;
f9a76cac
AM
21
22import java.util.List;
d291a715 23import java.util.concurrent.TimeUnit;
f9a76cac 24
c4d57ac1 25import org.eclipse.jdt.annotation.NonNull;
e363eae1 26import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.Attributes;
e894a508 27import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
1dd75589 28import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
e894a508
AM
29import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
30import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
31import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
32import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
33import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
34import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
c4d57ac1 35import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
edbdb7ea
AM
36import org.junit.Before;
37import org.junit.Rule;
f9a76cac 38import org.junit.Test;
edbdb7ea
AM
39import org.junit.rules.TestRule;
40import org.junit.rules.Timeout;
f9a76cac
AM
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
f9a76cac 47 */
4e0b52e0 48@SuppressWarnings("javadoc")
f9a76cac
AM
49public abstract class StateSystemTest {
50
edbdb7ea
AM
51 /** Timeout the tests after 2 minutes */
52 @Rule
d291a715 53 public TestRule timeoutRule = new Timeout(2, TimeUnit.MINUTES);
edbdb7ea 54
9ac63b5b 55 /** Test trace used for these tests */
c4d57ac1 56 protected static final @NonNull CtfTestTrace testTrace = CtfTestTrace.TRACE2;
92ba8466
AM
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
edbdb7ea
AM
64 /** Offset in the trace + start time of the trace */
65 protected static final long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
66
92ba8466
AM
67 /** Number of nanoseconds in one second */
68 private static final long NANOSECS_PER_SEC = 1000000000L;
69
392bf486 70 protected static ITmfStateSystem fixture;
f9a76cac 71
58ba027e 72 /**
392bf486 73 * Test set-up
58ba027e 74 */
edbdb7ea
AM
75 @Before
76 public void setUp() {
392bf486 77 /* Subclasses should set-up 'fixture' */
edbdb7ea
AM
78 assertNotNull(fixture);
79 }
80
f9a76cac 81 @Test
c4d139aa 82 public void testFullQuery1() {
f9a76cac
AM
83 List<ITmfStateInterval> list;
84 ITmfStateInterval interval;
85 int quark, valueInt;
86 String valueStr;
87
c4d139aa 88 try {
edbdb7ea 89 list = fixture.queryFullState(interestingTimestamp1);
c4d139aa 90
edbdb7ea 91 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa
AM
92 interval = list.get(quark);
93 valueInt = interval.getStateValue().unboxInt();
94 assertEquals(1397, valueInt);
95
edbdb7ea 96 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
c4d139aa
AM
97 interval = list.get(quark);
98 valueStr = interval.getStateValue().unboxStr();
99 assertEquals("gdbus", valueStr);
100
edbdb7ea 101 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
c4d139aa
AM
102 interval = list.get(quark);
103 valueStr = interval.getStateValue().unboxStr();
7411cd67 104 assertEquals("sys_poll", valueStr);
c4d139aa 105
edbdb7ea 106 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
107 fail();
108 }
f9a76cac
AM
109 }
110
111 @Test
c4d139aa 112 public void testSingleQuery1() {
f9a76cac
AM
113 long timestamp = interestingTimestamp1;
114 int quark;
115 ITmfStateInterval interval;
116 String valueStr;
117
c4d139aa 118 try {
edbdb7ea
AM
119 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
120 interval = fixture.querySingleState(timestamp, quark);
c4d139aa
AM
121 valueStr = interval.getStateValue().unboxStr();
122 assertEquals("gdbus", valueStr);
123
edbdb7ea 124 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
125 fail();
126 }
f9a76cac
AM
127 }
128
129 /**
130 * Test a range query (with no resolution parameter, so all intervals)
131 */
132 @Test
c4d139aa 133 public void testRangeQuery1() {
f9a76cac 134 long time1 = interestingTimestamp1;
92ba8466 135 long time2 = time1 + 1L * NANOSECS_PER_SEC;
f9a76cac
AM
136 int quark;
137 List<ITmfStateInterval> intervals;
138
1dd75589
AM
139 final ITmfStateSystem ss = fixture;
140 assertNotNull(ss);
141
c4d139aa 142 try {
1dd75589
AM
143 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
144 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2);
c4d139aa
AM
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
edbdb7ea 149 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
150 fail();
151 }
f9a76cac
AM
152 }
153
154 /**
b9f6183a
AM
155 * Range query, but with a t2 far off the end of the trace. The result
156 * should still be valid.
f9a76cac
AM
157 */
158 @Test
c4d139aa 159 public void testRangeQuery2() {
f9a76cac
AM
160 List<ITmfStateInterval> intervals;
161
1dd75589
AM
162 final ITmfStateSystem ss = fixture;
163 assertNotNull(ss);
164
c4d139aa 165 try {
19ed6598 166 int quark = ss.getQuarkAbsolute(Attributes.CPUS, Integer.toString(0), Attributes.IRQS, "1");
1dd75589 167 long ts1 = ss.getStartTime(); /* start of the trace */
c4d139aa
AM
168 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */
169
1dd75589 170 intervals = StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
f9a76cac 171
c4d139aa
AM
172 /* Activity of IRQ 1 over the whole trace */
173 assertEquals(65, intervals.size());
f9a76cac 174
edbdb7ea 175 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
176 fail();
177 }
f9a76cac
AM
178 }
179
180 /**
181 * Test a range query with a resolution
182 */
183 @Test
c4d139aa 184 public void testRangeQuery3() {
f9a76cac 185 long time1 = interestingTimestamp1;
92ba8466 186 long time2 = time1 + 1L * NANOSECS_PER_SEC;
f9a76cac
AM
187 long resolution = 1000000; /* One query every millisecond */
188 int quark;
189 List<ITmfStateInterval> intervals;
190
1dd75589
AM
191 final ITmfStateSystem ss = fixture;
192 assertNotNull(ss);
193
c4d139aa 194 try {
1dd75589
AM
195 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
196 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2, resolution, null);
c4d139aa
AM
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
edbdb7ea 201 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
202 fail();
203 }
f9a76cac
AM
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 {
92ba8466 212 long ts = startTime + 20L * NANOSECS_PER_SEC;
edbdb7ea 213 fixture.queryFullState(ts);
f9a76cac
AM
214 }
215
216 @Test(expected = TimeRangeException.class)
217 public void testFullQueryInvalidTime2() throws TimeRangeException,
218 StateSystemDisposedException {
92ba8466 219 long ts = startTime - 20L * NANOSECS_PER_SEC;
edbdb7ea 220 fixture.queryFullState(ts);
f9a76cac
AM
221 }
222
223 @Test(expected = TimeRangeException.class)
c4d139aa
AM
224 public void testSingleQueryInvalidTime1() throws TimeRangeException {
225 try {
edbdb7ea 226 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa 227 long ts = startTime + 20L * NANOSECS_PER_SEC;
edbdb7ea 228 fixture.querySingleState(ts, quark);
c4d139aa 229
edbdb7ea 230 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
231 fail();
232 }
f9a76cac
AM
233 }
234
235 @Test(expected = TimeRangeException.class)
c4d139aa
AM
236 public void testSingleQueryInvalidTime2() throws TimeRangeException {
237 try {
edbdb7ea 238 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa 239 long ts = startTime - 20L * NANOSECS_PER_SEC;
edbdb7ea 240 fixture.querySingleState(ts, quark);
c4d139aa 241
edbdb7ea 242 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
243 fail();
244 }
f9a76cac
AM
245 }
246
247 @Test(expected = TimeRangeException.class)
c4d139aa 248 public void testRangeQueryInvalidTime1() throws TimeRangeException {
1dd75589
AM
249 final ITmfStateSystem ss = fixture;
250 assertNotNull(ss);
251
c4d139aa 252 try {
1dd75589 253 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa
AM
254 long ts1 = startTime - 20L * NANOSECS_PER_SEC; /* invalid */
255 long ts2 = startTime + 1L * NANOSECS_PER_SEC; /* valid */
1dd75589 256 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
c4d139aa
AM
257
258 } catch (AttributeNotFoundException e) {
259 fail();
260 } catch (StateSystemDisposedException e) {
261 fail();
262 }
f9a76cac
AM
263 }
264
265 @Test(expected = TimeRangeException.class)
c4d139aa 266 public void testRangeQueryInvalidTime2() throws TimeRangeException {
1dd75589
AM
267 final ITmfStateSystem ss = fixture;
268 assertNotNull(ss);
269
c4d139aa 270 try {
1dd75589 271 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa
AM
272 long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */
273 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */
1dd75589 274 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
c4d139aa 275
edbdb7ea 276 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
277 fail();
278 }
f9a76cac
AM
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 {
edbdb7ea 288 fixture.getQuarkAbsolute("There", "is", "no", "cow", "level");
f9a76cac
AM
289 }
290
291 /**
292 * Query but with the wrong State Value type
293 */
294 @Test(expected = StateValueTypeException.class)
c4d139aa 295 public void testQueryInvalidValuetype1() throws StateValueTypeException {
f9a76cac
AM
296 List<ITmfStateInterval> list;
297 ITmfStateInterval interval;
298 int quark;
299
c4d139aa 300 try {
edbdb7ea
AM
301 list = fixture.queryFullState(interestingTimestamp1);
302 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
c4d139aa
AM
303 interval = list.get(quark);
304
305 /* This is supposed to be an int value */
306 interval.getStateValue().unboxStr();
307
edbdb7ea 308 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
309 fail();
310 }
f9a76cac
AM
311 }
312
313 @Test(expected = StateValueTypeException.class)
c4d139aa 314 public void testQueryInvalidValuetype2() throws StateValueTypeException {
f9a76cac
AM
315 List<ITmfStateInterval> list;
316 ITmfStateInterval interval;
317 int quark;
318
c4d139aa 319 try {
edbdb7ea
AM
320 list = fixture.queryFullState(interestingTimestamp1);
321 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
c4d139aa
AM
322 interval = list.get(quark);
323
324 /* This is supposed to be a String value */
325 interval.getStateValue().unboxInt();
326
edbdb7ea 327 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
c4d139aa
AM
328 fail();
329 }
f9a76cac
AM
330 }
331
c44f0a0c
PT
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
f9a76cac 375 @Test
c4d139aa
AM
376 public void testFullAttributeName() {
377 try {
edbdb7ea
AM
378 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
379 String name = fixture.getFullAttributePath(quark);
c4d139aa
AM
380 assertEquals(name, "CPUs/0/Current_thread");
381
382 } catch (AttributeNotFoundException e) {
383 fail();
384 }
f9a76cac
AM
385 }
386
387 @Test
388 public void testGetQuarks_begin() {
edbdb7ea 389 List<Integer> list = fixture.getQuarks("*", "1577", Attributes.EXEC_NAME);
f9a76cac
AM
390
391 assertEquals(1, list.size());
392 }
393
394 @Test
395 public void testGetQuarks_middle() {
edbdb7ea 396 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
f9a76cac
AM
397
398 /* Number of different kernel threads in the trace */
399 assertEquals(168, list.size());
400 }
401
402 @Test
403 public void testGetQuarks_end() {
edbdb7ea 404 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "1577", "*");
f9a76cac 405
3bf563da
CM
406 /* There should be 5 sub-attributes for each Thread node */
407 assertEquals(5, list.size());
f9a76cac 408 }
b33f7554 409
c44f0a0c
PT
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
b33f7554
AM
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 {
edbdb7ea 444 List<ITmfStateInterval> state = fixture.queryFullState(interestingTimestamp1);
b33f7554
AM
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
b33f7554
AM
455 } catch (StateSystemDisposedException e) {
456 fail();
457 }
458 }
b9f6183a
AM
459
460 @Test
461 public void testFirstIntervalIsConsidered() {
462 try {
07ec1591 463 int quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1397", Attributes.STATUS);
edbdb7ea 464 List<ITmfStateInterval> list = fixture.queryFullState(1331668248014135800L);
07ec1591 465 ITmfStateInterval interval = list.get(quark);
b9f6183a
AM
466 assertEquals(1331668247516664825L, interval.getStartTime());
467
468 int valueInt = interval.getStateValue().unboxInt();
469 assertEquals(1, valueInt);
edbdb7ea 470
07ec1591 471 } catch (StateSystemDisposedException | AttributeNotFoundException e) {
b9f6183a 472 fail();
b9f6183a
AM
473 }
474 }
0fdd2c45
FG
475
476 @Test
477 public void testParentAttribute() {
478 String[] path = { "CPUs/0/Current_thread",
479 "CPUs/0",
480 "CPUs" };
481 try {
edbdb7ea 482 int q = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
0fdd2c45 483 for (int i = 0; i < path.length; i++) {
edbdb7ea 484 String name = fixture.getFullAttributePath(q);
0fdd2c45 485 assertEquals(path[i], name);
edbdb7ea 486 q = fixture.getParentAttributeQuark(q);
0fdd2c45 487 }
c44f0a0c 488 assertEquals(ROOT_ATTRIBUTE, q);
edbdb7ea 489 q = fixture.getParentAttributeQuark(q);
c44f0a0c 490 assertEquals(ROOT_ATTRIBUTE, q);
0fdd2c45
FG
491 } catch (AttributeNotFoundException e) {
492 fail();
493 }
494 }
495
f9a76cac 496}
This page took 0.13651 seconds and 5 git commands to generate.