Commit | Line | Data |
---|---|---|
a52fde77 | 1 | /******************************************************************************* |
c44f0a0c | 2 | * Copyright (c) 2012, 2016 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 | * |
e13bd4cd PT |
11 | * Contributors: |
12 | * Alexandre Montplaisir - Initial API and implementation | |
13 | * Patrick Tasse - Add message to exceptions | |
a52fde77 AM |
14 | *******************************************************************************/ |
15 | ||
e894a508 | 16 | package org.eclipse.tracecompass.internal.statesystem.core; |
a52fde77 | 17 | |
8d1346f0 AM |
18 | import java.io.File; |
19 | import java.io.IOException; | |
a52fde77 | 20 | import java.io.PrintWriter; |
8d1346f0 | 21 | import java.util.ArrayList; |
c44f0a0c | 22 | import java.util.Arrays; |
f94a0bac | 23 | import java.util.LinkedList; |
a52fde77 | 24 | import java.util.List; |
16576a7e | 25 | import java.util.concurrent.CountDownLatch; |
9287b6a2 | 26 | import java.util.concurrent.TimeUnit; |
a52fde77 | 27 | |
2e21b6d8 | 28 | import org.eclipse.jdt.annotation.NonNull; |
df2597e0 | 29 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 AM |
30 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
31 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; | |
32 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
33 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; | |
34 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
35 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
36 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
e894a508 | 37 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
e894a508 | 38 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type; |
1dd75589 | 39 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; |
a52fde77 | 40 | |
aa315d8b PT |
41 | import com.google.common.collect.ImmutableCollection.Builder; |
42 | import com.google.common.collect.ImmutableSet; | |
43 | ||
a52fde77 | 44 | /** |
8d1346f0 AM |
45 | * This is the core class of the Generic State System. It contains all the |
46 | * methods to build and query a state history. It's exposed externally through | |
47 | * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the | |
48 | * user needs read-only access or read-write access. | |
5df842b3 | 49 | * |
8d1346f0 AM |
50 | * When building, DON'T FORGET to call .closeHistory() when you are done |
51 | * inserting intervals, or the storage backend will have no way of knowing it | |
52 | * can close and write itself to disk, and its thread will keep running. | |
5df842b3 | 53 | * |
a52fde77 | 54 | * @author alexmont |
5df842b3 | 55 | * |
a52fde77 | 56 | */ |
f1f86dfb | 57 | public class StateSystem implements ITmfStateSystemBuilder { |
a52fde77 | 58 | |
aa315d8b PT |
59 | private static final String PARENT = ".."; //$NON-NLS-1$ |
60 | private static final String WILDCARD = "*"; //$NON-NLS-1$ | |
61 | ||
a52fde77 | 62 | /* References to the inner structures */ |
8d1346f0 AM |
63 | private final AttributeTree attributeTree; |
64 | private final TransientState transState; | |
65 | private final IStateHistoryBackend backend; | |
a52fde77 | 66 | |
16576a7e AM |
67 | /* Latch tracking if the state history is done building or not */ |
68 | private final CountDownLatch finishedLatch = new CountDownLatch(1); | |
69 | ||
1a4205d9 | 70 | private boolean buildCancelled = false; |
96345c5a | 71 | private boolean isDisposed = false; |
1a4205d9 | 72 | |
f9a76cac AM |
73 | /** |
74 | * New-file constructor. For when you build a state system with a new file, | |
75 | * or if the back-end does not require a file on disk. | |
76 | * | |
77 | * @param backend | |
78 | * Back-end plugin to use | |
79 | */ | |
b2f62cb5 | 80 | public StateSystem(@NonNull IStateHistoryBackend backend) { |
f9a76cac AM |
81 | this.backend = backend; |
82 | this.transState = new TransientState(backend); | |
83 | this.attributeTree = new AttributeTree(this); | |
84 | } | |
85 | ||
a52fde77 | 86 | /** |
8d1346f0 AM |
87 | * General constructor |
88 | * | |
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 | */ |
b2f62cb5 | 97 | public StateSystem(@NonNull IStateHistoryBackend backend, boolean newFile) |
8d1346f0 AM |
98 | throws IOException { |
99 | this.backend = backend; | |
100 | this.transState = new TransientState(backend); | |
a52fde77 | 101 | |
8d1346f0 AM |
102 | if (newFile) { |
103 | attributeTree = new AttributeTree(this); | |
104 | } else { | |
105 | /* We're opening an existing file */ | |
106 | this.attributeTree = new AttributeTree(this, backend.supplyAttributeTreeReader()); | |
107 | transState.setInactive(); | |
16576a7e AM |
108 | finishedLatch.countDown(); /* The history is already built */ |
109 | } | |
110 | } | |
111 | ||
84a9548a AM |
112 | @Override |
113 | public String getSSID() { | |
b2f62cb5 | 114 | return backend.getSSID(); |
84a9548a AM |
115 | } |
116 | ||
16576a7e | 117 | @Override |
2002c638 AM |
118 | public boolean isCancelled() { |
119 | return buildCancelled; | |
120 | } | |
121 | ||
122 | @Override | |
123 | public void waitUntilBuilt() { | |
16576a7e AM |
124 | try { |
125 | finishedLatch.await(); | |
126 | } catch (InterruptedException e) { | |
127 | e.printStackTrace(); | |
8d1346f0 | 128 | } |
1a4205d9 AM |
129 | } |
130 | ||
9287b6a2 AM |
131 | @Override |
132 | public boolean waitUntilBuilt(long timeout) { | |
133 | boolean ret = false; | |
134 | try { | |
135 | ret = finishedLatch.await(timeout, TimeUnit.MILLISECONDS); | |
136 | } catch (InterruptedException e) { | |
137 | e.printStackTrace(); | |
138 | } | |
139 | return ret; | |
140 | } | |
141 | ||
1a4205d9 AM |
142 | @Override |
143 | public synchronized void dispose() { | |
96345c5a | 144 | isDisposed = true; |
1a4205d9 AM |
145 | if (transState.isActive()) { |
146 | transState.setInactive(); | |
147 | buildCancelled = true; | |
148 | } | |
149 | backend.dispose(); | |
a52fde77 AM |
150 | } |
151 | ||
8d1346f0 AM |
152 | //-------------------------------------------------------------------------- |
153 | // General methods related to the attribute tree | |
154 | //-------------------------------------------------------------------------- | |
155 | ||
339d27b4 AM |
156 | /** |
157 | * Get the attribute tree associated with this state system. This should be | |
158 | * the only way of accessing it (and if subclasses want to point to a | |
159 | * different attribute tree than their own, they should only need to | |
160 | * override this). | |
161 | * | |
162 | * @return The attribute tree | |
163 | */ | |
164 | public AttributeTree getAttributeTree() { | |
165 | return attributeTree; | |
166 | } | |
167 | ||
8d1346f0 AM |
168 | /** |
169 | * Method used by the attribute tree when creating new attributes, to keep | |
170 | * the attribute count in the transient state in sync. | |
171 | */ | |
bcec0116 | 172 | public void addEmptyAttribute() { |
8d1346f0 AM |
173 | transState.addEmptyEntry(); |
174 | } | |
175 | ||
176 | @Override | |
4623f57f | 177 | public int getNbAttributes() { |
339d27b4 | 178 | return getAttributeTree().getNbAttributes(); |
4623f57f AM |
179 | } |
180 | ||
8d1346f0 AM |
181 | @Override |
182 | public String getAttributeName(int attributeQuark) { | |
339d27b4 | 183 | return getAttributeTree().getAttributeName(attributeQuark); |
8d1346f0 AM |
184 | } |
185 | ||
186 | @Override | |
187 | public String getFullAttributePath(int attributeQuark) { | |
339d27b4 | 188 | return getAttributeTree().getFullAttributeName(attributeQuark); |
8d1346f0 AM |
189 | } |
190 | ||
34638411 AM |
191 | @Override |
192 | public String[] getFullAttributePathArray(int attributeQuark) { | |
193 | return getAttributeTree().getFullAttributePathArray(attributeQuark); | |
194 | } | |
195 | ||
8d1346f0 AM |
196 | //-------------------------------------------------------------------------- |
197 | // Methods related to the storage backend | |
198 | //-------------------------------------------------------------------------- | |
a52fde77 | 199 | |
8d1346f0 AM |
200 | @Override |
201 | public long getStartTime() { | |
202 | return backend.getStartTime(); | |
203 | } | |
204 | ||
205 | @Override | |
206 | public long getCurrentEndTime() { | |
207 | return backend.getEndTime(); | |
208 | } | |
209 | ||
210 | @Override | |
211 | public void closeHistory(long endTime) throws TimeRangeException { | |
212 | File attributeTreeFile; | |
213 | long attributeTreeFilePos; | |
214 | long realEndTime = endTime; | |
215 | ||
216 | if (realEndTime < backend.getEndTime()) { | |
217 | /* | |
218 | * This can happen (empty nodes pushing the border further, etc.) | |
219 | * but shouldn't be too big of a deal. | |
220 | */ | |
221 | realEndTime = backend.getEndTime(); | |
222 | } | |
223 | transState.closeTransientState(realEndTime); | |
224 | backend.finishedBuilding(realEndTime); | |
225 | ||
226 | attributeTreeFile = backend.supplyAttributeTreeWriterFile(); | |
227 | attributeTreeFilePos = backend.supplyAttributeTreeWriterFilePosition(); | |
228 | if (attributeTreeFile != null) { | |
229 | /* | |
230 | * If null was returned, we simply won't save the attribute tree, | |
231 | * too bad! | |
232 | */ | |
339d27b4 | 233 | getAttributeTree().writeSelf(attributeTreeFile, attributeTreeFilePos); |
8d1346f0 | 234 | } |
16576a7e | 235 | finishedLatch.countDown(); /* Mark the history as finished building */ |
8d1346f0 AM |
236 | } |
237 | ||
238 | //-------------------------------------------------------------------------- | |
239 | // Quark-retrieving methods | |
240 | //-------------------------------------------------------------------------- | |
241 | ||
242 | @Override | |
a52fde77 AM |
243 | public int getQuarkAbsolute(String... attribute) |
244 | throws AttributeNotFoundException { | |
c44f0a0c PT |
245 | int quark = getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute); |
246 | if (quark == INVALID_ATTRIBUTE) { | |
247 | throw new AttributeNotFoundException(getSSID() + " Path:" + Arrays.toString(attribute)); //$NON-NLS-1$ | |
248 | } | |
249 | return quark; | |
250 | } | |
251 | ||
252 | @Override | |
253 | public int optQuarkAbsolute(String... attribute) { | |
254 | return getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute); | |
a52fde77 AM |
255 | } |
256 | ||
8d1346f0 | 257 | @Override |
a52fde77 | 258 | public int getQuarkAbsoluteAndAdd(String... attribute) { |
c44f0a0c | 259 | return getAttributeTree().getQuarkAndAdd(ROOT_ATTRIBUTE, attribute); |
a52fde77 AM |
260 | } |
261 | ||
8d1346f0 | 262 | @Override |
a52fde77 AM |
263 | public int getQuarkRelative(int startingNodeQuark, String... subPath) |
264 | throws AttributeNotFoundException { | |
c44f0a0c PT |
265 | int quark = getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath); |
266 | if (quark == INVALID_ATTRIBUTE) { | |
267 | throw new AttributeNotFoundException(getSSID() + " Quark:" + startingNodeQuark + ", SubPath:" + Arrays.toString(subPath)); //$NON-NLS-1$ //$NON-NLS-2$ | |
268 | } | |
269 | return quark; | |
270 | } | |
271 | ||
272 | @Override | |
273 | public int optQuarkRelative(int startingNodeQuark, String... subPath) { | |
339d27b4 | 274 | return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath); |
a52fde77 AM |
275 | } |
276 | ||
8d1346f0 | 277 | @Override |
a52fde77 | 278 | public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) { |
339d27b4 | 279 | return getAttributeTree().getQuarkAndAdd(startingNodeQuark, subPath); |
a52fde77 AM |
280 | } |
281 | ||
8d1346f0 | 282 | @Override |
ed48dc75 | 283 | public List<@NonNull Integer> getSubAttributes(int quark, boolean recursive) { |
339d27b4 | 284 | return getAttributeTree().getSubAttributes(quark, recursive); |
0a9de3d2 AM |
285 | } |
286 | ||
5206c858 | 287 | @Override |
ed48dc75 | 288 | public List<@NonNull Integer> getSubAttributes(int quark, boolean recursive, String pattern) { |
5206c858 | 289 | List<Integer> all = getSubAttributes(quark, recursive); |
aa315d8b | 290 | List<@NonNull Integer> ret = new LinkedList<>(); |
5206c858 AM |
291 | for (Integer attQuark : all) { |
292 | String name = getAttributeName(attQuark.intValue()); | |
293 | if (name.matches(pattern)) { | |
294 | ret.add(attQuark); | |
295 | } | |
296 | } | |
297 | return ret; | |
298 | } | |
299 | ||
0fdd2c45 FG |
300 | @Override |
301 | public int getParentAttributeQuark(int quark) { | |
302 | return getAttributeTree().getParentAttributeQuark(quark); | |
303 | } | |
304 | ||
8d1346f0 | 305 | @Override |
df2597e0 | 306 | public List<@NonNull Integer> getQuarks(String... pattern) { |
aa315d8b PT |
307 | return getQuarks(ROOT_ATTRIBUTE, pattern); |
308 | } | |
f94a0bac | 309 | |
aa315d8b PT |
310 | @Override |
311 | public List<@NonNull Integer> getQuarks(int startingNodeQuark, String... pattern) { | |
312 | Builder<@NonNull Integer> builder = ImmutableSet.builder(); | |
313 | if (pattern.length > 0) { | |
314 | getQuarks(builder, startingNodeQuark, Arrays.asList(pattern)); | |
c44f0a0c | 315 | } else { |
aa315d8b | 316 | builder.add(startingNodeQuark); |
f94a0bac | 317 | } |
aa315d8b PT |
318 | return builder.build().asList(); |
319 | } | |
f94a0bac | 320 | |
aa315d8b | 321 | private void getQuarks(Builder<@NonNull Integer> builder, int quark, List<String> pattern) { |
ed48dc75 PT |
322 | String element = pattern.get(0); |
323 | if (element == null) { | |
324 | return; | |
325 | } | |
326 | List<String> remainder = pattern.subList(1, pattern.size()); | |
327 | if (remainder.isEmpty()) { | |
328 | if (element.equals(WILDCARD)) { | |
329 | builder.addAll(getSubAttributes(quark, false)); | |
330 | } else if (element.equals(PARENT)){ | |
331 | builder.add(getParentAttributeQuark(quark)); | |
332 | } else { | |
333 | int subQuark = optQuarkRelative(quark, element); | |
334 | if (subQuark != INVALID_ATTRIBUTE) { | |
335 | builder.add(subQuark); | |
336 | } | |
f94a0bac | 337 | } |
ed48dc75 PT |
338 | } else { |
339 | if (element.equals(WILDCARD)) { | |
340 | for (@NonNull Integer subquark : getSubAttributes(quark, false)) { | |
341 | getQuarks(builder, subquark, remainder); | |
aa315d8b | 342 | } |
ed48dc75 PT |
343 | } else if (element.equals(PARENT)){ |
344 | getQuarks(builder, getParentAttributeQuark(quark), remainder); | |
aa315d8b | 345 | } else { |
ed48dc75 PT |
346 | int subQuark = optQuarkRelative(quark, element); |
347 | if (subQuark != INVALID_ATTRIBUTE) { | |
348 | getQuarks(builder, subQuark, remainder); | |
aa315d8b PT |
349 | } |
350 | } | |
f94a0bac | 351 | } |
f94a0bac AM |
352 | } |
353 | ||
8d1346f0 AM |
354 | //-------------------------------------------------------------------------- |
355 | // Methods related to insertions in the history | |
356 | //-------------------------------------------------------------------------- | |
a52fde77 | 357 | |
8d1346f0 | 358 | @Override |
a52fde77 | 359 | public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark) |
ed48dc75 | 360 | throws TimeRangeException, StateValueTypeException { |
feea3b3c AM |
361 | if (value == null) { |
362 | /* | |
363 | * TODO Replace with @NonNull parameter (will require fixing all the | |
364 | * state providers!) | |
365 | */ | |
366 | throw new IllegalArgumentException(); | |
367 | } | |
a52fde77 AM |
368 | transState.processStateChange(t, value, attributeQuark); |
369 | } | |
370 | ||
acec47ce | 371 | @Deprecated |
8d1346f0 | 372 | @Override |
a52fde77 | 373 | public void incrementAttribute(long t, int attributeQuark) |
ed48dc75 | 374 | throws StateValueTypeException, TimeRangeException { |
359eeba0 PT |
375 | ITmfStateValue stateValue = queryOngoingState(attributeQuark); |
376 | int prevValue = 0; | |
377 | /* if the attribute was previously null, start counting at 0 */ | |
378 | if (!stateValue.isNull()) { | |
379 | prevValue = stateValue.unboxInt(); | |
280bbdbb | 380 | } |
a52fde77 AM |
381 | modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1), |
382 | attributeQuark); | |
383 | } | |
384 | ||
8d1346f0 | 385 | @Override |
a52fde77 | 386 | public void pushAttribute(long t, ITmfStateValue value, int attributeQuark) |
ed48dc75 | 387 | throws TimeRangeException, StateValueTypeException { |
0126a8ca | 388 | int stackDepth; |
a52fde77 AM |
389 | int subAttributeQuark; |
390 | ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark); | |
391 | ||
392 | if (previousSV.isNull()) { | |
393 | /* | |
394 | * If the StateValue was null, this means this is the first time we | |
395 | * use this attribute. Leave stackDepth at 0. | |
396 | */ | |
cb42195c | 397 | stackDepth = 0; |
b67a2540 | 398 | } else if (previousSV.getType() == Type.INTEGER) { |
a52fde77 AM |
399 | /* Previous value was an integer, all is good, use it */ |
400 | stackDepth = previousSV.unboxInt(); | |
a52fde77 AM |
401 | } else { |
402 | /* Previous state of this attribute was another type? Not good! */ | |
e13bd4cd | 403 | throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Type:" + previousSV.getType() + ", Expected:" + Type.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
90a25ebe AM |
404 | } |
405 | ||
e8251298 | 406 | if (stackDepth >= 100000) { |
90a25ebe | 407 | /* |
a0f8fb9b FW |
408 | * Limit stackDepth to 100000, to avoid having Attribute Trees grow |
409 | * out of control due to buggy insertions | |
90a25ebe | 410 | */ |
e13bd4cd | 411 | String message = " Stack limit reached, not pushing"; //$NON-NLS-1$ |
ed48dc75 | 412 | throw new IllegalStateException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$ |
a52fde77 AM |
413 | } |
414 | ||
415 | stackDepth++; | |
0126a8ca | 416 | subAttributeQuark = getQuarkRelativeAndAdd(attributeQuark, String.valueOf(stackDepth)); |
a52fde77 | 417 | |
5896eb76 | 418 | modifyAttribute(t, TmfStateValue.newValueInt(stackDepth), attributeQuark); |
90a25ebe | 419 | modifyAttribute(t, value, subAttributeQuark); |
a52fde77 AM |
420 | } |
421 | ||
8d1346f0 | 422 | @Override |
5896eb76 | 423 | public ITmfStateValue popAttribute(long t, int attributeQuark) |
ed48dc75 | 424 | throws TimeRangeException, StateValueTypeException { |
e2eac108 | 425 | /* These are the state values of the stack-attribute itself */ |
e13bd4cd | 426 | ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark); |
a52fde77 AM |
427 | |
428 | if (previousSV.isNull()) { | |
e2eac108 AM |
429 | /* |
430 | * Trying to pop an empty stack. This often happens at the start of | |
431 | * traces, for example when we see a syscall_exit, without having | |
432 | * the corresponding syscall_entry in the trace. Just ignore | |
433 | * silently. | |
434 | */ | |
5896eb76 | 435 | return null; |
90a25ebe | 436 | } |
b67a2540 | 437 | if (previousSV.getType() != Type.INTEGER) { |
a52fde77 | 438 | /* |
b67a2540 AM |
439 | * The existing value was not an integer (which is expected for |
440 | * stack tops), this doesn't look like a valid stack attribute. | |
a52fde77 | 441 | */ |
e13bd4cd | 442 | throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Type:" + previousSV.getType() + ", Expected:" + Type.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
a52fde77 AM |
443 | } |
444 | ||
0126a8ca | 445 | int stackDepth = previousSV.unboxInt(); |
90a25ebe | 446 | |
e2eac108 | 447 | if (stackDepth <= 0) { |
a52fde77 | 448 | /* This on the other hand should not happen... */ |
e13bd4cd | 449 | throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark + ", Stack depth:" + stackDepth); //$NON-NLS-1$//$NON-NLS-2$ |
a52fde77 AM |
450 | } |
451 | ||
e2eac108 | 452 | /* The attribute should already exist at this point */ |
ed48dc75 PT |
453 | int subAttributeQuark; |
454 | try { | |
455 | subAttributeQuark = getQuarkRelative(attributeQuark, String.valueOf(stackDepth)); | |
456 | } catch (AttributeNotFoundException e) { | |
457 | String message = " Stack attribute missing sub-attribute for depth:" + stackDepth; //$NON-NLS-1$ | |
458 | throw new IllegalStateException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$ | |
459 | } | |
5896eb76 | 460 | ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark); |
a52fde77 | 461 | |
e2eac108 AM |
462 | /* Update the state value of the stack-attribute */ |
463 | ITmfStateValue nextSV; | |
a0f8fb9b | 464 | if (--stackDepth == 0) { |
359eeba0 | 465 | /* Store a null state value */ |
e2eac108 AM |
466 | nextSV = TmfStateValue.nullValue(); |
467 | } else { | |
468 | nextSV = TmfStateValue.newValueInt(stackDepth); | |
469 | } | |
470 | modifyAttribute(t, nextSV, attributeQuark); | |
471 | ||
472 | /* Delete the sub-attribute that contained the user's state value */ | |
a52fde77 | 473 | removeAttribute(t, subAttributeQuark); |
e2eac108 | 474 | |
5896eb76 | 475 | return poppedValue; |
a52fde77 AM |
476 | } |
477 | ||
8d1346f0 | 478 | @Override |
a52fde77 | 479 | public void removeAttribute(long t, int attributeQuark) |
ed48dc75 | 480 | throws TimeRangeException { |
c66426fd | 481 | /* |
feea3b3c | 482 | * Nullify our children first, recursively. We pass 'false' because we |
c66426fd AM |
483 | * handle the recursion ourselves. |
484 | */ | |
feea3b3c | 485 | List<Integer> childAttributes = getSubAttributes(attributeQuark, false); |
0126a8ca | 486 | for (int childNodeQuark : childAttributes) { |
feea3b3c AM |
487 | if (attributeQuark == childNodeQuark) { |
488 | /* Something went very wrong when building out attribute tree */ | |
489 | throw new IllegalStateException(); | |
490 | } | |
a52fde77 AM |
491 | removeAttribute(t, childNodeQuark); |
492 | } | |
493 | /* Nullify ourselves */ | |
7e0b2b56 | 494 | try { |
feea3b3c | 495 | transState.processStateChange(t, TmfStateValue.nullValue(), attributeQuark); |
7e0b2b56 | 496 | } catch (StateValueTypeException e) { |
50678114 AM |
497 | /* |
498 | * Will not happen since we're inserting null values only, but poor | |
499 | * compiler has no way of knowing this... | |
7e0b2b56 | 500 | */ |
cb42195c | 501 | throw new IllegalStateException(e); |
7e0b2b56 | 502 | } |
a52fde77 AM |
503 | } |
504 | ||
8d1346f0 AM |
505 | //-------------------------------------------------------------------------- |
506 | // "Current" query/update methods | |
507 | //-------------------------------------------------------------------------- | |
a52fde77 | 508 | |
8d1346f0 | 509 | @Override |
ed48dc75 | 510 | public ITmfStateValue queryOngoingState(int attributeQuark) { |
a52fde77 AM |
511 | return transState.getOngoingStateValue(attributeQuark); |
512 | } | |
513 | ||
602c0697 | 514 | @Override |
ed48dc75 | 515 | public long getOngoingStartTime(int attribute) { |
602c0697 AM |
516 | return transState.getOngoingStartTime(attribute); |
517 | } | |
518 | ||
8d1346f0 | 519 | @Override |
ed48dc75 | 520 | public void updateOngoingState(ITmfStateValue newValue, int attributeQuark) { |
a52fde77 AM |
521 | transState.changeOngoingStateValue(attributeQuark, newValue); |
522 | } | |
523 | ||
66866869 AM |
524 | /** |
525 | * Modify the whole "ongoing state" (state values + start times). This can | |
526 | * be used when "seeking" a state system to a different point in the trace | |
527 | * (and restoring the known stateInfo at this location). Use with care! | |
528 | * | |
529 | * @param newStateIntervals | |
530 | * The new List of state values to use as ongoing state info | |
531 | */ | |
df2597e0 | 532 | protected void replaceOngoingState(@NonNull List<@NonNull ITmfStateInterval> newStateIntervals) { |
66866869 | 533 | transState.replaceOngoingState(newStateIntervals); |
a0f8fb9b | 534 | } |
66866869 | 535 | |
8d1346f0 AM |
536 | //-------------------------------------------------------------------------- |
537 | // Regular query methods (sent to the back-end) | |
538 | //-------------------------------------------------------------------------- | |
539 | ||
540 | @Override | |
541 | public synchronized List<ITmfStateInterval> queryFullState(long t) | |
96345c5a AM |
542 | throws TimeRangeException, StateSystemDisposedException { |
543 | if (isDisposed) { | |
544 | throw new StateSystemDisposedException(); | |
545 | } | |
546 | ||
e62a23a9 | 547 | final int nbAttr = getNbAttributes(); |
df2597e0 | 548 | List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr); |
8d1346f0 AM |
549 | |
550 | /* Bring the size of the array to the current number of attributes */ | |
e62a23a9 | 551 | for (int i = 0; i < nbAttr; i++) { |
8d1346f0 AM |
552 | stateInfo.add(null); |
553 | } | |
554 | ||
8d1346f0 AM |
555 | /* |
556 | * If we are currently building the history, also query the "ongoing" | |
557 | * states for stuff that might not yet be written to the history. | |
558 | */ | |
559 | if (transState.isActive()) { | |
560 | transState.doQuery(stateInfo, t); | |
561 | } | |
562 | ||
e62a23a9 AM |
563 | /* Query the storage backend */ |
564 | backend.doQuery(stateInfo, t); | |
565 | ||
8d1346f0 AM |
566 | /* |
567 | * We should have previously inserted an interval for every attribute. | |
8d1346f0 | 568 | */ |
e62a23a9 AM |
569 | for (ITmfStateInterval interval : stateInfo) { |
570 | if (interval == null) { | |
571 | throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$ | |
8d1346f0 AM |
572 | } |
573 | } | |
574 | return stateInfo; | |
50678114 AM |
575 | } |
576 | ||
8d1346f0 AM |
577 | @Override |
578 | public ITmfStateInterval querySingleState(long t, int attributeQuark) | |
ed48dc75 | 579 | throws TimeRangeException, StateSystemDisposedException { |
96345c5a AM |
580 | if (isDisposed) { |
581 | throw new StateSystemDisposedException(); | |
582 | } | |
8d1346f0 | 583 | |
09e6fd9b AM |
584 | ITmfStateInterval ret = transState.getIntervalAt(t, attributeQuark); |
585 | if (ret == null) { | |
586 | /* | |
587 | * The transient state did not have the information, let's look into | |
588 | * the backend next. | |
589 | */ | |
8d1346f0 AM |
590 | ret = backend.doSingularQuery(t, attributeQuark); |
591 | } | |
592 | ||
8d1346f0 | 593 | if (ret == null) { |
e62a23a9 AM |
594 | /* |
595 | * If we did our job correctly, there should be intervals for every | |
596 | * possible attribute, over all the valid time range. | |
597 | */ | |
598 | throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$ | |
8d1346f0 AM |
599 | } |
600 | return ret; | |
601 | } | |
602 | ||
8d1346f0 AM |
603 | //-------------------------------------------------------------------------- |
604 | // Debug methods | |
605 | //-------------------------------------------------------------------------- | |
606 | ||
607 | static void logMissingInterval(int attribute, long timestamp) { | |
bcec0116 | 608 | Activator.getDefault().logInfo("No data found in history for attribute " + //$NON-NLS-1$ |
8d1346f0 AM |
609 | attribute + " at time " + timestamp + //$NON-NLS-1$ |
610 | ", returning dummy interval"); //$NON-NLS-1$ | |
a52fde77 AM |
611 | } |
612 | ||
613 | /** | |
614 | * Print out the contents of the inner structures. | |
5df842b3 | 615 | * |
a52fde77 AM |
616 | * @param writer |
617 | * The PrintWriter in which to print the output | |
618 | */ | |
feea3b3c | 619 | public void debugPrint(@NonNull PrintWriter writer) { |
339d27b4 | 620 | getAttributeTree().debugPrint(writer); |
a52fde77 | 621 | transState.debugPrint(writer); |
8d1346f0 | 622 | backend.debugPrint(writer); |
a52fde77 AM |
623 | } |
624 | ||
8d1346f0 | 625 | } |