66faffc321dde32319dffdf17cc952c9ae30f956
[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.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 StateHistorySystem shs;
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 shs = (StateHistorySystem) builder.getSS();
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, " +
82 "you might have leftovers state history files in /tmp");
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 @Test
91 public void testBuild() {
92 HistoryBuilder zebuilder;
93 IStateChangeInput zeinput;
94 IStateHistoryBackend zehp;
95
96 try {
97 zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
98 zehp = new HistoryTreeBackend(stateFileBenchmark,
99 zeinput.getStartTime());
100 zebuilder = new HistoryBuilder(zeinput, zehp);
101 zebuilder.run();
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106
107 @Test
108 public void testOpenExistingStateFile() {
109 IStateHistoryBackend hp2 = null;
110 StateHistorySystem shs2 = null;
111 try {
112 /* 'newStateFile' should have already been created */
113 hp2 = new HistoryTreeBackend(stateFile);
114 shs2 = new StateHistorySystem(hp2, false);
115 } catch (IOException e) {
116 e.printStackTrace();
117 }
118 assertTrue(shs2 != null);
119 }
120
121 @Test
122 public void testFullQuery1() throws StateValueTypeException,
123 AttributeNotFoundException, TimeRangeException {
124
125 List<ITmfStateInterval> list;
126 ITmfStateInterval interval;
127 int quark, quark2, valueInt;
128 String valueStr;
129
130 list = shs.loadStateAtTime(interestingTimestamp1);
131
132 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
133 interval = list.get(quark);
134 valueInt = interval.getStateValue().unboxInt();
135 assertEquals(1397, valueInt);
136
137 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
138 interval = list.get(quark);
139 valueStr = interval.getStateValue().unboxStr();
140 assertEquals("gdbus", valueStr);
141
142 /* Query a stack attribute, has to be done in two passes */
143 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
144 interval = list.get(quark);
145 valueInt = interval.getStateValue().unboxInt(); /* The stack depth */
146 quark2 = shs.getQuarkRelative(quark, Integer.toString(valueInt));
147 interval = list.get(quark2);
148 valueStr = interval.getStateValue().unboxStr();
149 assertTrue(valueStr.equals("sys_poll"));
150 }
151
152 @Test
153 public void testFullQuery2() {
154 //
155 }
156
157 @Test
158 public void testFullQuery3() {
159 //
160 }
161
162 @Test
163 public void testSingleQuery1() throws AttributeNotFoundException,
164 TimeRangeException, StateValueTypeException {
165
166 long timestamp = interestingTimestamp1;
167 int quark;
168 ITmfStateInterval interval;
169 String valueStr;
170
171 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
172 interval = shs.querySingleState(timestamp, quark);
173 valueStr = interval.getStateValue().unboxStr();
174 assertEquals("gdbus", valueStr);
175 }
176
177 @Test
178 public void testSingleQuery2() {
179 //
180 }
181
182 @Test
183 public void testSingleQuery3() {
184 //
185 }
186
187 /**
188 * Test a range query (with no resolution parameter, so all intervals)
189 */
190 @Test
191 public void testRangeQuery1() throws AttributeNotFoundException,
192 TimeRangeException, StateValueTypeException {
193
194 long time1 = interestingTimestamp1;
195 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
196 int quark;
197 List<ITmfStateInterval> intervals;
198
199 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
200 intervals = shs.queryHistoryRange(quark, time1, time2);
201 assertEquals(487, intervals.size()); /* Number of context switches! */
202 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
203 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
204 }
205
206 /**
207 * Test a range query with a resolution
208 */
209 @Test
210 public void testRangeQuery2() throws AttributeNotFoundException,
211 TimeRangeException, StateValueTypeException {
212
213 long time1 = interestingTimestamp1;
214 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
215 long resolution = 1000000; /* One query every millisecond */
216 int quark;
217 List<ITmfStateInterval> intervals;
218
219 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
220 intervals = shs.queryHistoryRange(quark, time1, time2, resolution);
221 assertEquals(129, intervals.size()); /* Number of context switches! */
222 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
223 assertEquals(1331668248784789238L, intervals.get(100).getEndTime());
224 }
225
226 /**
227 * Ask for a time range outside of the trace's range
228 *
229 * @throws TimeRangeException
230 */
231 @Test(expected = TimeRangeException.class)
232 public void testFullQueryInvalidTime1() throws TimeRangeException {
233 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
234 shs.loadStateAtTime(ts);
235
236 }
237
238 @Test(expected = TimeRangeException.class)
239 public void testFullQueryInvalidTime2() throws TimeRangeException {
240 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
241 shs.loadStateAtTime(ts);
242
243 }
244
245 @Test(expected = TimeRangeException.class)
246 public void testSingleQueryInvalidTime1()
247 throws AttributeNotFoundException, TimeRangeException {
248
249 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
250 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
251 shs.querySingleState(ts, quark);
252 }
253
254 @Test(expected = TimeRangeException.class)
255 public void testSingleQueryInvalidTime2()
256 throws AttributeNotFoundException, TimeRangeException {
257
258 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
259 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
260 shs.querySingleState(ts, quark);
261 }
262
263 @Test(expected = TimeRangeException.class)
264 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
265 TimeRangeException {
266
267 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
268 long ts1 = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
269 long ts2 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
270
271 shs.queryHistoryRange(quark, ts1, ts2);
272 }
273
274 @Test(expected = TimeRangeException.class)
275 public void testRangeQueryInvalidTime2() throws TimeRangeException,
276 AttributeNotFoundException {
277
278 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
279 long ts1 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
280 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
281
282 shs.queryHistoryRange(quark, ts1, ts2);
283 }
284
285 @Test(expected = TimeRangeException.class)
286 public void testRangeQueryInvalidTime3() throws TimeRangeException,
287 AttributeNotFoundException {
288
289 int quark = shs.getQuarkAbsolute("CPUs", "0", "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 shs.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 shs.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 = shs.loadStateAtTime(interestingTimestamp1);
322 quark = shs.getQuarkAbsolute("CPUs", "0", "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 = shs.loadStateAtTime(interestingTimestamp1);
337 quark = shs.getQuarkAbsolute("Threads", "1432", "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 = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
347 String name = shs.getFullAttributePath(quark);
348 assertEquals(name, "CPUs/0/Current_thread");
349 }
350
351 @Test
352 public void testGetQuarks_begin() {
353 List<Integer> list = shs.getQuarks("*", "1577", "Exec_name");
354
355 assertEquals(1, list.size());
356 assertEquals(Integer.valueOf(479), list.get(0));
357 }
358
359 @Test
360 public void testGetQuarks_middle() {
361 List<Integer> list = shs.getQuarks("Threads", "*", "Exec_name");
362
363 assertEquals(Integer.valueOf(36), list.get(4));
364 assertEquals(Integer.valueOf(100), list.get(10));
365 assertEquals(Integer.valueOf(116), list.get(12));
366 }
367
368 @Test
369 public void testGetQuarks_end() {
370 List<Integer> list = shs.getQuarks("Threads", "1577", "*");
371
372 assertEquals(3, list.size());
373 assertEquals(Integer.valueOf(479), list.get(1));
374 }
375
376 @Test
377 public void testDebugPrinting() throws FileNotFoundException {
378 PrintWriter pw = new PrintWriter(new File("/dev/null"));
379 shs.debugPrint(pw);
380 pw.close();
381 }
382 }
This page took 0.039993 seconds and 5 git commands to generate.