1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
3 * All rights reserved. This program and the accompanying materials are
4 * made available under the terms of the Eclipse Public License v1.0 which
5 * accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Alexandre Montplaisir - Initial API and implementation
10 * Patrick Tasse - Add message to exceptions
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.core
.statesystem
.backends
.partial
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNullContents
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.PrintWriter
;
21 import java
.util
.List
;
23 import java
.util
.TreeMap
;
24 import java
.util
.concurrent
.CountDownLatch
;
25 import java
.util
.stream
.Collectors
;
27 import org
.eclipse
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.jdt
.annotation
.Nullable
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.backend
.IStateHistoryBackend
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.TmfStateInterval
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.AbstractTmfStateProvider
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
46 * Partial state history back-end.
48 * This is a shim inserted between the real state system and a "real" history
49 * back-end. It will keep checkpoints, every n trace events (where n is called
50 * the granularity) and will only forward to the real state history the state
51 * intervals that crosses at least one checkpoint. Every other interval will
54 * This would mean that it can only answer queries exactly at the checkpoints.
55 * For any other timestamps (ie, most of the time), it will load the closest
56 * earlier checkpoint, and will re-feed the state-change-input with events from
57 * the trace, to restore the real state at the time that was requested.
59 * @author Alexandre Montplaisir
61 public class PartialHistoryBackend
implements IStateHistoryBackend
{
63 private final @NonNull String fSSID
;
66 * A partial history needs the state input plugin to re-generate state
67 * between checkpoints.
69 private final @NonNull ITmfStateProvider fPartialInput
;
72 * Fake state system that is used for partially rebuilding the states (when
73 * going from a checkpoint to a target query timestamp).
75 private final @NonNull PartialStateSystem fPartialSS
;
77 /** Reference to the "real" state history that is used for storage */
78 private final @NonNull IStateHistoryBackend fInnerHistory
;
80 /** Checkpoints map, <Timestamp, Rank in the trace> */
81 private final @NonNull TreeMap
<Long
, Long
> fCheckpoints
= new TreeMap
<>();
83 /** Latch tracking if the initial checkpoint registration is done */
84 private final @NonNull CountDownLatch fCheckpointsReady
= new CountDownLatch(1);
86 private final long fGranularity
;
88 private long fLatestTime
;
94 * The state system's ID
96 * The state change input object that was used to build the
97 * upstream state system. This partial history will make its own
98 * copy (since they have different targets).
100 * The partial history's inner state system. It should already be
101 * assigned to partialInput.
103 * The real state history back-end to use. It's supposed to be
104 * modular, so it should be able to be of any type.
106 * Configuration parameter indicating how many trace events there
107 * should be between each checkpoint
109 public PartialHistoryBackend(@NonNull String ssid
,
110 ITmfStateProvider partialInput
,
111 PartialStateSystem pss
,
112 IStateHistoryBackend realBackend
,
114 if (granularity
<= 0 || partialInput
== null || pss
== null ||
115 partialInput
.getAssignedStateSystem() != pss
) {
116 throw new IllegalArgumentException();
119 final long startTime
= realBackend
.getStartTime();
122 fPartialInput
= partialInput
;
125 fInnerHistory
= realBackend
;
126 fGranularity
= granularity
;
128 fLatestTime
= startTime
;
130 registerCheckpoints();
133 private void registerCheckpoints() {
134 ITmfEventRequest request
= new CheckpointsRequest(fPartialInput
, fCheckpoints
);
135 fPartialInput
.getTrace().sendRequest(request
);
136 /* The request will countDown the checkpoints latch once it's finished */
140 public String
getSSID() {
145 public long getStartTime() {
146 return fInnerHistory
.getStartTime();
150 public long getEndTime() {
155 public void insertPastState(long stateStartTime
, long stateEndTime
,
156 int quark
, ITmfStateValue value
) throws TimeRangeException
{
157 waitForCheckpoints();
159 /* Update the latest time */
160 if (stateEndTime
> fLatestTime
) {
161 fLatestTime
= stateEndTime
;
165 * Check if the interval intersects the previous checkpoint. If so,
166 * insert it in the real history back-end.
168 * FIXME since intervals are inserted in order of rank, we could avoid
169 * doing a map lookup every time here (just compare with the known
172 if (stateStartTime
<= fCheckpoints
.floorKey(stateEndTime
)) {
173 fInnerHistory
.insertPastState(stateStartTime
, stateEndTime
, quark
, value
);
178 public void finishedBuilding(long endTime
) throws TimeRangeException
{
179 fInnerHistory
.finishedBuilding(endTime
);
183 public FileInputStream
supplyAttributeTreeReader() {
184 return fInnerHistory
.supplyAttributeTreeReader();
188 public File
supplyAttributeTreeWriterFile() {
189 return fInnerHistory
.supplyAttributeTreeWriterFile();
193 public long supplyAttributeTreeWriterFilePosition() {
194 return fInnerHistory
.supplyAttributeTreeWriterFilePosition();
198 public void removeFiles() {
199 fInnerHistory
.removeFiles();
203 public void dispose() {
204 fPartialInput
.dispose();
205 fPartialSS
.dispose();
206 fInnerHistory
.dispose();
210 public void doQuery(List
<@Nullable ITmfStateInterval
> currentStateInfo
, long t
)
211 throws TimeRangeException
, StateSystemDisposedException
{
212 /* Wait for required steps to be done */
213 waitForCheckpoints();
214 fPartialSS
.getUpstreamSS().waitUntilBuilt();
216 if (!checkValidTime(t
)) {
217 throw new TimeRangeException(fSSID
+ " Time:" + t
+ ", Start:" + getStartTime() + ", End:" + getEndTime()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
220 /* Reload the previous checkpoint */
221 long checkpointTime
= fCheckpoints
.floorKey(t
);
222 fInnerHistory
.doQuery(currentStateInfo
, checkpointTime
);
225 * Set the initial contents of the partial state system (which is the
226 * contents of the query at the checkpoint).
228 List
<@NonNull ITmfStateInterval
> filledStateInfo
=
229 checkNotNullContents(currentStateInfo
.stream()).collect(Collectors
.toList());
231 fPartialSS
.takeQueryLock();
232 fPartialSS
.replaceOngoingState(filledStateInfo
);
234 /* Send an event request to update the state system to the target time. */
235 TmfTimeRange range
= new TmfTimeRange(
237 * The state at the checkpoint already includes any state change
238 * caused by the event(s) happening exactly at 'checkpointTime',
239 * if any. We must not include those events in the query.
241 TmfTimestamp
.fromNanos(checkpointTime
+ 1),
242 TmfTimestamp
.fromNanos(t
));
243 ITmfEventRequest request
= new PartialStateSystemRequest(fPartialInput
, range
);
244 fPartialInput
.getTrace().sendRequest(request
);
247 request
.waitForCompletion();
248 } catch (InterruptedException e
) {
253 * Now the partial state system should have the ongoing time we are
254 * looking for. However, the method expects a List of *state intervals*,
255 * not state values, so we'll create intervals with a dummy end time.
257 for (int i
= 0; i
< currentStateInfo
.size(); i
++) {
259 start
= ((ITmfStateSystem
) fPartialSS
).getOngoingStartTime(i
);
260 ITmfStateValue val
= ((ITmfStateSystem
) fPartialSS
).queryOngoingState(i
);
262 ITmfStateInterval interval
= new TmfStateInterval(start
, t
, i
, checkNotNull(val
));
263 currentStateInfo
.set(i
, interval
);
266 fPartialSS
.releaseQueryLock();
270 * Single queries are not supported in partial histories. To get the same
271 * result you can do a full query, then call fullState.get(attribute).
274 public ITmfStateInterval
doSingularQuery(long t
, int attributeQuark
) {
275 throw new UnsupportedOperationException();
278 private boolean checkValidTime(long t
) {
279 return (t
>= getStartTime() && t
<= getEndTime());
283 public void debugPrint(PrintWriter writer
) {
284 // TODO Auto-generated method stub
287 private void waitForCheckpoints() {
289 fCheckpointsReady
.await();
290 } catch (InterruptedException e
) {
295 // ------------------------------------------------------------------------
296 // Event requests types
297 // ------------------------------------------------------------------------
299 private class CheckpointsRequest
extends TmfEventRequest
{
300 private final ITmfTrace trace
;
301 private final Map
<Long
, Long
> checkpts
;
302 private long eventCount
;
303 private long lastCheckpointAt
;
305 public CheckpointsRequest(ITmfStateProvider input
, Map
<Long
, Long
> checkpoints
) {
306 super(ITmfEvent
.class,
307 TmfTimeRange
.ETERNITY
,
309 ITmfEventRequest
.ALL_DATA
,
310 ITmfEventRequest
.ExecutionType
.FOREGROUND
);
312 this.trace
= input
.getTrace();
313 this.checkpts
= checkpoints
;
315 lastCheckpointAt
= 0;
317 /* Insert a checkpoint at the start of the trace */
318 checkpoints
.put(input
.getStartTime(), 0L);
322 public void handleData(final ITmfEvent event
) {
323 super.handleData(event
);
324 if (event
.getTrace() == trace
) {
327 /* Check if we need to register a new checkpoint */
328 if (eventCount
>= lastCheckpointAt
+ fGranularity
) {
329 checkpts
.put(event
.getTimestamp().getValue(), eventCount
);
330 lastCheckpointAt
= eventCount
;
336 public void handleCompleted() {
337 super.handleCompleted();
338 fCheckpointsReady
.countDown();
342 private class PartialStateSystemRequest
extends TmfEventRequest
{
343 private final ITmfStateProvider sci
;
344 private final ITmfTrace trace
;
346 PartialStateSystemRequest(ITmfStateProvider sci
, TmfTimeRange range
) {
347 super(ITmfEvent
.class,
350 ITmfEventRequest
.ALL_DATA
,
351 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
353 this.trace
= sci
.getTrace();
357 public void handleData(final ITmfEvent event
) {
358 super.handleData(event
);
359 if (event
.getTrace() == trace
) {
360 sci
.processEvent(event
);
365 public void handleCompleted() {
367 * If we're using a threaded state provider, we need to make sure
368 * all events have been handled by the state system before doing
371 if (fPartialInput
instanceof AbstractTmfStateProvider
) {
372 ((AbstractTmfStateProvider
) fPartialInput
).waitForEmptyQueue();
374 super.handleCompleted();