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