tmf: Enforce non-null state sytem backends
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / TransientState.java
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>
5 *
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
10 *
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.core.statesystem;
14
15 import java.io.PrintWriter;
16 import java.util.ArrayList;
17 import java.util.List;
18
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;
29
30 /**
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.
34 *
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.
39 *
40 * @author Alexandre Montplaisir
41 */
42 public class TransientState {
43
44 /* Indicates where to insert state changes that we generate */
45 @NonNull private final IStateHistoryBackend backend;
46
47 private boolean isActive;
48 private long latestTime;
49
50 private List<ITmfStateValue> ongoingStateInfo;
51 private List<Long> ongoingStateStartTimes;
52 private List<Type> stateValueTypes;
53
54 /**
55 * Constructor
56 *
57 * @param backend
58 * The back-end in which to insert the generated state intervals
59 */
60 public TransientState(@NonNull IStateHistoryBackend backend) {
61 this.backend = backend;
62 isActive = true;
63 ongoingStateInfo = new ArrayList<>();
64 ongoingStateStartTimes = new ArrayList<>();
65 stateValueTypes = new ArrayList<>();
66
67 latestTime = backend.getStartTime();
68 }
69
70 /**
71 * Get the latest time we have seen so far.
72 *
73 * @return The latest time seen in the transient state
74 */
75 public long getLatestTime() {
76 return latestTime;
77 }
78
79 /**
80 * Retrieve the ongoing state value for a given index (attribute quark).
81 *
82 * @param 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
87 */
88 public ITmfStateValue getOngoingStateValue(int quark) throws AttributeNotFoundException {
89 checkValidAttribute(quark);
90 return ongoingStateInfo.get(quark);
91 }
92
93 /**
94 * Retrieve the start time of the state in which the given attribute is in.
95 *
96 * @param quark
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
101 */
102 public long getOngoingStartTime(int quark) throws AttributeNotFoundException {
103 checkValidAttribute(quark);
104 return ongoingStateStartTimes.get(quark);
105 }
106
107 /**
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.
110 *
111 * @param quark
112 * The quark of the attribute to modify
113 * @param newValue
114 * The state value the attribute should have
115 * @throws AttributeNotFoundException
116 * If the quark is invalid
117 */
118 public void changeOngoingStateValue(int quark, ITmfStateValue newValue)
119 throws AttributeNotFoundException {
120 checkValidAttribute(quark);
121 ongoingStateInfo.set(quark, newValue);
122 }
123
124 /**
125 * Convenience method to return the "ongoing" value for a given attribute as
126 * a dummy interval whose end time = the current latest time.
127 *
128 * @param quark
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
134 */
135 public ITmfStateInterval getOngoingInterval(int quark) throws AttributeNotFoundException {
136 checkValidAttribute(quark);
137 return new TmfStateInterval(ongoingStateStartTimes.get(quark), latestTime,
138 quark, ongoingStateInfo.get(quark));
139 }
140
141 private void checkValidAttribute(int quark) throws AttributeNotFoundException {
142 if (quark > ongoingStateInfo.size() - 1 || quark < 0) {
143 throw new AttributeNotFoundException();
144 }
145 }
146
147 /**
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
151 * WITH THIS!
152 *
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.
157 */
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);
163
164 for (ITmfStateInterval interval : newStateIntervals) {
165 ongoingStateInfo.add(interval.getStateValue());
166 ongoingStateStartTimes.add(interval.getStartTime());
167 stateValueTypes.add(interval.getStateValue().getType());
168 }
169 }
170
171 /**
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.
175 */
176 public synchronized void addEmptyEntry() {
177 /*
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
181 * change.
182 */
183 ongoingStateInfo.add(TmfStateValue.nullValue());
184 stateValueTypes.add(Type.NULL);
185
186 ongoingStateStartTimes.add(backend.getStartTime());
187 }
188
189 /**
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.
193 *
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)
196 *
197 * @param time
198 * The timestamp to look for
199 * @param quark
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
203 */
204 public boolean hasInfoAboutStateOf(long time, int quark) {
205 return (this.isActive() && time >= ongoingStateStartTimes.get(quark));
206 }
207
208 /**
209 * Process a state change to be inserted in the history.
210 *
211 * @param eventTime
212 * The timestamp associated with this state change
213 * @param value
214 * The new StateValue associated to this attribute
215 * @param quark
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.
224 */
225 public synchronized void processStateChange(long eventTime,
226 ITmfStateValue value, int quark) throws TimeRangeException,
227 AttributeNotFoundException, StateValueTypeException {
228 assert (this.isActive);
229
230 Type expectedSvType = stateValueTypes.get(quark);
231 checkValidAttribute(quark);
232
233 /*
234 * Make sure the state value type we're inserting is the same as the
235 * one registered for this attribute.
236 */
237 if (expectedSvType == Type.NULL) {
238 /*
239 * The value hasn't been used yet, set it to the value
240 * we're currently inserting (which might be null/-1 again).
241 */
242 stateValueTypes.set(quark, value.getType());
243 } else if ((value.getType() != Type.NULL) && (value.getType() != expectedSvType)) {
244 /*
245 * We authorize inserting null values in any type of attribute,
246 * but for every other types, it needs to match our expectations!
247 */
248 throw new StateValueTypeException();
249 }
250
251 /* Update the Transient State's lastestTime, if needed */
252 if (latestTime < eventTime) {
253 latestTime = eventTime;
254 }
255
256 if (ongoingStateInfo.get(quark).equals(value)) {
257 /*
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.
261 */
262 return;
263 }
264
265 if (ongoingStateStartTimes.get(quark) < eventTime) {
266 /*
267 * These two conditions are necessary to create an interval and
268 * update ongoingStateInfo.
269 */
270 backend.insertPastState(ongoingStateStartTimes.get(quark),
271 eventTime - 1, /* End Time */
272 quark, /* attribute quark */
273 ongoingStateInfo.get(quark)); /* StateValue */
274
275 ongoingStateStartTimes.set(quark, eventTime);
276 }
277 ongoingStateInfo.set(quark, value);
278 return;
279 }
280
281 /**
282 * Run a "get state at time" query on the Transient State only.
283 *
284 * @param stateInfo
285 * The stateInfo object in which we will put our relevant
286 * information
287 * @param t
288 * The requested timestamp
289 */
290 public void doQuery(List<ITmfStateInterval> stateInfo, long t) {
291 ITmfStateInterval interval;
292
293 if (!this.isActive) {
294 return;
295 }
296 assert (stateInfo.size() == ongoingStateInfo.size());
297
298 for (int i = 0; i < ongoingStateInfo.size(); i++) {
299 /*
300 * We build a dummy interval with end time = -1 to put in the answer
301 * to the query.
302 */
303 if (this.hasInfoAboutStateOf(t, i)) {
304 interval = new TmfStateInterval(ongoingStateStartTimes.get(i), -1,
305 i, ongoingStateInfo.get(i));
306 stateInfo.set(i, interval);
307 }
308 }
309 }
310
311 /**
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.
315 *
316 * @param endTime
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
319 * change)
320 */
321 public void closeTransientState(long endTime) {
322 assert (this.isActive);
323
324 for (int i = 0; i < ongoingStateInfo.size(); i++) {
325 if (ongoingStateStartTimes.get(i) > endTime) {
326 /*
327 * Handle the cases where trace end > timestamp of last state
328 * change. This can happen when inserting "future" changes.
329 */
330 continue;
331 }
332 try {
333 backend.insertPastState(ongoingStateStartTimes.get(i),
334 endTime, /* End Time */
335 i, /* attribute quark */
336 ongoingStateInfo.get(i)); /* StateValue */
337
338 } catch (TimeRangeException e) {
339 /*
340 * This shouldn't happen, since we control where the interval's
341 * start time comes from
342 */
343 throw new IllegalStateException(e);
344 }
345 }
346
347 ongoingStateInfo.clear();
348 ongoingStateStartTimes.clear();
349 this.isActive = false;
350 return;
351 }
352
353 /**
354 * Simply returns if this Transient State is currently being used or not
355 *
356 * @return True if this transient state is active
357 */
358 public boolean isActive() {
359 return this.isActive;
360 }
361
362 /**
363 * Mark this transient state as inactive
364 */
365 public void setInactive() {
366 isActive = false;
367 }
368
369 /**
370 * Debugging method that prints the contents of the transient state
371 *
372 * @param writer
373 * The writer to which the output should be written
374 */
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');
382 return;
383 }
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());
389 }
390 writer.println('\n');
391 return;
392 }
393
394 }
This page took 0.058055 seconds and 5 git commands to generate.