1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.tracecompass
.internal
.statesystem
.core
;
15 import java
.io
.PrintWriter
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
18 import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
20 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
33 * The Transient State is used to build intervals from punctual state changes.
34 * It contains a "state info" vector similar to the "current state", except here
35 * we also record the start time of every state stored in it.
37 * We can then build {@link ITmfStateInterval}'s, to be inserted in a
38 * {@link IStateHistoryBackend} when we detect state changes : the "start time"
39 * of the interval will be the recorded time we have here, and the "end time"
40 * will be the timestamp of the new state-changing event we just read.
42 * @author Alexandre Montplaisir
45 public class TransientState
{
47 /* Indicates where to insert state changes that we generate */
48 private final IStateHistoryBackend fBackend
;
50 private final ReentrantReadWriteLock fRWLock
= new ReentrantReadWriteLock(false);
52 private volatile boolean fIsActive
;
53 private volatile long fLatestTime
;
55 /* A method accessing these arrays will have to go through the lock */
56 private List
<ITmfStateValue
> fOngoingStateInfo
;
57 private List
<Long
> fOngoingStateStartTimes
;
58 private List
<Type
> fStateValueTypes
;
64 * The back-end in which to insert the generated state intervals
66 public TransientState(IStateHistoryBackend backend
) {
69 fOngoingStateInfo
= new ArrayList
<>();
70 fOngoingStateStartTimes
= new ArrayList
<>();
71 fStateValueTypes
= new ArrayList
<>();
73 fLatestTime
= backend
.getStartTime();
77 * Get the latest time we have seen so far.
79 * @return The latest time seen in the transient state
81 public long getLatestTime() {
86 * Retrieve the ongoing state value for a given index (attribute quark).
89 * The quark of the attribute to look for
90 * @return The corresponding state value
91 * @throws AttributeNotFoundException
92 * If the quark is invalid
94 public ITmfStateValue
getOngoingStateValue(int quark
) throws AttributeNotFoundException
{
95 fRWLock
.readLock().lock();
97 checkValidAttribute(quark
);
98 ITmfStateValue ret
= fOngoingStateInfo
.get(quark
);
100 throw new IllegalStateException("Null interval stored in transient state"); //$NON-NLS-1$
104 fRWLock
.readLock().unlock();
109 * Retrieve the start time of the state in which the given attribute is in.
112 * The quark of the attribute to look for
113 * @return The start time of the current state for this attribute
114 * @throws AttributeNotFoundException
115 * If the quark is invalid
117 public long getOngoingStartTime(int quark
) throws AttributeNotFoundException
{
118 fRWLock
.readLock().lock();
120 checkValidAttribute(quark
);
121 return fOngoingStateStartTimes
.get(quark
);
123 fRWLock
.readLock().unlock();
128 * Modify the current state for a given attribute. This will not update the
129 * "ongoing state start time" in any way, so be careful when using this.
132 * The quark of the attribute to modify
134 * The state value the attribute should have
135 * @throws AttributeNotFoundException
136 * If the quark is invalid
138 public void changeOngoingStateValue(int quark
, ITmfStateValue newValue
)
139 throws AttributeNotFoundException
{
140 fRWLock
.writeLock().lock();
142 checkValidAttribute(quark
);
143 fOngoingStateInfo
.set(quark
, newValue
);
145 fRWLock
.writeLock().unlock();
150 * Convenience method to return the "ongoing" value for a given attribute as
151 * a dummy interval whose end time = the current latest time.
154 * The quark of the attribute
155 * @return An interval representing the current state (but whose end time is
156 * the current one, and probably not the "final" one)
157 * @throws AttributeNotFoundException
158 * If the quark is invalid
160 public ITmfStateInterval
getOngoingInterval(int quark
) throws AttributeNotFoundException
{
161 fRWLock
.readLock().lock();
163 checkValidAttribute(quark
);
164 return new TmfStateInterval(fOngoingStateStartTimes
.get(quark
), fLatestTime
,
165 quark
, fOngoingStateInfo
.get(quark
));
167 fRWLock
.readLock().unlock();
172 * Try to get the state interval valid for time/quark, if it is present in
173 * this transient state. If it is not (for example, a new value is active
174 * since after the specified timestamp) then null will be returned.
177 * The timestamp to look for
179 * The quark of the attribute to look for
180 * @return The corresponding TmfStateInterval object if we could find it in
181 * this transient state, or null if we couldn't.
183 public @Nullable ITmfStateInterval
getIntervalAt(long time
, int quark
) {
184 fRWLock
.readLock().lock();
186 checkValidAttribute(quark
);
187 if (!isActive() || time
< fOngoingStateStartTimes
.get(quark
)) {
190 return new TmfStateInterval(fOngoingStateStartTimes
.get(quark
),
191 fLatestTime
, quark
, fOngoingStateInfo
.get(quark
));
192 } catch (AttributeNotFoundException e
) {
195 fRWLock
.readLock().unlock();
199 private void checkValidAttribute(int quark
) throws AttributeNotFoundException
{
200 if (quark
> fOngoingStateInfo
.size() - 1 || quark
< 0) {
201 throw new AttributeNotFoundException();
206 * More advanced version of {@link #changeOngoingStateValue}. Replaces the
207 * complete ongoingStateInfo in one go, and updates the
208 * ongoingStateStartTimes and #stateValuesTypes accordingly. BE VERY CAREFUL
211 * @param newStateIntervals
212 * The List of intervals that will represent the new
213 * "ongoing state". Their end times don't matter, we will only
214 * check their value and start times.
216 public void replaceOngoingState(List
<ITmfStateInterval
> newStateIntervals
) {
217 final int size
= newStateIntervals
.size();
219 fRWLock
.writeLock().lock();
221 fOngoingStateInfo
= new ArrayList
<>(size
);
222 fOngoingStateStartTimes
= new ArrayList
<>(size
);
223 fStateValueTypes
= new ArrayList
<>(size
);
225 for (ITmfStateInterval interval
: newStateIntervals
) {
226 fOngoingStateInfo
.add(interval
.getStateValue());
227 fOngoingStateStartTimes
.add(interval
.getStartTime());
228 fStateValueTypes
.add(interval
.getStateValue().getType());
231 fRWLock
.writeLock().unlock();
236 * Add an "empty line" to both "ongoing..." vectors. This is needed so the
237 * Ongoing... tables can stay in sync with the number of attributes in the
238 * attribute tree, namely when we add sub-path attributes.
240 public void addEmptyEntry() {
241 fRWLock
.writeLock().lock();
244 * Since this is a new attribute, we suppose it was in the
245 * "null state" since the beginning (so we can have intervals
246 * covering for all timestamps). A null interval will then get added
247 * at the first state change.
249 fOngoingStateInfo
.add(TmfStateValue
.nullValue());
250 fStateValueTypes
.add(Type
.NULL
);
252 fOngoingStateStartTimes
.add(fBackend
.getStartTime());
254 fRWLock
.writeLock().unlock();
259 * Process a state change to be inserted in the history.
262 * The timestamp associated with this state change
264 * The new StateValue associated to this attribute
266 * The quark of the attribute that is being modified
267 * @throws TimeRangeException
268 * If 'eventTime' is invalid
269 * @throws AttributeNotFoundException
270 * IF 'quark' does not represent an existing attribute
271 * @throws StateValueTypeException
272 * If the state value to be inserted is of a different type of
273 * what was inserted so far for this attribute.
275 public void processStateChange(long eventTime
, ITmfStateValue value
, int quark
)
276 throws TimeRangeException
, AttributeNotFoundException
, StateValueTypeException
{
277 if (!this.fIsActive
) {
281 fRWLock
.writeLock().lock();
283 Type expectedSvType
= fStateValueTypes
.get(quark
);
284 checkValidAttribute(quark
);
287 * Make sure the state value type we're inserting is the same as the
288 * one registered for this attribute.
290 if (expectedSvType
== Type
.NULL
) {
292 * The value hasn't been used yet, set it to the value we're
293 * currently inserting (which might be null/-1 again).
295 fStateValueTypes
.set(quark
, value
.getType());
296 } else if ((value
.getType() != Type
.NULL
) && (value
.getType() != expectedSvType
)) {
298 * We authorize inserting null values in any type of attribute,
299 * but for every other types, it needs to match our
302 throw new StateValueTypeException();
305 if (fOngoingStateInfo
.get(quark
).equals(value
)) {
307 * This is the case where the new value and the one already
308 * present in the Builder are the same. We do not need to create
309 * an interval, we'll just keep the current one going.
314 if (fOngoingStateStartTimes
.get(quark
) < eventTime
) {
316 * These two conditions are necessary to create an interval and
317 * update ongoingStateInfo.
319 fBackend
.insertPastState(fOngoingStateStartTimes
.get(quark
),
320 eventTime
- 1, /* End Time */
321 quark
, /* attribute quark */
322 fOngoingStateInfo
.get(quark
)); /* StateValue */
324 fOngoingStateStartTimes
.set(quark
, eventTime
);
326 fOngoingStateInfo
.set(quark
, value
);
328 /* Update the Transient State's lastestTime, if needed */
329 if (fLatestTime
< eventTime
) {
330 fLatestTime
= eventTime
;
334 fRWLock
.writeLock().unlock();
339 * Run a "get state at time" query on the Transient State only.
342 * The stateInfo object in which we will put our relevant
345 * The requested timestamp
347 public void doQuery(List
<ITmfStateInterval
> stateInfo
, long t
) {
348 fRWLock
.readLock().lock();
350 if (!this.fIsActive
) {
353 if (stateInfo
.size() > fOngoingStateInfo
.size()) {
354 throw new IllegalArgumentException();
357 for (int i
= 0; i
< stateInfo
.size(); i
++) {
359 * We build a dummy interval whose end time =
360 * "current transient state end time" to put in the answer to
363 final ITmfStateInterval interval
= getIntervalAt(t
, i
);
364 if (interval
!= null) {
365 stateInfo
.set(i
, interval
);
369 fRWLock
.readLock().unlock();
374 * Close off the Transient State, used for example when we are done reading
375 * a static trace file. All the information currently contained in it will
376 * be converted to intervals and "flushed" to the state history.
379 * The timestamp to use as end time for the state history (since
380 * it may be different than the timestamp of the last state
383 public void closeTransientState(long endTime
) {
384 if (!this.fIsActive
) {
388 fRWLock
.writeLock().lock();
390 for (int i
= 0; i
< fOngoingStateInfo
.size(); i
++) {
391 if (fOngoingStateStartTimes
.get(i
) > endTime
) {
393 * Handle the cases where trace end > timestamp of last
394 * state change. This can happen when inserting "future"
400 fBackend
.insertPastState(fOngoingStateStartTimes
.get(i
),
401 endTime
, /* End Time */
402 i
, /* attribute quark */
403 fOngoingStateInfo
.get(i
)); /* StateValue */
405 } catch (TimeRangeException e
) {
407 * This shouldn't happen, since we control where the
408 * interval's start time comes from
410 throw new IllegalStateException(e
);
414 fOngoingStateInfo
.clear();
415 fOngoingStateStartTimes
.clear();
416 this.fIsActive
= false;
419 fRWLock
.writeLock().unlock();
424 * Simply returns if this Transient State is currently being used or not
426 * @return True if this transient state is active
428 public boolean isActive() {
429 return this.fIsActive
;
433 * Mark this transient state as inactive
435 public void setInactive() {
440 * Debugging method that prints the contents of the transient state
443 * The writer to which the output should be written
445 public void debugPrint(PrintWriter writer
) {
446 /* Only used for debugging, shouldn't be externalized */
447 writer
.println("------------------------------"); //$NON-NLS-1$
448 writer
.println("Info stored in the Builder:"); //$NON-NLS-1$
449 if (!this.fIsActive
) {
450 writer
.println("Builder is currently inactive"); //$NON-NLS-1$
451 writer
.println('\n');
454 writer
.println("\nAttribute\tStateValue\tValid since time"); //$NON-NLS-1$
455 for (int i
= 0; i
< fOngoingStateInfo
.size(); i
++) {
456 writer
.format("%d\t\t", i
); //$NON-NLS-1$
457 writer
.print(fOngoingStateInfo
.get(i
).toString() + "\t\t"); //$NON-NLS-1$
458 writer
.println(fOngoingStateStartTimes
.get(i
).toString());
460 writer
.println('\n');