lttng: Change syscalls to a flat attribute instead of a stack
[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, 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 quark = ssb.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.SYSTEM_CALL);
146 interval = list.get(quark);
147 valueStr = interval.getStateValue().unboxStr();
148 assertTrue(valueStr.equals("sys_poll"));
149 }
150
151 @Test
152 public void testFullQuery2() {
153 //
154 }
155
156 @Test
157 public void testFullQuery3() {
158 //
159 }
160
161 @Test
162 public void testSingleQuery1() throws AttributeNotFoundException,
163 TimeRangeException, StateValueTypeException {
164
165 long timestamp = interestingTimestamp1;
166 int quark;
167 ITmfStateInterval interval;
168 String valueStr;
169
170 quark = ssb.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
171 interval = ssb.querySingleState(timestamp, quark);
172 valueStr = interval.getStateValue().unboxStr();
173 assertEquals("gdbus", valueStr);
174 }
175
176 @Test
177 public void testSingleQuery2() {
178 //
179 }
180
181 @Test
182 public void testSingleQuery3() {
183 //
184 }
185
186 /**
187 * Test a range query (with no resolution parameter, so all intervals)
188 */
189 @Test
190 public void testRangeQuery1() throws AttributeNotFoundException,
191 TimeRangeException, StateValueTypeException {
192
193 long time1 = interestingTimestamp1;
194 long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
195 int quark;
196 List<ITmfStateInterval> intervals;
197
198 quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
199 intervals = ssb.queryHistoryRange(quark, time1, time2);
200 assertEquals(487, intervals.size()); /* Number of context switches! */
201 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
202 assertEquals(1331668248427681372L, intervals.get(205).getEndTime());
203 }
204
205 /**
206 * Range query, but with a t2 far off the end of the trace.
207 * The result should still be valid.
208 */
209 @Test
210 public void testRangeQuery2() throws TimeRangeException,
211 AttributeNotFoundException {
212
213 List<ITmfStateInterval> intervals;
214
215 int quark = ssb.getQuarkAbsolute(Attributes.RESOURCES, Attributes.IRQS, "1");
216 long ts1 = ssb.getStartTime(); /* start of the trace */
217 long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid, but ignored */
218
219 intervals = ssb.queryHistoryRange(quark, ts1, ts2);
220
221 /* Activity of IRQ 1 over the whole trace */
222 assertEquals(65, intervals.size());
223 }
224
225 /**
226 * Test a range query with a resolution
227 */
228 @Test
229 public void testRangeQuery3() throws AttributeNotFoundException,
230 TimeRangeException, StateValueTypeException {
231
232 long time1 = interestingTimestamp1;
233 long time2 = time1 + 1L * CtfTestFiles.NANOSECS_PER_SEC;
234 long resolution = 1000000; /* One query every millisecond */
235 int quark;
236 List<ITmfStateInterval> intervals;
237
238 quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
239 intervals = ssb.queryHistoryRange(quark, time1, time2, resolution);
240 assertEquals(129, intervals.size()); /* Number of context switches! */
241 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
242 assertEquals(1331668248784789238L, intervals.get(100).getEndTime());
243 }
244
245 /**
246 * Ask for a time range outside of the trace's range
247 *
248 * @throws TimeRangeException
249 */
250 @Test(expected = TimeRangeException.class)
251 public void testFullQueryInvalidTime1() throws TimeRangeException {
252 long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
253 ssb.loadStateAtTime(ts);
254
255 }
256
257 @Test(expected = TimeRangeException.class)
258 public void testFullQueryInvalidTime2() throws TimeRangeException {
259 long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
260 ssb.loadStateAtTime(ts);
261
262 }
263
264 @Test(expected = TimeRangeException.class)
265 public void testSingleQueryInvalidTime1()
266 throws AttributeNotFoundException, TimeRangeException {
267
268 int quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
269 long ts = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC;
270 ssb.querySingleState(ts, quark);
271 }
272
273 @Test(expected = TimeRangeException.class)
274 public void testSingleQueryInvalidTime2()
275 throws AttributeNotFoundException, TimeRangeException {
276
277 int quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
278 long ts = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC;
279 ssb.querySingleState(ts, quark);
280 }
281
282 @Test(expected = TimeRangeException.class)
283 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
284 TimeRangeException {
285
286 int quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
287 long ts1 = CtfTestFiles.startTime - 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
288 long ts2 = CtfTestFiles.startTime + 1L * CtfTestFiles.NANOSECS_PER_SEC; /* valid */
289
290 ssb.queryHistoryRange(quark, ts1, ts2);
291 }
292
293 @Test(expected = TimeRangeException.class)
294 public void testRangeQueryInvalidTime2() throws TimeRangeException,
295 AttributeNotFoundException {
296
297 int quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
298 long ts1 = CtfTestFiles.startTime - 1L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
299 long ts2 = CtfTestFiles.startTime + 20L * CtfTestFiles.NANOSECS_PER_SEC; /* invalid */
300
301 ssb.queryHistoryRange(quark, ts1, ts2);
302 }
303
304 /**
305 * Ask for a non-existing attribute
306 *
307 * @throws AttributeNotFoundException
308 */
309 @Test(expected = AttributeNotFoundException.class)
310 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
311
312 ssb.getQuarkAbsolute("There", "is", "no", "cow", "level");
313 }
314
315 /**
316 * Query but with the wrong State Value type
317 *
318 * @throws StateValueTypeException
319 * @throws AttributeNotFoundException
320 * @throws TimeRangeException
321 */
322 @Test(expected = StateValueTypeException.class)
323 public void testQueryInvalidValuetype1() throws StateValueTypeException,
324 AttributeNotFoundException, TimeRangeException {
325 List<ITmfStateInterval> list;
326 ITmfStateInterval interval;
327 int quark;
328
329 list = ssb.loadStateAtTime(interestingTimestamp1);
330 quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
331 interval = list.get(quark);
332
333 /* This is supposed to be an int value */
334 interval.getStateValue().unboxStr();
335 }
336
337 @Test(expected = StateValueTypeException.class)
338 public void testQueryInvalidValuetype2() throws StateValueTypeException,
339 AttributeNotFoundException, TimeRangeException {
340 List<ITmfStateInterval> list;
341 ITmfStateInterval interval;
342 int quark;
343
344 list = ssb.loadStateAtTime(interestingTimestamp1);
345 quark = ssb.getQuarkAbsolute(Attributes.THREADS, "1432", Attributes.EXEC_NAME);
346 interval = list.get(quark);
347
348 /* This is supposed to be a String value */
349 interval.getStateValue().unboxInt();
350 }
351
352 @Test
353 public void testFullAttributeName() throws AttributeNotFoundException {
354 int quark = ssb.getQuarkAbsolute(Attributes.CPUS, "0", Attributes.CURRENT_THREAD);
355 String name = ssb.getFullAttributePath(quark);
356 assertEquals(name, "CPUs/0/Current_thread");
357 }
358
359 @Test
360 public void testGetQuarks_begin() {
361 List<Integer> list = ssb.getQuarks("*", "1577", Attributes.EXEC_NAME);
362
363 assertEquals(1, list.size());
364 }
365
366 @Test
367 public void testGetQuarks_middle() {
368 List<Integer> list = ssb.getQuarks(Attributes.THREADS, "*", Attributes.EXEC_NAME);
369
370 /* Number of different kernel threads in the trace */
371 assertEquals(168, list.size());
372 }
373
374 @Test
375 public void testGetQuarks_end() {
376 List<Integer> list = ssb.getQuarks(Attributes.THREADS, "1577", "*");
377
378 /* There should be 4 sub-attributes for each Thread node */
379 assertEquals(4, list.size());
380 }
381 }
This page took 0.039879 seconds and 5 git commands to generate.