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
;
18 import java
.io
.PrintWriter
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
23 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
35 * The Transient State is used to build intervals from punctual state changes.
36 * It contains a "state info" vector similar to the "current state", except here
37 * we also record the start time of every state stored in it.
39 * We can then build {@link ITmfStateInterval}'s, to be inserted in a
40 * {@link IStateHistoryBackend} when we detect state changes : the "start time"
41 * of the interval will be the recorded time we have here, and the "end time"
42 * will be the timestamp of the new state-changing event we just read.
44 * @author Alexandre Montplaisir
47 public class TransientState
{
49 /* Indicates where to insert state changes that we generate */
50 private final IStateHistoryBackend fBackend
;
52 private final ReentrantReadWriteLock fRWLock
= new ReentrantReadWriteLock(false);
54 private volatile boolean fIsActive
;
55 private volatile long fLatestTime
;
57 /* A method accessing these arrays will have to go through the lock */
58 private List
<ITmfStateValue
> fOngoingStateInfo
;
59 private List
<Long
> fOngoingStateStartTimes
;
60 private List
<Type
> fStateValueTypes
;
66 * The back-end in which to insert the generated state intervals
68 public TransientState(IStateHistoryBackend backend
) {
71 fOngoingStateInfo
= new ArrayList
<>();
72 fOngoingStateStartTimes
= new ArrayList
<>();
73 fStateValueTypes
= new ArrayList
<>();
75 fLatestTime
= backend
.getStartTime();
79 * Get the latest time we have seen so far.
81 * @return The latest time seen in the transient state
83 public long getLatestTime() {
88 * Retrieve the ongoing state value for a given index (attribute quark).
91 * The quark of the attribute to look for
92 * @return The corresponding state value
93 * @throws IndexOutOfBoundsException
94 * If the quark is out of range
96 public ITmfStateValue
getOngoingStateValue(int quark
) {
97 fRWLock
.readLock().lock();
99 return fOngoingStateInfo
.get(quark
);
101 fRWLock
.readLock().unlock();
106 * Retrieve the start time of the state in which the given attribute is in.
109 * The quark of the attribute to look for
110 * @return The start time of the current state for this attribute
111 * @throws IndexOutOfBoundsException
112 * If the quark is out of range
114 public long getOngoingStartTime(int quark
) {
115 fRWLock
.readLock().lock();
117 return fOngoingStateStartTimes
.get(quark
);
119 fRWLock
.readLock().unlock();
124 * Modify the current state for a given attribute. This will not update the
125 * "ongoing state start time" in any way, so be careful when using this.
128 * The quark of the attribute to modify
130 * The state value the attribute should have
131 * @throws IndexOutOfBoundsException
132 * If the quark is out of range
134 public void changeOngoingStateValue(int quark
, ITmfStateValue newValue
) {
135 fRWLock
.writeLock().lock();
137 fOngoingStateInfo
.set(quark
, newValue
);
139 fRWLock
.writeLock().unlock();
144 * Convenience method to return the "ongoing" value for a given attribute as
145 * a dummy interval whose end time = the current latest time.
148 * The quark of the attribute
149 * @return An interval representing the current state (but whose end time is
150 * the current one, and probably not the "final" one)
151 * @throws IndexOutOfBoundsException
152 * If the quark is out of range
154 public ITmfStateInterval
getOngoingInterval(int quark
) {
155 fRWLock
.readLock().lock();
157 return new TmfStateInterval(fOngoingStateStartTimes
.get(quark
), fLatestTime
,
158 quark
, fOngoingStateInfo
.get(quark
));
160 fRWLock
.readLock().unlock();
165 * Try to get the state interval valid for time/quark, if it is present in
166 * this transient state. If it is not (for example, a new value is active
167 * since after the specified timestamp) then null will be returned.
170 * The timestamp to look for
172 * The quark of the attribute to look for
173 * @return The corresponding TmfStateInterval object if we could find it in
174 * this transient state, or null if we couldn't.
175 * @throws IndexOutOfBoundsException
176 * If the quark is out of range
178 public @Nullable ITmfStateInterval
getIntervalAt(long time
, int quark
) {
179 fRWLock
.readLock().lock();
181 if (!isActive() || time
< fOngoingStateStartTimes
.get(quark
)) {
184 return new TmfStateInterval(fOngoingStateStartTimes
.get(quark
),
185 fLatestTime
, quark
, fOngoingStateInfo
.get(quark
));
187 fRWLock
.readLock().unlock();
192 * More advanced version of {@link #changeOngoingStateValue}. Replaces the
193 * complete ongoingStateInfo in one go, and updates the
194 * ongoingStateStartTimes and #stateValuesTypes accordingly. BE VERY CAREFUL
197 * @param newStateIntervals
198 * The List of intervals that will represent the new
199 * "ongoing state". Their end times don't matter, we will only
200 * check their value and start times.
202 public void replaceOngoingState(List
<ITmfStateInterval
> newStateIntervals
) {
203 final int size
= newStateIntervals
.size();
205 fRWLock
.writeLock().lock();
207 fOngoingStateInfo
= new ArrayList
<>(size
);
208 fOngoingStateStartTimes
= new ArrayList
<>(size
);
209 fStateValueTypes
= new ArrayList
<>(size
);
211 for (ITmfStateInterval interval
: newStateIntervals
) {
212 fOngoingStateInfo
.add(interval
.getStateValue());
213 fOngoingStateStartTimes
.add(interval
.getStartTime());
214 fStateValueTypes
.add(interval
.getStateValue().getType());
217 fRWLock
.writeLock().unlock();
222 * Add an "empty line" to both "ongoing..." vectors. This is needed so the
223 * Ongoing... tables can stay in sync with the number of attributes in the
224 * attribute tree, namely when we add sub-path attributes.
226 public void addEmptyEntry() {
227 fRWLock
.writeLock().lock();
230 * Since this is a new attribute, we suppose it was in the
231 * "null state" since the beginning (so we can have intervals
232 * covering for all timestamps). A null interval will then get added
233 * at the first state change.
235 fOngoingStateInfo
.add(TmfStateValue
.nullValue());
236 fStateValueTypes
.add(Type
.NULL
);
238 fOngoingStateStartTimes
.add(fBackend
.getStartTime());
240 fRWLock
.writeLock().unlock();
245 * Process a state change to be inserted in the history.
248 * The timestamp associated with this state change
250 * The new StateValue associated to this attribute
252 * The quark of the attribute that is being modified
253 * @throws TimeRangeException
254 * If 'eventTime' is invalid
255 * @throws IndexOutOfBoundsException
256 * If the quark is out of range
257 * @throws StateValueTypeException
258 * If the state value to be inserted is of a different type of
259 * what was inserted so far for this attribute.
261 public void processStateChange(long eventTime
, ITmfStateValue value
, int quark
)
262 throws TimeRangeException
, StateValueTypeException
{
263 if (!this.fIsActive
) {
267 fRWLock
.writeLock().lock();
269 Type expectedSvType
= fStateValueTypes
.get(quark
);
272 * Make sure the state value type we're inserting is the same as the
273 * one registered for this attribute.
275 if (expectedSvType
== Type
.NULL
) {
277 * The value hasn't been used yet, set it to the value we're
278 * currently inserting (which might be null/-1 again).
280 fStateValueTypes
.set(quark
, value
.getType());
281 } else if ((value
.getType() != Type
.NULL
) && (value
.getType() != expectedSvType
)) {
283 * We authorize inserting null values in any type of attribute,
284 * but for every other types, it needs to match our
287 throw new StateValueTypeException(fBackend
.getSSID() + " Quark:" + quark
+ ", Type:" + value
.getType() + ", Expected:" + expectedSvType
); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
290 if (fOngoingStateInfo
.get(quark
).equals(value
)) {
292 * This is the case where the new value and the one already
293 * present in the Builder are the same. We do not need to create
294 * an interval, we'll just keep the current one going.
299 if (fOngoingStateStartTimes
.get(quark
) < eventTime
) {
301 * These two conditions are necessary to create an interval and
302 * update ongoingStateInfo.
304 fBackend
.insertPastState(fOngoingStateStartTimes
.get(quark
),
305 eventTime
- 1, /* End Time */
306 quark
, /* attribute quark */
307 fOngoingStateInfo
.get(quark
)); /* StateValue */
309 fOngoingStateStartTimes
.set(quark
, eventTime
);
311 fOngoingStateInfo
.set(quark
, value
);
313 /* Update the Transient State's lastestTime, if needed */
314 if (fLatestTime
< eventTime
) {
315 fLatestTime
= eventTime
;
319 fRWLock
.writeLock().unlock();
324 * Run a "get state at time" query on the Transient State only.
327 * The stateInfo object in which we will put our relevant
330 * The requested timestamp
332 public void doQuery(List
<@Nullable ITmfStateInterval
> stateInfo
, long t
) {
333 fRWLock
.readLock().lock();
335 if (!this.fIsActive
) {
338 if (stateInfo
.size() > fOngoingStateInfo
.size()) {
339 throw new IllegalArgumentException();
342 for (int i
= 0; i
< stateInfo
.size(); i
++) {
344 * We build a dummy interval whose end time =
345 * "current transient state end time" to put in the answer to
348 final ITmfStateInterval interval
= getIntervalAt(t
, i
);
349 if (interval
!= null) {
350 stateInfo
.set(i
, interval
);
354 fRWLock
.readLock().unlock();
359 * Close off the Transient State, used for example when we are done reading
360 * a static trace file. All the information currently contained in it will
361 * be converted to intervals and "flushed" to the state history.
364 * The timestamp to use as end time for the state history (since
365 * it may be different than the timestamp of the last state
368 public void closeTransientState(long endTime
) {
369 if (!this.fIsActive
) {
373 fRWLock
.writeLock().lock();
375 for (int i
= 0; i
< fOngoingStateInfo
.size(); i
++) {
376 if (fOngoingStateStartTimes
.get(i
) > endTime
) {
378 * Handle the cases where trace end > timestamp of last
379 * state change. This can happen when inserting "future"
385 fBackend
.insertPastState(fOngoingStateStartTimes
.get(i
),
386 endTime
, /* End Time */
387 i
, /* attribute quark */
388 fOngoingStateInfo
.get(i
)); /* StateValue */
390 } catch (TimeRangeException e
) {
392 * This shouldn't happen, since we control where the
393 * interval's start time comes from
395 throw new IllegalStateException(e
);
399 fOngoingStateInfo
.clear();
400 fOngoingStateStartTimes
.clear();
401 this.fIsActive
= false;
404 fRWLock
.writeLock().unlock();
409 * Simply returns if this Transient State is currently being used or not
411 * @return True if this transient state is active
413 public boolean isActive() {
414 return this.fIsActive
;
418 * Mark this transient state as inactive
420 public void setInactive() {
425 * Debugging method that prints the contents of the transient state
428 * The writer to which the output should be written
430 public void debugPrint(PrintWriter writer
) {
431 /* Only used for debugging, shouldn't be externalized */
432 writer
.println("------------------------------"); //$NON-NLS-1$
433 writer
.println("Info stored in the Builder:"); //$NON-NLS-1$
434 if (!this.fIsActive
) {
435 writer
.println("Builder is currently inactive"); //$NON-NLS-1$
436 writer
.println('\n');
439 writer
.println("\nAttribute\tStateValue\tValid since time"); //$NON-NLS-1$
440 for (int i
= 0; i
< fOngoingStateInfo
.size(); i
++) {
441 writer
.format("%d\t\t", i
); //$NON-NLS-1$
442 writer
.print(fOngoingStateInfo
.get(i
).toString() + "\t\t"); //$NON-NLS-1$
443 writer
.println(fOngoingStateStartTimes
.get(i
).toString());
445 writer
.println('\n');