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
.io
.IOException
;
21 import java
.net
.URISyntaxException
;
22 import java
.util
.Collection
;
23 import java
.util
.Collections
;
24 import java
.util
.LinkedHashMap
;
25 import java
.util
.LinkedHashSet
;
26 import java
.util
.List
;
30 import org
.apache
.commons
.io
.FileUtils
;
31 import org
.eclipse
.core
.resources
.IFile
;
32 import org
.eclipse
.core
.resources
.IFolder
;
33 import org
.eclipse
.core
.resources
.IProject
;
34 import org
.eclipse
.core
.resources
.IResource
;
35 import org
.eclipse
.core
.runtime
.CoreException
;
36 import org
.eclipse
.core
.runtime
.URIUtil
;
37 import org
.eclipse
.jdt
.annotation
.NonNull
;
38 import org
.eclipse
.jdt
.annotation
.Nullable
;
39 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEventFilterAppliedSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceModelSignal
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfWindowRangeUpdatedSignal
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.experiment
.TmfExperiment
;
55 import com
.google
.common
.collect
.ImmutableSet
;
58 * Central trace manager for TMF. It tracks the currently opened traces and
59 * experiment, as well as the currently-selected time or time range and the
60 * current window time range for each one of those. It also tracks filters
61 * applied for each trace.
63 * It's a singleton class, so only one instance should exist (available via
64 * {@link #getInstance()}).
66 * @author Alexandre Montplaisir
68 public final class TmfTraceManager
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 private final Map
<ITmfTrace
, TmfTraceContext
> fTraces
;
76 /** The currently-selected trace. Should always be part of the trace map */
77 private ITmfTrace fCurrentTrace
= null;
79 private static final String TEMP_DIR_NAME
= ".temp"; //$NON-NLS-1$
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
85 private TmfTraceManager() {
86 fTraces
= new LinkedHashMap
<>();
87 TmfSignalManager
.registerVIP(this);
90 /** Singleton instance */
91 private static TmfTraceManager tm
= null;
94 * Get an instance of the trace manager.
96 * @return The trace manager
98 public static synchronized TmfTraceManager
getInstance() {
100 tm
= new TmfTraceManager();
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * Get the currently selected trace (normally, the focused editor).
112 * @return The active trace
114 public synchronized ITmfTrace
getActiveTrace() {
115 return fCurrentTrace
;
119 * Get the trace set of the currently active trace.
121 * @return The active trace set
122 * @see #getTraceSet(ITmfTrace)
124 public synchronized @NonNull Collection
<ITmfTrace
> getActiveTraceSet() {
125 final ITmfTrace trace
= fCurrentTrace
;
126 return getTraceSet(trace
);
130 * Get the currently-opened traces, as an unmodifiable set.
132 * @return A set containing the opened traces
134 public synchronized Set
<ITmfTrace
> getOpenedTraces() {
135 return Collections
.unmodifiableSet(fTraces
.keySet());
139 * Get the editor file for an opened trace.
143 * @return the editor file or null if the trace is not opened
145 public synchronized IFile
getTraceEditorFile(ITmfTrace trace
) {
146 TmfTraceContext ctx
= fTraces
.get(trace
);
148 return ctx
.getEditorFile();
154 * Get the {@link TmfTraceContext} of the current active trace. This can be
155 * used to retrieve the current active/selected time ranges and such.
157 * @return The trace's context.
160 public synchronized TmfTraceContext
getCurrentTraceContext() {
161 TmfTraceContext curCtx
= fTraces
.get(fCurrentTrace
);
162 if (curCtx
== null) {
163 /* There are no traces opened at the moment. */
164 return TmfTraceContext
.NULL_CONTEXT
;
169 // ------------------------------------------------------------------------
170 // Public utility methods
171 // ------------------------------------------------------------------------
174 * Get the trace set of a given trace. For a standard trace, this is simply
175 * an array with only that trace in it. For experiments, this is an array of
176 * all the traces contained in this experiment.
179 * The trace or experiment
180 * @return The corresponding trace set.
182 public static @NonNull Collection
<@NonNull ITmfTrace
> getTraceSet(ITmfTrace trace
) {
184 return ImmutableSet
.of();
186 List
<@NonNull ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
187 if (traces
.size() > 0) {
188 return ImmutableSet
.copyOf(traces
);
190 return ImmutableSet
.of(trace
);
194 * Get the trace set of a given trace or experiment, including the
195 * experiment. For a standard trace, this is simply a set containing only
196 * that trace. For experiments, it is the set of all the traces contained in
197 * this experiment, along with the experiment.
200 * The trace or experiment
201 * @return The corresponding trace set, including the experiment.
203 public static @NonNull Collection
<ITmfTrace
> getTraceSetWithExperiment(ITmfTrace trace
) {
205 return ImmutableSet
.of();
207 if (trace
instanceof TmfExperiment
) {
208 TmfExperiment exp
= (TmfExperiment
) trace
;
209 List
<ITmfTrace
> traces
= exp
.getTraces();
210 Set
<ITmfTrace
> alltraces
= new LinkedHashSet
<>(traces
);
212 return ImmutableSet
.copyOf(alltraces
);
214 return Collections
.singleton(trace
);
218 * Return the path (as a string) to the directory for supplementary files to
219 * use with a given trace. If no supplementary file directory has been
220 * configured, a temporary directory based on the trace's name will be
225 * @return The path to the supplementary file directory (trailing slash is
228 public static String
getSupplementaryFileDir(ITmfTrace trace
) {
229 IResource resource
= trace
.getResource();
230 if (resource
== null) {
231 return getTemporaryDir(trace
);
234 String supplDir
= null;
236 supplDir
= resource
.getPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
);
237 } catch (CoreException e
) {
238 return getTemporaryDir(trace
);
240 return supplDir
+ File
.separator
;
244 * Refresh the supplementary files resources for a trace, so it can pick up
245 * new files that got created.
248 * The trace for which to refresh the supplementary files
250 public static void refreshSupplementaryFiles(ITmfTrace trace
) {
251 IResource resource
= trace
.getResource();
252 if (resource
!= null && resource
.exists()) {
253 String supplFolderPath
= getSupplementaryFileDir(trace
);
254 IProject project
= resource
.getProject();
255 /* Remove the project's path from the supplementary path dir */
256 if (!supplFolderPath
.startsWith(project
.getLocation().toOSString())) {
257 Activator
.logWarning(String
.format("Supplementary files folder for trace %s is not within the project.", trace
.getName())); //$NON-NLS-1$
260 IFolder supplFolder
= project
.getFolder(supplFolderPath
.substring(project
.getLocationURI().getPath().length()));
261 if (supplFolder
.exists()) {
263 supplFolder
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
264 } catch (CoreException e
) {
265 Activator
.logError("Error refreshing resources", e
); //$NON-NLS-1$
272 * Delete the supplementary files of a given trace.
275 * The trace for which the supplementary files are to be deleted
278 public static void deleteSupplementaryFiles(ITmfTrace trace
) {
280 FileUtils
.cleanDirectory(new File(TmfTraceManager
.getSupplementaryFileDir(trace
)));
281 } catch (IOException e
) {
282 Activator
.logError("Error deleting supplementary files for trace " + trace
.getName(), e
); //$NON-NLS-1$
284 refreshSupplementaryFiles(trace
);
287 // ------------------------------------------------------------------------
289 // ------------------------------------------------------------------------
292 * Signal handler for the traceOpened signal.
295 * The incoming signal
298 public synchronized void traceOpened(final TmfTraceOpenedSignal signal
) {
299 final ITmfTrace trace
= signal
.getTrace();
300 final IFile editorFile
= signal
.getEditorFile();
301 final ITmfTimestamp startTs
= trace
.getStartTime();
303 long offset
= trace
.getInitialRangeOffset().toNanos();
304 long endTime
= startTs
.toNanos() + offset
;
305 final TmfTimeRange selectionRange
= new TmfTimeRange(startTs
, startTs
);
306 final TmfTimeRange windowRange
= new TmfTimeRange(startTs
, TmfTimestamp
.fromNanos(endTime
));
308 final TmfTraceContext startCtx
= trace
.createTraceContext(selectionRange
, windowRange
, editorFile
, null);
310 fTraces
.put(trace
, startCtx
);
312 /* We also want to set the newly-opened trace as the active trace */
313 fCurrentTrace
= trace
;
318 * @param signal any signal
322 public synchronized void signalReceived(final @NonNull TmfTraceModelSignal signal
) {
323 fTraces
.forEach((t
, u
) -> u
.receive(signal
));
328 * Handler for the TmfTraceSelectedSignal.
331 * The incoming signal
334 public synchronized void traceSelected(final TmfTraceSelectedSignal signal
) {
335 final ITmfTrace newTrace
= signal
.getTrace();
336 if (!fTraces
.containsKey(newTrace
)) {
337 throw new RuntimeException();
339 fCurrentTrace
= newTrace
;
343 * Signal handler for the filterApplied signal.
346 * The incoming signal
349 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal
) {
350 final ITmfTrace newTrace
= signal
.getTrace();
351 TmfTraceContext context
= fTraces
.get(newTrace
);
352 if (context
== null) {
353 throw new RuntimeException();
355 final TmfTraceContext newContext
= newTrace
.createTraceContext(context
.getSelectionRange(),
356 context
.getWindowRange(),
357 context
.getEditorFile(),
358 signal
.getEventFilter());
359 newContext
.setData(context
.getData());
360 fTraces
.put(newTrace
, newContext
);
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 selection range 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
392 public synchronized void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal
) {
393 final ITmfTimestamp beginTs
= signal
.getBeginTime();
394 final ITmfTimestamp endTs
= signal
.getEndTime();
396 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
397 final ITmfTrace trace
= entry
.getKey();
398 if (beginTs
.intersects(getValidTimeRange(trace
)) || endTs
.intersects(getValidTimeRange(trace
))) {
399 TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
402 * We want to update the selection range, but keep everything
403 * else the same as the previous trace context.
405 TmfTimeRange newSelectionRange
= new TmfTimeRange(beginTs
, endTs
);
406 TmfTraceContext newCtx
= trace
.createTraceContext(newSelectionRange
,
407 prevCtx
.getWindowRange(),
408 prevCtx
.getEditorFile(),
409 prevCtx
.getFilter());
410 newCtx
.setData(prevCtx
.getData());
411 entry
.setValue(newCtx
);
417 * Signal handler for the window range signal.
419 * The current window time range of *all* valid traces will be updated to
420 * the new requested times.
423 * The incoming signal
427 public synchronized void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal
) {
428 for (Map
.Entry
<ITmfTrace
, TmfTraceContext
> entry
: fTraces
.entrySet()) {
429 final ITmfTrace trace
= entry
.getKey();
430 final TmfTraceContext prevCtx
= checkNotNull(entry
.getValue());
432 final TmfTimeRange validTr
= getValidTimeRange(trace
);
433 if (validTr
== null) {
437 /* Determine the new time range */
438 TmfTimeRange targetTr
= signal
.getCurrentRange().getIntersection(validTr
);
439 TmfTimeRange newWindowTr
= (targetTr
== null ? prevCtx
.getWindowRange() : targetTr
);
441 /* Keep the values from the old context, except for the window range */
442 TmfTraceContext newCtx
= trace
.createTraceContext(prevCtx
.getSelectionRange(),
443 newWindowTr
, prevCtx
.getEditorFile(), prevCtx
.getFilter());
444 newCtx
.setData(prevCtx
.getData());
445 entry
.setValue(newCtx
);
449 // ------------------------------------------------------------------------
450 // Private utility methods
451 // ------------------------------------------------------------------------
454 * Return the valid time range of a trace (not the current window time
455 * range, but the range of all possible valid timestamps).
457 * For a real trace this is the whole range of the trace. For an experiment,
458 * it goes from the start time of the earliest trace to the end time of the
462 * The trace to check for
463 * @return The valid time span, or 'null' if the trace is not valid
465 private @Nullable TmfTimeRange
getValidTimeRange(ITmfTrace trace
) {
466 if (!fTraces
.containsKey(trace
)) {
467 /* Trace is not part of the currently opened traces */
471 List
<ITmfTrace
> traces
= trace
.getChildren(ITmfTrace
.class);
473 if (traces
.isEmpty()) {
474 /* "trace" is a single trace, return its time range directly */
475 return trace
.getTimeRange();
478 if (traces
.size() == 1) {
479 /* Trace is an experiment with only 1 trace */
480 return traces
.get(0).getTimeRange();
484 * Trace is an trace set with 2+ traces, so get the earliest start and
487 ITmfTimestamp start
= traces
.get(0).getStartTime();
488 ITmfTimestamp end
= traces
.get(0).getEndTime();
490 for (int i
= 1; i
< traces
.size(); i
++) {
491 ITmfTrace curTrace
= traces
.get(i
);
492 if (curTrace
.getStartTime().compareTo(start
) < 0) {
493 start
= curTrace
.getStartTime();
495 if (curTrace
.getEndTime().compareTo(end
) > 0) {
496 end
= curTrace
.getEndTime();
499 return new TmfTimeRange(start
, end
);
503 * Get the temporary directory path. If there is an instance of Eclipse
504 * running, the temporary directory will reside under the workspace.
506 * @return the temporary directory path suitable to be passed to the
507 * java.io.File constructor without a trailing separator
509 public static String
getTemporaryDirPath() {
510 // Get the workspace path from the properties
511 String property
= System
.getProperty("osgi.instance.area"); //$NON-NLS-1$
512 if (property
!= null) {
514 File dir
= URIUtil
.toFile(URIUtil
.fromString(property
));
515 dir
= new File(dir
.getAbsolutePath() + File
.separator
+ TEMP_DIR_NAME
);
519 return dir
.getAbsolutePath();
520 } catch (URISyntaxException e
) {
521 Activator
.logError(e
.getLocalizedMessage(), e
);
524 return System
.getProperty("java.io.tmpdir"); //$NON-NLS-1$
528 * Get a temporary directory based on a trace's name. We will create the
529 * directory if it doesn't exist, so that it's ready to be used.
531 private static String
getTemporaryDir(ITmfTrace trace
) {
532 String pathName
= getTemporaryDirPath() +
536 File dir
= new File(pathName
);