Commit | Line | Data |
---|---|---|
a52fde77 | 1 | /******************************************************************************* |
e13bd4cd | 2 | * Copyright (c) 2012, 2015 Ericsson |
a52fde77 AM |
3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> | |
97042f0a | 5 | * |
a52fde77 AM |
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 | |
97042f0a | 10 | * |
e13bd4cd PT |
11 | * Contributors: |
12 | * Alexandre Montplaisir - Initial API and implementation | |
13 | * Patrick Tasse - Add message to exceptions | |
a52fde77 AM |
14 | *******************************************************************************/ |
15 | ||
e894a508 | 16 | package org.eclipse.tracecompass.internal.statesystem.core; |
a52fde77 AM |
17 | |
18 | import java.io.PrintWriter; | |
19 | import java.util.ArrayList; | |
20 | import java.util.List; | |
09e6fd9b | 21 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
a52fde77 | 22 | |
feea3b3c | 23 | import org.eclipse.jdt.annotation.NonNullByDefault; |
09e6fd9b | 24 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 AM |
25 | import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend; |
26 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
27 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
28 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
29 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; | |
30 | import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval; | |
31 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
e894a508 | 32 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type; |
feea3b3c | 33 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; |
a52fde77 AM |
34 | |
35 | /** | |
a6917276 AM |
36 | * The Transient State is used to build intervals from punctual state changes. |
37 | * It contains a "state info" vector similar to the "current state", except here | |
38 | * we also record the start time of every state stored in it. | |
97042f0a | 39 | * |
a6917276 AM |
40 | * We can then build {@link ITmfStateInterval}'s, to be inserted in a |
41 | * {@link IStateHistoryBackend} when we detect state changes : the "start time" | |
42 | * of the interval will be the recorded time we have here, and the "end time" | |
43 | * will be the timestamp of the new state-changing event we just read. | |
97042f0a | 44 | * |
a6917276 | 45 | * @author Alexandre Montplaisir |
a52fde77 | 46 | */ |
feea3b3c | 47 | @NonNullByDefault |
a6917276 | 48 | public class TransientState { |
a52fde77 AM |
49 | |
50 | /* Indicates where to insert state changes that we generate */ | |
086cd39c | 51 | private final IStateHistoryBackend fBackend; |
a52fde77 | 52 | |
086cd39c | 53 | private final ReentrantReadWriteLock fRWLock = new ReentrantReadWriteLock(false); |
a52fde77 | 54 | |
086cd39c MK |
55 | private volatile boolean fIsActive; |
56 | private volatile long fLatestTime; | |
09e6fd9b AM |
57 | |
58 | /* A method accessing these arrays will have to go through the lock */ | |
086cd39c MK |
59 | private List<ITmfStateValue> fOngoingStateInfo; |
60 | private List<Long> fOngoingStateStartTimes; | |
61 | private List<Type> fStateValueTypes; | |
a52fde77 | 62 | |
a6917276 AM |
63 | /** |
64 | * Constructor | |
65 | * | |
66 | * @param backend | |
67 | * The back-end in which to insert the generated state intervals | |
68 | */ | |
feea3b3c | 69 | public TransientState(IStateHistoryBackend backend) { |
086cd39c MK |
70 | fBackend = backend; |
71 | fIsActive = true; | |
72 | fOngoingStateInfo = new ArrayList<>(); | |
73 | fOngoingStateStartTimes = new ArrayList<>(); | |
74 | fStateValueTypes = new ArrayList<>(); | |
a52fde77 | 75 | |
086cd39c | 76 | fLatestTime = backend.getStartTime(); |
a52fde77 AM |
77 | } |
78 | ||
a6917276 AM |
79 | /** |
80 | * Get the latest time we have seen so far. | |
81 | * | |
82 | * @return The latest time seen in the transient state | |
83 | */ | |
84 | public long getLatestTime() { | |
086cd39c | 85 | return fLatestTime; |
a52fde77 AM |
86 | } |
87 | ||
a6917276 AM |
88 | /** |
89 | * Retrieve the ongoing state value for a given index (attribute quark). | |
90 | * | |
91 | * @param quark | |
92 | * The quark of the attribute to look for | |
93 | * @return The corresponding state value | |
94 | * @throws AttributeNotFoundException | |
95 | * If the quark is invalid | |
96 | */ | |
97 | public ITmfStateValue getOngoingStateValue(int quark) throws AttributeNotFoundException { | |
086cd39c | 98 | fRWLock.readLock().lock(); |
09e6fd9b AM |
99 | try { |
100 | checkValidAttribute(quark); | |
4c4e2816 | 101 | return fOngoingStateInfo.get(quark); |
09e6fd9b | 102 | } finally { |
086cd39c | 103 | fRWLock.readLock().unlock(); |
09e6fd9b | 104 | } |
a52fde77 AM |
105 | } |
106 | ||
a6917276 AM |
107 | /** |
108 | * Retrieve the start time of the state in which the given attribute is in. | |
109 | * | |
110 | * @param quark | |
111 | * The quark of the attribute to look for | |
112 | * @return The start time of the current state for this attribute | |
113 | * @throws AttributeNotFoundException | |
114 | * If the quark is invalid | |
115 | */ | |
116 | public long getOngoingStartTime(int quark) throws AttributeNotFoundException { | |
086cd39c | 117 | fRWLock.readLock().lock(); |
09e6fd9b AM |
118 | try { |
119 | checkValidAttribute(quark); | |
086cd39c | 120 | return fOngoingStateStartTimes.get(quark); |
09e6fd9b | 121 | } finally { |
086cd39c | 122 | fRWLock.readLock().unlock(); |
09e6fd9b | 123 | } |
602c0697 AM |
124 | } |
125 | ||
a6917276 AM |
126 | /** |
127 | * Modify the current state for a given attribute. This will not update the | |
128 | * "ongoing state start time" in any way, so be careful when using this. | |
129 | * | |
130 | * @param quark | |
131 | * The quark of the attribute to modify | |
132 | * @param newValue | |
133 | * The state value the attribute should have | |
134 | * @throws AttributeNotFoundException | |
135 | * If the quark is invalid | |
136 | */ | |
137 | public void changeOngoingStateValue(int quark, ITmfStateValue newValue) | |
a52fde77 | 138 | throws AttributeNotFoundException { |
086cd39c | 139 | fRWLock.writeLock().lock(); |
09e6fd9b AM |
140 | try { |
141 | checkValidAttribute(quark); | |
086cd39c | 142 | fOngoingStateInfo.set(quark, newValue); |
09e6fd9b | 143 | } finally { |
086cd39c | 144 | fRWLock.writeLock().unlock(); |
09e6fd9b | 145 | } |
a52fde77 AM |
146 | } |
147 | ||
148 | /** | |
a6917276 | 149 | * Convenience method to return the "ongoing" value for a given attribute as |
c3c783f6 | 150 | * a dummy interval whose end time = the current latest time. |
97042f0a | 151 | * |
a52fde77 | 152 | * @param quark |
a6917276 AM |
153 | * The quark of the attribute |
154 | * @return An interval representing the current state (but whose end time is | |
c3c783f6 | 155 | * the current one, and probably not the "final" one) |
a52fde77 | 156 | * @throws AttributeNotFoundException |
a6917276 | 157 | * If the quark is invalid |
a52fde77 | 158 | */ |
a6917276 | 159 | public ITmfStateInterval getOngoingInterval(int quark) throws AttributeNotFoundException { |
086cd39c | 160 | fRWLock.readLock().lock(); |
09e6fd9b AM |
161 | try { |
162 | checkValidAttribute(quark); | |
086cd39c MK |
163 | return new TmfStateInterval(fOngoingStateStartTimes.get(quark), fLatestTime, |
164 | quark, fOngoingStateInfo.get(quark)); | |
09e6fd9b | 165 | } finally { |
086cd39c | 166 | fRWLock.readLock().unlock(); |
09e6fd9b AM |
167 | } |
168 | } | |
169 | ||
170 | /** | |
171 | * Try to get the state interval valid for time/quark, if it is present in | |
172 | * this transient state. If it is not (for example, a new value is active | |
173 | * since after the specified timestamp) then null will be returned. | |
174 | * | |
175 | * @param time | |
176 | * The timestamp to look for | |
177 | * @param quark | |
178 | * The quark of the attribute to look for | |
179 | * @return The corresponding TmfStateInterval object if we could find it in | |
180 | * this transient state, or null if we couldn't. | |
181 | */ | |
feea3b3c | 182 | public @Nullable ITmfStateInterval getIntervalAt(long time, int quark) { |
086cd39c | 183 | fRWLock.readLock().lock(); |
09e6fd9b AM |
184 | try { |
185 | checkValidAttribute(quark); | |
086cd39c | 186 | if (!isActive() || time < fOngoingStateStartTimes.get(quark)) { |
09e6fd9b AM |
187 | return null; |
188 | } | |
086cd39c MK |
189 | return new TmfStateInterval(fOngoingStateStartTimes.get(quark), |
190 | fLatestTime, quark, fOngoingStateInfo.get(quark)); | |
09e6fd9b AM |
191 | } catch (AttributeNotFoundException e) { |
192 | return null; | |
193 | } finally { | |
086cd39c | 194 | fRWLock.readLock().unlock(); |
09e6fd9b | 195 | } |
a52fde77 AM |
196 | } |
197 | ||
602c0697 | 198 | private void checkValidAttribute(int quark) throws AttributeNotFoundException { |
086cd39c | 199 | if (quark > fOngoingStateInfo.size() - 1 || quark < 0) { |
e13bd4cd | 200 | throw new AttributeNotFoundException(fBackend.getSSID() + " Quark:" + quark); //$NON-NLS-1$ |
a52fde77 AM |
201 | } |
202 | } | |
203 | ||
204 | /** | |
66866869 | 205 | * More advanced version of {@link #changeOngoingStateValue}. Replaces the |
a6917276 AM |
206 | * complete ongoingStateInfo in one go, and updates the |
207 | * ongoingStateStartTimes and #stateValuesTypes accordingly. BE VERY CAREFUL | |
208 | * WITH THIS! | |
97042f0a | 209 | * |
66866869 AM |
210 | * @param newStateIntervals |
211 | * The List of intervals that will represent the new | |
212 | * "ongoing state". Their end times don't matter, we will only | |
213 | * check their value and start times. | |
a52fde77 | 214 | */ |
09e6fd9b AM |
215 | public void replaceOngoingState(List<ITmfStateInterval> newStateIntervals) { |
216 | final int size = newStateIntervals.size(); | |
217 | ||
086cd39c | 218 | fRWLock.writeLock().lock(); |
09e6fd9b | 219 | try { |
086cd39c MK |
220 | fOngoingStateInfo = new ArrayList<>(size); |
221 | fOngoingStateStartTimes = new ArrayList<>(size); | |
222 | fStateValueTypes = new ArrayList<>(size); | |
09e6fd9b AM |
223 | |
224 | for (ITmfStateInterval interval : newStateIntervals) { | |
086cd39c MK |
225 | fOngoingStateInfo.add(interval.getStateValue()); |
226 | fOngoingStateStartTimes.add(interval.getStartTime()); | |
227 | fStateValueTypes.add(interval.getStateValue().getType()); | |
09e6fd9b AM |
228 | } |
229 | } finally { | |
086cd39c | 230 | fRWLock.writeLock().unlock(); |
66866869 | 231 | } |
a52fde77 AM |
232 | } |
233 | ||
234 | /** | |
235 | * Add an "empty line" to both "ongoing..." vectors. This is needed so the | |
236 | * Ongoing... tables can stay in sync with the number of attributes in the | |
237 | * attribute tree, namely when we add sub-path attributes. | |
238 | */ | |
09e6fd9b | 239 | public void addEmptyEntry() { |
086cd39c | 240 | fRWLock.writeLock().lock(); |
09e6fd9b AM |
241 | try { |
242 | /* | |
243 | * Since this is a new attribute, we suppose it was in the | |
244 | * "null state" since the beginning (so we can have intervals | |
245 | * covering for all timestamps). A null interval will then get added | |
246 | * at the first state change. | |
247 | */ | |
086cd39c MK |
248 | fOngoingStateInfo.add(TmfStateValue.nullValue()); |
249 | fStateValueTypes.add(Type.NULL); | |
a52fde77 | 250 | |
086cd39c | 251 | fOngoingStateStartTimes.add(fBackend.getStartTime()); |
09e6fd9b | 252 | } finally { |
086cd39c | 253 | fRWLock.writeLock().unlock(); |
09e6fd9b | 254 | } |
a52fde77 AM |
255 | } |
256 | ||
257 | /** | |
a6917276 | 258 | * Process a state change to be inserted in the history. |
97042f0a | 259 | * |
a52fde77 AM |
260 | * @param eventTime |
261 | * The timestamp associated with this state change | |
a6917276 AM |
262 | * @param value |
263 | * The new StateValue associated to this attribute | |
264 | * @param quark | |
265 | * The quark of the attribute that is being modified | |
a52fde77 | 266 | * @throws TimeRangeException |
a6917276 | 267 | * If 'eventTime' is invalid |
a52fde77 | 268 | * @throws AttributeNotFoundException |
a6917276 | 269 | * IF 'quark' does not represent an existing attribute |
97042f0a | 270 | * @throws StateValueTypeException |
a6917276 AM |
271 | * If the state value to be inserted is of a different type of |
272 | * what was inserted so far for this attribute. | |
a52fde77 | 273 | */ |
09e6fd9b AM |
274 | public void processStateChange(long eventTime, ITmfStateValue value, int quark) |
275 | throws TimeRangeException, AttributeNotFoundException, StateValueTypeException { | |
086cd39c | 276 | if (!this.fIsActive) { |
feea3b3c AM |
277 | return; |
278 | } | |
97042f0a | 279 | |
086cd39c | 280 | fRWLock.writeLock().lock(); |
09e6fd9b | 281 | try { |
086cd39c | 282 | Type expectedSvType = fStateValueTypes.get(quark); |
09e6fd9b | 283 | checkValidAttribute(quark); |
97042f0a | 284 | |
97042f0a | 285 | /* |
09e6fd9b AM |
286 | * Make sure the state value type we're inserting is the same as the |
287 | * one registered for this attribute. | |
7e0b2b56 | 288 | */ |
09e6fd9b AM |
289 | if (expectedSvType == Type.NULL) { |
290 | /* | |
291 | * The value hasn't been used yet, set it to the value we're | |
292 | * currently inserting (which might be null/-1 again). | |
293 | */ | |
086cd39c | 294 | fStateValueTypes.set(quark, value.getType()); |
09e6fd9b AM |
295 | } else if ((value.getType() != Type.NULL) && (value.getType() != expectedSvType)) { |
296 | /* | |
297 | * We authorize inserting null values in any type of attribute, | |
298 | * but for every other types, it needs to match our | |
299 | * expectations! | |
300 | */ | |
e13bd4cd | 301 | throw new StateValueTypeException(fBackend.getSSID() + " Quark:" + quark + ", Type:" + value.getType() + ", Expected:" + expectedSvType); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
09e6fd9b | 302 | } |
a52fde77 | 303 | |
086cd39c | 304 | if (fOngoingStateInfo.get(quark).equals(value)) { |
09e6fd9b AM |
305 | /* |
306 | * This is the case where the new value and the one already | |
307 | * present in the Builder are the same. We do not need to create | |
308 | * an interval, we'll just keep the current one going. | |
309 | */ | |
310 | return; | |
311 | } | |
a52fde77 | 312 | |
086cd39c | 313 | if (fOngoingStateStartTimes.get(quark) < eventTime) { |
09e6fd9b AM |
314 | /* |
315 | * These two conditions are necessary to create an interval and | |
316 | * update ongoingStateInfo. | |
317 | */ | |
086cd39c | 318 | fBackend.insertPastState(fOngoingStateStartTimes.get(quark), |
09e6fd9b AM |
319 | eventTime - 1, /* End Time */ |
320 | quark, /* attribute quark */ | |
086cd39c | 321 | fOngoingStateInfo.get(quark)); /* StateValue */ |
a52fde77 | 322 | |
086cd39c | 323 | fOngoingStateStartTimes.set(quark, eventTime); |
09e6fd9b | 324 | } |
086cd39c | 325 | fOngoingStateInfo.set(quark, value); |
09e6fd9b AM |
326 | |
327 | /* Update the Transient State's lastestTime, if needed */ | |
086cd39c MK |
328 | if (fLatestTime < eventTime) { |
329 | fLatestTime = eventTime; | |
09e6fd9b | 330 | } |
a52fde77 | 331 | |
09e6fd9b | 332 | } finally { |
086cd39c | 333 | fRWLock.writeLock().unlock(); |
a52fde77 | 334 | } |
a52fde77 AM |
335 | } |
336 | ||
337 | /** | |
338 | * Run a "get state at time" query on the Transient State only. | |
97042f0a | 339 | * |
a52fde77 AM |
340 | * @param stateInfo |
341 | * The stateInfo object in which we will put our relevant | |
342 | * information | |
343 | * @param t | |
344 | * The requested timestamp | |
345 | */ | |
a6917276 | 346 | public void doQuery(List<ITmfStateInterval> stateInfo, long t) { |
086cd39c | 347 | fRWLock.readLock().lock(); |
09e6fd9b | 348 | try { |
086cd39c | 349 | if (!this.fIsActive) { |
09e6fd9b AM |
350 | return; |
351 | } | |
086cd39c | 352 | if (stateInfo.size() > fOngoingStateInfo.size()) { |
7f0344d8 GB |
353 | throw new IllegalArgumentException(); |
354 | } | |
a52fde77 | 355 | |
7f0344d8 | 356 | for (int i = 0; i < stateInfo.size(); i++) { |
09e6fd9b | 357 | /* |
feea3b3c AM |
358 | * We build a dummy interval whose end time = |
359 | * "current transient state end time" to put in the answer to | |
360 | * the query. | |
09e6fd9b AM |
361 | */ |
362 | final ITmfStateInterval interval = getIntervalAt(t, i); | |
363 | if (interval != null) { | |
364 | stateInfo.set(i, interval); | |
365 | } | |
a52fde77 | 366 | } |
09e6fd9b | 367 | } finally { |
086cd39c | 368 | fRWLock.readLock().unlock(); |
a52fde77 AM |
369 | } |
370 | } | |
371 | ||
372 | /** | |
a6917276 AM |
373 | * Close off the Transient State, used for example when we are done reading |
374 | * a static trace file. All the information currently contained in it will | |
375 | * be converted to intervals and "flushed" to the state history. | |
376 | * | |
377 | * @param endTime | |
378 | * The timestamp to use as end time for the state history (since | |
379 | * it may be different than the timestamp of the last state | |
380 | * change) | |
a52fde77 | 381 | */ |
a6917276 | 382 | public void closeTransientState(long endTime) { |
086cd39c | 383 | if (!this.fIsActive) { |
feea3b3c AM |
384 | return; |
385 | } | |
386 | ||
086cd39c | 387 | fRWLock.writeLock().lock(); |
09e6fd9b | 388 | try { |
086cd39c MK |
389 | for (int i = 0; i < fOngoingStateInfo.size(); i++) { |
390 | if (fOngoingStateStartTimes.get(i) > endTime) { | |
09e6fd9b AM |
391 | /* |
392 | * Handle the cases where trace end > timestamp of last | |
393 | * state change. This can happen when inserting "future" | |
394 | * changes. | |
395 | */ | |
396 | continue; | |
397 | } | |
398 | try { | |
086cd39c | 399 | fBackend.insertPastState(fOngoingStateStartTimes.get(i), |
09e6fd9b AM |
400 | endTime, /* End Time */ |
401 | i, /* attribute quark */ | |
086cd39c | 402 | fOngoingStateInfo.get(i)); /* StateValue */ |
09e6fd9b AM |
403 | |
404 | } catch (TimeRangeException e) { | |
405 | /* | |
406 | * This shouldn't happen, since we control where the | |
407 | * interval's start time comes from | |
408 | */ | |
409 | throw new IllegalStateException(e); | |
410 | } | |
a52fde77 | 411 | } |
a52fde77 | 412 | |
086cd39c MK |
413 | fOngoingStateInfo.clear(); |
414 | fOngoingStateStartTimes.clear(); | |
415 | this.fIsActive = false; | |
a52fde77 | 416 | |
09e6fd9b | 417 | } finally { |
086cd39c | 418 | fRWLock.writeLock().unlock(); |
09e6fd9b | 419 | } |
a52fde77 AM |
420 | } |
421 | ||
422 | /** | |
423 | * Simply returns if this Transient State is currently being used or not | |
97042f0a | 424 | * |
a6917276 | 425 | * @return True if this transient state is active |
a52fde77 | 426 | */ |
a6917276 | 427 | public boolean isActive() { |
086cd39c | 428 | return this.fIsActive; |
a52fde77 AM |
429 | } |
430 | ||
a6917276 AM |
431 | /** |
432 | * Mark this transient state as inactive | |
433 | */ | |
434 | public void setInactive() { | |
086cd39c | 435 | fIsActive = false; |
a52fde77 AM |
436 | } |
437 | ||
438 | /** | |
a6917276 | 439 | * Debugging method that prints the contents of the transient state |
97042f0a | 440 | * |
a52fde77 | 441 | * @param writer |
a6917276 | 442 | * The writer to which the output should be written |
a52fde77 | 443 | */ |
a6917276 | 444 | public void debugPrint(PrintWriter writer) { |
a52fde77 AM |
445 | /* Only used for debugging, shouldn't be externalized */ |
446 | writer.println("------------------------------"); //$NON-NLS-1$ | |
447 | writer.println("Info stored in the Builder:"); //$NON-NLS-1$ | |
086cd39c | 448 | if (!this.fIsActive) { |
a52fde77 AM |
449 | writer.println("Builder is currently inactive"); //$NON-NLS-1$ |
450 | writer.println('\n'); | |
451 | return; | |
452 | } | |
453 | writer.println("\nAttribute\tStateValue\tValid since time"); //$NON-NLS-1$ | |
086cd39c | 454 | for (int i = 0; i < fOngoingStateInfo.size(); i++) { |
a52fde77 | 455 | writer.format("%d\t\t", i); //$NON-NLS-1$ |
086cd39c MK |
456 | writer.print(fOngoingStateInfo.get(i).toString() + "\t\t"); //$NON-NLS-1$ |
457 | writer.println(fOngoingStateStartTimes.get(i).toString()); | |
a52fde77 AM |
458 | } |
459 | writer.println('\n'); | |
460 | return; | |
461 | } | |
462 | ||
faa38350 | 463 | } |