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