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