tmf: Make the basic query method multi-thread safe
[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.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 static File stateFile;
46 static File stateFileBenchmark;
47
48 static HistoryBuilder builder;
49 static IStateChangeInput input;
50 static IStateHistoryBackend hp;
51 static StateHistorySystem shs;
52
53 private final static long interestingTimestamp1 = 18670067372290L;
54
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 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 }
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
124 List<ITmfStateInterval> list;
125 ITmfStateInterval interval;
126 int quark, quark2, valueInt;
127 String valueStr;
128
129 list = shs.loadStateAtTime(interestingTimestamp1);
130
131 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
132 interval = list.get(quark);
133 valueInt = interval.getStateValue().unboxInt();
134 assertEquals(1397, valueInt);
135
136 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
137 interval = list.get(quark);
138 valueStr = interval.getStateValue().unboxStr();
139 assertEquals("gdbus", valueStr);
140
141 /* Query a stack attribute, has to be done in two passes */
142 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_mode_stack");
143 interval = shs.queryState(quark);
144 valueInt = interval.getStateValue().unboxInt(); /* The stack depth */
145 quark2 = shs.getQuarkRelative(quark, Integer.toString(valueInt));
146 interval = shs.queryState(quark2);
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 = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
171 interval = shs.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 @Test
187 public void testRangeQuery1() throws AttributeNotFoundException,
188 TimeRangeException, StateValueTypeException {
189
190 long time1 = interestingTimestamp1;
191 long time2 = time1 + 1L * CTFTestFiles.NANOSECS_PER_SEC;
192 int quark;
193 List<ITmfStateInterval> intervals;
194
195 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
196 intervals = shs.queryHistoryRange(quark, time1, time2);
197 assertEquals(487, intervals.size()); /* Number of context switches! */
198 assertEquals(1685, intervals.get(100).getStateValue().unboxInt());
199 assertEquals(18670480869135L, intervals.get(205).getEndTime());
200 }
201
202 /**
203 * Ask for a time range outside of the trace's range
204 *
205 * @throws TimeRangeException
206 */
207 @Test(expected = TimeRangeException.class)
208 public void testFullQueryInvalidTime1() throws TimeRangeException {
209 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
210 shs.loadStateAtTime(ts);
211
212 }
213
214 @Test(expected = TimeRangeException.class)
215 public void testFullQueryInvalidTime2() throws TimeRangeException {
216 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
217 shs.loadStateAtTime(ts);
218
219 }
220
221 @Test(expected = TimeRangeException.class)
222 public void testSingleQueryInvalidTime1()
223 throws AttributeNotFoundException, TimeRangeException {
224
225 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
226 long ts = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC;
227 shs.querySingleState(ts, quark);
228 }
229
230 @Test(expected = TimeRangeException.class)
231 public void testSingleQueryInvalidTime2()
232 throws AttributeNotFoundException, TimeRangeException {
233
234 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
235 long ts = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC;
236 shs.querySingleState(ts, quark);
237 }
238
239 @Test(expected = TimeRangeException.class)
240 public void testRangeQueryInvalidTime1() throws AttributeNotFoundException,
241 TimeRangeException {
242
243 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
244 long ts1 = CTFTestFiles.startTime - 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
245 long ts2 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
246
247 shs.queryHistoryRange(quark, ts1, ts2);
248 }
249
250 @Test(expected = TimeRangeException.class)
251 public void testRangeQueryInvalidTime2() throws TimeRangeException,
252 AttributeNotFoundException {
253
254 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
255 long ts1 = CTFTestFiles.startTime + 1L * CTFTestFiles.NANOSECS_PER_SEC; /* valid */
256 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
257
258 shs.queryHistoryRange(quark, ts1, ts2);
259 }
260
261 @Test(expected = TimeRangeException.class)
262 public void testRangeQueryInvalidTime3() throws TimeRangeException,
263 AttributeNotFoundException {
264
265 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
266 long ts1 = CTFTestFiles.startTime - 1L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
267 long ts2 = CTFTestFiles.startTime + 20L * CTFTestFiles.NANOSECS_PER_SEC; /* invalid */
268
269 shs.queryHistoryRange(quark, ts1, ts2);
270 }
271
272 /**
273 * Ask for a non-existing attribute
274 *
275 * @throws AttributeNotFoundException
276 */
277 @Test(expected = AttributeNotFoundException.class)
278 public void testQueryInvalidAttribute() throws AttributeNotFoundException {
279
280 shs.getQuarkAbsolute("There", "is", "no", "cow", "level");
281 }
282
283 /**
284 * Query but with the wrong State Value type
285 *
286 * @throws StateValueTypeException
287 * @throws AttributeNotFoundException
288 * @throws TimeRangeException
289 */
290 @Test(expected = StateValueTypeException.class)
291 public void testQueryInvalidValuetype1() throws StateValueTypeException,
292 AttributeNotFoundException, TimeRangeException {
293 List<ITmfStateInterval> list;
294 ITmfStateInterval interval;
295 int quark;
296
297 list = shs.loadStateAtTime(interestingTimestamp1);
298 quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
299 interval = list.get(quark);
300
301 /* This is supposed to be an int value */
302 interval.getStateValue().unboxStr();
303 }
304
305 @Test(expected = StateValueTypeException.class)
306 public void testQueryInvalidValuetype2() throws StateValueTypeException,
307 AttributeNotFoundException, TimeRangeException {
308 List<ITmfStateInterval> list;
309 ITmfStateInterval interval;
310 int quark;
311
312 list = shs.loadStateAtTime(interestingTimestamp1);
313 quark = shs.getQuarkAbsolute("Threads", "1432", "Exec_name");
314 interval = list.get(quark);
315
316 /* This is supposed to be a String value */
317 interval.getStateValue().unboxInt();
318 }
319
320 @Test
321 public void testFullAttributeName() throws AttributeNotFoundException {
322 int quark = shs.getQuarkAbsolute("CPUs", "0", "Current_thread");
323 String name = shs.getFullAttributePath(quark);
324 assertEquals(name, "CPUs/0/Current_thread");
325 }
326
327 @Test
328 public void testGetQuarks_begin() {
329 List<Integer> list = shs.getQuarks("*", "1577", "Exec_name");
330
331 assertEquals(1, list.size());
332 assertEquals(Integer.valueOf(479), list.get(0));
333 }
334
335 @Test
336 public void testGetQuarks_middle() {
337 List<Integer> list = shs.getQuarks("Threads", "*", "Exec_name");
338
339 assertEquals(Integer.valueOf(36), list.get(4));
340 assertEquals(Integer.valueOf(100), list.get(10));
341 assertEquals(Integer.valueOf(116), list.get(12));
342 }
343
344 @Test
345 public void testGetQuarks_end() {
346 List<Integer> list = shs.getQuarks("Threads", "1577", "*");
347
348 assertEquals(3, list.size());
349 assertEquals(Integer.valueOf(479), list.get(1));
350 }
351
352 @Test
353 public void testDebugPrinting() throws FileNotFoundException {
354 shs.debugPrint(new PrintWriter(new File("/dev/null")));
355 }
356 }
This page took 0.04092 seconds and 6 git commands to generate.