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>
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
;
15 import java
.io
.PrintWriter
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.IStateHistoryBackend
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.TmfStateInterval
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
.Type
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
31 * The Transient State is used to build intervals from punctual state changes.
32 * It contains a "state info" vector similar to the "current state", except here
33 * we also record the start time of every state stored in it.
35 * We can then build {@link ITmfStateInterval}'s, to be inserted in a
36 * {@link IStateHistoryBackend} when we detect state changes : the "start time"
37 * of the interval will be the recorded time we have here, and the "end time"
38 * will be the timestamp of the new state-changing event we just read.
40 * @author Alexandre Montplaisir
42 public class TransientState
{
44 /* Indicates where to insert state changes that we generate */
45 @NonNull private final IStateHistoryBackend backend
;
47 private boolean isActive
;
48 private long latestTime
;
50 private List
<ITmfStateValue
> ongoingStateInfo
;
51 private List
<Long
> ongoingStateStartTimes
;
52 private List
<Type
> stateValueTypes
;
58 * The back-end in which to insert the generated state intervals
60 public TransientState(@NonNull IStateHistoryBackend backend
) {
61 this.backend
= backend
;
63 ongoingStateInfo
= new ArrayList
<>();
64 ongoingStateStartTimes
= new ArrayList
<>();
65 stateValueTypes
= new ArrayList
<>();
67 latestTime
= backend
.getStartTime();
71 * Get the latest time we have seen so far.
73 * @return The latest time seen in the transient state
75 public long getLatestTime() {
80 * Retrieve the ongoing state value for a given index (attribute quark).
83 * The quark of the attribute to look for
84 * @return The corresponding state value
85 * @throws AttributeNotFoundException
86 * If the quark is invalid
88 public ITmfStateValue
getOngoingStateValue(int quark
) throws AttributeNotFoundException
{
89 checkValidAttribute(quark
);
90 return ongoingStateInfo
.get(quark
);
94 * Retrieve the start time of the state in which the given attribute is in.
97 * The quark of the attribute to look for
98 * @return The start time of the current state for this attribute
99 * @throws AttributeNotFoundException
100 * If the quark is invalid
102 public long getOngoingStartTime(int quark
) throws AttributeNotFoundException
{
103 checkValidAttribute(quark
);
104 return ongoingStateStartTimes
.get(quark
);
108 * Modify the current state for a given attribute. This will not update the
109 * "ongoing state start time" in any way, so be careful when using this.
112 * The quark of the attribute to modify
114 * The state value the attribute should have
115 * @throws AttributeNotFoundException
116 * If the quark is invalid
118 public void changeOngoingStateValue(int quark
, ITmfStateValue newValue
)
119 throws AttributeNotFoundException
{
120 checkValidAttribute(quark
);
121 ongoingStateInfo
.set(quark
, newValue
);
125 * Convenience method to return the "ongoing" value for a given attribute as
126 * a dummy interval whose end time = the current latest time.
129 * The quark of the attribute
130 * @return An interval representing the current state (but whose end time is
131 * the current one, and probably not the "final" one)
132 * @throws AttributeNotFoundException
133 * If the quark is invalid
135 public ITmfStateInterval
getOngoingInterval(int quark
) throws AttributeNotFoundException
{
136 checkValidAttribute(quark
);
137 return new TmfStateInterval(ongoingStateStartTimes
.get(quark
), latestTime
,
138 quark
, ongoingStateInfo
.get(quark
));
141 private void checkValidAttribute(int quark
) throws AttributeNotFoundException
{
142 if (quark
> ongoingStateInfo
.size() - 1 || quark
< 0) {
143 throw new AttributeNotFoundException();
148 * More advanced version of {@link #changeOngoingStateValue}. Replaces the
149 * complete ongoingStateInfo in one go, and updates the
150 * ongoingStateStartTimes and #stateValuesTypes accordingly. BE VERY CAREFUL
153 * @param newStateIntervals
154 * The List of intervals that will represent the new
155 * "ongoing state". Their end times don't matter, we will only
156 * check their value and start times.
158 public synchronized void replaceOngoingState(List
<ITmfStateInterval
> newStateIntervals
) {
159 int size
= newStateIntervals
.size();
160 ongoingStateInfo
= new ArrayList
<>(size
);
161 ongoingStateStartTimes
= new ArrayList
<>(size
);
162 stateValueTypes
= new ArrayList
<>(size
);
164 for (ITmfStateInterval interval
: newStateIntervals
) {
165 ongoingStateInfo
.add(interval
.getStateValue());
166 ongoingStateStartTimes
.add(interval
.getStartTime());
167 stateValueTypes
.add(interval
.getStateValue().getType());
172 * Add an "empty line" to both "ongoing..." vectors. This is needed so the
173 * Ongoing... tables can stay in sync with the number of attributes in the
174 * attribute tree, namely when we add sub-path attributes.
176 public synchronized void addEmptyEntry() {
178 * Since this is a new attribute, we suppose it was in the "null state"
179 * since the beginning (so we can have intervals covering for all
180 * timestamps). A null interval will then get added at the first state
183 ongoingStateInfo
.add(TmfStateValue
.nullValue());
184 stateValueTypes
.add(Type
.NULL
);
186 ongoingStateStartTimes
.add(backend
.getStartTime());
190 * Ask if the state information about attribute 'quark' at time 'time' is
191 * present in the Builder as it is right now. If it's not, it's either in
192 * the History Tree, or not in the system at all.
194 * Note that this method does not return the value itself (we don't even
195 * look for it, we can know by just looking at the timestamp)
198 * The timestamp to look for
200 * The quark of the attribute to look for
201 * @return True if the value is present in the Transient State at this
202 * moment in time, false if it's not
204 public boolean hasInfoAboutStateOf(long time
, int quark
) {
205 return (this.isActive() && time
>= ongoingStateStartTimes
.get(quark
));
209 * Process a state change to be inserted in the history.
212 * The timestamp associated with this state change
214 * The new StateValue associated to this attribute
216 * The quark of the attribute that is being modified
217 * @throws TimeRangeException
218 * If 'eventTime' is invalid
219 * @throws AttributeNotFoundException
220 * IF 'quark' does not represent an existing attribute
221 * @throws StateValueTypeException
222 * If the state value to be inserted is of a different type of
223 * what was inserted so far for this attribute.
225 public synchronized void processStateChange(long eventTime
,
226 ITmfStateValue value
, int quark
) throws TimeRangeException
,
227 AttributeNotFoundException
, StateValueTypeException
{
228 assert (this.isActive
);
230 Type expectedSvType
= stateValueTypes
.get(quark
);
231 checkValidAttribute(quark
);
234 * Make sure the state value type we're inserting is the same as the
235 * one registered for this attribute.
237 if (expectedSvType
== Type
.NULL
) {
239 * The value hasn't been used yet, set it to the value
240 * we're currently inserting (which might be null/-1 again).
242 stateValueTypes
.set(quark
, value
.getType());
243 } else if ((value
.getType() != Type
.NULL
) && (value
.getType() != expectedSvType
)) {
245 * We authorize inserting null values in any type of attribute,
246 * but for every other types, it needs to match our expectations!
248 throw new StateValueTypeException();
251 /* Update the Transient State's lastestTime, if needed */
252 if (latestTime
< eventTime
) {
253 latestTime
= eventTime
;
256 if (ongoingStateInfo
.get(quark
).equals(value
)) {
258 * This is the case where the new value and the one already present
259 * in the Builder are the same. We do not need to create an
260 * interval, we'll just keep the current one going.
265 if (ongoingStateStartTimes
.get(quark
) < eventTime
) {
267 * These two conditions are necessary to create an interval and
268 * update ongoingStateInfo.
270 backend
.insertPastState(ongoingStateStartTimes
.get(quark
),
271 eventTime
- 1, /* End Time */
272 quark
, /* attribute quark */
273 ongoingStateInfo
.get(quark
)); /* StateValue */
275 ongoingStateStartTimes
.set(quark
, eventTime
);
277 ongoingStateInfo
.set(quark
, value
);
282 * Run a "get state at time" query on the Transient State only.
285 * The stateInfo object in which we will put our relevant
288 * The requested timestamp
290 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
) {
291 ITmfStateInterval interval
;
293 if (!this.isActive
) {
296 assert (stateInfo
.size() == ongoingStateInfo
.size());
298 for (int i
= 0; i
< ongoingStateInfo
.size(); i
++) {
300 * We build a dummy interval with end time = -1 to put in the answer
303 if (this.hasInfoAboutStateOf(t
, i
)) {
304 interval
= new TmfStateInterval(ongoingStateStartTimes
.get(i
), -1,
305 i
, ongoingStateInfo
.get(i
));
306 stateInfo
.set(i
, interval
);
312 * Close off the Transient State, used for example when we are done reading
313 * a static trace file. All the information currently contained in it will
314 * be converted to intervals and "flushed" to the state history.
317 * The timestamp to use as end time for the state history (since
318 * it may be different than the timestamp of the last state
321 public void closeTransientState(long endTime
) {
322 assert (this.isActive
);
324 for (int i
= 0; i
< ongoingStateInfo
.size(); i
++) {
325 if (ongoingStateStartTimes
.get(i
) > endTime
) {
327 * Handle the cases where trace end > timestamp of last state
328 * change. This can happen when inserting "future" changes.
333 backend
.insertPastState(ongoingStateStartTimes
.get(i
),
334 endTime
, /* End Time */
335 i
, /* attribute quark */
336 ongoingStateInfo
.get(i
)); /* StateValue */
338 } catch (TimeRangeException e
) {
340 * This shouldn't happen, since we control where the interval's
341 * start time comes from
343 throw new IllegalStateException(e
);
347 ongoingStateInfo
.clear();
348 ongoingStateStartTimes
.clear();
349 this.isActive
= false;
354 * Simply returns if this Transient State is currently being used or not
356 * @return True if this transient state is active
358 public boolean isActive() {
359 return this.isActive
;
363 * Mark this transient state as inactive
365 public void setInactive() {
370 * Debugging method that prints the contents of the transient state
373 * The writer to which the output should be written
375 public void debugPrint(PrintWriter writer
) {
376 /* Only used for debugging, shouldn't be externalized */
377 writer
.println("------------------------------"); //$NON-NLS-1$
378 writer
.println("Info stored in the Builder:"); //$NON-NLS-1$
379 if (!this.isActive
) {
380 writer
.println("Builder is currently inactive"); //$NON-NLS-1$
381 writer
.println('\n');
384 writer
.println("\nAttribute\tStateValue\tValid since time"); //$NON-NLS-1$
385 for (int i
= 0; i
< ongoingStateInfo
.size(); i
++) {
386 writer
.format("%d\t\t", i
); //$NON-NLS-1$
387 writer
.print(ongoingStateInfo
.get(i
).toString() + "\t\t"); //$NON-NLS-1$
388 writer
.println(ongoingStateStartTimes
.get(i
).toString());
390 writer
.println('\n');