lttng: Small regression test for the new range queries
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.kernel.core.tests / src / org / eclipse / linuxtools / lttng2 / kernel / core / tests / stateprovider / StateSystemFullHistoryTest.java
CommitLineData
efc403bb
AM
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
13package org.eclipse.linuxtools.lttng2.kernel.core.tests.stateprovider;
14
15import static org.junit.Assert.*;
16
17import java.io.File;
18import java.io.FileNotFoundException;
19import java.io.IOException;
20import java.io.PrintWriter;
21import java.util.List;
22
23import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
24import org.eclipse.linuxtools.tmf.core.statesystem.AttributeNotFoundException;
25import org.eclipse.linuxtools.tmf.core.statesystem.StateHistorySystem;
26import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException;
27import org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree.HistoryTreeBackend;
28import org.eclipse.linuxtools.tmf.core.statesystem.helpers.HistoryBuilder;
29import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateChangeInput;
30import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend;
31import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException;
32import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CTFKernelStateInput;
33import 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")
43public class StateSystemFullHistoryTest {
44
ebd67b34
AM
45 static File stateFile;
46 static File stateFileBenchmark;
efc403bb 47
ebd67b34
AM
48 static HistoryBuilder builder;
49 static IStateChangeInput input;
50 static IStateHistoryBackend hp;
51 static StateHistorySystem shs;
efc403bb 52
2359ecca
AM
53 private final static long interestingTimestamp1 = 18670067372290L;
54
efc403bb
AM
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() {
ebd67b34
AM
76 boolean ret1, ret2;
77 ret1 = stateFile.delete();
78 ret2 = stateFileBenchmark.delete();
79 if ( !(ret1 && ret2) ) {
80 System.err.println("Error cleaning up during unit testing, " +
81 "you might have leftovers state history files in /tmp");
82 }
efc403bb
AM
83 }
84
85 /**
86 * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow
87 * us to @Test the @BeforeClass...
88 */
89 @Test
90 public void testBuild() {
91 HistoryBuilder zebuilder;
92 IStateChangeInput zeinput;
93 IStateHistoryBackend zehp;
94
95 try {
96 zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace());
97 zehp = new HistoryTreeBackend(stateFileBenchmark,
98 zeinput.getStartTime());
99 zebuilder = new HistoryBuilder(zeinput, zehp);
100 zebuilder.run();
101 } catch (Exception e) {
102 e.printStackTrace();
103 }
104 }
105
106 @Test
107 public void testOpenExistingStateFile() {
108 IStateHistoryBackend hp2 = null;
109 StateHistorySystem shs2 = null;
110 try {
111 /* 'newStateFile' should have already been created */
112 hp2 = new HistoryTreeBackend(stateFile);
113 shs2 = new StateHistorySystem(hp2, false);
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 assertTrue(shs2 != null);
118 }
119
120 @Test
121 public void testFullQuery1() throws StateValueTypeException,
122 AttributeNotFoundException, TimeRangeException {
123
dad01d27 124 List<ITmfStateInterval> list;
efc403bb 125 ITmfStateInterval interval;
ae54340d 126 int quark, quark2, valueInt;
efc403bb
AM
127 String valueStr;
128
dad01d27 129 list = shs.loadStateAtTime(interestingTimestamp1);
efc403bb
AM
130
131 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
dad01d27 132 interval = list.get(quark);
efc403bb 133 valueInt = interval.getStateValue().unboxInt();
2359ecca 134 assertEquals(1397, valueInt);
efc403bb 135
2359ecca 136 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
dad01d27 137 interval = list.get(quark);
efc403bb 138 valueStr = interval.getStateValue().unboxStr();
2359ecca 139 assertEquals("gdbus", valueStr);
efc403bb 140
ae54340d
AM
141 /* Query a stack attribute, has to be done in two passes */
142 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
ab9bc51b 143 interval = list.get(quark);
ae54340d
AM
144 valueInt = interval.getStateValue().unboxInt(); /* The stack depth */
145 quark2 = shs.getQuarkRelative(quark, Integer.toString(valueInt));
ab9bc51b 146 interval = list.get(quark2);
ae54340d
AM
147 valueStr = interval.getStateValue().unboxStr();
148 assertTrue(valueStr.equals("sys_poll"));
efc403bb
AM
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
2359ecca 165 long timestamp = interestingTimestamp1;
efc403bb
AM
166 int quark;
167 ITmfStateInterval interval;
168 String valueStr;
169
2359ecca 170 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
efc403bb
AM
171 interval = shs.querySingleState(timestamp, quark);
172 valueStr = interval.getStateValue().unboxStr();
2359ecca 173 assertEquals("gdbus", valueStr);
efc403bb
AM
174 }
175
176 @Test
177 public void testSingleQuery2() {
178 //
179 }
180
181 @Test
182 public void testSingleQuery3() {
183 //
184 }
185
ab9bc51b
AM
186 /**
187 * Test a range query (with no resolution parameter, so all intervals)
188 */
efc403bb
AM
189 @Test
190 public void testRangeQuery1() throws AttributeNotFoundException,
191 TimeRangeException, StateValueTypeException {
192
2359ecca 193 long time1 = interestingTimestamp1;
efc403bb
AM
194 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
195 int quark;
196 List<ITmfStateInterval> intervals;
197
198 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
199 intervals = shs.queryHistoryRange(quark, time1, time2);
2359ecca
AM
200 assertEquals(487, intervals.size()); /* Number of context switches! */
201 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
202 assertEquals(18670480869135L, intervals.get(205).getEndTime());
efc403bb
AM
203 }
204
ab9bc51b
AM
205 /**
206 * Test a range query with a resolution
207 */
208 @Test
209 public void testRangeQuery2() throws AttributeNotFoundException,
210 TimeRangeException, StateValueTypeException {
211
212 long time1 = interestingTimestamp1;
213 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
214 long resolution = 1000000; /* One query every millisecond */
215 int quark;
216 List<ITmfStateInterval> intervals;
217
218 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
219 intervals = shs.queryHistoryRange(quark, time1, time2, resolution);
220 assertEquals(129, intervals.size()); /* Number of context switches! */
221 assertEquals(1452, intervals.get(50).getStateValue().unboxInt());
222 assertEquals(18670837977001L, intervals.get(100).getEndTime());
223 }
224
efc403bb
AM
225 /**
226 * Ask for a time range outside of the trace's range
227 *
228 * @throws TimeRangeException
229 */
230 @Test(expected = TimeRangeException.class)
231 public void testFullQueryInvalidTime1() throws TimeRangeException {
2359ecca
AM
232 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
233 shs.loadStateAtTime(ts);
efc403bb
AM
234
235 }
236
237 @Test(expected = TimeRangeException.class)
238 public void testFullQueryInvalidTime2() throws TimeRangeException {
2359ecca
AM
239 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
240 shs.loadStateAtTime(ts);
efc403bb
AM
241
242 }
243
244 @Test(expected = TimeRangeException.class)
245 public void testSingleQueryInvalidTime1()
246 throws AttributeNotFoundException, TimeRangeException {
247
248 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
2359ecca
AM
249 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
250 shs.querySingleState(ts, quark);
efc403bb
AM
251 }
252
253 @Test(expected = TimeRangeException.class)
254 public void testSingleQueryInvalidTime2()
255 throws AttributeNotFoundException, TimeRangeException {
256
257 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
2359ecca
AM
258 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
259 shs.querySingleState(ts, quark);
efc403bb
AM
260 }
261
262 @Test(expected = TimeRangeException.class)
263 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
264 TimeRangeException {
265
266 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
2359ecca
AM
267 long ts1 = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
268 long ts2 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
efc403bb 269
2359ecca 270 shs.queryHistoryRange(quark, ts1, ts2);
efc403bb
AM
271 }
272
273 @Test(expected = TimeRangeException.class)
274 public void testRangeQueryInvalidTime2() throws TimeRangeException,
275 AttributeNotFoundException {
276
277 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
2359ecca
AM
278 long ts1 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
279 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
efc403bb 280
2359ecca 281 shs.queryHistoryRange(quark, ts1, ts2);
efc403bb
AM
282 }
283
284 @Test(expected = TimeRangeException.class)
285 public void testRangeQueryInvalidTime3() throws TimeRangeException,
286 AttributeNotFoundException {
287
288 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
2359ecca
AM
289 long ts1 = CTFTestFiles.startTime - 1L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
290 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
efc403bb 291
2359ecca 292 shs.queryHistoryRange(quark, ts1, ts2);
efc403bb
AM
293 }
294
295 /**
296 * Ask for a non-existing attribute
297 *
298 * @throws AttributeNotFoundException
299 */
300 @Test(expected = AttributeNotFoundException.class)
301 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
302
303 shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
304 }
305
306 /**
307 * Query but with the wrong State Value type
308 *
309 * @throws StateValueTypeException
310 * @throws AttributeNotFoundException
311 * @throws TimeRangeException
312 */
313 @Test(expected = StateValueTypeException.class)
314 public void testQueryInvalidValuetype1() throws StateValueTypeException,
315 AttributeNotFoundException, TimeRangeException {
dad01d27 316 List<ITmfStateInterval> list;
efc403bb
AM
317 ITmfStateInterval interval;
318 int quark;
319
dad01d27 320 list = shs.loadStateAtTime(interestingTimestamp1);
efc403bb 321 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
dad01d27 322 interval = list.get(quark);
2359ecca
AM
323
324 /* This is supposed to be an int value */
325 interval.getStateValue().unboxStr();
efc403bb
AM
326 }
327
328 @Test(expected = StateValueTypeException.class)
329 public void testQueryInvalidValuetype2() throws StateValueTypeException,
330 AttributeNotFoundException, TimeRangeException {
dad01d27 331 List<ITmfStateInterval> list;
efc403bb
AM
332 ITmfStateInterval interval;
333 int quark;
334
dad01d27 335 list = shs.loadStateAtTime(interestingTimestamp1);
2359ecca 336 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
dad01d27 337 interval = list.get(quark);
2359ecca
AM
338
339 /* This is supposed to be a String value */
340 interval.getStateValue().unboxInt();
efc403bb
AM
341 }
342
343 @Test
344 public void testFullAttributeName() throws AttributeNotFoundException {
345 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
346 String name = shs.getFullAttributePath(quark);
2359ecca 347 assertEquals(name, "CPUs/0/Current_thread");
efc403bb
AM
348 }
349
6abc2d88
AM
350 @Test
351 public void testGetQuarks_begin() {
352 List<Integer> list = shs.getQuarks("*", "1577", "Exec_name");
353
354 assertEquals(1, list.size());
355 assertEquals(Integer.valueOf(479), list.get(0));
356 }
357
358 @Test
359 public void testGetQuarks_middle() {
360 List<Integer> list = shs.getQuarks("Threads", "*", "Exec_name");
361
362 assertEquals(Integer.valueOf(36), list.get(4));
363 assertEquals(Integer.valueOf(100), list.get(10));
364 assertEquals(Integer.valueOf(116), list.get(12));
365 }
366
367 @Test
368 public void testGetQuarks_end() {
369 List<Integer> list = shs.getQuarks("Threads", "1577", "*");
370
371 assertEquals(3, list.size());
372 assertEquals(Integer.valueOf(479), list.get(1));
373 }
374
efc403bb
AM
375 @Test
376 public void testDebugPrinting() throws FileNotFoundException {
377 shs.debugPrint(new PrintWriter(new File("/dev/null")));
378 }
379}
This page took 0.041506 seconds and 5 git commands to generate.