1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
;
19 import java
.io
.IOException
;
20 import java
.io
.PrintWriter
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Arrays
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
25 import java
.util
.concurrent
.CountDownLatch
;
26 import java
.util
.concurrent
.TimeUnit
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
42 * This is the core class of the Generic State System. It contains all the
43 * methods to build and query a state history. It's exposed externally through
44 * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the
45 * user needs read-only access or read-write access.
47 * When building, DON'T FORGET to call .closeHistory() when you are done
48 * inserting intervals, or the storage backend will have no way of knowing it
49 * can close and write itself to disk, and its thread will keep running.
54 public class StateSystem
implements ITmfStateSystemBuilder
{
56 /* References to the inner structures */
57 private final AttributeTree attributeTree
;
58 private final TransientState transState
;
59 private final IStateHistoryBackend backend
;
61 /* Latch tracking if the state history is done building or not */
62 private final CountDownLatch finishedLatch
= new CountDownLatch(1);
64 private boolean buildCancelled
= false;
65 private boolean isDisposed
= false;
68 * New-file constructor. For when you build a state system with a new file,
69 * or if the back-end does not require a file on disk.
72 * Back-end plugin to use
74 public StateSystem(@NonNull IStateHistoryBackend backend
) {
75 this.backend
= backend
;
76 this.transState
= new TransientState(backend
);
77 this.attributeTree
= new AttributeTree(this);
84 * The "state history storage" back-end to use.
86 * Put true if this is a new history started from scratch. It is
87 * used to tell the state system where to get its attribute tree.
89 * If there was a problem creating the new history file
91 public StateSystem(@NonNull IStateHistoryBackend backend
, boolean newFile
)
93 this.backend
= backend
;
94 this.transState
= new TransientState(backend
);
97 attributeTree
= new AttributeTree(this);
99 /* We're opening an existing file */
100 this.attributeTree
= new AttributeTree(this, backend
.supplyAttributeTreeReader());
101 transState
.setInactive();
102 finishedLatch
.countDown(); /* The history is already built */
107 public String
getSSID() {
108 return backend
.getSSID();
112 public boolean isCancelled() {
113 return buildCancelled
;
117 public void waitUntilBuilt() {
119 finishedLatch
.await();
120 } catch (InterruptedException e
) {
126 public boolean waitUntilBuilt(long timeout
) {
129 ret
= finishedLatch
.await(timeout
, TimeUnit
.MILLISECONDS
);
130 } catch (InterruptedException e
) {
137 public synchronized void dispose() {
139 if (transState
.isActive()) {
140 transState
.setInactive();
141 buildCancelled
= true;
146 //--------------------------------------------------------------------------
147 // General methods related to the attribute tree
148 //--------------------------------------------------------------------------
151 * Get the attribute tree associated with this state system. This should be
152 * the only way of accessing it (and if subclasses want to point to a
153 * different attribute tree than their own, they should only need to
156 * @return The attribute tree
158 public AttributeTree
getAttributeTree() {
159 return attributeTree
;
163 * Method used by the attribute tree when creating new attributes, to keep
164 * the attribute count in the transient state in sync.
166 public void addEmptyAttribute() {
167 transState
.addEmptyEntry();
171 public int getNbAttributes() {
172 return getAttributeTree().getNbAttributes();
176 public String
getAttributeName(int attributeQuark
) {
177 return getAttributeTree().getAttributeName(attributeQuark
);
181 public String
getFullAttributePath(int attributeQuark
) {
182 return getAttributeTree().getFullAttributeName(attributeQuark
);
186 public String
[] getFullAttributePathArray(int attributeQuark
) {
187 return getAttributeTree().getFullAttributePathArray(attributeQuark
);
190 //--------------------------------------------------------------------------
191 // Methods related to the storage backend
192 //--------------------------------------------------------------------------
195 public long getStartTime() {
196 return backend
.getStartTime();
200 public long getCurrentEndTime() {
201 return backend
.getEndTime();
205 public void closeHistory(long endTime
) throws TimeRangeException
{
206 File attributeTreeFile
;
207 long attributeTreeFilePos
;
208 long realEndTime
= endTime
;
210 if (realEndTime
< backend
.getEndTime()) {
212 * This can happen (empty nodes pushing the border further, etc.)
213 * but shouldn't be too big of a deal.
215 realEndTime
= backend
.getEndTime();
217 transState
.closeTransientState(realEndTime
);
218 backend
.finishedBuilding(realEndTime
);
220 attributeTreeFile
= backend
.supplyAttributeTreeWriterFile();
221 attributeTreeFilePos
= backend
.supplyAttributeTreeWriterFilePosition();
222 if (attributeTreeFile
!= null) {
224 * If null was returned, we simply won't save the attribute tree,
227 getAttributeTree().writeSelf(attributeTreeFile
, attributeTreeFilePos
);
229 finishedLatch
.countDown(); /* Mark the history as finished building */
232 //--------------------------------------------------------------------------
233 // Quark-retrieving methods
234 //--------------------------------------------------------------------------
237 public int getQuarkAbsolute(String
... attribute
)
238 throws AttributeNotFoundException
{
239 int quark
= getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE
, attribute
);
240 if (quark
== INVALID_ATTRIBUTE
) {
241 throw new AttributeNotFoundException(getSSID() + " Path:" + Arrays
.toString(attribute
)); //$NON-NLS-1$
247 public int optQuarkAbsolute(String
... attribute
) {
248 return getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE
, attribute
);
252 public int getQuarkAbsoluteAndAdd(String
... attribute
) {
253 return getAttributeTree().getQuarkAndAdd(ROOT_ATTRIBUTE
, attribute
);
257 public int getQuarkRelative(int startingNodeQuark
, String
... subPath
)
258 throws AttributeNotFoundException
{
259 int quark
= getAttributeTree().getQuarkDontAdd(startingNodeQuark
, subPath
);
260 if (quark
== INVALID_ATTRIBUTE
) {
261 throw new AttributeNotFoundException(getSSID() + " Quark:" + startingNodeQuark
+ ", SubPath:" + Arrays
.toString(subPath
)); //$NON-NLS-1$ //$NON-NLS-2$
267 public int optQuarkRelative(int startingNodeQuark
, String
... subPath
) {
268 return getAttributeTree().getQuarkDontAdd(startingNodeQuark
, subPath
);
272 public int getQuarkRelativeAndAdd(int startingNodeQuark
, String
... subPath
) {
273 return getAttributeTree().getQuarkAndAdd(startingNodeQuark
, subPath
);
277 public List
<Integer
> getSubAttributes(int quark
, boolean recursive
)
278 throws AttributeNotFoundException
{
279 return getAttributeTree().getSubAttributes(quark
, recursive
);
283 public List
<Integer
> getSubAttributes(int quark
, boolean recursive
, String pattern
)
284 throws AttributeNotFoundException
{
285 List
<Integer
> all
= getSubAttributes(quark
, recursive
);
286 List
<Integer
> ret
= new LinkedList
<>();
287 for (Integer attQuark
: all
) {
288 String name
= getAttributeName(attQuark
.intValue());
289 if (name
.matches(pattern
)) {
297 public int getParentAttributeQuark(int quark
) {
298 return getAttributeTree().getParentAttributeQuark(quark
);
302 public List
<@NonNull Integer
> getQuarks(String
... pattern
) {
303 List
<@NonNull Integer
> quarks
= new LinkedList
<>();
304 List
<String
> prefix
= new LinkedList
<>();
305 List
<String
> suffix
= new LinkedList
<>();
306 boolean split
= false;
309 List
<Integer
> directChildren
;
310 int startingAttribute
;
312 /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */
313 for (String entry
: pattern
) {
314 if (entry
.equals("*")) { //$NON-NLS-1$
317 * Split was already true? This means there was more than
318 * one wildcard. This is not supported, return an empty
333 prefixStr
= prefix
.toArray(new String
[prefix
.size()]);
334 suffixStr
= suffix
.toArray(new String
[suffix
.size()]);
337 * If there was no wildcard, we'll only return the one matching
338 * attribute, if there is one.
343 quark
= getQuarkAbsolute(prefixStr
);
344 } catch (AttributeNotFoundException e
) {
345 /* It's fine, we'll just return the empty List */
352 if (prefix
.isEmpty()) {
354 * If 'prefix' is empty, this means the wildcard was the first
355 * element. Look for the root node's sub-attributes.
357 startingAttribute
= ROOT_ATTRIBUTE
;
359 startingAttribute
= optQuarkAbsolute(prefixStr
);
360 if (startingAttribute
== INVALID_ATTRIBUTE
) {
361 /* That attribute path did not exist, return the empty array */
366 directChildren
= getSubAttributes(startingAttribute
, false);
367 } catch (AttributeNotFoundException e
) {
368 /* Should not happen, starting attribute is a valid quark */
369 throw new IllegalStateException();
373 * Iterate of all the sub-attributes, and only keep those who match the
374 * 'suffix' part of the initial pattern.
376 for (int childQuark
: directChildren
) {
377 int matchingQuark
= optQuarkRelative(childQuark
, suffixStr
);
378 if (matchingQuark
!= INVALID_ATTRIBUTE
) {
379 quarks
.add(matchingQuark
);
386 //--------------------------------------------------------------------------
387 // Methods related to insertions in the history
388 //--------------------------------------------------------------------------
391 public void modifyAttribute(long t
, ITmfStateValue value
, int attributeQuark
)
392 throws TimeRangeException
, AttributeNotFoundException
,
393 StateValueTypeException
{
396 * TODO Replace with @NonNull parameter (will require fixing all the
399 throw new IllegalArgumentException();
401 transState
.processStateChange(t
, value
, attributeQuark
);
406 public void incrementAttribute(long t
, int attributeQuark
)
407 throws StateValueTypeException
, TimeRangeException
,
408 AttributeNotFoundException
{
409 ITmfStateValue stateValue
= queryOngoingState(attributeQuark
);
411 /* if the attribute was previously null, start counting at 0 */
412 if (!stateValue
.isNull()) {
413 prevValue
= stateValue
.unboxInt();
415 modifyAttribute(t
, TmfStateValue
.newValueInt(prevValue
+ 1),
420 public void pushAttribute(long t
, ITmfStateValue value
, int attributeQuark
)
421 throws TimeRangeException
, AttributeNotFoundException
,
422 StateValueTypeException
{
424 int subAttributeQuark
;
425 ITmfStateValue previousSV
= transState
.getOngoingStateValue(attributeQuark
);
427 if (previousSV
.isNull()) {
429 * If the StateValue was null, this means this is the first time we
430 * use this attribute. Leave stackDepth at 0.
433 } else if (previousSV
.getType() == Type
.INTEGER
) {
434 /* Previous value was an integer, all is good, use it */
435 stackDepth
= previousSV
.unboxInt();
437 /* Previous state of this attribute was another type? Not good! */
438 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark
+ ", Type:" + previousSV
.getType() + ", Expected:" + Type
.INTEGER
); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
441 if (stackDepth
>= 100000) {
443 * Limit stackDepth to 100000, to avoid having Attribute Trees grow
444 * out of control due to buggy insertions
446 String message
= " Stack limit reached, not pushing"; //$NON-NLS-1$
447 throw new AttributeNotFoundException(getSSID() + " Quark:" + attributeQuark
+ message
); //$NON-NLS-1$
451 subAttributeQuark
= getQuarkRelativeAndAdd(attributeQuark
, String
.valueOf(stackDepth
));
453 modifyAttribute(t
, TmfStateValue
.newValueInt(stackDepth
), attributeQuark
);
454 modifyAttribute(t
, value
, subAttributeQuark
);
458 public ITmfStateValue
popAttribute(long t
, int attributeQuark
)
459 throws AttributeNotFoundException
, TimeRangeException
,
460 StateValueTypeException
{
461 /* These are the state values of the stack-attribute itself */
462 ITmfStateValue previousSV
= transState
.getOngoingStateValue(attributeQuark
);
464 if (previousSV
.isNull()) {
466 * Trying to pop an empty stack. This often happens at the start of
467 * traces, for example when we see a syscall_exit, without having
468 * the corresponding syscall_entry in the trace. Just ignore
473 if (previousSV
.getType() != Type
.INTEGER
) {
475 * The existing value was not an integer (which is expected for
476 * stack tops), this doesn't look like a valid stack attribute.
478 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark
+ ", Type:" + previousSV
.getType() + ", Expected:" + Type
.INTEGER
); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
481 int stackDepth
= previousSV
.unboxInt();
483 if (stackDepth
<= 0) {
484 /* This on the other hand should not happen... */
485 throw new StateValueTypeException(getSSID() + " Quark:" + attributeQuark
+ ", Stack depth:" + stackDepth
); //$NON-NLS-1$//$NON-NLS-2$
488 /* The attribute should already exist at this point */
489 int subAttributeQuark
= getQuarkRelative(attributeQuark
, String
.valueOf(stackDepth
));
490 ITmfStateValue poppedValue
= queryOngoingState(subAttributeQuark
);
492 /* Update the state value of the stack-attribute */
493 ITmfStateValue nextSV
;
494 if (--stackDepth
== 0) {
495 /* Store a null state value */
496 nextSV
= TmfStateValue
.nullValue();
498 nextSV
= TmfStateValue
.newValueInt(stackDepth
);
500 modifyAttribute(t
, nextSV
, attributeQuark
);
502 /* Delete the sub-attribute that contained the user's state value */
503 removeAttribute(t
, subAttributeQuark
);
509 public void removeAttribute(long t
, int attributeQuark
)
510 throws TimeRangeException
, AttributeNotFoundException
{
511 if (attributeQuark
< 0) {
512 throw new IllegalArgumentException();
516 * Nullify our children first, recursively. We pass 'false' because we
517 * handle the recursion ourselves.
519 List
<Integer
> childAttributes
= getSubAttributes(attributeQuark
, false);
520 for (int childNodeQuark
: childAttributes
) {
521 if (attributeQuark
== childNodeQuark
) {
522 /* Something went very wrong when building out attribute tree */
523 throw new IllegalStateException();
525 removeAttribute(t
, childNodeQuark
);
527 /* Nullify ourselves */
529 transState
.processStateChange(t
, TmfStateValue
.nullValue(), attributeQuark
);
530 } catch (StateValueTypeException e
) {
532 * Will not happen since we're inserting null values only, but poor
533 * compiler has no way of knowing this...
535 throw new IllegalStateException(e
);
539 //--------------------------------------------------------------------------
540 // "Current" query/update methods
541 //--------------------------------------------------------------------------
544 public ITmfStateValue
queryOngoingState(int attributeQuark
)
545 throws AttributeNotFoundException
{
546 return transState
.getOngoingStateValue(attributeQuark
);
550 public long getOngoingStartTime(int attribute
)
551 throws AttributeNotFoundException
{
552 return transState
.getOngoingStartTime(attribute
);
556 public void updateOngoingState(ITmfStateValue newValue
, int attributeQuark
)
557 throws AttributeNotFoundException
{
558 transState
.changeOngoingStateValue(attributeQuark
, newValue
);
562 * Modify the whole "ongoing state" (state values + start times). This can
563 * be used when "seeking" a state system to a different point in the trace
564 * (and restoring the known stateInfo at this location). Use with care!
566 * @param newStateIntervals
567 * The new List of state values to use as ongoing state info
569 protected void replaceOngoingState(@NonNull List
<@NonNull ITmfStateInterval
> newStateIntervals
) {
570 transState
.replaceOngoingState(newStateIntervals
);
573 //--------------------------------------------------------------------------
574 // Regular query methods (sent to the back-end)
575 //--------------------------------------------------------------------------
578 public synchronized List
<ITmfStateInterval
> queryFullState(long t
)
579 throws TimeRangeException
, StateSystemDisposedException
{
581 throw new StateSystemDisposedException();
584 final int nbAttr
= getNbAttributes();
585 List
<@Nullable ITmfStateInterval
> stateInfo
= new ArrayList
<>(nbAttr
);
587 /* Bring the size of the array to the current number of attributes */
588 for (int i
= 0; i
< nbAttr
; i
++) {
593 * If we are currently building the history, also query the "ongoing"
594 * states for stuff that might not yet be written to the history.
596 if (transState
.isActive()) {
597 transState
.doQuery(stateInfo
, t
);
600 /* Query the storage backend */
601 backend
.doQuery(stateInfo
, t
);
604 * We should have previously inserted an interval for every attribute.
606 for (ITmfStateInterval interval
: stateInfo
) {
607 if (interval
== null) {
608 throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
615 public ITmfStateInterval
querySingleState(long t
, int attributeQuark
)
616 throws AttributeNotFoundException
, TimeRangeException
,
617 StateSystemDisposedException
{
619 throw new StateSystemDisposedException();
622 ITmfStateInterval ret
= transState
.getIntervalAt(t
, attributeQuark
);
625 * The transient state did not have the information, let's look into
628 ret
= backend
.doSingularQuery(t
, attributeQuark
);
633 * If we did our job correctly, there should be intervals for every
634 * possible attribute, over all the valid time range.
636 throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
641 //--------------------------------------------------------------------------
643 //--------------------------------------------------------------------------
645 static void logMissingInterval(int attribute
, long timestamp
) {
646 Activator
.getDefault().logInfo("No data found in history for attribute " + //$NON-NLS-1$
647 attribute
+ " at time " + timestamp
+ //$NON-NLS-1$
648 ", returning dummy interval"); //$NON-NLS-1$
652 * Print out the contents of the inner structures.
655 * The PrintWriter in which to print the output
657 public void debugPrint(@NonNull PrintWriter writer
) {
658 getAttributeTree().debugPrint(writer
);
659 transState
.debugPrint(writer
);
660 backend
.debugPrint(writer
);