eb28f3e157ea961e0193ecf62740b364aeb22976
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceManager.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Patrick Tasse - Support selection range
12 * Xavier Raynaud - Support filters tracking
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.core.trace;
16
17 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
19 import java.io.File;
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;
26 import java.util.Map;
27 import java.util.Set;
28
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.TmfTraceModelSignal;
45 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
46 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
47 import org.eclipse.tracecompass.tmf.core.signal.TmfWindowRangeUpdatedSignal;
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;
52
53 import com.google.common.collect.ImmutableSet;
54
55 /**
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.
60 *
61 * It's a singleton class, so only one instance should exist (available via
62 * {@link #getInstance()}).
63 *
64 * @author Alexandre Montplaisir
65 */
66 public final class TmfTraceManager {
67
68 // ------------------------------------------------------------------------
69 // Attributes
70 // ------------------------------------------------------------------------
71
72 private final Map<ITmfTrace, TmfTraceContext> fTraces;
73
74 /** The currently-selected trace. Should always be part of the trace map */
75 private ITmfTrace fCurrentTrace = null;
76
77 private static final String TEMP_DIR_NAME = ".temp"; //$NON-NLS-1$
78
79 // ------------------------------------------------------------------------
80 // Constructor
81 // ------------------------------------------------------------------------
82
83 private TmfTraceManager() {
84 fTraces = new LinkedHashMap<>();
85 TmfSignalManager.registerVIP(this);
86 }
87
88 /** Singleton instance */
89 private static TmfTraceManager tm = null;
90
91 /**
92 * Get an instance of the trace manager.
93 *
94 * @return The trace manager
95 */
96 public static synchronized TmfTraceManager getInstance() {
97 if (tm == null) {
98 tm = new TmfTraceManager();
99 }
100 return tm;
101 }
102
103 // ------------------------------------------------------------------------
104 // Accessors
105 // ------------------------------------------------------------------------
106
107 /**
108 * Get the currently selected trace (normally, the focused editor).
109 *
110 * @return The active trace
111 */
112 public synchronized ITmfTrace getActiveTrace() {
113 return fCurrentTrace;
114 }
115
116 /**
117 * Get the trace set of the currently active trace.
118 *
119 * @return The active trace set
120 * @see #getTraceSet(ITmfTrace)
121 */
122 public synchronized @NonNull Collection<ITmfTrace> getActiveTraceSet() {
123 final ITmfTrace trace = fCurrentTrace;
124 return getTraceSet(trace);
125 }
126
127 /**
128 * Get the currently-opened traces, as an unmodifiable set.
129 *
130 * @return A set containing the opened traces
131 */
132 public synchronized Set<ITmfTrace> getOpenedTraces() {
133 return Collections.unmodifiableSet(fTraces.keySet());
134 }
135
136 /**
137 * Get the editor file for an opened trace.
138 *
139 * @param trace
140 * the trace
141 * @return the editor file or null if the trace is not opened
142 */
143 public synchronized IFile getTraceEditorFile(ITmfTrace trace) {
144 TmfTraceContext ctx = fTraces.get(trace);
145 if (ctx != null) {
146 return ctx.getEditorFile();
147 }
148 return null;
149 }
150
151 /**
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.
154 *
155 * @return The trace's context.
156 * @since 1.0
157 */
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;
163 }
164 return curCtx;
165 }
166
167 // ------------------------------------------------------------------------
168 // Public utility methods
169 // ------------------------------------------------------------------------
170
171 /**
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.
175 *
176 * @param trace
177 * The trace or experiment
178 * @return The corresponding trace set.
179 */
180 public static @NonNull Collection<@NonNull ITmfTrace> getTraceSet(ITmfTrace trace) {
181 if (trace == null) {
182 return ImmutableSet.of();
183 }
184 List<@NonNull ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
185 if (traces.size() > 0) {
186 return ImmutableSet.copyOf(traces);
187 }
188 return ImmutableSet.of(trace);
189 }
190
191 /**
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.
196 *
197 * @param trace
198 * The trace or experiment
199 * @return The corresponding trace set, including the experiment.
200 */
201 public static @NonNull Collection<ITmfTrace> getTraceSetWithExperiment(ITmfTrace trace) {
202 if (trace == null) {
203 return ImmutableSet.of();
204 }
205 if (trace instanceof TmfExperiment) {
206 TmfExperiment exp = (TmfExperiment) trace;
207 List<ITmfTrace> traces = exp.getTraces();
208 Set<ITmfTrace> alltraces = new LinkedHashSet<>(traces);
209 alltraces.add(exp);
210 return ImmutableSet.copyOf(alltraces);
211 }
212 return Collections.singleton(trace);
213 }
214
215 /**
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
219 * provided.
220 *
221 * @param trace
222 * The trace
223 * @return The path to the supplementary file directory (trailing slash is
224 * INCLUDED!)
225 */
226 public static String getSupplementaryFileDir(ITmfTrace trace) {
227 IResource resource = trace.getResource();
228 if (resource == null) {
229 return getTemporaryDir(trace);
230 }
231
232 String supplDir = null;
233 try {
234 supplDir = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
235 } catch (CoreException e) {
236 return getTemporaryDir(trace);
237 }
238 return supplDir + File.separator;
239 }
240
241 /**
242 * Refresh the supplementary files resources for a trace, so it can pick up
243 * new files that got created.
244 *
245 * @param trace
246 * The trace for which to refresh the supplementary files
247 */
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$
256 return;
257 }
258 IFolder supplFolder = project.getFolder(supplFolderPath.substring(project.getLocationURI().getPath().length()));
259 if (supplFolder.exists()) {
260 try {
261 supplFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
262 } catch (CoreException e) {
263 Activator.logError("Error refreshing resources", e); //$NON-NLS-1$
264 }
265 }
266 }
267 }
268
269 // ------------------------------------------------------------------------
270 // Signal handlers
271 // ------------------------------------------------------------------------
272
273 /**
274 * Signal handler for the traceOpened signal.
275 *
276 * @param signal
277 * The incoming signal
278 */
279 @TmfSignalHandler
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();
284
285 long offset = trace.getInitialRangeOffset().toNanos();
286 long endTime = startTs.toNanos() + offset;
287 final TmfTimeRange selectionRange = new TmfTimeRange(startTs, startTs);
288 final TmfTimeRange windowRange = new TmfTimeRange(startTs, TmfTimestamp.fromNanos(endTime));
289
290 final TmfTraceContext startCtx = trace.createTraceContext(selectionRange, windowRange, editorFile, null);
291
292 fTraces.put(trace, startCtx);
293
294 /* We also want to set the newly-opened trace as the active trace */
295 fCurrentTrace = trace;
296 }
297
298 /**
299 * Signal propagator
300 * @param signal any signal
301 * @since 2.0
302 */
303 @TmfSignalHandler
304 public synchronized void signalReceived(final @NonNull TmfTraceModelSignal signal) {
305 fTraces.forEach((t, u) -> u.receive(signal));
306 }
307
308
309 /**
310 * Handler for the TmfTraceSelectedSignal.
311 *
312 * @param signal
313 * The incoming signal
314 */
315 @TmfSignalHandler
316 public synchronized void traceSelected(final TmfTraceSelectedSignal signal) {
317 final ITmfTrace newTrace = signal.getTrace();
318 if (!fTraces.containsKey(newTrace)) {
319 throw new RuntimeException();
320 }
321 fCurrentTrace = newTrace;
322 }
323
324 /**
325 * Signal handler for the filterApplied signal.
326 *
327 * @param signal
328 * The incoming signal
329 */
330 @TmfSignalHandler
331 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal) {
332 final ITmfTrace newTrace = signal.getTrace();
333 TmfTraceContext context = fTraces.get(newTrace);
334 if (context == null) {
335 throw new RuntimeException();
336 }
337 final TmfTraceContext newContext = newTrace.createTraceContext(context.getSelectionRange(),
338 context.getWindowRange(),
339 context.getEditorFile(),
340 signal.getEventFilter());
341 newContext.setData(context.getData());
342 fTraces.put(newTrace, newContext);
343 }
344
345 /**
346 * Signal handler for the traceClosed signal.
347 *
348 * @param signal
349 * The incoming signal
350 */
351 @TmfSignalHandler
352 public synchronized void traceClosed(final TmfTraceClosedSignal signal) {
353 fTraces.remove(signal.getTrace());
354 if (fTraces.size() == 0) {
355 fCurrentTrace = null;
356 /*
357 * In other cases, we should receive a traceSelected signal that
358 * will indicate which trace is the new one.
359 */
360 }
361 }
362
363 /**
364 * Signal handler for the selection range signal.
365 *
366 * The current time of *all* traces whose range contains the requested new
367 * selection time range will be updated.
368 *
369 * @param signal
370 * The incoming signal
371 * @since 1.0
372 */
373 @TmfSignalHandler
374 public synchronized void selectionRangeUpdated(final TmfSelectionRangeUpdatedSignal signal) {
375 final ITmfTimestamp beginTs = signal.getBeginTime();
376 final ITmfTimestamp endTs = signal.getEndTime();
377
378 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
379 final ITmfTrace trace = entry.getKey();
380 if (beginTs.intersects(getValidTimeRange(trace)) || endTs.intersects(getValidTimeRange(trace))) {
381 TmfTraceContext prevCtx = checkNotNull(entry.getValue());
382
383 /*
384 * We want to update the selection range, but keep everything
385 * else the same as the previous trace context.
386 */
387 TmfTimeRange newSelectionRange = new TmfTimeRange(beginTs, endTs);
388 TmfTraceContext newCtx = trace.createTraceContext(newSelectionRange,
389 prevCtx.getWindowRange(),
390 prevCtx.getEditorFile(),
391 prevCtx.getFilter());
392 newCtx.setData(prevCtx.getData());
393 entry.setValue(newCtx);
394 }
395 }
396 }
397
398 /**
399 * Signal handler for the window range signal.
400 *
401 * The current window time range of *all* valid traces will be updated to
402 * the new requested times.
403 *
404 * @param signal
405 * The incoming signal
406 * @since 1.0
407 */
408 @TmfSignalHandler
409 public synchronized void windowRangeUpdated(final TmfWindowRangeUpdatedSignal signal) {
410 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
411 final ITmfTrace trace = entry.getKey();
412 final TmfTraceContext prevCtx = checkNotNull(entry.getValue());
413
414 final TmfTimeRange validTr = getValidTimeRange(trace);
415 if (validTr == null) {
416 return;
417 }
418
419 /* Determine the new time range */
420 TmfTimeRange targetTr = signal.getCurrentRange().getIntersection(validTr);
421 TmfTimeRange newWindowTr = (targetTr == null ? prevCtx.getWindowRange() : targetTr);
422
423 /* Keep the values from the old context, except for the window range */
424 TmfTraceContext newCtx = trace.createTraceContext(prevCtx.getSelectionRange(),
425 newWindowTr, prevCtx.getEditorFile(), prevCtx.getFilter());
426 newCtx.setData(prevCtx.getData());
427 entry.setValue(newCtx);
428 }
429 }
430
431 // ------------------------------------------------------------------------
432 // Private utility methods
433 // ------------------------------------------------------------------------
434
435 /**
436 * Return the valid time range of a trace (not the current window time
437 * range, but the range of all possible valid timestamps).
438 *
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
441 * latest one.
442 *
443 * @param trace
444 * The trace to check for
445 * @return The valid time span, or 'null' if the trace is not valid
446 */
447 private @Nullable TmfTimeRange getValidTimeRange(ITmfTrace trace) {
448 if (!fTraces.containsKey(trace)) {
449 /* Trace is not part of the currently opened traces */
450 return null;
451 }
452
453 List<ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
454
455 if (traces.isEmpty()) {
456 /* "trace" is a single trace, return its time range directly */
457 return trace.getTimeRange();
458 }
459
460 if (traces.size() == 1) {
461 /* Trace is an experiment with only 1 trace */
462 return traces.get(0).getTimeRange();
463 }
464
465 /*
466 * Trace is an trace set with 2+ traces, so get the earliest start and
467 * the latest end.
468 */
469 ITmfTimestamp start = traces.get(0).getStartTime();
470 ITmfTimestamp end = traces.get(0).getEndTime();
471
472 for (int i = 1; i < traces.size(); i++) {
473 ITmfTrace curTrace = traces.get(i);
474 if (curTrace.getStartTime().compareTo(start) < 0) {
475 start = curTrace.getStartTime();
476 }
477 if (curTrace.getEndTime().compareTo(end) > 0) {
478 end = curTrace.getEndTime();
479 }
480 }
481 return new TmfTimeRange(start, end);
482 }
483
484 /**
485 * Get the temporary directory path. If there is an instance of Eclipse
486 * running, the temporary directory will reside under the workspace.
487 *
488 * @return the temporary directory path suitable to be passed to the
489 * java.io.File constructor without a trailing separator
490 */
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) {
495 try {
496 File dir = URIUtil.toFile(URIUtil.fromString(property));
497 dir = new File(dir.getAbsolutePath() + File.separator + TEMP_DIR_NAME);
498 if (!dir.exists()) {
499 dir.mkdirs();
500 }
501 return dir.getAbsolutePath();
502 } catch (URISyntaxException e) {
503 Activator.logError(e.getLocalizedMessage(), e);
504 }
505 }
506 return System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
507 }
508
509 /**
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.
512 */
513 private static String getTemporaryDir(ITmfTrace trace) {
514 String pathName = getTemporaryDirPath() +
515 File.separator +
516 trace.getName() +
517 File.separator;
518 File dir = new File(pathName);
519 if (!dir.exists()) {
520 dir.mkdirs();
521 }
522 return pathName;
523 }
524 }
This page took 0.043358 seconds and 4 git commands to generate.