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