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
;
16 import java
.util
.LinkedHashMap
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
34 * Central trace manager for TMF. It tracks the currently opened traces and
35 * experiment, as well as the currently-selected timestamps and time ranges for
38 * It's a singleton class, so only one instance should exist (available via
39 * {@link #getInstance()}).
41 * @author Alexandre Montplaisir
44 public final class TmfTraceManager
{
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
50 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
52 /** The currently-selected trace. Should always be part of the trace map */
53 private ITmfTrace fCurrentTrace
= null;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 private TmfTraceManager() {
60 fTraces
= new LinkedHashMap
<ITmfTrace
, TmfTraceContext
>();
61 TmfSignalManager
.registerVIP(this);
64 /** Singleton instance */
65 private static TmfTraceManager tm
= null;
68 * Get an instance of the trace manager.
70 * @return The trace manager
72 public static synchronized TmfTraceManager
getInstance() {
74 tm
= new TmfTraceManager();
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Return the current selected time.
86 * @return the current time stamp
88 public synchronized ITmfTimestamp
getCurrentTime() {
89 return getCurrentTraceContext().getTimestamp();
93 * Return the current selected range.
95 * @return the current time range
97 public synchronized TmfTimeRange
getCurrentRange() {
98 return getCurrentTraceContext().getTimerange();
102 * Get the currently selected trace (normally, the focused editor).
104 * @return The active trace
106 public synchronized ITmfTrace
getActiveTrace() {
107 return fCurrentTrace
;
111 * Get the trace set of the currently active trace.
113 * @return The active trace set
114 * @see #getTraceSet(ITmfTrace)
116 public synchronized ITmfTrace
[] getActiveTraceSet() {
117 final ITmfTrace trace
= fCurrentTrace
;
118 return getTraceSet(trace
);
121 private TmfTraceContext
getCurrentTraceContext() {
122 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
123 if (curCtx
== null) {
124 /* There are no traces opened at the moment. */
125 return TmfTraceContext
.NULL_CONTEXT
;
130 // ------------------------------------------------------------------------
131 // Public utility methods
132 // ------------------------------------------------------------------------
135 * Get the trace set of a given trace. For a standard trace, this is simply
136 * an array with only that trace in it. For experiments, this is an array of
137 * all the traces contained in this experiment.
140 * The trace or experiment
141 * @return The corresponding trace set
143 public static ITmfTrace
[] getTraceSet(ITmfTrace trace
) {
147 if (trace
instanceof TmfExperiment
) {
148 TmfExperiment exp
= (TmfExperiment
) trace
;
149 return exp
.getTraces();
151 return new ITmfTrace
[] { trace
};
155 * Return the path (as a string) to the directory for supplementary files to
156 * use with a given trace. If no supplementary file directory has been
157 * configured, a temporary directory based on the trace's name will be
162 * @return The path to the supplementary file directory (trailing slash is
165 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
166 IResource resource
= trace
.getResource();
167 if (resource
== null) {
168 return getTemporaryDir(trace
);
171 String supplDir
= null;
173 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
174 } catch (CoreException e
) {
175 return getTemporaryDir(trace
);
177 return supplDir
+ File
.separator
;
180 // ------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
185 * Signal handler for the traceOpened signal.
188 * The incoming signal
191 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
192 final ITmfTrace trace
= signal
.getTrace();
193 final ITmfTimestamp startTs
= trace
.getStartTime();
195 /* Calculate the initial time range */
196 final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
197 long offset
= trace
.getInitialRangeOffset().normalize(0, SCALE
).getValue();
198 long endTime
= startTs
.normalize(0, SCALE
).getValue() + offset
;
199 final TmfTimeRange startTr
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, SCALE
));
201 final TmfTraceContext startCtx
= new TmfTraceContext(startTs
, startTr
);
203 fTraces
.put(trace
, startCtx
);
205 /* We also want to set the newly-opened trace as the active trace */
206 fCurrentTrace
= trace
;
211 * Handler for the TmfTraceSelectedSignal.
214 * The incoming signal
217 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
218 final ITmfTrace newTrace
= signal
.getTrace();
219 if (!fTraces
.containsKey(newTrace
)) {
220 throw new RuntimeException();
222 fCurrentTrace
= newTrace
;
226 * Signal handler for the traceClosed signal.
229 * The incoming signal
232 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
233 fTraces
.remove(signal
.getTrace());
234 if (fTraces
.size() == 0) {
235 fCurrentTrace
= null;
237 * In other cases, we should receive a traceSelected signal that
238 * will indicate which trace is the new one.
244 * Signal handler for the TmfTimeSynchSignal signal.
246 * The current time of *all* traces whose range contains the requested new
247 * time will be updated.
250 * The incoming signal
253 public synchronized void timeUpdated(final TmfTimeSynchSignal signal
) {
254 final ITmfTimestamp ts
= signal
.getCurrentTime();
256 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
257 final ITmfTrace trace
= entry
.getKey();
258 if (ts
.intersects(getValidTimeRange(trace
))) {
259 TmfTraceContext prevCtx
= entry
.getValue();
260 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
, ts
);
261 entry
.setValue(newCtx
);
267 * Signal handler for the TmfRangeSynchSignal signal.
269 * The current timestamp and timerange of *all* valid traces will be updated
270 * to the new requested times.
273 * The incoming signal
276 public synchronized void timeRangeUpdated(final TmfRangeSynchSignal signal
) {
277 final ITmfTimestamp signalTs
= signal
.getCurrentTime();
279 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
280 final ITmfTrace trace
= entry
.getKey();
281 final TmfTraceContext curCtx
= entry
.getValue();
283 final TmfTimeRange validTr
= getValidTimeRange(trace
);
285 /* Determine the new time stamp */
287 if (signalTs
!= null && signalTs
.intersects(validTr
)) {
290 newTs
= curCtx
.getTimestamp();
293 /* Determine the new time range */
294 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
295 TmfTimeRange newTr
= (targetTr
== null ? curCtx
.getTimerange() : targetTr
);
297 /* Update the values */
298 TmfTraceContext newCtx
= new TmfTraceContext(newTs
, newTr
);
299 entry
.setValue(newCtx
);
303 // ------------------------------------------------------------------------
304 // Private utility methods
305 // ------------------------------------------------------------------------
308 * Return the valid time range of a trace (not the "current time range", but
309 * the range of all possible valid timestamps).
311 * For a real trace this is the whole range of the trace. For an experiment,
312 * it goes from the start time of the earliest trace to the end time of the
316 * The trace to check for
317 * @return The valid time span, or 'null' if the trace is not valid
319 private TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
320 if (!fTraces
.containsKey(trace
)) {
321 /* Trace is not part of the currently opened traces */
324 if (!(trace
instanceof TmfExperiment
)) {
325 /* "trace" is a single trace, return its time range directly */
326 return trace
.getTimeRange();
328 final ITmfTrace
[] traces
= ((TmfExperiment
) trace
).getTraces();
329 if (traces
.length
== 0) {
330 /* We are being trolled */
333 if (traces
.length
== 1) {
334 /* Trace is an experiment with only 1 trace */
335 return traces
[0].getTimeRange();
338 * Trace is an experiment with 2+ traces, so get the earliest start and
341 ITmfTimestamp start
= traces
[0].getStartTime();
342 ITmfTimestamp end
= traces
[0].getEndTime();
343 for (int i
= 1; i
< traces
.length
; i
++) {
344 ITmfTrace curTrace
= traces
[i
];
345 if (curTrace
.getStartTime().compareTo(start
) < 0) {
346 start
= curTrace
.getStartTime();
348 if (curTrace
.getEndTime().compareTo(end
) > 0) {
349 end
= curTrace
.getEndTime();
352 return new TmfTimeRange(start
, end
);
356 * Get a temporary directory based on a trace's name
358 private static String
getTemporaryDir(ITmfTrace trace
) {
359 return System
.getProperty("java.io.tmpdir") + //$NON-NLS-1$