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
.tracecompass
.tmf
.core
.trace
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
20 import java
.net
.URISyntaxException
;
21 import java
.util
.Collection
;
22 import java
.util
.Collections
;
23 import java
.util
.LinkedHashMap
;
24 import java
.util
.LinkedHashSet
;
25 import java
.util
.List
;
29 import org
.eclipse
.core
.resources
.IFile
;
30 import org
.eclipse
.core
.resources
.IFolder
;
31 import org
.eclipse
.core
.resources
.IProject
;
32 import org
.eclipse
.core
.resources
.IResource
;
33 import org
.eclipse
.core
.runtime
.CoreException
;
34 import org
.eclipse
.core
.runtime
.URIUtil
;
35 import org
.eclipse
.jdt
.annotation
.NonNull
;
36 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEventFilterAppliedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfRangeSynchSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTimeSynchSignal
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
53 import com
.google
.common
.collect
.ImmutableSet
;
56 * Central trace manager for TMF. It tracks the currently opened traces and
57 * experiment, as well as the currently-selected time or time range and the
58 * current window time range for each one of those. It also tracks filters
59 * applied for each trace.
61 * It's a singleton class, so only one instance should exist (available via
62 * {@link #getInstance()}).
64 * @author Alexandre Montplaisir
67 public final class TmfTraceManager
{
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
73 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
75 /** The currently-selected trace. Should always be part of the trace map */
76 private ITmfTrace fCurrentTrace
= null;
78 private static final String TEMP_DIR_NAME
= ".temp"; //$NON-NLS-1$
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 private TmfTraceManager() {
85 fTraces
= new LinkedHashMap
<>();
86 TmfSignalManager
.registerVIP(this);
89 /** Singleton instance */
90 private static TmfTraceManager tm
= null;
93 * Get an instance of the trace manager.
95 * @return The trace manager
97 public static synchronized TmfTraceManager
getInstance() {
99 tm
= new TmfTraceManager();
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * @return The begin timestamp of selection
112 public ITmfTimestamp
getSelectionBeginTime() {
113 return getCurrentTraceContext().getSelectionBegin();
117 * @return The end timestamp of selection
120 public ITmfTimestamp
getSelectionEndTime() {
121 return getCurrentTraceContext().getSelectionEnd();
125 * Return the current window time range.
127 * @return the current window time range
129 public synchronized TmfTimeRange
getCurrentRange() {
130 return getCurrentTraceContext().getWindowRange();
134 * Gets the filter applied to the current trace
136 * @return a filter, or <code>null</code>
139 public synchronized ITmfFilter
getCurrentFilter() {
140 return getCurrentTraceContext().getFilter();
144 * Get the currently selected trace (normally, the focused editor).
146 * @return The active trace
148 public synchronized ITmfTrace
getActiveTrace() {
149 return fCurrentTrace
;
153 * Get the trace set of the currently active trace.
155 * @return The active trace set
156 * @see #getTraceSet(ITmfTrace)
158 public synchronized @NonNull Collection
<ITmfTrace
> getActiveTraceSet() {
159 final ITmfTrace trace
= fCurrentTrace
;
160 return getTraceSet(trace
);
164 * Get the currently-opened traces, as an unmodifiable set.
166 * @return A set containing the opened traces
168 public synchronized Set
<ITmfTrace
> getOpenedTraces() {
169 return Collections
.unmodifiableSet(fTraces
.keySet());
173 * Get the editor file for an opened trace.
177 * @return the editor file or null if the trace is not opened
180 public synchronized IFile
getTraceEditorFile(ITmfTrace trace
) {
181 TmfTraceContext ctx
= fTraces
.get(trace
);
183 return ctx
.getEditorFile();
188 private TmfTraceContext
getCurrentTraceContext() {
189 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
190 if (curCtx
== null) {
191 /* There are no traces opened at the moment. */
192 return TmfTraceContext
.NULL_CONTEXT
;
197 // ------------------------------------------------------------------------
198 // Public utility methods
199 // ------------------------------------------------------------------------
202 * Get the trace set of a given trace. For a standard trace, this is simply
203 * an array with only that trace in it. For experiments, this is an array of
204 * all the traces contained in this experiment.
207 * The trace or experiment
208 * @return The corresponding trace set.
210 public static @NonNull Collection
<ITmfTrace
> getTraceSet(ITmfTrace trace
) {
212 return NonNullUtils
.checkNotNull(ImmutableSet
.<ITmfTrace
> of());
214 List
<ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
215 if (traces
.size() > 0) {
216 return NonNullUtils
.checkNotNull(ImmutableSet
.copyOf(traces
));
218 return NonNullUtils
.checkNotNull(ImmutableSet
.of(trace
));
222 * Get the trace set of a given trace or experiment, including the
223 * experiment. For a standard trace, this is simply a set containing only
224 * that trace. For experiments, it is the set of all the traces contained in
225 * this experiment, along with the experiment.
228 * The trace or experiment
229 * @return The corresponding trace set, including the experiment.
232 public static @NonNull Collection
<ITmfTrace
> getTraceSetWithExperiment(ITmfTrace trace
) {
234 return checkNotNull(ImmutableSet
.<ITmfTrace
> of());
236 if (trace
instanceof TmfExperiment
) {
237 TmfExperiment exp
= (TmfExperiment
) trace
;
238 List
<ITmfTrace
> traces
= exp
.getTraces();
239 Set
<ITmfTrace
> alltraces
= new LinkedHashSet
<>(traces
);
241 return NonNullUtils
.checkNotNull(ImmutableSet
.copyOf(alltraces
));
243 return checkNotNull(Collections
.singleton(trace
));
247 * Return the path (as a string) to the directory for supplementary files to
248 * use with a given trace. If no supplementary file directory has been
249 * configured, a temporary directory based on the trace's name will be
254 * @return The path to the supplementary file directory (trailing slash is
257 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
258 IResource resource
= trace
.getResource();
259 if (resource
== null) {
260 return getTemporaryDir(trace
);
263 String supplDir
= null;
265 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
266 } catch (CoreException e
) {
267 return getTemporaryDir(trace
);
269 return supplDir
+ File
.separator
;
273 * Refresh the supplementary files resources for a trace, so it can pick up
274 * new files that got created.
277 * The trace for which to refresh the supplementary files
280 public static void refreshSupplementaryFiles(ITmfTrace trace
) {
281 IResource resource
= trace
.getResource();
282 if (resource
!= null && resource
.exists()) {
283 String supplFolderPath
= getSupplementaryFileDir(trace
);
284 IProject project
= resource
.getProject();
285 /* Remove the project's path from the supplementary path dir */
286 if (!supplFolderPath
.startsWith(project
.getLocationURI().getPath())) {
287 Activator
.logWarning(String
.format("Supplementary files folder for trace %s is not within the project.", trace
.getName())); //$NON-NLS-1$
290 IFolder supplFolder
= project
.getFolder(supplFolderPath
.substring(project
.getLocationURI().getPath().length()));
291 if (supplFolder
.exists()) {
293 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
294 } catch (CoreException e
) {
295 Activator
.logError("Error refreshing resources", e
); //$NON-NLS-1$
301 // ------------------------------------------------------------------------
303 // ------------------------------------------------------------------------
306 * Signal handler for the traceOpened signal.
309 * The incoming signal
312 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
313 final ITmfTrace trace
= signal
.getTrace();
314 final IFile editorFile
= signal
.getEditorFile();
315 final ITmfTimestamp startTs
= trace
.getStartTime();
317 /* Calculate the initial time range */
318 final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
319 long offset
= trace
.getInitialRangeOffset().normalize(0, SCALE
).getValue();
320 long endTime
= startTs
.normalize(0, SCALE
).getValue() + offset
;
321 final TmfTimeRange startTr
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, SCALE
));
323 final TmfTraceContext startCtx
= new TmfTraceContext(startTs
, startTs
, startTr
, editorFile
);
325 fTraces
.put(trace
, startCtx
);
327 /* We also want to set the newly-opened trace as the active trace */
328 fCurrentTrace
= trace
;
332 * Handler for the TmfTraceSelectedSignal.
335 * The incoming signal
338 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
339 final ITmfTrace newTrace
= signal
.getTrace();
340 if (!fTraces
.containsKey(newTrace
)) {
341 throw new RuntimeException();
343 fCurrentTrace
= newTrace
;
347 * Signal handler for the filterApplied signal.
350 * The incoming signal
354 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal
) {
355 final ITmfTrace newTrace
= signal
.getTrace();
356 TmfTraceContext context
= fTraces
.get(newTrace
);
357 if (context
== null) {
358 throw new RuntimeException();
360 fTraces
.put(newTrace
, new TmfTraceContext(context
, signal
.getEventFilter()));
364 * Signal handler for the traceClosed signal.
367 * The incoming signal
370 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
371 fTraces
.remove(signal
.getTrace());
372 if (fTraces
.size() == 0) {
373 fCurrentTrace
= null;
375 * In other cases, we should receive a traceSelected signal that
376 * will indicate which trace is the new one.
382 * Signal handler for the TmfTimeSynchSignal signal.
384 * The current time of *all* traces whose range contains the requested new
385 * selection time range will be updated.
388 * The incoming signal
391 public synchronized void timeUpdated(final TmfTimeSynchSignal signal
) {
392 final ITmfTimestamp beginTs
= signal
.getBeginTime();
393 final ITmfTimestamp endTs
= signal
.getEndTime();
395 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
396 final ITmfTrace trace
= entry
.getKey();
397 if (beginTs
.intersects(getValidTimeRange(trace
)) || endTs
.intersects(getValidTimeRange(trace
))) {
398 TmfTraceContext prevCtx
= entry
.getValue();
399 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
, beginTs
, endTs
);
400 entry
.setValue(newCtx
);
406 * Signal handler for the TmfRangeSynchSignal signal.
408 * The current window time range of *all* valid traces will be updated to
409 * the new requested times.
412 * The incoming signal
415 public synchronized void timeRangeUpdated(final TmfRangeSynchSignal signal
) {
416 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
417 final ITmfTrace trace
= entry
.getKey();
418 final TmfTraceContext curCtx
= entry
.getValue();
420 final TmfTimeRange validTr
= getValidTimeRange(trace
);
422 /* Determine the new time range */
423 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
424 TmfTimeRange newTr
= (targetTr
== null ? curCtx
.getWindowRange() : targetTr
);
426 /* Update the values */
427 TmfTraceContext newCtx
= new TmfTraceContext(curCtx
, newTr
);
428 entry
.setValue(newCtx
);
432 // ------------------------------------------------------------------------
433 // Private utility methods
434 // ------------------------------------------------------------------------
437 * Return the valid time range of a trace (not the current window time
438 * range, but the range of all possible valid timestamps).
440 * For a real trace this is the whole range of the trace. For an experiment,
441 * it goes from the start time of the earliest trace to the end time of the
445 * The trace to check for
446 * @return The valid time span, or 'null' if the trace is not valid
448 private TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
449 if (!fTraces
.containsKey(trace
)) {
450 /* Trace is not part of the currently opened traces */
454 List
<ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
456 if (traces
.isEmpty()) {
457 /* "trace" is a single trace, return its time range directly */
458 return trace
.getTimeRange();
461 if (traces
.size() == 1) {
462 /* Trace is an experiment with only 1 trace */
463 return traces
.get(0).getTimeRange();
467 * Trace is an trace set with 2+ traces, so get the earliest start and
470 ITmfTimestamp start
= traces
.get(0).getStartTime();
471 ITmfTimestamp end
= traces
.get(0).getEndTime();
473 for (int i
= 1; i
< traces
.size(); i
++) {
474 ITmfTrace curTrace
= traces
.get(i
);
475 if (curTrace
.getStartTime().compareTo(start
) < 0) {
476 start
= curTrace
.getStartTime();
478 if (curTrace
.getEndTime().compareTo(end
) > 0) {
479 end
= curTrace
.getEndTime();
482 return new TmfTimeRange(start
, end
);
486 * Get the temporary directory path. If there is an instance of Eclipse
487 * running, the temporary directory will reside under the workspace.
489 * @return the temporary directory path suitable to be passed to the
490 * java.io.File constructor without a trailing separator
493 public static String
getTemporaryDirPath() {
494 // Get the workspace path from the properties
495 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
496 if (property
!= null) {
498 File dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
499 dir
= new File(dir
.getAbsolutePath() + File
.separator
+ TEMP_DIR_NAME
);
503 return dir
.getAbsolutePath();
504 } catch (URISyntaxException e
) {
505 Activator
.logError(e
.getLocalizedMessage(), e
);
508 return System
.getProperty("java.io.tmpdir"); //$NON-NLS-1$
512 * Get a temporary directory based on a trace's name. We will create the
513 * directory if it doesn't exist, so that it's ready to be used.
515 private static String
getTemporaryDir(ITmfTrace trace
) {
516 String pathName
= getTemporaryDirPath() +
520 File dir
= new File(pathName
);