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
.internal
.tmf
.core
.Activator
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEventFilterAppliedSignal
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
52 import com
.google
.common
.collect
.ImmutableSet
;
55 * Central trace manager for TMF. It tracks the currently opened traces and
56 * experiment, as well as the currently-selected time or time range and the
57 * current window time range for each one of those. It also tracks filters
58 * applied for each trace.
60 * It's a singleton class, so only one instance should exist (available via
61 * {@link #getInstance()}).
63 * @author Alexandre Montplaisir
65 public final class TmfTraceManager
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
73 /** The currently-selected trace. Should always be part of the trace map */
74 private ITmfTrace fCurrentTrace
= null;
76 private static final String TEMP_DIR_NAME
= ".temp"; //$NON-NLS-1$
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
82 private TmfTraceManager() {
83 fTraces
= new LinkedHashMap
<>();
84 TmfSignalManager
.registerVIP(this);
87 /** Singleton instance */
88 private static TmfTraceManager tm
= null;
91 * Get an instance of the trace manager.
93 * @return The trace manager
95 public static synchronized TmfTraceManager
getInstance() {
97 tm
= new TmfTraceManager();
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * Get the currently selected trace (normally, the focused editor).
109 * @return The active trace
111 public synchronized ITmfTrace
getActiveTrace() {
112 return fCurrentTrace
;
116 * Get the trace set of the currently active trace.
118 * @return The active trace set
119 * @see #getTraceSet(ITmfTrace)
121 public synchronized @NonNull Collection
<ITmfTrace
> getActiveTraceSet() {
122 final ITmfTrace trace
= fCurrentTrace
;
123 return getTraceSet(trace
);
127 * Get the currently-opened traces, as an unmodifiable set.
129 * @return A set containing the opened traces
131 public synchronized Set
<ITmfTrace
> getOpenedTraces() {
132 return Collections
.unmodifiableSet(fTraces
.keySet());
136 * Get the editor file for an opened trace.
140 * @return the editor file or null if the trace is not opened
142 public synchronized IFile
getTraceEditorFile(ITmfTrace trace
) {
143 TmfTraceContext ctx
= fTraces
.get(trace
);
145 return ctx
.getEditorFile();
151 * Get the {@link TmfTraceContext} of the current active trace. This can be
152 * used to retrieve the current active/selected time ranges and such.
154 * @return The trace's context.
157 public synchronized TmfTraceContext
getCurrentTraceContext() {
158 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
159 if (curCtx
== null) {
160 /* There are no traces opened at the moment. */
161 return TmfTraceContext
.NULL_CONTEXT
;
166 // ------------------------------------------------------------------------
167 // Public utility methods
168 // ------------------------------------------------------------------------
171 * Get the trace set of a given trace. For a standard trace, this is simply
172 * an array with only that trace in it. For experiments, this is an array of
173 * all the traces contained in this experiment.
176 * The trace or experiment
177 * @return The corresponding trace set.
179 public static @NonNull Collection
<@NonNull ITmfTrace
> getTraceSet(ITmfTrace trace
) {
181 return ImmutableSet
.of();
183 List
<@NonNull ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
184 if (traces
.size() > 0) {
185 return ImmutableSet
.copyOf(traces
);
187 return ImmutableSet
.of(trace
);
191 * Get the trace set of a given trace or experiment, including the
192 * experiment. For a standard trace, this is simply a set containing only
193 * that trace. For experiments, it is the set of all the traces contained in
194 * this experiment, along with the experiment.
197 * The trace or experiment
198 * @return The corresponding trace set, including the experiment.
200 public static @NonNull Collection
<ITmfTrace
> getTraceSetWithExperiment(ITmfTrace trace
) {
202 return ImmutableSet
.of();
204 if (trace
instanceof TmfExperiment
) {
205 TmfExperiment exp
= (TmfExperiment
) trace
;
206 List
<ITmfTrace
> traces
= exp
.getTraces();
207 Set
<ITmfTrace
> alltraces
= new LinkedHashSet
<>(traces
);
209 return ImmutableSet
.copyOf(alltraces
);
211 return Collections
.singleton(trace
);
215 * Return the path (as a string) to the directory for supplementary files to
216 * use with a given trace. If no supplementary file directory has been
217 * configured, a temporary directory based on the trace's name will be
222 * @return The path to the supplementary file directory (trailing slash is
225 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
226 IResource resource
= trace
.getResource();
227 if (resource
== null) {
228 return getTemporaryDir(trace
);
231 String supplDir
= null;
233 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
234 } catch (CoreException e
) {
235 return getTemporaryDir(trace
);
237 return supplDir
+ File
.separator
;
241 * Refresh the supplementary files resources for a trace, so it can pick up
242 * new files that got created.
245 * The trace for which to refresh the supplementary files
247 public static void refreshSupplementaryFiles(ITmfTrace trace
) {
248 IResource resource
= trace
.getResource();
249 if (resource
!= null && resource
.exists()) {
250 String supplFolderPath
= getSupplementaryFileDir(trace
);
251 IProject project
= resource
.getProject();
252 /* Remove the project's path from the supplementary path dir */
253 if (!supplFolderPath
.startsWith(project
.getLocation().toOSString())) {
254 Activator
.logWarning(String
.format("Supplementary files folder for trace %s is not within the project.", trace
.getName())); //$NON-NLS-1$
257 IFolder supplFolder
= project
.getFolder(supplFolderPath
.substring(project
.getLocationURI().getPath().length()));
258 if (supplFolder
.exists()) {
260 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
261 } catch (CoreException e
) {
262 Activator
.logError("Error refreshing resources", e
); //$NON-NLS-1$
268 // ------------------------------------------------------------------------
270 // ------------------------------------------------------------------------
273 * Signal handler for the traceOpened signal.
276 * The incoming signal
279 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
280 final ITmfTrace trace
= signal
.getTrace();
281 final IFile editorFile
= signal
.getEditorFile();
282 final ITmfTimestamp startTs
= trace
.getStartTime();
284 long offset
= trace
.getInitialRangeOffset().toNanos();
285 long endTime
= startTs
.toNanos() + offset
;
286 final TmfTimeRange selectionRange
= new TmfTimeRange(startTs
, startTs
);
287 final TmfTimeRange windowRange
= new TmfTimeRange(startTs
, new TmfTimestamp(endTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
289 final TmfTraceContext startCtx
= new TmfTraceContext(selectionRange
, windowRange
, editorFile
, null);
291 fTraces
.put(trace
, startCtx
);
293 /* We also want to set the newly-opened trace as the active trace */
294 fCurrentTrace
= trace
;
298 * Handler for the TmfTraceSelectedSignal.
301 * The incoming signal
304 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
305 final ITmfTrace newTrace
= signal
.getTrace();
306 if (!fTraces
.containsKey(newTrace
)) {
307 throw new RuntimeException();
309 fCurrentTrace
= newTrace
;
313 * Signal handler for the filterApplied signal.
316 * The incoming signal
319 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal
) {
320 final ITmfTrace newTrace
= signal
.getTrace();
321 TmfTraceContext context
= fTraces
.get(newTrace
);
322 if (context
== null) {
323 throw new RuntimeException();
325 fTraces
.put(newTrace
, new TmfTraceContext(context
.getSelectionRange(),
326 context
.getWindowRange(),
327 context
.getEditorFile(),
328 signal
.getEventFilter()));
332 * Signal handler for the traceClosed signal.
335 * The incoming signal
338 public synchronized void traceClosed(final TmfTraceClosedSignal signal
) {
339 fTraces
.remove(signal
.getTrace());
340 if (fTraces
.size() == 0) {
341 fCurrentTrace
= null;
343 * In other cases, we should receive a traceSelected signal that
344 * will indicate which trace is the new one.
350 * Signal handler for the selection range signal.
352 * The current time of *all* traces whose range contains the requested new
353 * selection time range will be updated.
356 * The incoming signal
360 public synchronized void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal
) {
361 final ITmfTimestamp beginTs
= signal
.getBeginTime();
362 final ITmfTimestamp endTs
= signal
.getEndTime();
364 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
365 final ITmfTrace trace
= entry
.getKey();
366 if (beginTs
.intersects(getValidTimeRange(trace
)) || endTs
.intersects(getValidTimeRange(trace
))) {
367 TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
370 * We want to update the selection range, but keep everything
371 * else the same as the previous trace context.
373 TmfTimeRange newSelectionRange
= new TmfTimeRange(beginTs
, endTs
);
374 TmfTraceContext newCtx
= new TmfTraceContext(newSelectionRange
,
375 prevCtx
.getWindowRange(),
376 prevCtx
.getEditorFile(),
377 prevCtx
.getFilter());
378 entry
.setValue(newCtx
);
384 * Signal handler for the window range signal.
386 * The current window time range of *all* valid traces will be updated to
387 * the new requested times.
390 * The incoming signal
394 public synchronized void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal
) {
395 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
396 final ITmfTrace trace
= entry
.getKey();
397 final TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
399 final TmfTimeRange validTr
= getValidTimeRange(trace
);
400 if (validTr
== null) {
404 /* Determine the new time range */
405 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
406 TmfTimeRange newWindowTr
= (targetTr
== null ? prevCtx
.getWindowRange() : targetTr
);
408 /* Keep the values from the old context, except for the window range */
409 TmfTraceContext newCtx
= new TmfTraceContext(prevCtx
.getSelectionRange(),
410 newWindowTr
, prevCtx
.getEditorFile(), prevCtx
.getFilter());
411 entry
.setValue(newCtx
);
415 // ------------------------------------------------------------------------
416 // Private utility methods
417 // ------------------------------------------------------------------------
420 * Return the valid time range of a trace (not the current window time
421 * range, but the range of all possible valid timestamps).
423 * For a real trace this is the whole range of the trace. For an experiment,
424 * it goes from the start time of the earliest trace to the end time of the
428 * The trace to check for
429 * @return The valid time span, or 'null' if the trace is not valid
431 private @Nullable TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
432 if (!fTraces
.containsKey(trace
)) {
433 /* Trace is not part of the currently opened traces */
437 List
<ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
439 if (traces
.isEmpty()) {
440 /* "trace" is a single trace, return its time range directly */
441 return trace
.getTimeRange();
444 if (traces
.size() == 1) {
445 /* Trace is an experiment with only 1 trace */
446 return traces
.get(0).getTimeRange();
450 * Trace is an trace set with 2+ traces, so get the earliest start and
453 ITmfTimestamp start
= traces
.get(0).getStartTime();
454 ITmfTimestamp end
= traces
.get(0).getEndTime();
456 for (int i
= 1; i
< traces
.size(); i
++) {
457 ITmfTrace curTrace
= traces
.get(i
);
458 if (curTrace
.getStartTime().compareTo(start
) < 0) {
459 start
= curTrace
.getStartTime();
461 if (curTrace
.getEndTime().compareTo(end
) > 0) {
462 end
= curTrace
.getEndTime();
465 return new TmfTimeRange(start
, end
);
469 * Get the temporary directory path. If there is an instance of Eclipse
470 * running, the temporary directory will reside under the workspace.
472 * @return the temporary directory path suitable to be passed to the
473 * java.io.File constructor without a trailing separator
475 public static String
getTemporaryDirPath() {
476 // Get the workspace path from the properties
477 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
478 if (property
!= null) {
480 File dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
481 dir
= new File(dir
.getAbsolutePath() + File
.separator
+ TEMP_DIR_NAME
);
485 return dir
.getAbsolutePath();
486 } catch (URISyntaxException e
) {
487 Activator
.logError(e
.getLocalizedMessage(), e
);
490 return System
.getProperty("java.io.tmpdir"); //$NON-NLS-1$
494 * Get a temporary directory based on a trace's name. We will create the
495 * directory if it doesn't exist, so that it's ready to be used.
497 private static String
getTemporaryDir(ITmfTrace trace
) {
498 String pathName
= getTemporaryDirPath() +
502 File dir
= new File(pathName
);