tmf: Add an ID to each state system that gets built
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemFullHistoryTest.java
CommitLineData
efc403bb
AM
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
e743c3b8 5 *
efc403bb
AM
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
e743c3b8 10 *
efc403bb
AM
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
14
6e71ce46
AM
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertTrue;
efc403bb
AM
18
19import java.io.File;
efc403bb
AM
20import java.util.List;
21
6e71ce46
AM
22import org.eclipse.linuxtools.internal.lttng2.kernel.core.Attributes;
23import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CtfKernelStateInput;
6d08acca
AM
24import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
25import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
26import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
1e4bb526 27import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
efc403bb 28import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
18ab1d18 29import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
f1f86dfb 30import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
6e71ce46
AM
31import org.eclipse.linuxtools.tmf.core.statesystem.StateSystemManager;
32import org.junit.AfterClass;
33import org.junit.BeforeClass;
34import org.junit.Test;
efc403bb
AM
35
36/**
37 * Unit tests for the StateHistorySystem, which uses a full (non-partial)
38 * history and the non-threaded CTF kernel handler.
e743c3b8 39 *
efc403bb 40 * @author alexmont
e743c3b8 41 *
efc403bb 42 */
e743c3b8 43@SuppressWarnings({"nls", "javadoc"})
efc403bb
AM
44public class StateSystemFullHistoryTest {
45
ebd67b34
AM
46 static File stateFile;
47 static File stateFileBenchmark;
efc403bb 48
ebd67b34 49 static IStateChangeInput input;
f1f86dfb 50 static ITmfStateSystem ssq;
efc403bb 51
cc2292bd
AM
52 /* Offset in the trace + start time of the trace */
53 private final static long interestingTimestamp1 = 18670067372290L + 1331649577946812237L;
2359ecca 54
a76f1067
AM
55 /* ID we give to the state system we build */
56 private static final String STATE_ID = "test-ss";
57
efc403bb
AM
58 protected static String getTestFileName() {
59 return "/tmp/statefile.ht"; //$NON-NLS-1$
60 }
61
62 @BeforeClass
63 public static void initialize() {
64 stateFile = new File(getTestFileName());
65 stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
66 try {
dc0f7bfe 67 input = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
a76f1067 68 ssq = StateSystemManager.loadStateHistory(stateFile, input, STATE_ID, true);
efc403bb
AM
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
efc403bb
AM
72 }
73
74 @AfterClass
75 public static void cleanup() {
ebd67b34
AM
76 boolean ret1, ret2;
77 ret1 = stateFile.delete();
78 ret2 = stateFileBenchmark.delete();
79 if ( !(ret1 && ret2) ) {
6b78e55b
AM
80 System.err.println("Error cleaning up during unit testing, " + //$NON-NLS-1$
81 "you might have leftovers state history files in /tmp"); //$NON-NLS-1$
ebd67b34 82 }
efc403bb
AM
83 }
84
85 /**
86 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
87 * us to @Test the @BeforeClass...
e743c3b8
AM
88 *
89 * @throws IOException
90 * @throws TmfTraceException
efc403bb
AM
91 */
92 @Test
51e216bd 93 public void testBuild() throws TmfTraceException {
6e71ce46 94 IStateChangeInput input2;
f1f86dfb 95 ITmfStateSystem ssb2;
e743c3b8 96
6e71ce46 97 input2 = new CtfKernelStateInput(CtfTestFiles.getTestTrace());
a76f1067 98 ssb2 = StateSystemManager.loadStateHistory(stateFileBenchmark, input2, STATE_ID, true);
1e4bb526 99
6e71ce46
AM
100 assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
101 assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
efc403bb
AM
102 }
103
104 @Test
51e216bd 105 public void testOpenExistingStateFile() throws TmfTraceException {
f1f86dfb 106 ITmfStateSystem ssb2;
1e4bb526
AM
107
108 /* 'newStateFile' should have already been created */
a76f1067 109 ssb2 = StateSystemManager.loadStateHistory(stateFile, null, STATE_ID, true);
1e4bb526 110
d26f90fd 111 assertNotNull(ssb2);
6e71ce46
AM
112 assertEquals(CtfTestFiles.startTime, ssb2.getStartTime());
113 assertEquals(CtfTestFiles.endTime, ssb2.getCurrentEndTime());
efc403bb
AM
114 }
115
116 @Test
117 public void testFullQuery1() throws StateValueTypeException,
118 AttributeNotFoundException, TimeRangeException {
119
dad01d27 120 List<ITmfStateInterval> list;
efc403bb 121 ITmfStateInterval interval;
4b3ed6ff 122 int quark, valueInt;
efc403bb
AM
123 String valueStr;
124
6e71ce46 125 list = ssq.queryFullState(interestingTimestamp1);
efc403bb 126
6e71ce46 127 quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dad01d27 128 interval = list.get(quark);
efc403bb 129 valueInt = interval.getStateValue().unboxInt();
2359ecca 130 assertEquals(1397, valueInt);
efc403bb 131
6e71ce46 132 quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
dad01d27 133 interval = list.get(quark);
efc403bb 134 valueStr = interval.getStateValue().unboxStr();
2359ecca 135 assertEquals("gdbus", valueStr);
efc403bb 136
6e71ce46 137 quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
ab9bc51b 138 interval = list.get(quark);
ae54340d
AM
139 valueStr = interval.getStateValue().unboxStr();
140 assertTrue(valueStr.equals("sys_poll"));
efc403bb
AM
141 }
142
143 @Test
144 public void testFullQuery2() {
145 //
146 }
147
148 @Test
149 public void testFullQuery3() {
150 //
151 }
152
153 @Test
154 public void testSingleQuery1() throws AttributeNotFoundException,
155 TimeRangeException, StateValueTypeException {
156
2359ecca 157 long timestamp = interestingTimestamp1;
efc403bb
AM
158 int quark;
159 ITmfStateInterval interval;
160 String valueStr;
161
6e71ce46
AM
162 quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
163 interval = ssq.querySingleState(timestamp, quark);
efc403bb 164 valueStr = interval.getStateValue().unboxStr();
2359ecca 165 assertEquals("gdbus", valueStr);
efc403bb
AM
166 }
167
168 @Test
169 public void testSingleQuery2() {
170 //
171 }
172
173 @Test
174 public void testSingleQuery3() {
175 //
176 }
177
ab9bc51b
AM
178 /**
179 * Test a range query (with no resolution parameter, so all intervals)
180 */
efc403bb
AM
181 @Test
182 public void testRangeQuery1() throws AttributeNotFoundException,
183 TimeRangeException, StateValueTypeException {
184
2359ecca 185 long time1 = interestingTimestamp1;
dc0f7bfe 186 long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
efc403bb
AM
187 int quark;
188 List<ITmfStateInterval> intervals;
189
6e71ce46
AM
190 quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
191 intervals = ssq.queryHistoryRange(quark, time1, time2);
2359ecca
AM
192 assertEquals(487, intervals.size()); /* Number of context switches! */
193 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
cc2292bd 194 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
efc403bb
AM
195 }
196
1d3d1293
AM
197 /**
198 * Range query, but with a t2 far off the end of the trace.
199 * The result should still be valid.
200 */
201 @Test
202 public void testRangeQuery2() throws TimeRangeException,
203 AttributeNotFoundException {
204
205 List<ITmfStateInterval> intervals;
e743c3b8 206
6e71ce46
AM
207 int quark = ssq.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1");
208 long ts1 = ssq.getStartTime(); /* start of the trace */
1d3d1293
AM
209 long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid, but ignored */
210
6e71ce46 211 intervals = ssq.queryHistoryRange(quark, ts1, ts2);
1d3d1293 212
06552532
AM
213 /* Activity of IRQ 1 over the whole trace */
214 assertEquals(65, intervals.size());
1d3d1293
AM
215 }
216
ab9bc51b
AM
217 /**
218 * Test a range query with a resolution
219 */
220 @Test
1d3d1293 221 public void testRangeQuery3() throws AttributeNotFoundException,
ab9bc51b
AM
222 TimeRangeException, StateValueTypeException {
223
224 long time1 = interestingTimestamp1;
dc0f7bfe 225 long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
ab9bc51b
AM
226 long resolution = 1000000; /* One query every millisecond */
227 int quark;
228 List<ITmfStateInterval> intervals;
229
6e71ce46 230 quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
b5a8d0cc 231 intervals = ssq.queryHistoryRange(quark, time1, time2, resolution, null);
a9f966a2 232 assertEquals(126, intervals.size()); /* Number of context switches! */
ab9bc51b 233 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
a9f966a2 234 assertEquals(1331668248815698779L, intervals.get(100).getEndTime());
ab9bc51b
AM
235 }
236
efc403bb
AM
237 /**
238 * Ask for a time range outside of the trace's range
e743c3b8 239 *
efc403bb
AM
240 * @throws TimeRangeException
241 */
242 @Test(expected = TimeRangeException.class)
243 public void testFullQueryInvalidTime1() throws TimeRangeException {
dc0f7bfe 244 long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
6e71ce46 245 ssq.queryFullState(ts);
efc403bb
AM
246
247 }
248
249 @Test(expected = TimeRangeException.class)
250 public void testFullQueryInvalidTime2() throws TimeRangeException {
dc0f7bfe 251 long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
6e71ce46 252 ssq.queryFullState(ts);
efc403bb
AM
253
254 }
255
256 @Test(expected = TimeRangeException.class)
257 public void testSingleQueryInvalidTime1()
258 throws AttributeNotFoundException, TimeRangeException {
259
6e71ce46 260 int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dc0f7bfe 261 long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
6e71ce46 262 ssq.querySingleState(ts, quark);
efc403bb
AM
263 }
264
265 @Test(expected = TimeRangeException.class)
266 public void testSingleQueryInvalidTime2()
267 throws AttributeNotFoundException, TimeRangeException {
268
6e71ce46 269 int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dc0f7bfe 270 long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
6e71ce46 271 ssq.querySingleState(ts, quark);
efc403bb
AM
272 }
273
274 @Test(expected = TimeRangeException.class)
275 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
276 TimeRangeException {
277
6e71ce46 278 int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dc0f7bfe
AM
279 long ts1 = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
280 long ts2 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */
efc403bb 281
6e71ce46 282 ssq.queryHistoryRange(quark, ts1, ts2);
efc403bb
AM
283 }
284
285 @Test(expected = TimeRangeException.class)
286 public void testRangeQueryInvalidTime2() throws TimeRangeException,
287 AttributeNotFoundException {
288
6e71ce46 289 int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dc0f7bfe
AM
290 long ts1 = CtfTestFiles.startTime - 1L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
291 long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
efc403bb 292
6e71ce46 293 ssq.queryHistoryRange(quark, ts1, ts2);
efc403bb
AM
294 }
295
296 /**
297 * Ask for a non-existing attribute
e743c3b8 298 *
efc403bb
AM
299 * @throws AttributeNotFoundException
300 */
301 @Test(expected = AttributeNotFoundException.class)
302 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
303
6e71ce46 304 ssq.getQuarkAbsolute("There", "is", "no", "cow", "level");
efc403bb
AM
305 }
306
307 /**
308 * Query but with the wrong State Value type
e743c3b8 309 *
efc403bb
AM
310 * @throws StateValueTypeException
311 * @throws AttributeNotFoundException
312 * @throws TimeRangeException
313 */
314 @Test(expected = StateValueTypeException.class)
315 public void testQueryInvalidValuetype1() throws StateValueTypeException,
316 AttributeNotFoundException, TimeRangeException {
dad01d27 317 List<ITmfStateInterval> list;
efc403bb
AM
318 ITmfStateInterval interval;
319 int quark;
320
6e71ce46
AM
321 list = ssq.queryFullState(interestingTimestamp1);
322 quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
dad01d27 323 interval = list.get(quark);
2359ecca
AM
324
325 /* This is supposed to be an int value */
326 interval.getStateValue().unboxStr();
efc403bb
AM
327 }
328
329 @Test(expected = StateValueTypeException.class)
330 public void testQueryInvalidValuetype2() throws StateValueTypeException,
331 AttributeNotFoundException, TimeRangeException {
dad01d27 332 List<ITmfStateInterval> list;
efc403bb
AM
333 ITmfStateInterval interval;
334 int quark;
335
6e71ce46
AM
336 list = ssq.queryFullState(interestingTimestamp1);
337 quark = ssq.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
dad01d27 338 interval = list.get(quark);
2359ecca
AM
339
340 /* This is supposed to be a String value */
341 interval.getStateValue().unboxInt();
efc403bb
AM
342 }
343
344 @Test
345 public void testFullAttributeName() throws AttributeNotFoundException {
6e71ce46
AM
346 int quark = ssq.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
347 String name = ssq.getFullAttributePath(quark);
2359ecca 348 assertEquals(name, "CPUs/0/Current_thread");
efc403bb
AM
349 }
350
6abc2d88
AM
351 @Test
352 public void testGetQuarks_begin() {
6e71ce46 353 List<Integer> list = ssq.getQuarks("*", "1577", Attributes.EXEC_NAME);
6abc2d88
AM
354
355 assertEquals(1, list.size());
6abc2d88
AM
356 }
357
358 @Test
359 public void testGetQuarks_middle() {
6e71ce46 360 List<Integer> list = ssq.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
6abc2d88 361
1ba498e5
AM
362 /* Number of different kernel threads in the trace */
363 assertEquals(168, list.size());
6abc2d88
AM
364 }
365
366 @Test
367 public void testGetQuarks_end() {
6e71ce46 368 List<Integer> list = ssq.getQuarks(Attributes.THREADS, "1577", "*");
6abc2d88 369
1ba498e5
AM
370 /* There should be 4 sub-attributes for each Thread node */
371 assertEquals(4, list.size());
6abc2d88 372 }
efc403bb 373}
This page took 0.048331 seconds and 5 git commands to generate.