lttng: Fix the last StateSystem unit test
[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 protected static File stateFile;
46 protected static File stateFileBenchmark;
47
48 protected static HistoryBuilder builder;
49 protected static IStateChangeInput input;
50 protected static IStateHistoryBackend hp;
51 protected static StateHistorySystem shs;
52
53 private final static long interestingTimestamp1 = 18670067372290L;
54
55 protected static String getTestFileName() {
56 return "/tmp/statefile.ht"; //$NON-NLS-1$
57 }
58
59 @BeforeClass
60 public static void initialize() {
61 stateFile = new File(getTestFileName());
62 stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
63 try {
64 input = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
65 hp = new HistoryTreeBackend(stateFile, input.getStartTime());
66 builder = new HistoryBuilder(input, hp);
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 builder.run();
71 shs = (StateHistorySystem) builder.getSS();
72 }
73
74 @AfterClass
75 public static void cleanup() {
76 stateFile.delete();
77 stateFileBenchmark.delete();
78 }
79
80 /**
81 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
82 * us to @Test the @BeforeClass...
83 */
84 @Test
85 public void testBuild() {
86 HistoryBuilder zebuilder;
87 IStateChangeInput zeinput;
88 IStateHistoryBackend zehp;
89
90 try {
91 zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
92 zehp = new HistoryTreeBackend(stateFileBenchmark,
93 zeinput.getStartTime());
94 zebuilder = new HistoryBuilder(zeinput, zehp);
95 zebuilder.run();
96 } catch (Exception e) {
97 e.printStackTrace();
98 }
99 }
100
101 @Test
102 public void testOpenExistingStateFile() {
103 IStateHistoryBackend hp2 = null;
104 StateHistorySystem shs2 = null;
105 try {
106 /* 'newStateFile' should have already been created */
107 hp2 = new HistoryTreeBackend(stateFile);
108 shs2 = new StateHistorySystem(hp2, false);
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 assertTrue(shs2 != null);
113 }
114
115 @Test
116 public void testFullQuery1() throws StateValueTypeException,
117 AttributeNotFoundException, TimeRangeException {
118
119 ITmfStateInterval interval;
120 int quark, quark2, valueInt;
121 String valueStr;
122
123 shs.loadStateAtTime(interestingTimestamp1);
124
125 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
126 interval = shs.queryState(quark);
127 valueInt = interval.getStateValue().unboxInt();
128 assertEquals(1397, valueInt);
129
130 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
131 interval = shs.queryState(quark);
132 valueStr = interval.getStateValue().unboxStr();
133 assertEquals("gdbus", valueStr);
134
135 /* Query a stack attribute, has to be done in two passes */
136 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
137 interval = shs.queryState(quark);
138 valueInt = interval.getStateValue().unboxInt(); /* The stack depth */
139 quark2 = shs.getQuarkRelative(quark, Integer.toString(valueInt));
140 interval = shs.queryState(quark2);
141 valueStr = interval.getStateValue().unboxStr();
142 assertTrue(valueStr.equals("sys_poll"));
143 }
144
145 @Test
146 public void testFullQuery2() {
147 //
148 }
149
150 @Test
151 public void testFullQuery3() {
152 //
153 }
154
155 @Test
156 public void testSingleQuery1() throws AttributeNotFoundException,
157 TimeRangeException, StateValueTypeException {
158
159 long timestamp = interestingTimestamp1;
160 int quark;
161 ITmfStateInterval interval;
162 String valueStr;
163
164 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
165 interval = shs.querySingleState(timestamp, quark);
166 valueStr = interval.getStateValue().unboxStr();
167 assertEquals("gdbus", valueStr);
168 }
169
170 @Test
171 public void testSingleQuery2() {
172 //
173 }
174
175 @Test
176 public void testSingleQuery3() {
177 //
178 }
179
180 @Test
181 public void testRangeQuery1() throws AttributeNotFoundException,
182 TimeRangeException, StateValueTypeException {
183
184 long time1 = interestingTimestamp1;
185 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
186 int quark;
187 List<ITmfStateInterval> intervals;
188
189 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
190 intervals = shs.queryHistoryRange(quark, time1, time2);
191 assertEquals(487, intervals.size()); /* Number of context switches! */
192 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
193 assertEquals(18670480869135L, intervals.get(205).getEndTime());
194 }
195
196 /**
197 * Ask for a time range outside of the trace's range
198 *
199 * @throws TimeRangeException
200 */
201 @Test(expected = TimeRangeException.class)
202 public void testFullQueryInvalidTime1() throws TimeRangeException {
203 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
204 shs.loadStateAtTime(ts);
205
206 }
207
208 @Test(expected = TimeRangeException.class)
209 public void testFullQueryInvalidTime2() throws TimeRangeException {
210 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
211 shs.loadStateAtTime(ts);
212
213 }
214
215 @Test(expected = TimeRangeException.class)
216 public void testSingleQueryInvalidTime1()
217 throws AttributeNotFoundException, TimeRangeException {
218
219 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
220 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
221 shs.querySingleState(ts, quark);
222 }
223
224 @Test(expected = TimeRangeException.class)
225 public void testSingleQueryInvalidTime2()
226 throws AttributeNotFoundException, TimeRangeException {
227
228 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
229 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
230 shs.querySingleState(ts, quark);
231 }
232
233 @Test(expected = TimeRangeException.class)
234 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
235 TimeRangeException {
236
237 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
238 long ts1 = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
239 long ts2 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
240
241 shs.queryHistoryRange(quark, ts1, ts2);
242 }
243
244 @Test(expected = TimeRangeException.class)
245 public void testRangeQueryInvalidTime2() throws TimeRangeException,
246 AttributeNotFoundException {
247
248 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
249 long ts1 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
250 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
251
252 shs.queryHistoryRange(quark, ts1, ts2);
253 }
254
255 @Test(expected = TimeRangeException.class)
256 public void testRangeQueryInvalidTime3() throws TimeRangeException,
257 AttributeNotFoundException {
258
259 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
260 long ts1 = CTFTestFiles.startTime - 1L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
261 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
262
263 shs.queryHistoryRange(quark, ts1, ts2);
264 }
265
266 /**
267 * Ask for a non-existing attribute
268 *
269 * @throws AttributeNotFoundException
270 */
271 @Test(expected = AttributeNotFoundException.class)
272 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
273
274 shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
275 }
276
277 /**
278 * Query but with the wrong State Value type
279 *
280 * @throws StateValueTypeException
281 * @throws AttributeNotFoundException
282 * @throws TimeRangeException
283 */
284 @Test(expected = StateValueTypeException.class)
285 public void testQueryInvalidValuetype1() throws StateValueTypeException,
286 AttributeNotFoundException, TimeRangeException {
287 ITmfStateInterval interval;
288 int quark;
289
290 shs.loadStateAtTime(interestingTimestamp1);
291 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
292 interval = shs.queryState(quark);
293
294 /* This is supposed to be an int value */
295 interval.getStateValue().unboxStr();
296 }
297
298 @Test(expected = StateValueTypeException.class)
299 public void testQueryInvalidValuetype2() throws StateValueTypeException,
300 AttributeNotFoundException, TimeRangeException {
301 ITmfStateInterval interval;
302 int quark;
303
304 shs.loadStateAtTime(interestingTimestamp1);
305 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
306 interval = shs.queryState(quark);
307
308 /* This is supposed to be a String value */
309 interval.getStateValue().unboxInt();
310 }
311
312 @Test
313 public void testFullAttributeName() throws AttributeNotFoundException {
314 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
315 String name = shs.getFullAttributePath(quark);
316 assertEquals(name, "CPUs/0/Current_thread");
317 }
318
319 @Test
320 public void testGetQuarks_begin() {
321 List<Integer> list = shs.getQuarks("*", "1577", "Exec_name");
322
323 assertEquals(1, list.size());
324 assertEquals(Integer.valueOf(479), list.get(0));
325 }
326
327 @Test
328 public void testGetQuarks_middle() {
329 List<Integer> list = shs.getQuarks("Threads", "*", "Exec_name");
330
331 assertEquals(Integer.valueOf(36), list.get(4));
332 assertEquals(Integer.valueOf(100), list.get(10));
333 assertEquals(Integer.valueOf(116), list.get(12));
334 }
335
336 @Test
337 public void testGetQuarks_end() {
338 List<Integer> list = shs.getQuarks("Threads", "1577", "*");
339
340 assertEquals(3, list.size());
341 assertEquals(Integer.valueOf(479), list.get(1));
342 }
343
344 @Test
345 public void testDebugPrinting() throws FileNotFoundException {
346 shs.debugPrint(new PrintWriter(new File("/dev/null")));
347 }
348 }
This page took 0.04146 seconds and 6 git commands to generate.