TMF: Add some non null annotations to TmfTimeRange
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfTraceManager.java
CommitLineData
fc526aef 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2013, 2015 Ericsson
fc526aef
AM
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
0fcf3b09 11 * Patrick Tasse - Support selection range
d3de0920 12 * Xavier Raynaud - Support filters tracking
fc526aef
AM
13 *******************************************************************************/
14
2bdf0193 15package org.eclipse.tracecompass.tmf.core.trace;
fc526aef 16
5db5a3a4
AM
17import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
e1385db9 19import java.io.File;
3ec38c4c 20import java.net.URISyntaxException;
c14c0757 21import java.util.Collection;
fab18d20 22import java.util.Collections;
fc526aef 23import java.util.LinkedHashMap;
0f8687b4 24import java.util.LinkedHashSet;
e2239aea 25import java.util.List;
fc526aef 26import java.util.Map;
fab18d20 27import java.util.Set;
fc526aef 28
deaae6e1 29import org.eclipse.core.resources.IFile;
b5e8ee95
GB
30import org.eclipse.core.resources.IFolder;
31import org.eclipse.core.resources.IProject;
e1385db9
AM
32import org.eclipse.core.resources.IResource;
33import org.eclipse.core.runtime.CoreException;
1ac53e54 34import org.eclipse.core.runtime.URIUtil;
0f8687b4 35import org.eclipse.jdt.annotation.NonNull;
c14c0757 36import org.eclipse.tracecompass.common.core.NonNullUtils;
2bdf0193
AM
37import org.eclipse.tracecompass.internal.tmf.core.Activator;
38import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
39import org.eclipse.tracecompass.tmf.core.filter.ITmfFilter;
40import org.eclipse.tracecompass.tmf.core.signal.TmfEventFilterAppliedSignal;
41import org.eclipse.tracecompass.tmf.core.signal.TmfRangeSynchSignal;
42import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
43import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
44import org.eclipse.tracecompass.tmf.core.signal.TmfTimeSynchSignal;
45import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
46import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
47import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
48import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
49import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
50import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
5c5fa260 51import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
fc526aef 52
c14c0757
GB
53import com.google.common.collect.ImmutableSet;
54
fc526aef
AM
55/**
56 * Central trace manager for TMF. It tracks the currently opened traces and
0fcf3b09 57 * experiment, as well as the currently-selected time or time range and the
d3de0920
XR
58 * current window time range for each one of those. It also tracks filters
59 * applied for each trace.
fc526aef
AM
60 *
61 * It's a singleton class, so only one instance should exist (available via
62 * {@link #getInstance()}).
63 *
64 * @author Alexandre Montplaisir
fc526aef
AM
65 */
66public 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
3ec38c4c
MAL
77 private static final String TEMP_DIR_NAME = ".temp"; //$NON-NLS-1$
78
fc526aef
AM
79 // ------------------------------------------------------------------------
80 // Constructor
81 // ------------------------------------------------------------------------
82
83 private TmfTraceManager() {
a4524c1b 84 fTraces = new LinkedHashMap<>();
fc526aef
AM
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
fc526aef 107 /**
0fcf3b09 108 * @return The begin timestamp of selection
0fcf3b09 109 */
6cfc180e 110 public @NonNull ITmfTimestamp getSelectionBeginTime() {
0fcf3b09
PT
111 return getCurrentTraceContext().getSelectionBegin();
112 }
113
114 /**
115 * @return The end timestamp of selection
0fcf3b09 116 */
6cfc180e 117 public @NonNull ITmfTimestamp getSelectionEndTime() {
0fcf3b09
PT
118 return getCurrentTraceContext().getSelectionEnd();
119 }
120
121 /**
122 * Return the current window time range.
fc526aef 123 *
0fcf3b09 124 * @return the current window time range
fc526aef
AM
125 */
126 public synchronized TmfTimeRange getCurrentRange() {
0fcf3b09 127 return getCurrentTraceContext().getWindowRange();
fc526aef
AM
128 }
129
d3de0920
XR
130 /**
131 * Gets the filter applied to the current trace
132 *
5db5a3a4 133 * @return a filter, or <code>null</code>
d3de0920
XR
134 */
135 public synchronized ITmfFilter getCurrentFilter() {
136 return getCurrentTraceContext().getFilter();
137 }
138
fc526aef
AM
139 /**
140 * Get the currently selected trace (normally, the focused editor).
141 *
142 * @return The active trace
143 */
144 public synchronized ITmfTrace getActiveTrace() {
145 return fCurrentTrace;
146 }
147
148 /**
a6fc3a28 149 * Get the trace set of the currently active trace.
fc526aef
AM
150 *
151 * @return The active trace set
a6fc3a28 152 * @see #getTraceSet(ITmfTrace)
fc526aef 153 */
c14c0757 154 public synchronized @NonNull Collection<ITmfTrace> getActiveTraceSet() {
fc526aef 155 final ITmfTrace trace = fCurrentTrace;
a6fc3a28 156 return getTraceSet(trace);
fc526aef
AM
157 }
158
fab18d20
AM
159 /**
160 * Get the currently-opened traces, as an unmodifiable set.
161 *
162 * @return A set containing the opened traces
163 */
164 public synchronized Set<ITmfTrace> getOpenedTraces() {
165 return Collections.unmodifiableSet(fTraces.keySet());
166 }
167
deaae6e1
PT
168 /**
169 * Get the editor file for an opened trace.
170 *
171 * @param trace
172 * the trace
173 * @return the editor file or null if the trace is not opened
deaae6e1
PT
174 */
175 public synchronized IFile getTraceEditorFile(ITmfTrace trace) {
176 TmfTraceContext ctx = fTraces.get(trace);
177 if (ctx != null) {
178 return ctx.getEditorFile();
179 }
180 return null;
181 }
182
6cfc180e 183 private @NonNull TmfTraceContext getCurrentTraceContext() {
fc526aef
AM
184 TmfTraceContext curCtx = fTraces.get(fCurrentTrace);
185 if (curCtx == null) {
186 /* There are no traces opened at the moment. */
187 return TmfTraceContext.NULL_CONTEXT;
188 }
189 return curCtx;
190 }
191
e1385db9
AM
192 // ------------------------------------------------------------------------
193 // Public utility methods
194 // ------------------------------------------------------------------------
195
a6fc3a28
AM
196 /**
197 * Get the trace set of a given trace. For a standard trace, this is simply
198 * an array with only that trace in it. For experiments, this is an array of
199 * all the traces contained in this experiment.
200 *
201 * @param trace
202 * The trace or experiment
c14c0757 203 * @return The corresponding trace set.
a6fc3a28 204 */
c14c0757 205 public static @NonNull Collection<ITmfTrace> getTraceSet(ITmfTrace trace) {
a6fc3a28 206 if (trace == null) {
c14c0757 207 return NonNullUtils.checkNotNull(ImmutableSet.<ITmfTrace> of());
a6fc3a28 208 }
e2239aea
BH
209 List<ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
210 if (traces.size() > 0) {
c14c0757 211 return NonNullUtils.checkNotNull(ImmutableSet.copyOf(traces));
a6fc3a28 212 }
c14c0757 213 return NonNullUtils.checkNotNull(ImmutableSet.of(trace));
a6fc3a28
AM
214 }
215
0f8687b4
GB
216 /**
217 * Get the trace set of a given trace or experiment, including the
218 * experiment. For a standard trace, this is simply a set containing only
219 * that trace. For experiments, it is the set of all the traces contained in
220 * this experiment, along with the experiment.
221 *
222 * @param trace
223 * The trace or experiment
c14c0757 224 * @return The corresponding trace set, including the experiment.
0f8687b4 225 */
c14c0757 226 public static @NonNull Collection<ITmfTrace> getTraceSetWithExperiment(ITmfTrace trace) {
0f8687b4 227 if (trace == null) {
c14c0757 228 return checkNotNull(ImmutableSet.<ITmfTrace> of());
0f8687b4
GB
229 }
230 if (trace instanceof TmfExperiment) {
231 TmfExperiment exp = (TmfExperiment) trace;
fa62dc1d
BH
232 List<ITmfTrace> traces = exp.getTraces();
233 Set<ITmfTrace> alltraces = new LinkedHashSet<>(traces);
0f8687b4 234 alltraces.add(exp);
c14c0757 235 return NonNullUtils.checkNotNull(ImmutableSet.copyOf(alltraces));
0f8687b4 236 }
5db5a3a4 237 return checkNotNull(Collections.singleton(trace));
0f8687b4
GB
238 }
239
e1385db9
AM
240 /**
241 * Return the path (as a string) to the directory for supplementary files to
242 * use with a given trace. If no supplementary file directory has been
243 * configured, a temporary directory based on the trace's name will be
244 * provided.
245 *
246 * @param trace
247 * The trace
248 * @return The path to the supplementary file directory (trailing slash is
249 * INCLUDED!)
250 */
251 public static String getSupplementaryFileDir(ITmfTrace trace) {
252 IResource resource = trace.getResource();
253 if (resource == null) {
254 return getTemporaryDir(trace);
255 }
256
257 String supplDir = null;
258 try {
259 supplDir = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
260 } catch (CoreException e) {
261 return getTemporaryDir(trace);
262 }
263 return supplDir + File.separator;
264 }
265
b5e8ee95
GB
266 /**
267 * Refresh the supplementary files resources for a trace, so it can pick up
268 * new files that got created.
269 *
270 * @param trace
271 * The trace for which to refresh the supplementary files
b5e8ee95
GB
272 */
273 public static void refreshSupplementaryFiles(ITmfTrace trace) {
274 IResource resource = trace.getResource();
88567ed2 275 if (resource != null && resource.exists()) {
b5e8ee95
GB
276 String supplFolderPath = getSupplementaryFileDir(trace);
277 IProject project = resource.getProject();
278 /* Remove the project's path from the supplementary path dir */
279 if (!supplFolderPath.startsWith(project.getLocationURI().getPath())) {
280 Activator.logWarning(String.format("Supplementary files folder for trace %s is not within the project.", trace.getName())); //$NON-NLS-1$
281 return;
282 }
283 IFolder supplFolder = project.getFolder(supplFolderPath.substring(project.getLocationURI().getPath().length()));
284 if (supplFolder.exists()) {
285 try {
286 supplFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
287 } catch (CoreException e) {
288 Activator.logError("Error refreshing resources", e); //$NON-NLS-1$
289 }
290 }
291 }
292 }
293
fc526aef
AM
294 // ------------------------------------------------------------------------
295 // Signal handlers
296 // ------------------------------------------------------------------------
297
298 /**
299 * Signal handler for the traceOpened signal.
300 *
301 * @param signal
302 * The incoming signal
303 */
304 @TmfSignalHandler
305 public synchronized void traceOpened(final TmfTraceOpenedSignal signal) {
306 final ITmfTrace trace = signal.getTrace();
deaae6e1 307 final IFile editorFile = signal.getEditorFile();
fc526aef
AM
308 final ITmfTimestamp startTs = trace.getStartTime();
309
310 /* Calculate the initial time range */
311 final int SCALE = ITmfTimestamp.NANOSECOND_SCALE;
312 long offset = trace.getInitialRangeOffset().normalize(0, SCALE).getValue();
313 long endTime = startTs.normalize(0, SCALE).getValue() + offset;
314 final TmfTimeRange startTr = new TmfTimeRange(startTs, new TmfTimestamp(endTime, SCALE));
315
deaae6e1 316 final TmfTraceContext startCtx = new TmfTraceContext(startTs, startTs, startTr, editorFile);
fc526aef
AM
317
318 fTraces.put(trace, startCtx);
319
320 /* We also want to set the newly-opened trace as the active trace */
321 fCurrentTrace = trace;
322 }
323
fc526aef
AM
324 /**
325 * Handler for the TmfTraceSelectedSignal.
326 *
327 * @param signal
328 * The incoming signal
329 */
330 @TmfSignalHandler
331 public synchronized void traceSelected(final TmfTraceSelectedSignal signal) {
332 final ITmfTrace newTrace = signal.getTrace();
333 if (!fTraces.containsKey(newTrace)) {
334 throw new RuntimeException();
335 }
336 fCurrentTrace = newTrace;
337 }
338
d3de0920
XR
339 /**
340 * Signal handler for the filterApplied signal.
341 *
342 * @param signal
343 * The incoming signal
d3de0920
XR
344 */
345 @TmfSignalHandler
346 public synchronized void filterApplied(TmfEventFilterAppliedSignal signal) {
347 final ITmfTrace newTrace = signal.getTrace();
348 TmfTraceContext context = fTraces.get(newTrace);
349 if (context == null) {
350 throw new RuntimeException();
351 }
352 fTraces.put(newTrace, new TmfTraceContext(context, signal.getEventFilter()));
353 }
354
fc526aef
AM
355 /**
356 * Signal handler for the traceClosed signal.
357 *
358 * @param signal
359 * The incoming signal
360 */
361 @TmfSignalHandler
362 public synchronized void traceClosed(final TmfTraceClosedSignal signal) {
3fcf269e 363 fTraces.remove(signal.getTrace());
fc526aef
AM
364 if (fTraces.size() == 0) {
365 fCurrentTrace = null;
366 /*
367 * In other cases, we should receive a traceSelected signal that
368 * will indicate which trace is the new one.
369 */
370 }
371 }
372
373 /**
374 * Signal handler for the TmfTimeSynchSignal signal.
375 *
376 * The current time of *all* traces whose range contains the requested new
0fcf3b09 377 * selection time range will be updated.
fc526aef
AM
378 *
379 * @param signal
380 * The incoming signal
381 */
382 @TmfSignalHandler
383 public synchronized void timeUpdated(final TmfTimeSynchSignal signal) {
0fcf3b09
PT
384 final ITmfTimestamp beginTs = signal.getBeginTime();
385 final ITmfTimestamp endTs = signal.getEndTime();
fc526aef
AM
386
387 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
388 final ITmfTrace trace = entry.getKey();
0fcf3b09 389 if (beginTs.intersects(getValidTimeRange(trace)) || endTs.intersects(getValidTimeRange(trace))) {
6cfc180e 390 TmfTraceContext prevCtx = NonNullUtils.checkNotNull(entry.getValue());
0fcf3b09 391 TmfTraceContext newCtx = new TmfTraceContext(prevCtx, beginTs, endTs);
fc526aef
AM
392 entry.setValue(newCtx);
393 }
394 }
395 }
396
397 /**
398 * Signal handler for the TmfRangeSynchSignal signal.
399 *
5db5a3a4
AM
400 * The current window time range of *all* valid traces will be updated to
401 * the new requested times.
fc526aef
AM
402 *
403 * @param signal
404 * The incoming signal
405 */
406 @TmfSignalHandler
407 public synchronized void timeRangeUpdated(final TmfRangeSynchSignal signal) {
fc526aef
AM
408 for (Map.Entry<ITmfTrace, TmfTraceContext> entry : fTraces.entrySet()) {
409 final ITmfTrace trace = entry.getKey();
6cfc180e 410 final TmfTraceContext curCtx = NonNullUtils.checkNotNull(entry.getValue());
fc526aef
AM
411
412 final TmfTimeRange validTr = getValidTimeRange(trace);
413
fc526aef 414 /* Determine the new time range */
6cfc180e
GB
415 TmfTimeRange targetTr = null;
416 if (validTr != null) {
417 targetTr = signal.getCurrentRange().getIntersection(validTr);
418 }
0fcf3b09 419 TmfTimeRange newTr = (targetTr == null ? curCtx.getWindowRange() : targetTr);
fc526aef
AM
420
421 /* Update the values */
0fcf3b09 422 TmfTraceContext newCtx = new TmfTraceContext(curCtx, newTr);
fc526aef
AM
423 entry.setValue(newCtx);
424 }
425 }
426
427 // ------------------------------------------------------------------------
e1385db9 428 // Private utility methods
fc526aef
AM
429 // ------------------------------------------------------------------------
430
431 /**
0fcf3b09
PT
432 * Return the valid time range of a trace (not the current window time
433 * range, but the range of all possible valid timestamps).
fc526aef
AM
434 *
435 * For a real trace this is the whole range of the trace. For an experiment,
436 * it goes from the start time of the earliest trace to the end time of the
437 * latest one.
438 *
439 * @param trace
440 * The trace to check for
441 * @return The valid time span, or 'null' if the trace is not valid
442 */
443 private TmfTimeRange getValidTimeRange(ITmfTrace trace) {
444 if (!fTraces.containsKey(trace)) {
445 /* Trace is not part of the currently opened traces */
446 return null;
447 }
e2239aea
BH
448
449 List<ITmfTrace> traces = trace.getChildren(ITmfTrace.class);
450
451 if (traces.isEmpty()) {
fc526aef
AM
452 /* "trace" is a single trace, return its time range directly */
453 return trace.getTimeRange();
454 }
e2239aea
BH
455
456 if (traces.size() == 1) {
fc526aef 457 /* Trace is an experiment with only 1 trace */
e2239aea 458 return traces.get(0).getTimeRange();
fc526aef 459 }
e2239aea 460
fc526aef 461 /*
e2239aea 462 * Trace is an trace set with 2+ traces, so get the earliest start and
fc526aef
AM
463 * the latest end.
464 */
e2239aea
BH
465 ITmfTimestamp start = traces.get(0).getStartTime();
466 ITmfTimestamp end = traces.get(0).getEndTime();
467
468 for (int i = 1; i < traces.size(); i++) {
469 ITmfTrace curTrace = traces.get(i);
fc526aef
AM
470 if (curTrace.getStartTime().compareTo(start) < 0) {
471 start = curTrace.getStartTime();
472 }
473 if (curTrace.getEndTime().compareTo(end) > 0) {
474 end = curTrace.getEndTime();
475 }
476 }
477 return new TmfTimeRange(start, end);
478 }
e1385db9 479
3ec38c4c
MAL
480 /**
481 * Get the temporary directory path. If there is an instance of Eclipse
482 * running, the temporary directory will reside under the workspace.
483 *
484 * @return the temporary directory path suitable to be passed to the
485 * java.io.File constructor without a trailing separator
3ec38c4c
MAL
486 */
487 public static String getTemporaryDirPath() {
488 // Get the workspace path from the properties
489 String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
490 if (property != null) {
491 try {
1ac53e54 492 File dir = URIUtil.toFile(URIUtil.fromString(property));
3ec38c4c
MAL
493 dir = new File(dir.getAbsolutePath() + File.separator + TEMP_DIR_NAME);
494 if (!dir.exists()) {
495 dir.mkdirs();
496 }
497 return dir.getAbsolutePath();
498 } catch (URISyntaxException e) {
499 Activator.logError(e.getLocalizedMessage(), e);
500 }
501 }
502 return System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
503 }
504
e1385db9 505 /**
6e4358bd
AM
506 * Get a temporary directory based on a trace's name. We will create the
507 * directory if it doesn't exist, so that it's ready to be used.
e1385db9
AM
508 */
509 private static String getTemporaryDir(ITmfTrace trace) {
3ec38c4c 510 String pathName = getTemporaryDirPath() +
fa62dc1d
BH
511 File.separator +
512 trace.getName() +
513 File.separator;
6e4358bd
AM
514 File dir = new File(pathName);
515 if (!dir.exists()) {
516 dir.mkdirs();
517 }
518 return pathName;
e1385db9 519 }
fc526aef 520}
This page took 0.089899 seconds and 5 git commands to generate.