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