1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
15 import java
.util
.LinkedHashMap
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
30 * Central trace manager for TMF. It tracks the currently opened traces and
31 * experiment, as well as the currently-selected timestamps and time ranges for
34 * It's a singleton class, so only one instance should exist (available via
35 * {@link #getInstance()}).
37 * @author Alexandre Montplaisir
40 public final class TmfTraceManager
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
46 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
48 /** The currently-selected trace. Should always be part of the trace map */
49 private ITmfTrace fCurrentTrace
= null;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private TmfTraceManager() {
56 fTraces
= new LinkedHashMap
<ITmfTrace
, TmfTraceContext
>();
57 TmfSignalManager
.registerVIP(this);
60 /** Singleton instance */
61 private static TmfTraceManager tm
= null;
64 * Get an instance of the trace manager.
66 * @return The trace manager
68 public static synchronized TmfTraceManager
getInstance() {
70 tm
= new TmfTraceManager();
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * Return the current selected time.
82 * @return the current time stamp
84 public synchronized ITmfTimestamp
getCurrentTime() {
85 return getCurrentTraceContext().getTimestamp();
89 * Return the current selected range.
91 * @return the current time range
93 public synchronized TmfTimeRange
getCurrentRange() {
94 return getCurrentTraceContext().getTimerange();
98 * Get the currently selected trace (normally, the focused editor).
100 * @return The active trace
102 public synchronized ITmfTrace
getActiveTrace() {
103 return fCurrentTrace
;
107 * Get the currently active trace set. For a 'normal' trace, this is simply
108 * an array with only that trace in it. For trace experiments, this will be
109 * an array containing the 'real' child traces in the experiment.
111 * @return The active trace set
113 public synchronized ITmfTrace
[] getActiveTraceSet() {
114 final ITmfTrace trace
= fCurrentTrace
;
115 if (trace
instanceof TmfExperiment
) {
116 final TmfExperiment exp
= (TmfExperiment
) trace
;
117 return exp
.getTraces();
119 return new ITmfTrace
[] { trace
};
122 private TmfTraceContext
getCurrentTraceContext() {
123 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
124 if (curCtx
== null) {
125 /* There are no traces opened at the moment. */
126 return TmfTraceContext
.NULL_CONTEXT
;
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 * Signal handler for the traceOpened signal.
139 * The incoming signal
142 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
143 final ITmfTrace trace
= signal
.getTrace();
144 final ITmfTimestamp startTs
= trace
.getStartTime();
146 /* Calculate the initial time range */
147 final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
148 long offset
= trace
.getInitialRangeOffset().normalize(0, SCALE
).getValue();
149 long endTime
= startTs
.normalize(0, SCALE
).getValue() + offset
;
150 final TmfTimeRange startTr
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, SCALE
));
152 final TmfTraceContext startCtx
= new TmfTraceContext(startTs
, startTr
);
154 fTraces
.put(trace
, startCtx
);
156 /* We also want to set the newly-opened trace as the active trace */
157 fCurrentTrace
= trace
;
162 * Handler for the TmfTraceSelectedSignal.
165 * The incoming signal
168 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
169 final ITmfTrace newTrace
= signal
.getTrace();
170 if (!fTraces
.containsKey(newTrace
)) {
171 throw new RuntimeException();
173 fCurrentTrace
= newTrace
;
177 * Signal handler for the traceClosed signal.
180 * The incoming signal
183 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
184 fTraces
.remove(signal
.getTrace());
185 if (fTraces
.size() == 0) {
186 fCurrentTrace
= null;
188 * In other cases, we should receive a traceSelected signal that
189 * will indicate which trace is the new one.
195 * Signal handler for the TmfTimeSynchSignal signal.
197 * The current time of *all* traces whose range contains the requested new
198 * time will be updated.
201 * The incoming signal
204 public synchronized void timeUpdated(final TmfTimeSynchSignal signal
) {
205 final ITmfTimestamp ts
= signal
.getCurrentTime();
207 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
208 final ITmfTrace trace
= entry
.getKey();
209 if (ts
.intersects(getValidTimeRange(trace
))) {
210 TmfTraceContext prevCtx
= entry
.getValue();
211 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
, ts
);
212 entry
.setValue(newCtx
);
218 * Signal handler for the TmfRangeSynchSignal signal.
220 * The current timestamp and timerange of *all* valid traces will be updated
221 * to the new requested times.
224 * The incoming signal
227 public synchronized void timeRangeUpdated(final TmfRangeSynchSignal signal
) {
228 final ITmfTimestamp signalTs
= signal
.getCurrentTime();
230 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
231 final ITmfTrace trace
= entry
.getKey();
232 final TmfTraceContext curCtx
= entry
.getValue();
234 final TmfTimeRange validTr
= getValidTimeRange(trace
);
236 /* Determine the new time stamp */
238 if (signalTs
!= null && signalTs
.intersects(validTr
)) {
241 newTs
= curCtx
.getTimestamp();
244 /* Determine the new time range */
245 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
246 TmfTimeRange newTr
= (targetTr
== null ? curCtx
.getTimerange() : targetTr
);
248 /* Update the values */
249 TmfTraceContext newCtx
= new TmfTraceContext(newTs
, newTr
);
250 entry
.setValue(newCtx
);
254 // ------------------------------------------------------------------------
256 // ------------------------------------------------------------------------
259 * Return the valid time range of a trace (not the "current time range", but
260 * the range of all possible valid timestamps).
262 * For a real trace this is the whole range of the trace. For an experiment,
263 * it goes from the start time of the earliest trace to the end time of the
267 * The trace to check for
268 * @return The valid time span, or 'null' if the trace is not valid
270 private TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
271 if (!fTraces
.containsKey(trace
)) {
272 /* Trace is not part of the currently opened traces */
275 if (!(trace
instanceof TmfExperiment
)) {
276 /* "trace" is a single trace, return its time range directly */
277 return trace
.getTimeRange();
279 final ITmfTrace
[] traces
= ((TmfExperiment
) trace
).getTraces();
280 if (traces
.length
== 0) {
281 /* We are being trolled */
284 if (traces
.length
== 1) {
285 /* Trace is an experiment with only 1 trace */
286 return traces
[0].getTimeRange();
289 * Trace is an experiment with 2+ traces, so get the earliest start and
292 ITmfTimestamp start
= traces
[0].getStartTime();
293 ITmfTimestamp end
= traces
[0].getEndTime();
294 for (int i
= 1; i
< traces
.length
; i
++) {
295 ITmfTrace curTrace
= traces
[i
];
296 if (curTrace
.getStartTime().compareTo(start
) < 0) {
297 start
= curTrace
.getStartTime();
299 if (curTrace
.getEndTime().compareTo(end
) > 0) {
300 end
= curTrace
.getEndTime();
303 return new TmfTimeRange(start
, end
);