Commit | Line | Data |
---|---|---|
a52fde77 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
a52fde77 AM |
3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> | |
5df842b3 | 5 | * |
a52fde77 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 | |
5df842b3 | 10 | * |
a52fde77 AM |
11 | *******************************************************************************/ |
12 | ||
18ab1d18 | 13 | package org.eclipse.linuxtools.internal.tmf.core.statesystem; |
a52fde77 | 14 | |
8d1346f0 AM |
15 | import java.io.File; |
16 | import java.io.IOException; | |
a52fde77 | 17 | import java.io.PrintWriter; |
8d1346f0 | 18 | import java.util.ArrayList; |
f94a0bac | 19 | import java.util.LinkedList; |
a52fde77 | 20 | import java.util.List; |
16576a7e | 21 | import java.util.concurrent.CountDownLatch; |
a52fde77 | 22 | |
8d1346f0 AM |
23 | import org.eclipse.core.runtime.IProgressMonitor; |
24 | import org.eclipse.core.runtime.NullProgressMonitor; | |
5500a7f0 | 25 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
f9a76cac | 26 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend; |
6d08acca | 27 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
96345c5a | 28 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
6d08acca AM |
29 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
30 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; | |
8d1346f0 AM |
31 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; |
32 | import org.eclipse.linuxtools.tmf.core.interval.TmfStateInterval; | |
f1f86dfb | 33 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder; |
a52fde77 | 34 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
a52fde77 AM |
35 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; |
36 | ||
37 | /** | |
8d1346f0 AM |
38 | * This is the core class of the Generic State System. It contains all the |
39 | * methods to build and query a state history. It's exposed externally through | |
40 | * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the | |
41 | * user needs read-only access or read-write access. | |
5df842b3 | 42 | * |
8d1346f0 AM |
43 | * When building, DON'T FORGET to call .closeHistory() when you are done |
44 | * inserting intervals, or the storage backend will have no way of knowing it | |
45 | * can close and write itself to disk, and its thread will keep running. | |
5df842b3 | 46 | * |
a52fde77 | 47 | * @author alexmont |
5df842b3 | 48 | * |
a52fde77 | 49 | */ |
f1f86dfb | 50 | public class StateSystem implements ITmfStateSystemBuilder { |
a52fde77 AM |
51 | |
52 | /* References to the inner structures */ | |
8d1346f0 AM |
53 | private final AttributeTree attributeTree; |
54 | private final TransientState transState; | |
55 | private final IStateHistoryBackend backend; | |
a52fde77 | 56 | |
16576a7e AM |
57 | /* Latch tracking if the state history is done building or not */ |
58 | private final CountDownLatch finishedLatch = new CountDownLatch(1); | |
59 | ||
1a4205d9 | 60 | private boolean buildCancelled = false; |
96345c5a | 61 | private boolean isDisposed = false; |
1a4205d9 | 62 | |
f9a76cac AM |
63 | /** |
64 | * New-file constructor. For when you build a state system with a new file, | |
65 | * or if the back-end does not require a file on disk. | |
66 | * | |
67 | * @param backend | |
68 | * Back-end plugin to use | |
69 | */ | |
70 | public StateSystem(IStateHistoryBackend backend) { | |
71 | this.backend = backend; | |
72 | this.transState = new TransientState(backend); | |
73 | this.attributeTree = new AttributeTree(this); | |
74 | } | |
75 | ||
a52fde77 | 76 | /** |
8d1346f0 AM |
77 | * General constructor |
78 | * | |
79 | * @param backend | |
f9a76cac | 80 | * The "state history storage" back-end to use. |
8d1346f0 AM |
81 | * @param newFile |
82 | * Put true if this is a new history started from scratch. It is | |
83 | * used to tell the state system where to get its attribute tree. | |
84 | * @throws IOException | |
85 | * If there was a problem creating the new history file | |
a52fde77 | 86 | */ |
8d1346f0 AM |
87 | public StateSystem(IStateHistoryBackend backend, boolean newFile) |
88 | throws IOException { | |
89 | this.backend = backend; | |
90 | this.transState = new TransientState(backend); | |
a52fde77 | 91 | |
8d1346f0 AM |
92 | if (newFile) { |
93 | attributeTree = new AttributeTree(this); | |
94 | } else { | |
95 | /* We're opening an existing file */ | |
96 | this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader()); | |
97 | transState.setInactive(); | |
16576a7e AM |
98 | finishedLatch.countDown(); /* The history is already built */ |
99 | } | |
100 | } | |
101 | ||
102 | @Override | |
1a4205d9 | 103 | public boolean waitUntilBuilt() { |
16576a7e AM |
104 | try { |
105 | finishedLatch.await(); | |
106 | } catch (InterruptedException e) { | |
107 | e.printStackTrace(); | |
8d1346f0 | 108 | } |
1a4205d9 AM |
109 | return !buildCancelled; |
110 | } | |
111 | ||
112 | @Override | |
113 | public synchronized void dispose() { | |
96345c5a | 114 | isDisposed = true; |
1a4205d9 AM |
115 | if (transState.isActive()) { |
116 | transState.setInactive(); | |
117 | buildCancelled = true; | |
118 | } | |
119 | backend.dispose(); | |
a52fde77 AM |
120 | } |
121 | ||
8d1346f0 AM |
122 | //-------------------------------------------------------------------------- |
123 | // General methods related to the attribute tree | |
124 | //-------------------------------------------------------------------------- | |
125 | ||
126 | /** | |
127 | * Method used by the attribute tree when creating new attributes, to keep | |
128 | * the attribute count in the transient state in sync. | |
129 | */ | |
130 | void addEmptyAttribute() { | |
131 | transState.addEmptyEntry(); | |
132 | } | |
133 | ||
134 | @Override | |
4623f57f AM |
135 | public int getNbAttributes() { |
136 | return attributeTree.getNbAttributes(); | |
137 | } | |
138 | ||
f5295294 AM |
139 | @Override |
140 | public boolean isLastAttribute(int quark) { | |
141 | return (quark == getNbAttributes() - 1) ? true : false; | |
142 | } | |
143 | ||
8d1346f0 AM |
144 | @Override |
145 | public String getAttributeName(int attributeQuark) { | |
146 | return attributeTree.getAttributeName(attributeQuark); | |
147 | } | |
148 | ||
149 | @Override | |
150 | public String getFullAttributePath(int attributeQuark) { | |
151 | return attributeTree.getFullAttributeName(attributeQuark); | |
152 | } | |
153 | ||
154 | //-------------------------------------------------------------------------- | |
155 | // Methods related to the storage backend | |
156 | //-------------------------------------------------------------------------- | |
a52fde77 | 157 | |
8d1346f0 AM |
158 | @Override |
159 | public long getStartTime() { | |
160 | return backend.getStartTime(); | |
161 | } | |
162 | ||
163 | @Override | |
164 | public long getCurrentEndTime() { | |
165 | return backend.getEndTime(); | |
166 | } | |
167 | ||
168 | @Override | |
169 | public void closeHistory(long endTime) throws TimeRangeException { | |
170 | File attributeTreeFile; | |
171 | long attributeTreeFilePos; | |
172 | long realEndTime = endTime; | |
173 | ||
174 | if (realEndTime < backend.getEndTime()) { | |
175 | /* | |
176 | * This can happen (empty nodes pushing the border further, etc.) | |
177 | * but shouldn't be too big of a deal. | |
178 | */ | |
179 | realEndTime = backend.getEndTime(); | |
180 | } | |
181 | transState.closeTransientState(realEndTime); | |
182 | backend.finishedBuilding(realEndTime); | |
183 | ||
184 | attributeTreeFile = backend.supplyAttributeTreeWriterFile(); | |
185 | attributeTreeFilePos = backend.supplyAttributeTreeWriterFilePosition(); | |
186 | if (attributeTreeFile != null) { | |
187 | /* | |
188 | * If null was returned, we simply won't save the attribute tree, | |
189 | * too bad! | |
190 | */ | |
191 | attributeTree.writeSelf(attributeTreeFile, attributeTreeFilePos); | |
192 | } | |
16576a7e | 193 | finishedLatch.countDown(); /* Mark the history as finished building */ |
8d1346f0 AM |
194 | } |
195 | ||
196 | //-------------------------------------------------------------------------- | |
197 | // Quark-retrieving methods | |
198 | //-------------------------------------------------------------------------- | |
199 | ||
200 | @Override | |
a52fde77 AM |
201 | public int getQuarkAbsolute(String... attribute) |
202 | throws AttributeNotFoundException { | |
203 | return attributeTree.getQuarkDontAdd(-1, attribute); | |
204 | } | |
205 | ||
8d1346f0 | 206 | @Override |
a52fde77 AM |
207 | public int getQuarkAbsoluteAndAdd(String... attribute) { |
208 | return attributeTree.getQuarkAndAdd(-1, attribute); | |
209 | } | |
210 | ||
8d1346f0 | 211 | @Override |
a52fde77 AM |
212 | public int getQuarkRelative(int startingNodeQuark, String... subPath) |
213 | throws AttributeNotFoundException { | |
214 | return attributeTree.getQuarkDontAdd(startingNodeQuark, subPath); | |
215 | } | |
216 | ||
8d1346f0 | 217 | @Override |
a52fde77 AM |
218 | public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) { |
219 | return attributeTree.getQuarkAndAdd(startingNodeQuark, subPath); | |
220 | } | |
221 | ||
8d1346f0 | 222 | @Override |
c66426fd | 223 | public List<Integer> getSubAttributes(int quark, boolean recursive) |
0a9de3d2 | 224 | throws AttributeNotFoundException { |
c66426fd | 225 | return attributeTree.getSubAttributes(quark, recursive); |
0a9de3d2 AM |
226 | } |
227 | ||
8d1346f0 | 228 | @Override |
f94a0bac AM |
229 | public List<Integer> getQuarks(String... pattern) { |
230 | List<Integer> quarks = new LinkedList<Integer>(); | |
231 | List<String> prefix = new LinkedList<String>(); | |
232 | List<String> suffix = new LinkedList<String>(); | |
233 | boolean split = false; | |
234 | String[] prefixStr; | |
235 | String[] suffixStr; | |
236 | List<Integer> directChildren; | |
237 | int startingAttribute; | |
238 | ||
239 | /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */ | |
240 | for (String entry : pattern) { | |
241 | if (entry.equals("*")) { //$NON-NLS-1$ | |
242 | if (split) { | |
243 | /* | |
244 | * Split was already true? This means there was more than | |
245 | * one wildcard. This is not supported, return an empty | |
246 | * list. | |
247 | */ | |
248 | return quarks; | |
249 | } | |
250 | split = true; | |
251 | continue; | |
252 | } | |
253 | ||
254 | if (split) { | |
255 | suffix.add(entry); | |
256 | } else { | |
257 | prefix.add(entry); | |
258 | } | |
259 | } | |
260 | prefixStr = prefix.toArray(new String[prefix.size()]); | |
261 | suffixStr = suffix.toArray(new String[suffix.size()]); | |
262 | ||
263 | /* | |
264 | * If there was no wildcard, we'll only return the one matching | |
265 | * attribute, if there is one. | |
266 | */ | |
267 | if (split == false) { | |
268 | int quark; | |
269 | try { | |
270 | quark = getQuarkAbsolute(prefixStr); | |
271 | } catch (AttributeNotFoundException e) { | |
272 | /* It's fine, we'll just return the empty List */ | |
273 | return quarks; | |
274 | } | |
275 | quarks.add(quark); | |
276 | return quarks; | |
277 | } | |
278 | ||
279 | try { | |
280 | if (prefix.size() == 0) { | |
281 | /* | |
282 | * If 'prefix' is empty, this means the wildcard was the first | |
283 | * element. Look for the root node's sub-attributes. | |
284 | */ | |
285 | startingAttribute = -1; | |
286 | } else { | |
287 | startingAttribute = getQuarkAbsolute(prefixStr); | |
288 | } | |
289 | directChildren = attributeTree.getSubAttributes(startingAttribute, | |
290 | false); | |
291 | } catch (AttributeNotFoundException e) { | |
292 | /* That attribute path did not exist, return the empty array */ | |
293 | return quarks; | |
294 | } | |
295 | ||
296 | /* | |
297 | * Iterate of all the sub-attributes, and only keep those who match the | |
298 | * 'suffix' part of the initial pattern. | |
299 | */ | |
300 | for (int childQuark : directChildren) { | |
301 | int matchingQuark; | |
302 | try { | |
303 | matchingQuark = getQuarkRelative(childQuark, suffixStr); | |
304 | } catch (AttributeNotFoundException e) { | |
305 | continue; | |
306 | } | |
307 | quarks.add(matchingQuark); | |
308 | } | |
309 | ||
310 | return quarks; | |
311 | } | |
312 | ||
8d1346f0 AM |
313 | //-------------------------------------------------------------------------- |
314 | // Methods related to insertions in the history | |
315 | //-------------------------------------------------------------------------- | |
a52fde77 | 316 | |
8d1346f0 | 317 | @Override |
a52fde77 | 318 | public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark) |
7e0b2b56 AM |
319 | throws TimeRangeException, AttributeNotFoundException, |
320 | StateValueTypeException { | |
a52fde77 AM |
321 | transState.processStateChange(t, value, attributeQuark); |
322 | } | |
323 | ||
8d1346f0 | 324 | @Override |
a52fde77 AM |
325 | public void incrementAttribute(long t, int attributeQuark) |
326 | throws StateValueTypeException, TimeRangeException, | |
327 | AttributeNotFoundException { | |
328 | int prevValue = queryOngoingState(attributeQuark).unboxInt(); | |
280bbdbb AM |
329 | if (prevValue == -1) { |
330 | /* if the attribute was previously null, start counting at 0 */ | |
331 | prevValue = 0; | |
332 | } | |
a52fde77 AM |
333 | modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1), |
334 | attributeQuark); | |
335 | } | |
336 | ||
8d1346f0 | 337 | @Override |
a52fde77 AM |
338 | public void pushAttribute(long t, ITmfStateValue value, int attributeQuark) |
339 | throws TimeRangeException, AttributeNotFoundException, | |
340 | StateValueTypeException { | |
a52fde77 AM |
341 | Integer stackDepth = 0; |
342 | int subAttributeQuark; | |
343 | ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark); | |
344 | ||
345 | if (previousSV.isNull()) { | |
346 | /* | |
347 | * If the StateValue was null, this means this is the first time we | |
348 | * use this attribute. Leave stackDepth at 0. | |
349 | */ | |
350 | } else if (previousSV.getType() == 0) { | |
351 | /* Previous value was an integer, all is good, use it */ | |
352 | stackDepth = previousSV.unboxInt(); | |
a52fde77 AM |
353 | } else { |
354 | /* Previous state of this attribute was another type? Not good! */ | |
90a25ebe AM |
355 | throw new StateValueTypeException(); |
356 | } | |
357 | ||
358 | if (stackDepth >= 10) { | |
359 | /* | |
360 | * Limit stackDepth to 10, to avoid having Attribute Trees grow out | |
361 | * of control due to buggy insertions | |
362 | */ | |
363 | String message = "Stack limit reached, not pushing"; //$NON-NLS-1$ | |
364 | throw new AttributeNotFoundException(message); | |
a52fde77 AM |
365 | } |
366 | ||
367 | stackDepth++; | |
5896eb76 | 368 | subAttributeQuark = getQuarkRelativeAndAdd(attributeQuark, stackDepth.toString()); |
a52fde77 | 369 | |
5896eb76 | 370 | modifyAttribute(t, TmfStateValue.newValueInt(stackDepth), attributeQuark); |
90a25ebe | 371 | modifyAttribute(t, value, subAttributeQuark); |
a52fde77 AM |
372 | } |
373 | ||
8d1346f0 | 374 | @Override |
5896eb76 | 375 | public ITmfStateValue popAttribute(long t, int attributeQuark) |
a52fde77 AM |
376 | throws AttributeNotFoundException, TimeRangeException, |
377 | StateValueTypeException { | |
e2eac108 | 378 | /* These are the state values of the stack-attribute itself */ |
5896eb76 | 379 | ITmfStateValue previousSV = queryOngoingState(attributeQuark); |
a52fde77 AM |
380 | |
381 | if (previousSV.isNull()) { | |
e2eac108 AM |
382 | /* |
383 | * Trying to pop an empty stack. This often happens at the start of | |
384 | * traces, for example when we see a syscall_exit, without having | |
385 | * the corresponding syscall_entry in the trace. Just ignore | |
386 | * silently. | |
387 | */ | |
5896eb76 | 388 | return null; |
90a25ebe | 389 | } |
e2eac108 | 390 | if (previousSV.getType() != ITmfStateValue.TYPE_INTEGER) { |
a52fde77 | 391 | /* |
90a25ebe AM |
392 | * The existing value was a string, this doesn't look like a valid |
393 | * stack attribute. | |
a52fde77 | 394 | */ |
90a25ebe | 395 | throw new StateValueTypeException(); |
a52fde77 AM |
396 | } |
397 | ||
5896eb76 | 398 | Integer stackDepth = previousSV.unboxInt(); |
90a25ebe | 399 | |
e2eac108 | 400 | if (stackDepth <= 0) { |
a52fde77 | 401 | /* This on the other hand should not happen... */ |
e2eac108 AM |
402 | /* the case where == -1 was handled previously by .isNull() */ |
403 | String message = "A top-level stack attribute cannot " + //$NON-NLS-1$ | |
404 | "have a value of 0 or less (except -1/null)."; //$NON-NLS-1$ | |
90a25ebe | 405 | throw new StateValueTypeException(message); |
a52fde77 AM |
406 | } |
407 | ||
e2eac108 | 408 | /* The attribute should already exist at this point */ |
5896eb76 AM |
409 | int subAttributeQuark = getQuarkRelative(attributeQuark, stackDepth.toString()); |
410 | ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark); | |
a52fde77 | 411 | |
e2eac108 AM |
412 | /* Update the state value of the stack-attribute */ |
413 | ITmfStateValue nextSV; | |
414 | if (--stackDepth == 0 ) { | |
415 | /* Jump over "0" and store -1 (a null state value) */ | |
416 | nextSV = TmfStateValue.nullValue(); | |
417 | } else { | |
418 | nextSV = TmfStateValue.newValueInt(stackDepth); | |
419 | } | |
420 | modifyAttribute(t, nextSV, attributeQuark); | |
421 | ||
422 | /* Delete the sub-attribute that contained the user's state value */ | |
a52fde77 | 423 | removeAttribute(t, subAttributeQuark); |
e2eac108 | 424 | |
5896eb76 | 425 | return poppedValue; |
a52fde77 AM |
426 | } |
427 | ||
8d1346f0 | 428 | @Override |
a52fde77 AM |
429 | public void removeAttribute(long t, int attributeQuark) |
430 | throws TimeRangeException, AttributeNotFoundException { | |
431 | assert (attributeQuark >= 0); | |
c66426fd AM |
432 | List<Integer> childAttributes; |
433 | ||
434 | /* | |
435 | * "Nullify our children first, recursively. We pass 'false' because we | |
436 | * handle the recursion ourselves. | |
437 | */ | |
438 | childAttributes = attributeTree.getSubAttributes(attributeQuark, false); | |
a52fde77 AM |
439 | for (Integer childNodeQuark : childAttributes) { |
440 | assert (attributeQuark != childNodeQuark); | |
441 | removeAttribute(t, childNodeQuark); | |
442 | } | |
443 | /* Nullify ourselves */ | |
7e0b2b56 AM |
444 | try { |
445 | transState.processStateChange(t, TmfStateValue.nullValue(), | |
446 | attributeQuark); | |
447 | } catch (StateValueTypeException e) { | |
50678114 AM |
448 | /* |
449 | * Will not happen since we're inserting null values only, but poor | |
450 | * compiler has no way of knowing this... | |
7e0b2b56 AM |
451 | */ |
452 | e.printStackTrace(); | |
453 | } | |
a52fde77 AM |
454 | } |
455 | ||
8d1346f0 AM |
456 | //-------------------------------------------------------------------------- |
457 | // "Current" query/update methods | |
458 | //-------------------------------------------------------------------------- | |
a52fde77 | 459 | |
8d1346f0 | 460 | @Override |
a52fde77 AM |
461 | public ITmfStateValue queryOngoingState(int attributeQuark) |
462 | throws AttributeNotFoundException { | |
463 | return transState.getOngoingStateValue(attributeQuark); | |
464 | } | |
465 | ||
602c0697 AM |
466 | @Override |
467 | public long getOngoingStartTime(int attribute) | |
468 | throws AttributeNotFoundException { | |
469 | return transState.getOngoingStartTime(attribute); | |
470 | } | |
471 | ||
8d1346f0 | 472 | @Override |
a52fde77 AM |
473 | public void updateOngoingState(ITmfStateValue newValue, int attributeQuark) |
474 | throws AttributeNotFoundException { | |
475 | transState.changeOngoingStateValue(attributeQuark, newValue); | |
476 | } | |
477 | ||
66866869 AM |
478 | /** |
479 | * Modify the whole "ongoing state" (state values + start times). This can | |
480 | * be used when "seeking" a state system to a different point in the trace | |
481 | * (and restoring the known stateInfo at this location). Use with care! | |
482 | * | |
483 | * @param newStateIntervals | |
484 | * The new List of state values to use as ongoing state info | |
485 | */ | |
486 | protected void replaceOngoingState(List<ITmfStateInterval> newStateIntervals) { | |
487 | transState.replaceOngoingState(newStateIntervals); | |
488 | } | |
489 | ||
8d1346f0 AM |
490 | //-------------------------------------------------------------------------- |
491 | // Regular query methods (sent to the back-end) | |
492 | //-------------------------------------------------------------------------- | |
493 | ||
494 | @Override | |
495 | public synchronized List<ITmfStateInterval> queryFullState(long t) | |
96345c5a AM |
496 | throws TimeRangeException, StateSystemDisposedException { |
497 | if (isDisposed) { | |
498 | throw new StateSystemDisposedException(); | |
499 | } | |
500 | ||
8d1346f0 AM |
501 | List<ITmfStateInterval> stateInfo = new ArrayList<ITmfStateInterval>( |
502 | attributeTree.getNbAttributes()); | |
503 | ||
504 | /* Bring the size of the array to the current number of attributes */ | |
505 | for (int i = 0; i < attributeTree.getNbAttributes(); i++) { | |
506 | stateInfo.add(null); | |
507 | } | |
508 | ||
509 | /* Query the storage backend */ | |
510 | backend.doQuery(stateInfo, t); | |
511 | ||
512 | /* | |
513 | * If we are currently building the history, also query the "ongoing" | |
514 | * states for stuff that might not yet be written to the history. | |
515 | */ | |
516 | if (transState.isActive()) { | |
517 | transState.doQuery(stateInfo, t); | |
518 | } | |
519 | ||
520 | /* | |
521 | * We should have previously inserted an interval for every attribute. | |
522 | * If we do happen do see a 'null' object here, just replace it with a a | |
523 | * dummy internal with a null value, to avoid NPE's further up. | |
524 | */ | |
525 | for (int i = 0; i < stateInfo.size(); i++) { | |
526 | if (stateInfo.get(i) == null) { | |
527 | //logMissingInterval(i, t); | |
528 | stateInfo.set(i, new TmfStateInterval(t, t, i, TmfStateValue.nullValue())); | |
529 | } | |
530 | } | |
531 | return stateInfo; | |
50678114 AM |
532 | } |
533 | ||
8d1346f0 AM |
534 | @Override |
535 | public ITmfStateInterval querySingleState(long t, int attributeQuark) | |
96345c5a AM |
536 | throws AttributeNotFoundException, TimeRangeException, |
537 | StateSystemDisposedException { | |
538 | if (isDisposed) { | |
539 | throw new StateSystemDisposedException(); | |
540 | } | |
8d1346f0 | 541 | |
96345c5a | 542 | ITmfStateInterval ret; |
8d1346f0 AM |
543 | if (transState.hasInfoAboutStateOf(t, attributeQuark)) { |
544 | ret = transState.getOngoingInterval(attributeQuark); | |
545 | } else { | |
546 | ret = backend.doSingularQuery(t, attributeQuark); | |
547 | } | |
548 | ||
549 | /* | |
550 | * Return a fake interval if we could not find anything in the history. | |
551 | * We do NOT want to return 'null' here. | |
552 | */ | |
553 | if (ret == null) { | |
554 | //logMissingInterval(attributeQuark, t); | |
555 | return new TmfStateInterval(t, this.getCurrentEndTime(), | |
556 | attributeQuark, TmfStateValue.nullValue()); | |
557 | } | |
558 | return ret; | |
559 | } | |
560 | ||
4bff6e6e AM |
561 | @Override |
562 | public ITmfStateInterval querySingleStackTop(long t, int stackAttributeQuark) | |
563 | throws StateValueTypeException, AttributeNotFoundException, | |
96345c5a | 564 | TimeRangeException, StateSystemDisposedException { |
4bff6e6e AM |
565 | Integer curStackDepth = querySingleState(t, stackAttributeQuark).getStateValue().unboxInt(); |
566 | ||
567 | if (curStackDepth == -1) { | |
568 | /* There is nothing stored in this stack at this moment */ | |
569 | return null; | |
570 | } else if (curStackDepth < -1 || curStackDepth == 0) { | |
571 | /* | |
572 | * This attribute is an integer attribute, but it doesn't seem like | |
573 | * it's used as a stack-attribute... | |
574 | */ | |
575 | throw new StateValueTypeException(); | |
576 | } | |
577 | ||
578 | int subAttribQuark = getQuarkRelative(stackAttributeQuark, curStackDepth.toString()); | |
579 | ITmfStateInterval ret = querySingleState(t, subAttribQuark); | |
580 | return ret; | |
581 | } | |
582 | ||
8d1346f0 AM |
583 | @Override |
584 | public List<ITmfStateInterval> queryHistoryRange(int attributeQuark, | |
585 | long t1, long t2) throws TimeRangeException, | |
96345c5a AM |
586 | AttributeNotFoundException, StateSystemDisposedException { |
587 | if (isDisposed) { | |
588 | throw new StateSystemDisposedException(); | |
589 | } | |
590 | ||
8d1346f0 AM |
591 | List<ITmfStateInterval> intervals; |
592 | ITmfStateInterval currentInterval; | |
593 | long ts, tEnd; | |
594 | ||
595 | /* Make sure the time range makes sense */ | |
596 | if (t2 <= t1) { | |
597 | throw new TimeRangeException(); | |
598 | } | |
599 | ||
600 | /* Set the actual, valid end time of the range query */ | |
601 | if (t2 > this.getCurrentEndTime()) { | |
602 | tEnd = this.getCurrentEndTime(); | |
603 | } else { | |
604 | tEnd = t2; | |
605 | } | |
606 | ||
607 | /* Get the initial state at time T1 */ | |
608 | intervals = new ArrayList<ITmfStateInterval>(); | |
609 | currentInterval = querySingleState(t1, attributeQuark); | |
610 | intervals.add(currentInterval); | |
611 | ||
612 | /* Get the following state changes */ | |
613 | ts = currentInterval.getEndTime(); | |
614 | while (ts != -1 && ts < tEnd) { | |
615 | ts++; /* To "jump over" to the next state in the history */ | |
616 | currentInterval = querySingleState(ts, attributeQuark); | |
617 | intervals.add(currentInterval); | |
618 | ts = currentInterval.getEndTime(); | |
619 | } | |
620 | return intervals; | |
621 | } | |
622 | ||
623 | @Override | |
624 | public List<ITmfStateInterval> queryHistoryRange(int attributeQuark, | |
b5a8d0cc | 625 | long t1, long t2, long resolution, IProgressMonitor monitor) |
96345c5a AM |
626 | throws TimeRangeException, AttributeNotFoundException, |
627 | StateSystemDisposedException { | |
628 | if (isDisposed) { | |
629 | throw new StateSystemDisposedException(); | |
630 | } | |
631 | ||
8d1346f0 AM |
632 | List<ITmfStateInterval> intervals; |
633 | ITmfStateInterval currentInterval; | |
634 | long ts, tEnd; | |
635 | ||
41b5c37f AM |
636 | IProgressMonitor mon = monitor; |
637 | if (mon == null) { | |
638 | mon = new NullProgressMonitor(); | |
b5a8d0cc AM |
639 | } |
640 | ||
8d1346f0 AM |
641 | /* Make sure the time range makes sense */ |
642 | if (t2 < t1 || resolution <= 0) { | |
643 | throw new TimeRangeException(); | |
644 | } | |
645 | ||
646 | /* Set the actual, valid end time of the range query */ | |
647 | if (t2 > this.getCurrentEndTime()) { | |
648 | tEnd = this.getCurrentEndTime(); | |
649 | } else { | |
650 | tEnd = t2; | |
651 | } | |
652 | ||
653 | /* Get the initial state at time T1 */ | |
654 | intervals = new ArrayList<ITmfStateInterval>(); | |
655 | currentInterval = querySingleState(t1, attributeQuark); | |
656 | intervals.add(currentInterval); | |
657 | ||
658 | /* | |
659 | * Iterate over the "resolution points". We skip unneeded queries in the | |
660 | * case the current interval is longer than the resolution. | |
661 | */ | |
662 | for (ts = t1; (currentInterval.getEndTime() != -1) && (ts < tEnd); | |
663 | ts += resolution) { | |
41b5c37f | 664 | if (mon.isCanceled()) { |
8d1346f0 AM |
665 | return intervals; |
666 | } | |
667 | if (ts <= currentInterval.getEndTime()) { | |
668 | continue; | |
669 | } | |
670 | currentInterval = querySingleState(ts, attributeQuark); | |
671 | intervals.add(currentInterval); | |
672 | } | |
673 | ||
674 | /* Add the interval at t2, if it wasn't included already. */ | |
675 | if (currentInterval.getEndTime() < tEnd) { | |
676 | currentInterval = querySingleState(tEnd, attributeQuark); | |
677 | intervals.add(currentInterval); | |
678 | } | |
679 | return intervals; | |
680 | } | |
681 | ||
682 | //-------------------------------------------------------------------------- | |
683 | // Debug methods | |
684 | //-------------------------------------------------------------------------- | |
685 | ||
686 | static void logMissingInterval(int attribute, long timestamp) { | |
5500a7f0 | 687 | Activator.logInfo("No data found in history for attribute " + //$NON-NLS-1$ |
8d1346f0 AM |
688 | attribute + " at time " + timestamp + //$NON-NLS-1$ |
689 | ", returning dummy interval"); //$NON-NLS-1$ | |
a52fde77 AM |
690 | } |
691 | ||
692 | /** | |
693 | * Print out the contents of the inner structures. | |
5df842b3 | 694 | * |
a52fde77 AM |
695 | * @param writer |
696 | * The PrintWriter in which to print the output | |
697 | */ | |
698 | public void debugPrint(PrintWriter writer) { | |
699 | attributeTree.debugPrint(writer); | |
700 | transState.debugPrint(writer); | |
8d1346f0 | 701 | backend.debugPrint(writer); |
a52fde77 AM |
702 | } |
703 | ||
8d1346f0 | 704 | } |