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