Commit | Line | Data |
---|---|---|
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 | ||
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.FileNotFoundException; | |
19 | import java.io.IOException; | |
20 | import java.io.PrintWriter; | |
21 | import java.util.List; | |
22 | ||
23 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; | |
24 | import org.eclipse.linuxtools.tmf.core.statesystem.AttributeNotFoundException; | |
25 | import org.eclipse.linuxtools.tmf.core.statesystem.StateHistorySystem; | |
26 | import org.eclipse.linuxtools.tmf.core.statesystem.TimeRangeException; | |
27 | import org.eclipse.linuxtools.tmf.core.statesystem.backend.historytree.HistoryTreeBackend; | |
28 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.HistoryBuilder; | |
29 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateChangeInput; | |
30 | import org.eclipse.linuxtools.tmf.core.statesystem.helpers.IStateHistoryBackend; | |
31 | import org.eclipse.linuxtools.tmf.core.statevalue.StateValueTypeException; | |
32 | import org.eclipse.linuxtools.internal.lttng2.kernel.core.stateprovider.CTFKernelStateInput; | |
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 | protected static File stateFile; | |
46 | protected static File stateFileBenchmark; | |
47 | ||
48 | protected static HistoryBuilder builder; | |
49 | protected static IStateChangeInput input; | |
50 | protected static IStateHistoryBackend hp; | |
51 | protected static StateHistorySystem shs; | |
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() { | |
76 | stateFile.delete(); | |
77 | stateFileBenchmark.delete(); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Rebuild independently so we can benchmark it. Too bad JUnit doesn't allow | |
82 | * us to @Test the @BeforeClass... | |
83 | */ | |
84 | @Test | |
85 | public void testBuild() { | |
86 | HistoryBuilder zebuilder; | |
87 | IStateChangeInput zeinput; | |
88 | IStateHistoryBackend zehp; | |
89 | ||
90 | try { | |
91 | zeinput = new CTFKernelStateInput(CTFTestFiles.getTestTrace()); | |
92 | zehp = new HistoryTreeBackend(stateFileBenchmark, | |
93 | zeinput.getStartTime()); | |
94 | zebuilder = new HistoryBuilder(zeinput, zehp); | |
95 | zebuilder.run(); | |
96 | } catch (Exception e) { | |
97 | e.printStackTrace(); | |
98 | } | |
99 | } | |
100 | ||
101 | @Test | |
102 | public void testOpenExistingStateFile() { | |
103 | IStateHistoryBackend hp2 = null; | |
104 | StateHistorySystem shs2 = null; | |
105 | try { | |
106 | /* 'newStateFile' should have already been created */ | |
107 | hp2 = new HistoryTreeBackend(stateFile); | |
108 | shs2 = new StateHistorySystem(hp2, false); | |
109 | } catch (IOException e) { | |
110 | e.printStackTrace(); | |
111 | } | |
112 | assertTrue(shs2 != null); | |
113 | } | |
114 | ||
115 | @Test | |
116 | public void testFullQuery1() throws StateValueTypeException, | |
117 | AttributeNotFoundException, TimeRangeException { | |
118 | ||
119 | ITmfStateInterval interval; | |
120 | int quark, valueInt; | |
121 | String valueStr; | |
122 | ||
2359ecca | 123 | shs.loadStateAtTime(interestingTimestamp1); |
efc403bb AM |
124 | |
125 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
126 | interval = shs.queryState(quark); | |
127 | valueInt = interval.getStateValue().unboxInt(); | |
2359ecca | 128 | assertEquals(1397, valueInt); |
efc403bb | 129 | |
2359ecca | 130 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); |
efc403bb AM |
131 | interval = shs.queryState(quark); |
132 | valueStr = interval.getStateValue().unboxStr(); | |
2359ecca | 133 | assertEquals("gdbus", valueStr); |
efc403bb AM |
134 | |
135 | // FIXME fails at the moment (attribute type is int, and = 3129??), I'll | |
136 | // figure it out later | |
137 | // quark = shs.getQuarkAbsolute("Threads", "3109", "Exec_mode_stack"); | |
138 | // interval = shs.getState(quark); | |
139 | // valueStr = interval.getStateValue().unboxStr(); | |
140 | // assertTrue( valueStr.equals("bloup") ); | |
141 | } | |
142 | ||
143 | @Test | |
144 | public void testFullQuery2() { | |
145 | // | |
146 | } | |
147 | ||
148 | @Test | |
149 | public void testFullQuery3() { | |
150 | // | |
151 | } | |
152 | ||
153 | @Test | |
154 | public void testSingleQuery1() throws AttributeNotFoundException, | |
155 | TimeRangeException, StateValueTypeException { | |
156 | ||
2359ecca | 157 | long timestamp = interestingTimestamp1; |
efc403bb AM |
158 | int quark; |
159 | ITmfStateInterval interval; | |
160 | String valueStr; | |
161 | ||
2359ecca | 162 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); |
efc403bb AM |
163 | interval = shs.querySingleState(timestamp, quark); |
164 | valueStr = interval.getStateValue().unboxStr(); | |
2359ecca | 165 | assertEquals("gdbus", valueStr); |
efc403bb AM |
166 | } |
167 | ||
168 | @Test | |
169 | public void testSingleQuery2() { | |
170 | // | |
171 | } | |
172 | ||
173 | @Test | |
174 | public void testSingleQuery3() { | |
175 | // | |
176 | } | |
177 | ||
178 | @Test | |
179 | public void testRangeQuery1() throws AttributeNotFoundException, | |
180 | TimeRangeException, StateValueTypeException { | |
181 | ||
2359ecca | 182 | long time1 = interestingTimestamp1; |
efc403bb AM |
183 | long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC; |
184 | int quark; | |
185 | List<ITmfStateInterval> intervals; | |
186 | ||
187 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
188 | intervals = shs.queryHistoryRange(quark, time1, time2); | |
2359ecca AM |
189 | assertEquals(487, intervals.size()); /* Number of context switches! */ |
190 | assertEquals(1685, intervals.get(100).getStateValue().unboxInt()); | |
191 | assertEquals(18670480869135L, intervals.get(205).getEndTime()); | |
efc403bb AM |
192 | } |
193 | ||
194 | /** | |
195 | * Ask for a time range outside of the trace's range | |
196 | * | |
197 | * @throws TimeRangeException | |
198 | */ | |
199 | @Test(expected = TimeRangeException.class) | |
200 | public void testFullQueryInvalidTime1() throws TimeRangeException { | |
2359ecca AM |
201 | long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; |
202 | shs.loadStateAtTime(ts); | |
efc403bb AM |
203 | |
204 | } | |
205 | ||
206 | @Test(expected = TimeRangeException.class) | |
207 | public void testFullQueryInvalidTime2() throws TimeRangeException { | |
2359ecca AM |
208 | long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; |
209 | shs.loadStateAtTime(ts); | |
efc403bb AM |
210 | |
211 | } | |
212 | ||
213 | @Test(expected = TimeRangeException.class) | |
214 | public void testSingleQueryInvalidTime1() | |
215 | throws AttributeNotFoundException, TimeRangeException { | |
216 | ||
217 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
2359ecca AM |
218 | long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; |
219 | shs.querySingleState(ts, quark); | |
efc403bb AM |
220 | } |
221 | ||
222 | @Test(expected = TimeRangeException.class) | |
223 | public void testSingleQueryInvalidTime2() | |
224 | throws AttributeNotFoundException, TimeRangeException { | |
225 | ||
226 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
2359ecca AM |
227 | long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; |
228 | shs.querySingleState(ts, quark); | |
efc403bb AM |
229 | } |
230 | ||
231 | @Test(expected = TimeRangeException.class) | |
232 | public void testRangeQueryInvalidTime1() throws AttributeNotFoundException, | |
233 | TimeRangeException { | |
234 | ||
235 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
2359ecca AM |
236 | long ts1 = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */ |
237 | long ts2 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */ | |
efc403bb | 238 | |
2359ecca | 239 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
240 | } |
241 | ||
242 | @Test(expected = TimeRangeException.class) | |
243 | public void testRangeQueryInvalidTime2() throws TimeRangeException, | |
244 | AttributeNotFoundException { | |
245 | ||
246 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
2359ecca AM |
247 | long ts1 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */ |
248 | long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */ | |
efc403bb | 249 | |
2359ecca | 250 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
251 | } |
252 | ||
253 | @Test(expected = TimeRangeException.class) | |
254 | public void testRangeQueryInvalidTime3() throws TimeRangeException, | |
255 | AttributeNotFoundException { | |
256 | ||
257 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
2359ecca AM |
258 | long ts1 = CTFTestFiles.startTime - 1L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */ |
259 | long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */ | |
efc403bb | 260 | |
2359ecca | 261 | shs.queryHistoryRange(quark, ts1, ts2); |
efc403bb AM |
262 | } |
263 | ||
264 | /** | |
265 | * Ask for a non-existing attribute | |
266 | * | |
267 | * @throws AttributeNotFoundException | |
268 | */ | |
269 | @Test(expected = AttributeNotFoundException.class) | |
270 | public void testQueryInvalidAttribute() throws AttributeNotFoundException { | |
271 | ||
272 | shs.getQuarkAbsolute("There", "is", "no", "cow", "level"); | |
273 | } | |
274 | ||
275 | /** | |
276 | * Query but with the wrong State Value type | |
277 | * | |
278 | * @throws StateValueTypeException | |
279 | * @throws AttributeNotFoundException | |
280 | * @throws TimeRangeException | |
281 | */ | |
282 | @Test(expected = StateValueTypeException.class) | |
283 | public void testQueryInvalidValuetype1() throws StateValueTypeException, | |
284 | AttributeNotFoundException, TimeRangeException { | |
efc403bb AM |
285 | ITmfStateInterval interval; |
286 | int quark; | |
287 | ||
2359ecca | 288 | shs.loadStateAtTime(interestingTimestamp1); |
efc403bb AM |
289 | quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); |
290 | interval = shs.queryState(quark); | |
2359ecca AM |
291 | |
292 | /* This is supposed to be an int value */ | |
293 | interval.getStateValue().unboxStr(); | |
efc403bb AM |
294 | } |
295 | ||
296 | @Test(expected = StateValueTypeException.class) | |
297 | public void testQueryInvalidValuetype2() throws StateValueTypeException, | |
298 | AttributeNotFoundException, TimeRangeException { | |
efc403bb AM |
299 | ITmfStateInterval interval; |
300 | int quark; | |
301 | ||
2359ecca AM |
302 | shs.loadStateAtTime(interestingTimestamp1); |
303 | quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name"); | |
efc403bb | 304 | interval = shs.queryState(quark); |
2359ecca AM |
305 | |
306 | /* This is supposed to be a String value */ | |
307 | interval.getStateValue().unboxInt(); | |
efc403bb AM |
308 | } |
309 | ||
310 | @Test | |
311 | public void testFullAttributeName() throws AttributeNotFoundException { | |
312 | int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread"); | |
313 | String name = shs.getFullAttributePath(quark); | |
2359ecca | 314 | assertEquals(name, "CPUs/0/Current_thread"); |
efc403bb AM |
315 | } |
316 | ||
317 | @Test | |
318 | public void testDebugPrinting() throws FileNotFoundException { | |
319 | shs.debugPrint(new PrintWriter(new File("/dev/null"))); | |
320 | } | |
321 | } |