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