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