Merge Kernel state system input provider
[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 protected static String getTestFileName() {
54 return "/tmp/statefile.ht"; //$NON-NLS-1$
55 }
56
57 @BeforeClass
58 public static void initialize() {
59 stateFile = new File(getTestFileName());
60 stateFileBenchmark = new File(getTestFileName() + ".benchmark"); //$NON-NLS-1$
61 try {
62 input = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
63 hp = new HistoryTreeBackend(stateFile, input.getStartTime());
64 builder = new HistoryBuilder(input, hp);
65 } catch (Exception e) {
66 e.printStackTrace();
67 }
68 builder.run();
69 shs = (StateHistorySystem) builder.getSS();
70 }
71
72 @AfterClass
73 public static void cleanup() {
74 stateFile.delete();
75 stateFileBenchmark.delete();
76 }
77
78 /**
79 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
80 * us to @Test the @BeforeClass...
81 */
82 @Test
83 public void testBuild() {
84 HistoryBuilder zebuilder;
85 IStateChangeInput zeinput;
86 IStateHistoryBackend zehp;
87
88 try {
89 zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
90 zehp = new HistoryTreeBackend(stateFileBenchmark,
91 zeinput.getStartTime());
92 zebuilder = new HistoryBuilder(zeinput, zehp);
93 zebuilder.run();
94 } catch (Exception e) {
95 e.printStackTrace();
96 }
97 }
98
99 @Test
100 public void testOpenExistingStateFile() {
101 IStateHistoryBackend hp2 = null;
102 StateHistorySystem shs2 = null;
103 try {
104 /* 'newStateFile' should have already been created */
105 hp2 = new HistoryTreeBackend(stateFile);
106 shs2 = new StateHistorySystem(hp2, false);
107 } catch (IOException e) {
108 e.printStackTrace();
109 }
110 assertTrue(shs2 != null);
111 }
112
113 @Test
114 public void testFullQuery1() throws StateValueTypeException,
115 AttributeNotFoundException, TimeRangeException {
116
117 ITmfStateInterval interval;
118 int quark, valueInt;
119 String valueStr;
120
121 shs.loadStateAtTime(17622841472359L);
122
123 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
124 interval = shs.queryState(quark);
125 valueInt = interval.getStateValue().unboxInt();
126 assertTrue(valueInt == 1929);
127
128 quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
129 interval = shs.queryState(quark);
130 valueStr = interval.getStateValue().unboxStr();
131 assertTrue(valueStr.equals("apache2"));
132
133 // FIXME fails at the moment (attribute type is int, and = 3129??), I'll
134 // figure it out later
135 // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack");
136 // interval = shs.getState(quark);
137 // valueStr = interval.getStateValue().unboxStr();
138 // assertTrue( valueStr.equals("bloup") );
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 = 17622841472359L;
156 int quark;
157 ITmfStateInterval interval;
158 String valueStr;
159
160 quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
161 interval = shs.querySingleState(timestamp, quark);
162 valueStr = interval.getStateValue().unboxStr();
163 assertTrue(valueStr.equals("apache2"));
164 }
165
166 @Test
167 public void testSingleQuery2() {
168 //
169 }
170
171 @Test
172 public void testSingleQuery3() {
173 //
174 }
175
176 @Test
177 public void testRangeQuery1() throws AttributeNotFoundException,
178 TimeRangeException, StateValueTypeException {
179
180 long time1 = 17622841472359L;
181 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
182 int quark;
183 List<ITmfStateInterval> intervals;
184
185 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
186 intervals = shs.queryHistoryRange(quark, time1, time2);
187 assertTrue(intervals.size() == 1018); /* Number of context switches! */
188 assertTrue(intervals.get(100).getStateValue().unboxInt() == 2974);
189 assertTrue(intervals.get(205).getEndTime() == 17622977386059L);
190 }
191
192 /**
193 * Ask for a time range outside of the trace's range
194 *
195 * @throws TimeRangeException
196 */
197 @Test(expected = TimeRangeException.class)
198 public void testFullQueryInvalidTime1() throws TimeRangeException {
199 shs.loadStateAtTime(CTFTestFiles.startTime1 + 20L
200 * CTFTestFiles.NANOSECS_PER_SEC);
201
202 }
203
204 @Test(expected = TimeRangeException.class)
205 public void testFullQueryInvalidTime2() throws TimeRangeException {
206 shs.loadStateAtTime(CTFTestFiles.startTime1 - 20L
207 * CTFTestFiles.NANOSECS_PER_SEC);
208
209 }
210
211 @Test(expected = TimeRangeException.class)
212 public void testSingleQueryInvalidTime1()
213 throws AttributeNotFoundException, TimeRangeException {
214
215 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
216 long time = CTFTestFiles.startTime1 + 20L
217 * CTFTestFiles.NANOSECS_PER_SEC;
218 shs.querySingleState(time, quark);
219 }
220
221 @Test(expected = TimeRangeException.class)
222 public void testSingleQueryInvalidTime2()
223 throws AttributeNotFoundException, TimeRangeException {
224
225 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
226 long time = CTFTestFiles.startTime1 - 20L
227 * CTFTestFiles.NANOSECS_PER_SEC;
228 shs.querySingleState(time, quark);
229 }
230
231 @Test(expected = TimeRangeException.class)
232 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
233 TimeRangeException {
234
235 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
236 long time1 = CTFTestFiles.startTime1 - 20L
237 * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
238 long time2 = CTFTestFiles.startTime1 + 1L
239 * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
240
241 shs.queryHistoryRange(quark, time1, time2);
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 time1 = CTFTestFiles.startTime1 + 1L
250 * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
251 long time2 = CTFTestFiles.startTime1 + 20L
252 * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
253
254 shs.queryHistoryRange(quark, time1, time2);
255 }
256
257 @Test(expected = TimeRangeException.class)
258 public void testRangeQueryInvalidTime3() throws TimeRangeException,
259 AttributeNotFoundException {
260
261 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
262 long time1 = CTFTestFiles.startTime1 - 1L
263 * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
264 long time2 = CTFTestFiles.startTime1 + 20L
265 * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
266
267 shs.queryHistoryRange(quark, time1, time2);
268 }
269
270 /**
271 * Ask for a non-existing attribute
272 *
273 * @throws AttributeNotFoundException
274 */
275 @Test(expected = AttributeNotFoundException.class)
276 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
277
278 shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
279 }
280
281 /**
282 * Query but with the wrong State Value type
283 *
284 * @throws StateValueTypeException
285 * @throws AttributeNotFoundException
286 * @throws TimeRangeException
287 */
288 @Test(expected = StateValueTypeException.class)
289 public void testQueryInvalidValuetype1() throws StateValueTypeException,
290 AttributeNotFoundException, TimeRangeException {
291
292 ITmfStateInterval interval;
293 int quark;
294
295 shs.loadStateAtTime(17622841472359L);
296
297 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
298 interval = shs.queryState(quark);
299 interval.getStateValue().unboxStr(); /*
300 * This is supposed to be a int
301 * value
302 */
303 }
304
305 @Test(expected = StateValueTypeException.class)
306 public void testQueryInvalidValuetype2() throws StateValueTypeException,
307 AttributeNotFoundException, TimeRangeException {
308
309 ITmfStateInterval interval;
310 int quark;
311
312 shs.loadStateAtTime(17622841472359L);
313
314 quark = shs.getQuarkAbsolute("Threads", "1197", "Exec_name");
315 interval = shs.queryState(quark);
316 interval.getStateValue().unboxInt(); /*
317 * This is supposed to be a String
318 * value
319 */
320 }
321
322 @Test
323 public void testFullAttributeName() throws AttributeNotFoundException {
324 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
325 String name = shs.getFullAttributePath(quark);
326 assertTrue(name.equals("CPUs/0/Current_thread"));
327 }
328
329 @Test
330 public void testDebugPrinting() throws FileNotFoundException {
331 shs.debugPrint(new PrintWriter(new File("/dev/null")));
332 }
333 }
This page took 0.068924 seconds and 6 git commands to generate.