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