1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 * Patrick Tasse - Support selection range
12 * Xavier Raynaud - Support filters tracking
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
18 import java
.net
.URISyntaxException
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collections
;
21 import java
.util
.LinkedHashMap
;
22 import java
.util
.LinkedHashSet
;
26 import org
.eclipse
.core
.resources
.IFile
;
27 import org
.eclipse
.core
.resources
.IFolder
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.URIUtil
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.ITmfFilter
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEventFilterAppliedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfRangeSynchSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTimeSynchSignal
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
46 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
49 * Central trace manager for TMF. It tracks the currently opened traces and
50 * experiment, as well as the currently-selected time or time range and the
51 * current window time range for each one of those. It also tracks filters
52 * applied for each trace.
54 * It's a singleton class, so only one instance should exist (available via
55 * {@link #getInstance()}).
57 * @author Alexandre Montplaisir
60 public final class TmfTraceManager
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
68 /** The currently-selected trace. Should always be part of the trace map */
69 private ITmfTrace fCurrentTrace
= null;
71 private static final String TEMP_DIR_NAME
= ".temp"; //$NON-NLS-1$
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
77 private TmfTraceManager() {
78 fTraces
= new LinkedHashMap
<>();
79 TmfSignalManager
.registerVIP(this);
82 /** Singleton instance */
83 private static TmfTraceManager tm
= null;
86 * Get an instance of the trace manager.
88 * @return The trace manager
90 public static synchronized TmfTraceManager
getInstance() {
92 tm
= new TmfTraceManager();
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 * @return The begin timestamp of selection
105 public ITmfTimestamp
getSelectionBeginTime() {
106 return getCurrentTraceContext().getSelectionBegin();
110 * @return The end timestamp of selection
113 public ITmfTimestamp
getSelectionEndTime() {
114 return getCurrentTraceContext().getSelectionEnd();
118 * Return the current window time range.
120 * @return the current window time range
122 public synchronized TmfTimeRange
getCurrentRange() {
123 return getCurrentTraceContext().getWindowRange();
127 * Gets the filter applied to the current trace
130 * a filter, or <code>null</code>
133 public synchronized ITmfFilter
getCurrentFilter() {
134 return getCurrentTraceContext().getFilter();
138 * Get the currently selected trace (normally, the focused editor).
140 * @return The active trace
142 public synchronized ITmfTrace
getActiveTrace() {
143 return fCurrentTrace
;
147 * Get the trace set of the currently active trace.
149 * @return The active trace set
150 * @see #getTraceSet(ITmfTrace)
152 public synchronized ITmfTrace
[] getActiveTraceSet() {
153 final ITmfTrace trace
= fCurrentTrace
;
154 return getTraceSet(trace
);
158 * Get the currently-opened traces, as an unmodifiable set.
160 * @return A set containing the opened traces
162 public synchronized Set
<ITmfTrace
> getOpenedTraces() {
163 return Collections
.unmodifiableSet(fTraces
.keySet());
167 * Get the editor file for an opened trace.
171 * @return the editor file or null if the trace is not opened
174 public synchronized IFile
getTraceEditorFile(ITmfTrace trace
) {
175 TmfTraceContext ctx
= fTraces
.get(trace
);
177 return ctx
.getEditorFile();
182 private TmfTraceContext
getCurrentTraceContext() {
183 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
184 if (curCtx
== null) {
185 /* There are no traces opened at the moment. */
186 return TmfTraceContext
.NULL_CONTEXT
;
191 // ------------------------------------------------------------------------
192 // Public utility methods
193 // ------------------------------------------------------------------------
196 * Get the trace set of a given trace. For a standard trace, this is simply
197 * an array with only that trace in it. For experiments, this is an array of
198 * all the traces contained in this experiment.
201 * The trace or experiment
202 * @return The corresponding trace set
204 public static ITmfTrace
[] getTraceSet(ITmfTrace trace
) {
208 if (trace
instanceof TmfExperiment
) {
209 TmfExperiment exp
= (TmfExperiment
) trace
;
210 return exp
.getTraces();
212 return new ITmfTrace
[] { trace
};
216 * Get the trace set of a given trace or experiment, including the
217 * experiment. For a standard trace, this is simply a set containing only
218 * that trace. For experiments, it is the set of all the traces contained in
219 * this experiment, along with the experiment.
222 * The trace or experiment
223 * @return The corresponding trace set, including the experiment
226 public static @NonNull Set
<ITmfTrace
> getTraceSetWithExperiment(ITmfTrace trace
) {
228 @SuppressWarnings("null")
229 @NonNull Set
<ITmfTrace
> emptySet
= Collections
.EMPTY_SET
;
232 if (trace
instanceof TmfExperiment
) {
233 TmfExperiment exp
= (TmfExperiment
) trace
;
234 ITmfTrace
[] traces
= exp
.getTraces();
235 Set
<ITmfTrace
> alltraces
= new LinkedHashSet
<>(Arrays
.asList(traces
));
239 @SuppressWarnings("null")
240 @NonNull Set
<ITmfTrace
> singleton
= Collections
.singleton(trace
);
245 * Return the path (as a string) to the directory for supplementary files to
246 * use with a given trace. If no supplementary file directory has been
247 * configured, a temporary directory based on the trace's name will be
252 * @return The path to the supplementary file directory (trailing slash is
255 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
256 IResource resource
= trace
.getResource();
257 if (resource
== null) {
258 return getTemporaryDir(trace
);
261 String supplDir
= null;
263 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
264 } catch (CoreException e
) {
265 return getTemporaryDir(trace
);
267 return supplDir
+ File
.separator
;
271 * Refresh the supplementary files resources for a trace, so it can pick up
272 * new files that got created.
275 * The trace for which to refresh the supplementary files
278 public static void refreshSupplementaryFiles(ITmfTrace trace
) {
279 IResource resource
= trace
.getResource();
280 if (resource
!= null && resource
.exists()) {
281 String supplFolderPath
= getSupplementaryFileDir(trace
);
282 IProject project
= resource
.getProject();
283 /* Remove the project's path from the supplementary path dir */
284 if (!supplFolderPath
.startsWith(project
.getLocationURI().getPath())) {
285 Activator
.logWarning(String
.format("Supplementary files folder for trace %s is not within the project.", trace
.getName())); //$NON-NLS-1$
288 IFolder supplFolder
= project
.getFolder(supplFolderPath
.substring(project
.getLocationURI().getPath().length()));
289 if (supplFolder
.exists()) {
291 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
292 } catch (CoreException e
) {
293 Activator
.logError("Error refreshing resources", e
); //$NON-NLS-1$
299 // ------------------------------------------------------------------------
301 // ------------------------------------------------------------------------
304 * Signal handler for the traceOpened signal.
307 * The incoming signal
310 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
311 final ITmfTrace trace
= signal
.getTrace();
312 final IFile editorFile
= signal
.getEditorFile();
313 final ITmfTimestamp startTs
= trace
.getStartTime();
315 /* Calculate the initial time range */
316 final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
317 long offset
= trace
.getInitialRangeOffset().normalize(0, SCALE
).getValue();
318 long endTime
= startTs
.normalize(0, SCALE
).getValue() + offset
;
319 final TmfTimeRange startTr
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, SCALE
));
321 final TmfTraceContext startCtx
= new TmfTraceContext(startTs
, startTs
, startTr
, editorFile
);
323 fTraces
.put(trace
, startCtx
);
325 /* We also want to set the newly-opened trace as the active trace */
326 fCurrentTrace
= trace
;
331 * Handler for the TmfTraceSelectedSignal.
334 * The incoming signal
337 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
338 final ITmfTrace newTrace
= signal
.getTrace();
339 if (!fTraces
.containsKey(newTrace
)) {
340 throw new RuntimeException();
342 fCurrentTrace
= newTrace
;
346 * Signal handler for the filterApplied signal.
349 * The incoming signal
353 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal
) {
354 final ITmfTrace newTrace
= signal
.getTrace();
355 TmfTraceContext context
= fTraces
.get(newTrace
);
356 if (context
== null) {
357 throw new RuntimeException();
359 fTraces
.put(newTrace
, new TmfTraceContext(context
, signal
.getEventFilter()));
363 * Signal handler for the traceClosed signal.
366 * The incoming signal
369 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
370 fTraces
.remove(signal
.getTrace());
371 if (fTraces
.size() == 0) {
372 fCurrentTrace
= null;
374 * In other cases, we should receive a traceSelected signal that
375 * will indicate which trace is the new one.
381 * Signal handler for the TmfTimeSynchSignal signal.
383 * The current time of *all* traces whose range contains the requested new
384 * selection time range will be updated.
387 * The incoming signal
390 public synchronized void timeUpdated(final TmfTimeSynchSignal signal
) {
391 final ITmfTimestamp beginTs
= signal
.getBeginTime();
392 final ITmfTimestamp endTs
= signal
.getEndTime();
394 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
395 final ITmfTrace trace
= entry
.getKey();
396 if (beginTs
.intersects(getValidTimeRange(trace
)) || endTs
.intersects(getValidTimeRange(trace
))) {
397 TmfTraceContext prevCtx
= entry
.getValue();
398 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
, beginTs
, endTs
);
399 entry
.setValue(newCtx
);
405 * Signal handler for the TmfRangeSynchSignal signal.
407 * The current window time range of *all* valid traces will be updated
408 * to the new requested times.
411 * The incoming signal
414 public synchronized void timeRangeUpdated(final TmfRangeSynchSignal signal
) {
415 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
416 final ITmfTrace trace
= entry
.getKey();
417 final TmfTraceContext curCtx
= entry
.getValue();
419 final TmfTimeRange validTr
= getValidTimeRange(trace
);
421 /* Determine the new time range */
422 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
423 TmfTimeRange newTr
= (targetTr
== null ? curCtx
.getWindowRange() : targetTr
);
425 /* Update the values */
426 TmfTraceContext newCtx
= new TmfTraceContext(curCtx
, newTr
);
427 entry
.setValue(newCtx
);
431 // ------------------------------------------------------------------------
432 // Private utility methods
433 // ------------------------------------------------------------------------
436 * Return the valid time range of a trace (not the current window time
437 * range, but the range of all possible valid timestamps).
439 * For a real trace this is the whole range of the trace. For an experiment,
440 * it goes from the start time of the earliest trace to the end time of the
444 * The trace to check for
445 * @return The valid time span, or 'null' if the trace is not valid
447 private TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
448 if (!fTraces
.containsKey(trace
)) {
449 /* Trace is not part of the currently opened traces */
452 if (!(trace
instanceof TmfExperiment
)) {
453 /* "trace" is a single trace, return its time range directly */
454 return trace
.getTimeRange();
456 final ITmfTrace
[] traces
= ((TmfExperiment
) trace
).getTraces();
457 if (traces
.length
== 0) {
458 /* We are being trolled */
461 if (traces
.length
== 1) {
462 /* Trace is an experiment with only 1 trace */
463 return traces
[0].getTimeRange();
466 * Trace is an experiment with 2+ traces, so get the earliest start and
469 ITmfTimestamp start
= traces
[0].getStartTime();
470 ITmfTimestamp end
= traces
[0].getEndTime();
471 for (int i
= 1; i
< traces
.length
; i
++) {
472 ITmfTrace curTrace
= traces
[i
];
473 if (curTrace
.getStartTime().compareTo(start
) < 0) {
474 start
= curTrace
.getStartTime();
476 if (curTrace
.getEndTime().compareTo(end
) > 0) {
477 end
= curTrace
.getEndTime();
480 return new TmfTimeRange(start
, end
);
484 * Get the temporary directory path. If there is an instance of Eclipse
485 * running, the temporary directory will reside under the workspace.
487 * @return the temporary directory path suitable to be passed to the
488 * java.io.File constructor without a trailing separator
491 public static String
getTemporaryDirPath() {
492 // Get the workspace path from the properties
493 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
494 if (property
!= null) {
496 File dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
497 dir
= new File(dir
.getAbsolutePath() + File
.separator
+ TEMP_DIR_NAME
);
501 return dir
.getAbsolutePath();
502 } catch (URISyntaxException e
) {
503 Activator
.logError(e
.getLocalizedMessage(), e
);
506 return System
.getProperty("java.io.tmpdir"); //$NON-NLS-1$
510 * Get a temporary directory based on a trace's name. We will create the
511 * directory if it doesn't exist, so that it's ready to be used.
513 private static String
getTemporaryDir(ITmfTrace trace
) {
514 String pathName
= getTemporaryDirPath() +
518 File dir
= new File(pathName
);