1 /*******************************************************************************
2 * Copyright (c) 2012 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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
;
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
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Tracer
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.TmfStateInterval
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemBuilder
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
35 * This is the core class of the Generic State System. It contains all the
36 * methods to build and query a state history. It's exposed externally through
37 * the IStateSystemQuerier and IStateSystemBuilder interfaces, depending if the
38 * user needs read-only access or read-write access.
40 * When building, DON'T FORGET to call .closeHistory() when you are done
41 * inserting intervals, or the storage backend will have no way of knowing it
42 * can close and write itself to disk, and its thread will keep running.
47 public class StateSystem
implements IStateSystemBuilder
{
49 /* References to the inner structures */
50 private final AttributeTree attributeTree
;
51 private final TransientState transState
;
52 private final IStateHistoryBackend backend
;
58 * The "state history storage" backend to use.
60 * Put true if this is a new history started from scratch. It is
61 * used to tell the state system where to get its attribute tree.
63 * If there was a problem creating the new history file
65 public StateSystem(IStateHistoryBackend backend
, boolean newFile
)
67 this.backend
= backend
;
68 this.transState
= new TransientState(backend
);
71 attributeTree
= new AttributeTree(this);
73 /* We're opening an existing file */
74 this.attributeTree
= new AttributeTree(this, backend
.supplyAttributeTreeReader());
75 transState
.setInactive();
79 //--------------------------------------------------------------------------
80 // General methods related to the attribute tree
81 //--------------------------------------------------------------------------
84 * Method used by the attribute tree when creating new attributes, to keep
85 * the attribute count in the transient state in sync.
87 void addEmptyAttribute() {
88 transState
.addEmptyEntry();
92 public int getNbAttributes() {
93 return attributeTree
.getNbAttributes();
97 public String
getAttributeName(int attributeQuark
) {
98 return attributeTree
.getAttributeName(attributeQuark
);
102 public String
getFullAttributePath(int attributeQuark
) {
103 return attributeTree
.getFullAttributeName(attributeQuark
);
106 //--------------------------------------------------------------------------
107 // Methods related to the storage backend
108 //--------------------------------------------------------------------------
111 public long getStartTime() {
112 return backend
.getStartTime();
116 public long getCurrentEndTime() {
117 return backend
.getEndTime();
121 public void closeHistory(long endTime
) throws TimeRangeException
{
122 File attributeTreeFile
;
123 long attributeTreeFilePos
;
124 long realEndTime
= endTime
;
126 if (realEndTime
< backend
.getEndTime()) {
128 * This can happen (empty nodes pushing the border further, etc.)
129 * but shouldn't be too big of a deal.
131 realEndTime
= backend
.getEndTime();
133 transState
.closeTransientState(realEndTime
);
134 backend
.finishedBuilding(realEndTime
);
136 attributeTreeFile
= backend
.supplyAttributeTreeWriterFile();
137 attributeTreeFilePos
= backend
.supplyAttributeTreeWriterFilePosition();
138 if (attributeTreeFile
!= null) {
140 * If null was returned, we simply won't save the attribute tree,
143 attributeTree
.writeSelf(attributeTreeFile
, attributeTreeFilePos
);
147 //--------------------------------------------------------------------------
148 // Quark-retrieving methods
149 //--------------------------------------------------------------------------
152 public int getQuarkAbsolute(String
... attribute
)
153 throws AttributeNotFoundException
{
154 return attributeTree
.getQuarkDontAdd(-1, attribute
);
158 public int getQuarkAbsoluteAndAdd(String
... attribute
) {
159 return attributeTree
.getQuarkAndAdd(-1, attribute
);
163 public int getQuarkRelative(int startingNodeQuark
, String
... subPath
)
164 throws AttributeNotFoundException
{
165 return attributeTree
.getQuarkDontAdd(startingNodeQuark
, subPath
);
169 public int getQuarkRelativeAndAdd(int startingNodeQuark
, String
... subPath
) {
170 return attributeTree
.getQuarkAndAdd(startingNodeQuark
, subPath
);
174 public List
<Integer
> getSubAttributes(int quark
, boolean recursive
)
175 throws AttributeNotFoundException
{
176 return attributeTree
.getSubAttributes(quark
, recursive
);
180 public List
<Integer
> getQuarks(String
... pattern
) {
181 List
<Integer
> quarks
= new LinkedList
<Integer
>();
182 List
<String
> prefix
= new LinkedList
<String
>();
183 List
<String
> suffix
= new LinkedList
<String
>();
184 boolean split
= false;
187 List
<Integer
> directChildren
;
188 int startingAttribute
;
190 /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */
191 for (String entry
: pattern
) {
192 if (entry
.equals("*")) { //$NON-NLS-1$
195 * Split was already true? This means there was more than
196 * one wildcard. This is not supported, return an empty
211 prefixStr
= prefix
.toArray(new String
[prefix
.size()]);
212 suffixStr
= suffix
.toArray(new String
[suffix
.size()]);
215 * If there was no wildcard, we'll only return the one matching
216 * attribute, if there is one.
218 if (split
== false) {
221 quark
= getQuarkAbsolute(prefixStr
);
222 } catch (AttributeNotFoundException e
) {
223 /* It's fine, we'll just return the empty List */
231 if (prefix
.size() == 0) {
233 * If 'prefix' is empty, this means the wildcard was the first
234 * element. Look for the root node's sub-attributes.
236 startingAttribute
= -1;
238 startingAttribute
= getQuarkAbsolute(prefixStr
);
240 directChildren
= attributeTree
.getSubAttributes(startingAttribute
,
242 } catch (AttributeNotFoundException e
) {
243 /* That attribute path did not exist, return the empty array */
248 * Iterate of all the sub-attributes, and only keep those who match the
249 * 'suffix' part of the initial pattern.
251 for (int childQuark
: directChildren
) {
254 matchingQuark
= getQuarkRelative(childQuark
, suffixStr
);
255 } catch (AttributeNotFoundException e
) {
258 quarks
.add(matchingQuark
);
264 //--------------------------------------------------------------------------
265 // Methods related to insertions in the history
266 //--------------------------------------------------------------------------
269 public void modifyAttribute(long t
, ITmfStateValue value
, int attributeQuark
)
270 throws TimeRangeException
, AttributeNotFoundException
,
271 StateValueTypeException
{
272 transState
.processStateChange(t
, value
, attributeQuark
);
276 public void incrementAttribute(long t
, int attributeQuark
)
277 throws StateValueTypeException
, TimeRangeException
,
278 AttributeNotFoundException
{
279 int prevValue
= queryOngoingState(attributeQuark
).unboxInt();
280 if (prevValue
== -1) {
281 /* if the attribute was previously null, start counting at 0 */
284 modifyAttribute(t
, TmfStateValue
.newValueInt(prevValue
+ 1),
289 public void pushAttribute(long t
, ITmfStateValue value
, int attributeQuark
)
290 throws TimeRangeException
, AttributeNotFoundException
,
291 StateValueTypeException
{
292 Integer stackDepth
= 0;
293 int subAttributeQuark
;
294 ITmfStateValue previousSV
= transState
.getOngoingStateValue(attributeQuark
);
296 if (previousSV
.isNull()) {
298 * If the StateValue was null, this means this is the first time we
299 * use this attribute. Leave stackDepth at 0.
301 } else if (previousSV
.getType() == 0) {
302 /* Previous value was an integer, all is good, use it */
303 stackDepth
= previousSV
.unboxInt();
305 /* Previous state of this attribute was another type? Not good! */
306 throw new StateValueTypeException();
309 if (stackDepth
>= 10) {
311 * Limit stackDepth to 10, to avoid having Attribute Trees grow out
312 * of control due to buggy insertions
314 String message
= "Stack limit reached, not pushing"; //$NON-NLS-1$
315 throw new AttributeNotFoundException(message
);
319 subAttributeQuark
= getQuarkRelativeAndAdd(attributeQuark
,
320 stackDepth
.toString());
322 modifyAttribute(t
, TmfStateValue
.newValueInt(stackDepth
),
324 modifyAttribute(t
, value
, subAttributeQuark
);
328 public void popAttribute(long t
, int attributeQuark
)
329 throws AttributeNotFoundException
, TimeRangeException
,
330 StateValueTypeException
{
332 int subAttributeQuark
;
333 ITmfStateValue previousSV
= transState
.getOngoingStateValue(attributeQuark
);
335 if (previousSV
.isNull()) {
336 /* Same as if stackDepth == 0, see below */
339 if (previousSV
.getType() != 0) {
341 * The existing value was a string, this doesn't look like a valid
344 throw new StateValueTypeException();
347 stackDepth
= previousSV
.unboxInt();
349 if (stackDepth
== 0) {
351 * Trying to pop an empty stack. This often happens at the start of
352 * traces, for example when we see a syscall_exit, without having
353 * the corresponding syscall_entry in the trace. Just ignore
359 if (stackDepth
< 0) {
360 /* This on the other hand should not happen... */
361 String message
= "A top-level stack attribute " + //$NON-NLS-1$
362 "cannot have a negative integer value."; //$NON-NLS-1$
363 throw new StateValueTypeException(message
);
366 /* The attribute should already exist... */
367 subAttributeQuark
= getQuarkRelative(attributeQuark
,
368 stackDepth
.toString());
371 modifyAttribute(t
, TmfStateValue
.newValueInt(stackDepth
),
373 removeAttribute(t
, subAttributeQuark
);
377 public void removeAttribute(long t
, int attributeQuark
)
378 throws TimeRangeException
, AttributeNotFoundException
{
379 assert (attributeQuark
>= 0);
380 List
<Integer
> childAttributes
;
383 * "Nullify our children first, recursively. We pass 'false' because we
384 * handle the recursion ourselves.
386 childAttributes
= attributeTree
.getSubAttributes(attributeQuark
, false);
387 for (Integer childNodeQuark
: childAttributes
) {
388 assert (attributeQuark
!= childNodeQuark
);
389 removeAttribute(t
, childNodeQuark
);
391 /* Nullify ourselves */
393 transState
.processStateChange(t
, TmfStateValue
.nullValue(),
395 } catch (StateValueTypeException e
) {
397 * Will not happen since we're inserting null values only, but poor
398 * compiler has no way of knowing this...
404 //--------------------------------------------------------------------------
405 // "Current" query/update methods
406 //--------------------------------------------------------------------------
409 public ITmfStateValue
queryOngoingState(int attributeQuark
)
410 throws AttributeNotFoundException
{
411 return transState
.getOngoingStateValue(attributeQuark
);
415 public void updateOngoingState(ITmfStateValue newValue
, int attributeQuark
)
416 throws AttributeNotFoundException
{
417 transState
.changeOngoingStateValue(attributeQuark
, newValue
);
422 //--------------------------------------------------------------------------
423 // Regular query methods (sent to the back-end)
424 //--------------------------------------------------------------------------
427 public synchronized List
<ITmfStateInterval
> queryFullState(long t
)
428 throws TimeRangeException
{
429 List
<ITmfStateInterval
> stateInfo
= new ArrayList
<ITmfStateInterval
>(
430 attributeTree
.getNbAttributes());
432 /* Bring the size of the array to the current number of attributes */
433 for (int i
= 0; i
< attributeTree
.getNbAttributes(); i
++) {
437 /* Query the storage backend */
438 backend
.doQuery(stateInfo
, t
);
441 * If we are currently building the history, also query the "ongoing"
442 * states for stuff that might not yet be written to the history.
444 if (transState
.isActive()) {
445 transState
.doQuery(stateInfo
, t
);
449 * We should have previously inserted an interval for every attribute.
450 * If we do happen do see a 'null' object here, just replace it with a a
451 * dummy internal with a null value, to avoid NPE's further up.
453 for (int i
= 0; i
< stateInfo
.size(); i
++) {
454 if (stateInfo
.get(i
) == null) {
455 //logMissingInterval(i, t);
456 stateInfo
.set(i
, new TmfStateInterval(t
, t
, i
, TmfStateValue
.nullValue()));
463 public ITmfStateInterval
querySingleState(long t
, int attributeQuark
)
464 throws AttributeNotFoundException
, TimeRangeException
{
465 ITmfStateInterval ret
;
467 if (transState
.hasInfoAboutStateOf(t
, attributeQuark
)) {
468 ret
= transState
.getOngoingInterval(attributeQuark
);
470 ret
= backend
.doSingularQuery(t
, attributeQuark
);
474 * Return a fake interval if we could not find anything in the history.
475 * We do NOT want to return 'null' here.
478 //logMissingInterval(attributeQuark, t);
479 return new TmfStateInterval(t
, this.getCurrentEndTime(),
480 attributeQuark
, TmfStateValue
.nullValue());
486 public List
<ITmfStateInterval
> queryHistoryRange(int attributeQuark
,
487 long t1
, long t2
) throws TimeRangeException
,
488 AttributeNotFoundException
{
489 List
<ITmfStateInterval
> intervals
;
490 ITmfStateInterval currentInterval
;
493 /* Make sure the time range makes sense */
495 throw new TimeRangeException();
498 /* Set the actual, valid end time of the range query */
499 if (t2
> this.getCurrentEndTime()) {
500 tEnd
= this.getCurrentEndTime();
505 /* Get the initial state at time T1 */
506 intervals
= new ArrayList
<ITmfStateInterval
>();
507 currentInterval
= querySingleState(t1
, attributeQuark
);
508 intervals
.add(currentInterval
);
510 /* Get the following state changes */
511 ts
= currentInterval
.getEndTime();
512 while (ts
!= -1 && ts
< tEnd
) {
513 ts
++; /* To "jump over" to the next state in the history */
514 currentInterval
= querySingleState(ts
, attributeQuark
);
515 intervals
.add(currentInterval
);
516 ts
= currentInterval
.getEndTime();
522 public List
<ITmfStateInterval
> queryHistoryRange(int attributeQuark
,
523 long t1
, long t2
, long resolution
, IProgressMonitor monitor
)
524 throws TimeRangeException
, AttributeNotFoundException
{
525 List
<ITmfStateInterval
> intervals
;
526 ITmfStateInterval currentInterval
;
529 if (monitor
== null) {
530 monitor
= new NullProgressMonitor();
533 /* Make sure the time range makes sense */
534 if (t2
< t1
|| resolution
<= 0) {
535 throw new TimeRangeException();
538 /* Set the actual, valid end time of the range query */
539 if (t2
> this.getCurrentEndTime()) {
540 tEnd
= this.getCurrentEndTime();
545 /* Get the initial state at time T1 */
546 intervals
= new ArrayList
<ITmfStateInterval
>();
547 currentInterval
= querySingleState(t1
, attributeQuark
);
548 intervals
.add(currentInterval
);
551 * Iterate over the "resolution points". We skip unneeded queries in the
552 * case the current interval is longer than the resolution.
554 for (ts
= t1
; (currentInterval
.getEndTime() != -1) && (ts
< tEnd
);
556 if (monitor
.isCanceled()) {
559 if (ts
<= currentInterval
.getEndTime()) {
562 currentInterval
= querySingleState(ts
, attributeQuark
);
563 intervals
.add(currentInterval
);
566 /* Add the interval at t2, if it wasn't included already. */
567 if (currentInterval
.getEndTime() < tEnd
) {
568 currentInterval
= querySingleState(tEnd
, attributeQuark
);
569 intervals
.add(currentInterval
);
574 //--------------------------------------------------------------------------
576 //--------------------------------------------------------------------------
578 static void logMissingInterval(int attribute
, long timestamp
) {
579 Tracer
.traceInfo("No data found in history for attribute " + //$NON-NLS-1$
580 attribute
+ " at time " + timestamp
+ //$NON-NLS-1$
581 ", returning dummy interval"); //$NON-NLS-1$
585 * Print out the contents of the inner structures.
588 * The PrintWriter in which to print the output
590 public void debugPrint(PrintWriter writer
) {
591 attributeTree
.debugPrint(writer
);
592 transState
.debugPrint(writer
);
593 backend
.debugPrint(writer
);