| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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.internal.tmf.core.statesystem; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.io.IOException; |
| 17 | import java.io.PrintWriter; |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import org.eclipse.core.runtime.IProgressMonitor; |
| 25 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 26 | import org.eclipse.jdt.annotation.NonNull; |
| 27 | import org.eclipse.linuxtools.internal.tmf.core.Activator; |
| 28 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend; |
| 29 | import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException; |
| 30 | import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException; |
| 31 | import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException; |
| 32 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; |
| 33 | import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval; |
| 34 | import org.eclipse.linuxtools.tmf.core.interval.TmfStateInterval; |
| 35 | import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder; |
| 36 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; |
| 37 | import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue.Type; |
| 38 | import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue; |
| 39 | |
| 40 | /** |
| 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. |
| 45 | * |
| 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. |
| 49 | * |
| 50 | * @author alexmont |
| 51 | * |
| 52 | */ |
| 53 | public class StateSystem implements ITmfStateSystemBuilder { |
| 54 | |
| 55 | private final String ssid; |
| 56 | |
| 57 | /* References to the inner structures */ |
| 58 | private final AttributeTree attributeTree; |
| 59 | private final TransientState transState; |
| 60 | private final IStateHistoryBackend backend; |
| 61 | |
| 62 | /* Latch tracking if the state history is done building or not */ |
| 63 | private final CountDownLatch finishedLatch = new CountDownLatch(1); |
| 64 | |
| 65 | private boolean buildCancelled = false; |
| 66 | private boolean isDisposed = false; |
| 67 | |
| 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 | * |
| 72 | * @param ssid |
| 73 | * The ID of this statesystem. It should be unique. |
| 74 | * @param backend |
| 75 | * Back-end plugin to use |
| 76 | */ |
| 77 | public StateSystem(@NonNull String ssid, @NonNull IStateHistoryBackend backend) { |
| 78 | this.ssid = ssid; |
| 79 | this.backend = backend; |
| 80 | this.transState = new TransientState(backend); |
| 81 | this.attributeTree = new AttributeTree(this); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * General constructor |
| 86 | * |
| 87 | * @param ssid |
| 88 | * The ID of this statesystem. It should be unique. |
| 89 | * @param backend |
| 90 | * The "state history storage" back-end to use. |
| 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 |
| 96 | */ |
| 97 | public StateSystem(@NonNull String ssid, @NonNull IStateHistoryBackend backend, boolean newFile) |
| 98 | throws IOException { |
| 99 | this.ssid = ssid; |
| 100 | this.backend = backend; |
| 101 | this.transState = new TransientState(backend); |
| 102 | |
| 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(); |
| 109 | finishedLatch.countDown(); /* The history is already built */ |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String getSSID() { |
| 115 | return ssid; |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean isCancelled() { |
| 120 | return buildCancelled; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void waitUntilBuilt() { |
| 125 | try { |
| 126 | finishedLatch.await(); |
| 127 | } catch (InterruptedException e) { |
| 128 | e.printStackTrace(); |
| 129 | } |
| 130 | } |
| 131 | |
| 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 | |
| 143 | @Override |
| 144 | public synchronized void dispose() { |
| 145 | isDisposed = true; |
| 146 | if (transState.isActive()) { |
| 147 | transState.setInactive(); |
| 148 | buildCancelled = true; |
| 149 | } |
| 150 | backend.dispose(); |
| 151 | } |
| 152 | |
| 153 | //-------------------------------------------------------------------------- |
| 154 | // General methods related to the attribute tree |
| 155 | //-------------------------------------------------------------------------- |
| 156 | |
| 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 | |
| 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 | */ |
| 173 | protected void addEmptyAttribute() { |
| 174 | transState.addEmptyEntry(); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public int getNbAttributes() { |
| 179 | return getAttributeTree().getNbAttributes(); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public String getAttributeName(int attributeQuark) { |
| 184 | return getAttributeTree().getAttributeName(attributeQuark); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public String getFullAttributePath(int attributeQuark) { |
| 189 | return getAttributeTree().getFullAttributeName(attributeQuark); |
| 190 | } |
| 191 | |
| 192 | //-------------------------------------------------------------------------- |
| 193 | // Methods related to the storage backend |
| 194 | //-------------------------------------------------------------------------- |
| 195 | |
| 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 | */ |
| 229 | getAttributeTree().writeSelf(attributeTreeFile, attributeTreeFilePos); |
| 230 | } |
| 231 | finishedLatch.countDown(); /* Mark the history as finished building */ |
| 232 | } |
| 233 | |
| 234 | //-------------------------------------------------------------------------- |
| 235 | // Quark-retrieving methods |
| 236 | //-------------------------------------------------------------------------- |
| 237 | |
| 238 | @Override |
| 239 | public int getQuarkAbsolute(String... attribute) |
| 240 | throws AttributeNotFoundException { |
| 241 | return getAttributeTree().getQuarkDontAdd(-1, attribute); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public int getQuarkAbsoluteAndAdd(String... attribute) { |
| 246 | return getAttributeTree().getQuarkAndAdd(-1, attribute); |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public int getQuarkRelative(int startingNodeQuark, String... subPath) |
| 251 | throws AttributeNotFoundException { |
| 252 | return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public int getQuarkRelativeAndAdd(int startingNodeQuark, String... subPath) { |
| 257 | return getAttributeTree().getQuarkAndAdd(startingNodeQuark, subPath); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public List<Integer> getSubAttributes(int quark, boolean recursive) |
| 262 | throws AttributeNotFoundException { |
| 263 | return getAttributeTree().getSubAttributes(quark, recursive); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public List<Integer> getQuarks(String... pattern) { |
| 268 | List<Integer> quarks = new LinkedList<>(); |
| 269 | List<String> prefix = new LinkedList<>(); |
| 270 | List<String> suffix = new LinkedList<>(); |
| 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 | */ |
| 305 | if (!split) { |
| 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 | } |
| 327 | directChildren = getSubAttributes(startingAttribute, false); |
| 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 | |
| 350 | //-------------------------------------------------------------------------- |
| 351 | // Methods related to insertions in the history |
| 352 | //-------------------------------------------------------------------------- |
| 353 | |
| 354 | @Override |
| 355 | public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark) |
| 356 | throws TimeRangeException, AttributeNotFoundException, |
| 357 | StateValueTypeException { |
| 358 | transState.processStateChange(t, value, attributeQuark); |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void incrementAttribute(long t, int attributeQuark) |
| 363 | throws StateValueTypeException, TimeRangeException, |
| 364 | AttributeNotFoundException { |
| 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(); |
| 370 | } |
| 371 | modifyAttribute(t, TmfStateValue.newValueInt(prevValue + 1), |
| 372 | attributeQuark); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void pushAttribute(long t, ITmfStateValue value, int attributeQuark) |
| 377 | throws TimeRangeException, AttributeNotFoundException, |
| 378 | StateValueTypeException { |
| 379 | Integer stackDepth; |
| 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 | */ |
| 388 | stackDepth = 0; |
| 389 | } else if (previousSV.getType() == Type.INTEGER) { |
| 390 | /* Previous value was an integer, all is good, use it */ |
| 391 | stackDepth = previousSV.unboxInt(); |
| 392 | } else { |
| 393 | /* Previous state of this attribute was another type? Not good! */ |
| 394 | throw new StateValueTypeException(); |
| 395 | } |
| 396 | |
| 397 | if (stackDepth >= 100000) { |
| 398 | /* |
| 399 | * Limit stackDepth to 100000, to avoid having Attribute Trees grow out |
| 400 | * of control due to buggy insertions |
| 401 | */ |
| 402 | String message = "Stack limit reached, not pushing"; //$NON-NLS-1$ |
| 403 | throw new AttributeNotFoundException(message); |
| 404 | } |
| 405 | |
| 406 | stackDepth++; |
| 407 | subAttributeQuark = getQuarkRelativeAndAdd(attributeQuark, stackDepth.toString()); |
| 408 | |
| 409 | modifyAttribute(t, TmfStateValue.newValueInt(stackDepth), attributeQuark); |
| 410 | modifyAttribute(t, value, subAttributeQuark); |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public ITmfStateValue popAttribute(long t, int attributeQuark) |
| 415 | throws AttributeNotFoundException, TimeRangeException, |
| 416 | StateValueTypeException { |
| 417 | /* These are the state values of the stack-attribute itself */ |
| 418 | ITmfStateValue previousSV = queryOngoingState(attributeQuark); |
| 419 | |
| 420 | if (previousSV.isNull()) { |
| 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 | */ |
| 427 | return null; |
| 428 | } |
| 429 | if (previousSV.getType() != Type.INTEGER) { |
| 430 | /* |
| 431 | * The existing value was not an integer (which is expected for |
| 432 | * stack tops), this doesn't look like a valid stack attribute. |
| 433 | */ |
| 434 | throw new StateValueTypeException(); |
| 435 | } |
| 436 | |
| 437 | Integer stackDepth = previousSV.unboxInt(); |
| 438 | |
| 439 | if (stackDepth <= 0) { |
| 440 | /* This on the other hand should not happen... */ |
| 441 | String message = "A top-level stack attribute cannot " + //$NON-NLS-1$ |
| 442 | "have a value of 0 or less."; //$NON-NLS-1$ |
| 443 | throw new StateValueTypeException(message); |
| 444 | } |
| 445 | |
| 446 | /* The attribute should already exist at this point */ |
| 447 | int subAttributeQuark = getQuarkRelative(attributeQuark, stackDepth.toString()); |
| 448 | ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark); |
| 449 | |
| 450 | /* Update the state value of the stack-attribute */ |
| 451 | ITmfStateValue nextSV; |
| 452 | if (--stackDepth == 0 ) { |
| 453 | /* Store a null state value */ |
| 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 */ |
| 461 | removeAttribute(t, subAttributeQuark); |
| 462 | |
| 463 | return poppedValue; |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void removeAttribute(long t, int attributeQuark) |
| 468 | throws TimeRangeException, AttributeNotFoundException { |
| 469 | assert (attributeQuark >= 0); |
| 470 | List<Integer> childAttributes; |
| 471 | |
| 472 | /* |
| 473 | * "Nullify our children first, recursively. We pass 'false' because we |
| 474 | * handle the recursion ourselves. |
| 475 | */ |
| 476 | childAttributes = getSubAttributes(attributeQuark, false); |
| 477 | for (Integer childNodeQuark : childAttributes) { |
| 478 | assert (attributeQuark != childNodeQuark); |
| 479 | removeAttribute(t, childNodeQuark); |
| 480 | } |
| 481 | /* Nullify ourselves */ |
| 482 | try { |
| 483 | transState.processStateChange(t, TmfStateValue.nullValue(), |
| 484 | attributeQuark); |
| 485 | } catch (StateValueTypeException e) { |
| 486 | /* |
| 487 | * Will not happen since we're inserting null values only, but poor |
| 488 | * compiler has no way of knowing this... |
| 489 | */ |
| 490 | throw new IllegalStateException(e); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | //-------------------------------------------------------------------------- |
| 495 | // "Current" query/update methods |
| 496 | //-------------------------------------------------------------------------- |
| 497 | |
| 498 | @Override |
| 499 | public ITmfStateValue queryOngoingState(int attributeQuark) |
| 500 | throws AttributeNotFoundException { |
| 501 | return transState.getOngoingStateValue(attributeQuark); |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public long getOngoingStartTime(int attribute) |
| 506 | throws AttributeNotFoundException { |
| 507 | return transState.getOngoingStartTime(attribute); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void updateOngoingState(ITmfStateValue newValue, int attributeQuark) |
| 512 | throws AttributeNotFoundException { |
| 513 | transState.changeOngoingStateValue(attributeQuark, newValue); |
| 514 | } |
| 515 | |
| 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 | |
| 528 | //-------------------------------------------------------------------------- |
| 529 | // Regular query methods (sent to the back-end) |
| 530 | //-------------------------------------------------------------------------- |
| 531 | |
| 532 | @Override |
| 533 | public synchronized List<ITmfStateInterval> queryFullState(long t) |
| 534 | throws TimeRangeException, StateSystemDisposedException { |
| 535 | if (isDisposed) { |
| 536 | throw new StateSystemDisposedException(); |
| 537 | } |
| 538 | |
| 539 | List<ITmfStateInterval> stateInfo = new ArrayList<>(getNbAttributes()); |
| 540 | |
| 541 | /* Bring the size of the array to the current number of attributes */ |
| 542 | for (int i = 0; i < getNbAttributes(); i++) { |
| 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) { |
| 564 | stateInfo.set(i, new TmfStateInterval(t, t, i, TmfStateValue.nullValue())); |
| 565 | } |
| 566 | } |
| 567 | return stateInfo; |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public ITmfStateInterval querySingleState(long t, int attributeQuark) |
| 572 | throws AttributeNotFoundException, TimeRangeException, |
| 573 | StateSystemDisposedException { |
| 574 | if (isDisposed) { |
| 575 | throw new StateSystemDisposedException(); |
| 576 | } |
| 577 | |
| 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 | */ |
| 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) { |
| 592 | return new TmfStateInterval(t, this.getCurrentEndTime(), |
| 593 | attributeQuark, TmfStateValue.nullValue()); |
| 594 | } |
| 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | @Override |
| 599 | public ITmfStateInterval querySingleStackTop(long t, int stackAttributeQuark) |
| 600 | throws StateValueTypeException, AttributeNotFoundException, |
| 601 | TimeRangeException, StateSystemDisposedException { |
| 602 | ITmfStateValue curStackStateValue = querySingleState(t, stackAttributeQuark).getStateValue(); |
| 603 | |
| 604 | if (curStackStateValue.isNull()) { |
| 605 | /* There is nothing stored in this stack at this moment */ |
| 606 | return null; |
| 607 | } |
| 608 | Integer curStackDepth = curStackStateValue.unboxInt(); |
| 609 | if (curStackDepth <= 0) { |
| 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()); |
| 618 | return querySingleState(t, subAttribQuark); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public List<ITmfStateInterval> queryHistoryRange(int attributeQuark, |
| 623 | long t1, long t2) throws TimeRangeException, |
| 624 | AttributeNotFoundException, StateSystemDisposedException { |
| 625 | if (isDisposed) { |
| 626 | throw new StateSystemDisposedException(); |
| 627 | } |
| 628 | |
| 629 | List<ITmfStateInterval> intervals; |
| 630 | ITmfStateInterval currentInterval; |
| 631 | long ts, tEnd; |
| 632 | |
| 633 | /* Make sure the time range makes sense */ |
| 634 | if (t2 < t1) { |
| 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 */ |
| 646 | intervals = new ArrayList<>(); |
| 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, |
| 663 | long t1, long t2, long resolution, IProgressMonitor monitor) |
| 664 | throws TimeRangeException, AttributeNotFoundException, |
| 665 | StateSystemDisposedException { |
| 666 | if (isDisposed) { |
| 667 | throw new StateSystemDisposedException(); |
| 668 | } |
| 669 | |
| 670 | List<ITmfStateInterval> intervals; |
| 671 | ITmfStateInterval currentInterval; |
| 672 | long ts, tEnd; |
| 673 | |
| 674 | IProgressMonitor mon = monitor; |
| 675 | if (mon == null) { |
| 676 | mon = new NullProgressMonitor(); |
| 677 | } |
| 678 | |
| 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 */ |
| 692 | intervals = new ArrayList<>(); |
| 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) { |
| 702 | if (mon.isCanceled()) { |
| 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) { |
| 725 | Activator.logInfo("No data found in history for attribute " + //$NON-NLS-1$ |
| 726 | attribute + " at time " + timestamp + //$NON-NLS-1$ |
| 727 | ", returning dummy interval"); //$NON-NLS-1$ |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Print out the contents of the inner structures. |
| 732 | * |
| 733 | * @param writer |
| 734 | * The PrintWriter in which to print the output |
| 735 | */ |
| 736 | public void debugPrint(PrintWriter writer) { |
| 737 | getAttributeTree().debugPrint(writer); |
| 738 | transState.debugPrint(writer); |
| 739 | backend.debugPrint(writer); |
| 740 | } |
| 741 | |
| 742 | } |