KernelAnalysis: Use Threads CoreAttributes to store "Status"
[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.Arrays;
23 import java.util.List;
24 import java.util.concurrent.TimeUnit;
25
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
28 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
29 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
30 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
31 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
32 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
33 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
34 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
35 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
36 import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.TestRule;
41 import org.junit.rules.Timeout;
42
43 /**
44 * Base unit tests for the StateHistorySystem. Extension can be made to test
45 * different state back-end types or configurations.
46 *
47 * @author Alexandre Montplaisir
48 */
49 @SuppressWarnings("javadoc")
50 public abstract class StateSystemTest {
51
52 /** Timeout the tests after 2 minutes */
53 @Rule
54 public TestRule timeoutRule = new Timeout(2, TimeUnit.MINUTES);
55
56 /** Test trace used for these tests */
57 protected static final @NonNull CtfTestTrace testTrace = CtfTestTrace.TRACE2;
58
59 /** Expected start time of the test trace/state history */
60 protected static final long startTime = 1331668247314038062L;
61
62 /** Expected end time of the state history built from the test trace */
63 protected static final long endTime = 1331668259054285979L;
64
65 /** Offset in the trace + start time of the trace */
66 protected static final long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
67
68 /** Number of nanoseconds in one second */
69 private static final long NANOSECS_PER_SEC = 1000000000L;
70
71 protected static ITmfStateSystem fixture;
72
73 /**
74 * Test set-up
75 */
76 @Before
77 public void setUp() {
78 /* Subclasses should set-up 'fixture' */
79 assertNotNull(fixture);
80 }
81
82 @Test
83 public void testFullQuery1() {
84 List<ITmfStateInterval> list;
85 ITmfStateInterval interval;
86 int quark, valueInt;
87 String valueStr;
88
89 try {
90 list = fixture.queryFullState(interestingTimestamp1);
91
92 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
93 interval = list.get(quark);
94 valueInt = interval.getStateValue().unboxInt();
95 assertEquals(1397, valueInt);
96
97 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
98 interval = list.get(quark);
99 valueStr = interval.getStateValue().unboxStr();
100 assertEquals("gdbus", valueStr);
101
102 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
103 interval = list.get(quark);
104 valueStr = interval.getStateValue().unboxStr();
105 assertEquals("sys_poll", valueStr);
106
107 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
108 fail();
109 }
110 }
111
112 @Test
113 public void testSingleQuery1() {
114 long timestamp = interestingTimestamp1;
115 int quark;
116 ITmfStateInterval interval;
117 String valueStr;
118
119 try {
120 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
121 interval = fixture.querySingleState(timestamp, quark);
122 valueStr = interval.getStateValue().unboxStr();
123 assertEquals("gdbus", valueStr);
124
125 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
126 fail();
127 }
128 }
129
130 /**
131 * Test a range query (with no resolution parameter, so all intervals)
132 */
133 @Test
134 public void testRangeQuery1() {
135 long time1 = interestingTimestamp1;
136 long time2 = time1 + 1L * NANOSECS_PER_SEC;
137 int quark;
138 List<ITmfStateInterval> intervals;
139
140 final ITmfStateSystem ss = fixture;
141 assertNotNull(ss);
142
143 try {
144 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
145 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2);
146 assertEquals(487, intervals.size()); /* Number of context switches! */
147 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
148 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
149
150 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
151 fail();
152 }
153 }
154
155 /**
156 * Range query, but with a t2 far off the end of the trace. The result
157 * should still be valid.
158 */
159 @Test
160 public void testRangeQuery2() {
161 List<ITmfStateInterval> intervals;
162
163 final ITmfStateSystem ss = fixture;
164 assertNotNull(ss);
165
166 try {
167 int quark = ss.getQuarkAbsolute(Attributes.CPUS, Integer.toString(0), Attributes.IRQS, "1");
168 long ts1 = ss.getStartTime(); /* start of the trace */
169 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid, but ignored */
170
171 intervals = StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
172
173 /* Activity of IRQ 1 over the whole trace */
174 assertEquals(65, intervals.size());
175
176 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
177 fail();
178 }
179 }
180
181 /**
182 * Test a range query with a resolution
183 */
184 @Test
185 public void testRangeQuery3() {
186 long time1 = interestingTimestamp1;
187 long time2 = time1 + 1L * NANOSECS_PER_SEC;
188 long resolution = 1000000; /* One query every millisecond */
189 int quark;
190 List<ITmfStateInterval> intervals;
191
192 final ITmfStateSystem ss = fixture;
193 assertNotNull(ss);
194
195 try {
196 quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
197 intervals = StateSystemUtils.queryHistoryRange(ss, quark, time1, time2, resolution, null);
198 assertEquals(126, intervals.size()); /* Number of context switches! */
199 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
200 assertEquals(1331668248815698779L, intervals.get(100).getEndTime());
201
202 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
203 fail();
204 }
205 }
206
207 /**
208 * Ask for a time range outside of the trace's range
209 */
210 @Test(expected = TimeRangeException.class)
211 public void testFullQueryInvalidTime1() throws TimeRangeException,
212 StateSystemDisposedException {
213 long ts = startTime + 20L * NANOSECS_PER_SEC;
214 fixture.queryFullState(ts);
215 }
216
217 @Test(expected = TimeRangeException.class)
218 public void testFullQueryInvalidTime2() throws TimeRangeException,
219 StateSystemDisposedException {
220 long ts = startTime - 20L * NANOSECS_PER_SEC;
221 fixture.queryFullState(ts);
222 }
223
224 @Test(expected = TimeRangeException.class)
225 public void testSingleQueryInvalidTime1() throws TimeRangeException {
226 try {
227 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
228 long ts = startTime + 20L * NANOSECS_PER_SEC;
229 fixture.querySingleState(ts, quark);
230
231 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
232 fail();
233 }
234 }
235
236 @Test(expected = TimeRangeException.class)
237 public void testSingleQueryInvalidTime2() throws TimeRangeException {
238 try {
239 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
240 long ts = startTime - 20L * NANOSECS_PER_SEC;
241 fixture.querySingleState(ts, quark);
242
243 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
244 fail();
245 }
246 }
247
248 @Test(expected = TimeRangeException.class)
249 public void testRangeQueryInvalidTime1() throws TimeRangeException {
250 final ITmfStateSystem ss = fixture;
251 assertNotNull(ss);
252
253 try {
254 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
255 long ts1 = startTime - 20L * NANOSECS_PER_SEC; /* invalid */
256 long ts2 = startTime + 1L * NANOSECS_PER_SEC; /* valid */
257 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
258
259 } catch (AttributeNotFoundException e) {
260 fail();
261 } catch (StateSystemDisposedException e) {
262 fail();
263 }
264 }
265
266 @Test(expected = TimeRangeException.class)
267 public void testRangeQueryInvalidTime2() throws TimeRangeException {
268 final ITmfStateSystem ss = fixture;
269 assertNotNull(ss);
270
271 try {
272 int quark = ss.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
273 long ts1 = startTime - 1L * NANOSECS_PER_SEC; /* invalid */
274 long ts2 = startTime + 20L * NANOSECS_PER_SEC; /* invalid */
275 StateSystemUtils.queryHistoryRange(ss, quark, ts1, ts2);
276
277 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
278 fail();
279 }
280 }
281
282 /**
283 * Ask for a non-existing attribute
284 *
285 * @throws AttributeNotFoundException
286 */
287 @Test(expected = AttributeNotFoundException.class)
288 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
289 fixture.getQuarkAbsolute("There", "is", "no", "cow", "level");
290 }
291
292 /**
293 * Query but with the wrong State Value type
294 */
295 @Test(expected = StateValueTypeException.class)
296 public void testQueryInvalidValuetype1() throws StateValueTypeException {
297 List<ITmfStateInterval> list;
298 ITmfStateInterval interval;
299 int quark;
300
301 try {
302 list = fixture.queryFullState(interestingTimestamp1);
303 quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
304 interval = list.get(quark);
305
306 /* This is supposed to be an int value */
307 interval.getStateValue().unboxStr();
308
309 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
310 fail();
311 }
312 }
313
314 @Test(expected = StateValueTypeException.class)
315 public void testQueryInvalidValuetype2() throws StateValueTypeException {
316 List<ITmfStateInterval> list;
317 ITmfStateInterval interval;
318 int quark;
319
320 try {
321 list = fixture.queryFullState(interestingTimestamp1);
322 quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
323 interval = list.get(quark);
324
325 /* This is supposed to be a String value */
326 interval.getStateValue().unboxInt();
327
328 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
329 fail();
330 }
331 }
332
333 @Test
334 public void testOptQuarkAbsolute() {
335 int quark = fixture.optQuarkAbsolute();
336 assertEquals(ROOT_ATTRIBUTE, quark);
337
338 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
339 assertNotEquals(INVALID_ATTRIBUTE, quark);
340 assertEquals(Attributes.EXEC_NAME, fixture.getAttributeName(quark));
341
342 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "1432", "absent");
343 assertEquals(INVALID_ATTRIBUTE, quark);
344
345 quark = fixture.optQuarkAbsolute(Attributes.THREADS, "absent", Attributes.EXEC_NAME);
346 assertEquals(INVALID_ATTRIBUTE, quark);
347
348 quark = fixture.optQuarkAbsolute("absent", "1432", Attributes.EXEC_NAME);
349 assertEquals(INVALID_ATTRIBUTE, quark);
350 }
351
352 @Test
353 public void testOptQuarkRelative() {
354 int threadsQuark = INVALID_ATTRIBUTE;
355 try {
356 threadsQuark = fixture.getQuarkAbsolute(Attributes.THREADS);
357 } catch (AttributeNotFoundException e) {
358 fail();
359 }
360 assertNotEquals(INVALID_ATTRIBUTE, threadsQuark);
361
362 int quark = fixture.optQuarkRelative(threadsQuark);
363 assertEquals(threadsQuark, quark);
364
365 quark = fixture.optQuarkRelative(threadsQuark, "1432", Attributes.EXEC_NAME);
366 assertNotEquals(INVALID_ATTRIBUTE, quark);
367 assertEquals(Attributes.EXEC_NAME, fixture.getAttributeName(quark));
368
369 quark = fixture.optQuarkRelative(threadsQuark, "1432", "absent");
370 assertEquals(INVALID_ATTRIBUTE, quark);
371
372 quark = fixture.optQuarkRelative(threadsQuark, "absent", Attributes.EXEC_NAME);
373 assertEquals(INVALID_ATTRIBUTE, quark);
374 }
375
376 @Test
377 public void testFullAttributeName() {
378 try {
379 int quark = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
380 String name = fixture.getFullAttributePath(quark);
381 assertEquals(name, "CPUs/0/Current_thread");
382
383 } catch (AttributeNotFoundException e) {
384 fail();
385 }
386 }
387
388 @Test
389 public void testGetQuarks_begin() {
390 List<Integer> list = fixture.getQuarks("*", "1577", Attributes.EXEC_NAME);
391
392 assertEquals(1, list.size());
393 }
394
395 @Test
396 public void testGetQuarks_middle() {
397 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
398
399 /* Number of different kernel threads in the trace */
400 assertEquals(169, list.size());
401 }
402
403 @Test
404 public void testGetQuarks_end() {
405 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "1577", "*");
406
407 /* There should be 4 sub-attributes for each Thread node */
408 assertEquals(4, list.size());
409 }
410
411 @Test
412 public void testGetQuarks_middle_end() {
413 List<Integer> list = fixture.getQuarks(Attributes.THREADS, "*", "*");
414
415 /* There should be 169 threads and 4 sub-attributes per thread */
416 assertEquals(169 * 4, list.size());
417 }
418
419 @Test
420 public void testGetQuarks_empty() {
421 List<Integer> list = fixture.getQuarks();
422
423 assertEquals(Arrays.asList(ITmfStateSystem.ROOT_ATTRIBUTE), list);
424 }
425
426 @Test
427 public void testGetQuarks_relative() {
428 int threadsQuark = INVALID_ATTRIBUTE;
429 try {
430 threadsQuark = fixture.getQuarkAbsolute(Attributes.THREADS);
431 } catch (AttributeNotFoundException e) {
432 fail();
433 }
434 assertNotEquals(INVALID_ATTRIBUTE, threadsQuark);
435
436 List<Integer> list = fixture.getQuarks(threadsQuark, "*", Attributes.EXEC_NAME);
437
438 /* Number of different kernel threads in the trace */
439 assertEquals(169, list.size());
440 }
441
442 @Test
443 public void testGetQuarks_relative_up_wildcard() {
444 int threadsQuark = INVALID_ATTRIBUTE;
445 try {
446 threadsQuark = fixture.getQuarkAbsolute(Attributes.THREADS);
447 } catch (AttributeNotFoundException e) {
448 fail();
449 }
450 assertNotEquals(INVALID_ATTRIBUTE, threadsQuark);
451
452 List<Integer> list = fixture.getQuarks(threadsQuark, "..", Attributes.CPUS, "*");
453
454 /* There should be 2 CPUs */
455 assertEquals(2, list.size());
456 }
457
458 @Test
459 public void testGetQuarks_relative_empty() {
460 int threadsQuark = INVALID_ATTRIBUTE;
461 try {
462 threadsQuark = fixture.getQuarkAbsolute(Attributes.THREADS);
463 } catch (AttributeNotFoundException e) {
464 fail();
465 }
466 assertNotEquals(INVALID_ATTRIBUTE, threadsQuark);
467
468 List<Integer> list = fixture.getQuarks(threadsQuark, new String[0]);
469 assertEquals(Arrays.asList(threadsQuark), list);
470
471 list = fixture.getQuarks(threadsQuark);
472 assertEquals(Arrays.asList(threadsQuark), list);
473 }
474
475 @Test
476 public void testGetQuarksNoMatch() {
477 List<Integer> list = fixture.getQuarks("invalid");
478 assertEquals(0, list.size());
479
480 list = fixture.getQuarks("*", "invalid");
481 assertEquals(0, list.size());
482
483 list = fixture.getQuarks("invalid", "*");
484 assertEquals(0, list.size());
485
486 list = fixture.getQuarks(Attributes.THREADS, "*", "invalid");
487 assertEquals(0, list.size());
488 }
489
490 // ------------------------------------------------------------------------
491 // Tests verifying the *complete* results of a full queries
492 // ------------------------------------------------------------------------
493
494 protected long getStartTimes(int idx) {
495 return TestValues.startTimes[idx];
496 }
497
498 protected long getEndTimes(int idx) {
499 return TestValues.endTimes[idx];
500 }
501
502 protected ITmfStateValue getStateValues(int idx) {
503 return TestValues.values[idx];
504 }
505
506 @Test
507 public void testFullQueryThorough() {
508 try {
509 List<ITmfStateInterval> state = fixture.queryFullState(interestingTimestamp1);
510 assertEquals(TestValues.size, state.size());
511
512 for (int i = 0; i < state.size(); i++) {
513 /* Test each component of the intervals */
514 assertEquals(getStartTimes(i), state.get(i).getStartTime());
515 assertEquals(getEndTimes(i), state.get(i).getEndTime());
516 assertEquals(i, state.get(i).getAttribute());
517 assertEquals(getStateValues(i), state.get(i).getStateValue());
518 }
519
520 } catch (StateSystemDisposedException e) {
521 fail();
522 }
523 }
524
525 @Test
526 public void testFirstIntervalIsConsidered() {
527 try {
528 int quark = fixture.getQuarkAbsolute(Attributes.THREADS, "1397");
529 List<ITmfStateInterval> list = fixture.queryFullState(1331668248014135800L);
530 ITmfStateInterval interval = list.get(quark);
531 assertEquals(1331668247516664825L, interval.getStartTime());
532
533 int valueInt = interval.getStateValue().unboxInt();
534 assertEquals(1, valueInt);
535
536 } catch (StateSystemDisposedException | AttributeNotFoundException e) {
537 fail();
538 }
539 }
540
541 @Test
542 public void testParentAttribute() {
543 String[] path = { "CPUs/0/Current_thread",
544 "CPUs/0",
545 "CPUs" };
546 try {
547 int q = fixture.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
548 for (int i = 0; i < path.length; i++) {
549 String name = fixture.getFullAttributePath(q);
550 assertEquals(path[i], name);
551 q = fixture.getParentAttributeQuark(q);
552 }
553 assertEquals(ROOT_ATTRIBUTE, q);
554 q = fixture.getParentAttributeQuark(q);
555 assertEquals(ROOT_ATTRIBUTE, q);
556 } catch (AttributeNotFoundException e) {
557 fail();
558 }
559 }
560
561 }
This page took 0.04851 seconds and 5 git commands to generate.