1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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
.jdt
.annotation
.Nullable
;
37 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEventFilterAppliedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
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
.TmfSelectionRangeUpdatedSignal
;
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
66 public final class TmfTraceManager
{
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
74 /** The currently-selected trace. Should always be part of the trace map */
75 private ITmfTrace fCurrentTrace
= null;
77 private static final String TEMP_DIR_NAME
= ".temp"; //$NON-NLS-1$
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
83 private TmfTraceManager() {
84 fTraces
= new LinkedHashMap
<>();
85 TmfSignalManager
.registerVIP(this);
88 /** Singleton instance */
89 private static TmfTraceManager tm
= null;
92 * Get an instance of the trace manager.
94 * @return The trace manager
96 public static synchronized TmfTraceManager
getInstance() {
98 tm
= new TmfTraceManager();
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * Get the currently selected trace (normally, the focused editor).
110 * @return The active trace
112 public synchronized ITmfTrace
getActiveTrace() {
113 return fCurrentTrace
;
117 * Get the trace set of the currently active trace.
119 * @return The active trace set
120 * @see #getTraceSet(ITmfTrace)
122 public synchronized @NonNull Collection
<ITmfTrace
> getActiveTraceSet() {
123 final ITmfTrace trace
= fCurrentTrace
;
124 return getTraceSet(trace
);
128 * Get the currently-opened traces, as an unmodifiable set.
130 * @return A set containing the opened traces
132 public synchronized Set
<ITmfTrace
> getOpenedTraces() {
133 return Collections
.unmodifiableSet(fTraces
.keySet());
137 * Get the editor file for an opened trace.
141 * @return the editor file or null if the trace is not opened
143 public synchronized IFile
getTraceEditorFile(ITmfTrace trace
) {
144 TmfTraceContext ctx
= fTraces
.get(trace
);
146 return ctx
.getEditorFile();
152 * Get the {@link TmfTraceContext} of the current active trace. This can be
153 * used to retrieve the current active/selected time ranges and such.
155 * @return The trace's context.
158 public synchronized TmfTraceContext
getCurrentTraceContext() {
159 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
160 if (curCtx
== null) {
161 /* There are no traces opened at the moment. */
162 return TmfTraceContext
.NULL_CONTEXT
;
167 // ------------------------------------------------------------------------
168 // Public utility methods
169 // ------------------------------------------------------------------------
172 * Get the trace set of a given trace. For a standard trace, this is simply
173 * an array with only that trace in it. For experiments, this is an array of
174 * all the traces contained in this experiment.
177 * The trace or experiment
178 * @return The corresponding trace set.
180 public static @NonNull Collection
<@NonNull ITmfTrace
> getTraceSet(ITmfTrace trace
) {
182 return NonNullUtils
.checkNotNull(ImmutableSet
.of());
184 List
<@NonNull ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
185 if (traces
.size() > 0) {
186 return NonNullUtils
.checkNotNull(ImmutableSet
.copyOf(traces
));
188 return NonNullUtils
.checkNotNull(ImmutableSet
.of(trace
));
192 * Get the trace set of a given trace or experiment, including the
193 * experiment. For a standard trace, this is simply a set containing only
194 * that trace. For experiments, it is the set of all the traces contained in
195 * this experiment, along with the experiment.
198 * The trace or experiment
199 * @return The corresponding trace set, including the experiment.
201 public static @NonNull Collection
<ITmfTrace
> getTraceSetWithExperiment(ITmfTrace trace
) {
203 return checkNotNull(ImmutableSet
.<ITmfTrace
> of());
205 if (trace
instanceof TmfExperiment
) {
206 TmfExperiment exp
= (TmfExperiment
) trace
;
207 List
<ITmfTrace
> traces
= exp
.getTraces();
208 Set
<ITmfTrace
> alltraces
= new LinkedHashSet
<>(traces
);
210 return NonNullUtils
.checkNotNull(ImmutableSet
.copyOf(alltraces
));
212 return checkNotNull(Collections
.singleton(trace
));
216 * Return the path (as a string) to the directory for supplementary files to
217 * use with a given trace. If no supplementary file directory has been
218 * configured, a temporary directory based on the trace's name will be
223 * @return The path to the supplementary file directory (trailing slash is
226 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
227 IResource resource
= trace
.getResource();
228 if (resource
== null) {
229 return getTemporaryDir(trace
);
232 String supplDir
= null;
234 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
235 } catch (CoreException e
) {
236 return getTemporaryDir(trace
);
238 return supplDir
+ File
.separator
;
242 * Refresh the supplementary files resources for a trace, so it can pick up
243 * new files that got created.
246 * The trace for which to refresh the supplementary files
248 public static void refreshSupplementaryFiles(ITmfTrace trace
) {
249 IResource resource
= trace
.getResource();
250 if (resource
!= null && resource
.exists()) {
251 String supplFolderPath
= getSupplementaryFileDir(trace
);
252 IProject project
= resource
.getProject();
253 /* Remove the project's path from the supplementary path dir */
254 if (!supplFolderPath
.startsWith(project
.getLocation().toOSString())) {
255 Activator
.logWarning(String
.format("Supplementary files folder for trace %s is not within the project.", trace
.getName())); //$NON-NLS-1$
258 IFolder supplFolder
= project
.getFolder(supplFolderPath
.substring(project
.getLocationURI().getPath().length()));
259 if (supplFolder
.exists()) {
261 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
262 } catch (CoreException e
) {
263 Activator
.logError("Error refreshing resources", e
); //$NON-NLS-1$
269 // ------------------------------------------------------------------------
271 // ------------------------------------------------------------------------
274 * Signal handler for the traceOpened signal.
277 * The incoming signal
280 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
281 final ITmfTrace trace
= signal
.getTrace();
282 final IFile editorFile
= signal
.getEditorFile();
283 final ITmfTimestamp startTs
= trace
.getStartTime();
285 /* Calculate the initial time range */
286 final int SCALE
= ITmfTimestamp
.NANOSECOND_SCALE
;
287 long offset
= trace
.getInitialRangeOffset().normalize(0, SCALE
).getValue();
288 long endTime
= startTs
.normalize(0, SCALE
).getValue() + offset
;
289 final TmfTimeRange selectionRange
= new TmfTimeRange(startTs
, startTs
);
290 final TmfTimeRange windowRange
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, SCALE
));
292 final TmfTraceContext startCtx
= new TmfTraceContext(selectionRange
, windowRange
, editorFile
, null);
294 fTraces
.put(trace
, startCtx
);
296 /* We also want to set the newly-opened trace as the active trace */
297 fCurrentTrace
= trace
;
301 * Handler for the TmfTraceSelectedSignal.
304 * The incoming signal
307 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
308 final ITmfTrace newTrace
= signal
.getTrace();
309 if (!fTraces
.containsKey(newTrace
)) {
310 throw new RuntimeException();
312 fCurrentTrace
= newTrace
;
316 * Signal handler for the filterApplied signal.
319 * The incoming signal
322 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal
) {
323 final ITmfTrace newTrace
= signal
.getTrace();
324 TmfTraceContext context
= fTraces
.get(newTrace
);
325 if (context
== null) {
326 throw new RuntimeException();
328 fTraces
.put(newTrace
, new TmfTraceContext(context
.getSelectionRange(),
329 context
.getWindowRange(),
330 context
.getEditorFile(),
331 signal
.getEventFilter()));
335 * Signal handler for the traceClosed signal.
338 * The incoming signal
341 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
342 fTraces
.remove(signal
.getTrace());
343 if (fTraces
.size() == 0) {
344 fCurrentTrace
= null;
346 * In other cases, we should receive a traceSelected signal that
347 * will indicate which trace is the new one.
353 * Signal handler for the selection range signal.
355 * The current time of *all* traces whose range contains the requested new
356 * selection time range will be updated.
359 * The incoming signal
363 public synchronized void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal
) {
364 final ITmfTimestamp beginTs
= signal
.getBeginTime();
365 final ITmfTimestamp endTs
= signal
.getEndTime();
367 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
368 final ITmfTrace trace
= entry
.getKey();
369 if (beginTs
.intersects(getValidTimeRange(trace
)) || endTs
.intersects(getValidTimeRange(trace
))) {
370 TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
373 * We want to update the selection range, but keep everything
374 * else the same as the previous trace context.
376 TmfTimeRange newSelectionRange
= new TmfTimeRange(beginTs
, endTs
);
377 TmfTraceContext newCtx
= new TmfTraceContext(newSelectionRange
,
378 prevCtx
.getWindowRange(),
379 prevCtx
.getEditorFile(),
380 prevCtx
.getFilter());
381 entry
.setValue(newCtx
);
387 * Signal handler for the window range signal.
389 * The current window time range of *all* valid traces will be updated to
390 * the new requested times.
393 * The incoming signal
397 public synchronized void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal
) {
398 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
399 final ITmfTrace trace
= entry
.getKey();
400 final TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
402 final TmfTimeRange validTr
= getValidTimeRange(trace
);
403 if (validTr
== null) {
407 /* Determine the new time range */
408 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
409 TmfTimeRange newWindowTr
= (targetTr
== null ? prevCtx
.getWindowRange() : targetTr
);
411 /* Keep the values from the old context, except for the window range */
412 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
.getSelectionRange(),
413 newWindowTr
, prevCtx
.getEditorFile(), prevCtx
.getFilter());
414 entry
.setValue(newCtx
);
418 // ------------------------------------------------------------------------
419 // Private utility methods
420 // ------------------------------------------------------------------------
423 * Return the valid time range of a trace (not the current window time
424 * range, but the range of all possible valid timestamps).
426 * For a real trace this is the whole range of the trace. For an experiment,
427 * it goes from the start time of the earliest trace to the end time of the
431 * The trace to check for
432 * @return The valid time span, or 'null' if the trace is not valid
434 private @Nullable TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
435 if (!fTraces
.containsKey(trace
)) {
436 /* Trace is not part of the currently opened traces */
440 List
<ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
442 if (traces
.isEmpty()) {
443 /* "trace" is a single trace, return its time range directly */
444 return trace
.getTimeRange();
447 if (traces
.size() == 1) {
448 /* Trace is an experiment with only 1 trace */
449 return traces
.get(0).getTimeRange();
453 * Trace is an trace set with 2+ traces, so get the earliest start and
456 ITmfTimestamp start
= traces
.get(0).getStartTime();
457 ITmfTimestamp end
= traces
.get(0).getEndTime();
459 for (int i
= 1; i
< traces
.size(); i
++) {
460 ITmfTrace curTrace
= traces
.get(i
);
461 if (curTrace
.getStartTime().compareTo(start
) < 0) {
462 start
= curTrace
.getStartTime();
464 if (curTrace
.getEndTime().compareTo(end
) > 0) {
465 end
= curTrace
.getEndTime();
468 return new TmfTimeRange(start
, end
);
472 * Get the temporary directory path. If there is an instance of Eclipse
473 * running, the temporary directory will reside under the workspace.
475 * @return the temporary directory path suitable to be passed to the
476 * java.io.File constructor without a trailing separator
478 public static String
getTemporaryDirPath() {
479 // Get the workspace path from the properties
480 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
481 if (property
!= null) {
483 File dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
484 dir
= new File(dir
.getAbsolutePath() + File
.separator
+ TEMP_DIR_NAME
);
488 return dir
.getAbsolutePath();
489 } catch (URISyntaxException e
) {
490 Activator
.logError(e
.getLocalizedMessage(), e
);
493 return System
.getProperty("java.io.tmpdir"); //$NON-NLS-1$
497 * Get a temporary directory based on a trace's name. We will create the
498 * directory if it doesn't exist, so that it's ready to be used.
500 private static String
getTemporaryDir(ITmfTrace trace
) {
501 String pathName
= getTemporaryDirPath() +
505 File dir
= new File(pathName
);